/[eiffelstudio]/trunk/Src/web/eiffel-cloud/modules/es_cloud/webapi/es_cloud_installations_webapi_handler.e
ViewVC logotype

Contents of /trunk/Src/web/eiffel-cloud/modules/es_cloud/webapi/es_cloud_installations_webapi_handler.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103675 - (show annotations)
Tue Nov 12 12:35:53 2019 UTC (2 months, 2 weeks ago) by jfiat
File size: 14469 byte(s)
Added webapi to access the plans.
1 note
2 description: "Summary description for {ES_CLOUD_INSTALLATIONS_WEBAPI_HANDLER}."
3 date: "$Date$"
4 revision: "$Revision$"
5
6 class
7 ES_CLOUD_INSTALLATIONS_WEBAPI_HANDLER
8
9 inherit
10 ES_CLOUD_WEBAPI_HANDLER
11
12 create
13 make
14
15 feature -- Execution
16
17 execute (a_version: READABLE_STRING_GENERAL; req: WSF_REQUEST; res: WSF_RESPONSE)
18 -- Execute handler for `req' and respond in `res'.
19 local
20 l_uid: READABLE_STRING_GENERAL
21 l_user: ES_CLOUD_USER
22 do
23 if
24 attached {WSF_STRING} req.path_parameter ("uid") as p_uid and then
25 attached user_by_uid (p_uid.value) as l_cms_user
26 then
27 create l_user.make (l_cms_user)
28 if req.is_get_request_method then
29 if attached {WSF_STRING} req.path_parameter ("installation_id") as iid then
30 if attached {WSF_STRING} req.path_parameter ("session_id") as sid then
31 handle_installation_session (a_version, l_user, iid.value, sid.value, req, res)
32 else
33 handle_installation (a_version, l_user, iid.value, req, res)
34 end
35 else
36 list_installations (a_version, l_user, req, res)
37 end
38 elseif req.is_post_request_method then
39 handle_user_post (a_version, l_user, req, res)
40 else
41 new_bad_request_error_response (Void, req, res).execute
42 end
43 else
44 new_not_found_error_response ("User not found", req, res).execute
45 end
46 end
47
48 handle_installation (a_version: READABLE_STRING_GENERAL; a_user: ES_CLOUD_USER; iid: READABLE_STRING_GENERAL; req: WSF_REQUEST; res: WSF_RESPONSE)
49 require
50 req.is_get_request_method
51 local
52 r: like new_response
53 tb,tb_sessions: STRING_TABLE [detachable ANY]
54 inst: ES_CLOUD_INSTALLATION
55 sess: ES_CLOUD_SESSION
56 l_include_sessions: BOOLEAN
57 do
58 if a_user.same_as (api.user) or else api.has_permission ("manage es accounts") then
59 r := new_response (req, res)
60 if attached es_cloud_api.user_installations (a_user) as lst then
61 l_include_sessions := req.percent_encoded_path_info.ends_with_general ("/session/")
62 across
63 lst as ic
64 until
65 inst /= Void
66 loop
67 inst := ic.item
68 if not iid.same_string (inst.installation_id) then
69 inst := Void
70 end
71 end
72 if inst /= Void then
73 r := new_response (req, res)
74 create tb.make (2)
75 tb.force (inst.installation_id, "id")
76 if attached inst.name as l_name then
77 tb.force (l_name, "name")
78 end
79 tb.force (inst.info, "info")
80 if attached inst.creation_date as dt then
81 tb.force (date_time_to_string (dt), "creation_date")
82 end
83 if inst.is_active then
84 tb.force ("yes", "is_active")
85 else
86 tb.force ("no", "is_active")
87 end
88 r.add_table_iterator_field ("es:installation", tb)
89 if l_include_sessions then
90 if attached es_cloud_api.user_sessions (a_user, inst.installation_id, False) as l_sessions then
91 create tb_sessions.make (l_sessions.count)
92 across
93 l_sessions as ic
94 loop
95 sess := ic.item
96 create tb.make (5)
97 tb.force (sess.id, "id")
98 if attached sess.title as l_title then
99 tb.force (l_title, "title")
100 end
101 inspect
102 sess.state
103 when {ES_CLOUD_SESSION}.state_paused_id then
104 tb.force ("paused", "state")
105 when {ES_CLOUD_SESSION}.state_ended_id then
106 tb.force ("ended", "state")
107 else -- state_normal_id
108 tb.force ("normal", "state")
109 end
110 if sess.is_expired (es_cloud_api) then
111 tb.force (True, "is_expired")
112 end
113 tb.force (date_time_to_string (sess.first_date), "first_date")
114 tb.force (date_time_to_string (sess.last_date), "last_date")
115 tb_sessions.force (tb, sess.id)
116 r.add_link (url_encoded (sess.id), detachable_html_encoded (sess.title), r.api.absolute_url (r.location + url_encoded (sess.id), Void))
117 end
118 else
119 create tb_sessions.make (0)
120 end
121 r.add_table_iterator_field ("es:sessions", tb_sessions)
122 else
123 r.add_link ("sessions", "sessions", r.api.absolute_url (r.location + "/session/", Void))
124 end
125 r.add_link ("installation", "installation", cloud_user_installation_link (a_version, a_user, iid))
126 r.add_self (r.location)
127 else
128 r := new_error_response ("Installation not found", req, res)
129 end
130 r.execute
131 else
132 r := new_error_response ("No installation found", req, res)
133 end
134 add_cloud_user_links_to (a_version, a_user, r)
135 add_user_links_to (a_user, r)
136 else
137 r := new_access_denied_error_response (Void, req, res)
138 end
139 r.execute
140 end
141
142 list_installations (a_version: READABLE_STRING_GENERAL; a_user: ES_CLOUD_USER; req: WSF_REQUEST; res: WSF_RESPONSE)
143 require
144 req.is_get_request_method
145 local
146 r: like new_response
147 tb: STRING_TABLE [detachable ANY]
148 tb_installations: STRING_TABLE [detachable ANY]
149 do
150 if a_user.same_as (api.user) or else api.has_permission ("manage es accounts") then
151 r := new_response (req, res)
152 if attached es_cloud_api.user_installations (a_user) as lst then
153 create tb_installations.make (lst.count)
154 across
155 lst as ic
156 loop
157 if attached ic.item as inst then
158 create tb.make (1)
159 tb.force (inst.info, "info")
160 if attached inst.name as l_name then
161 tb.force (l_name, "name")
162 end
163 tb_installations.force (tb, inst.installation_id)
164 r.add_link (url_encoded (inst.installation_id), detachable_html_encoded (inst.name), r.api.absolute_url (r.location + "/" + url_encoded (inst.installation_id), Void))
165 end
166 end
167 r.add_table_iterator_field ("es:installations", tb_installations)
168 end
169 add_cloud_user_links_to (a_version, a_user, r)
170 add_user_links_to (a_user, r)
171 else
172 r := new_access_denied_error_response (Void, req, res)
173 end
174 r.execute
175 end
176
177 handle_installation_session (a_version: READABLE_STRING_GENERAL; a_user: ES_CLOUD_USER; iid, sid: READABLE_STRING_GENERAL; req: WSF_REQUEST; res: WSF_RESPONSE)
178 require
179 req.is_get_request_method
180 local
181 r: like new_response
182 tb: STRING_TABLE [detachable ANY]
183 inst: ES_CLOUD_INSTALLATION
184 l_inst_location: STRING
185 do
186 if a_user.same_as (api.user) or else api.has_permission ("manage es accounts") then
187 r := new_response (req, res)
188 if attached {ES_CLOUD_SESSION} es_cloud_api.user_session (a_user, iid, sid) as sess then
189 r := new_response (req, res)
190 create tb.make (2)
191 tb.force (sess.installation_id, "installation_id")
192 tb.force (sess.id, "id")
193 inspect
194 sess.state
195 when {ES_CLOUD_SESSION}.state_normal_id then
196 tb.force ("normal", "state")
197 when {ES_CLOUD_SESSION}.state_paused_id then
198 tb.force ("paused", "state")
199 when {ES_CLOUD_SESSION}.state_ended_id then
200 tb.force ("ended", "state")
201 else
202 tb.force (sess.state.out, "state")
203 end
204 if sess.is_expired (es_cloud_api) then
205 tb.force (True, "is_expired")
206 end
207 if attached sess.first_date as dt then
208 tb.force (date_time_to_string (dt), "first_date")
209 end
210 if attached sess.last_date as dt then
211 tb.force (date_time_to_string (dt), "last_date")
212 end
213 if attached sess.title as l_title then
214 tb.force (l_title, "title")
215 end
216 r.add_table_iterator_field ("es:session", tb)
217 r.add_self (r.location)
218 l_inst_location := r.location.twin
219 remove_last_segment (l_inst_location, False)
220 remove_last_segment (l_inst_location, False)
221 r.add_link ("es:installation", "installation", (api.absolute_url (l_inst_location, Void)))
222 else
223 r := new_error_response ("Session not found", req, res)
224 end
225 r.add_link ("installation", "installation", cloud_user_installation_link (a_version, a_user, iid))
226 add_cloud_user_links_to (a_version, a_user, r)
227 add_user_links_to (a_user, r)
228 else
229 r := new_access_denied_error_response (Void, req, res)
230 end
231 r.execute
232 end
233
234 handle_user_post (a_version: READABLE_STRING_GENERAL; a_user: ES_CLOUD_USER; req: WSF_REQUEST; res: WSF_RESPONSE)
235 require
236 req.is_post_request_method
237 local
238 r: like new_response
239 tb: STRING_TABLE [detachable ANY]
240 f: CMS_FORM
241 l_user: ES_CLOUD_USER
242 l_install_id, l_session_id: detachable READABLE_STRING_GENERAL
243 l_installation: detachable ES_CLOUD_INSTALLATION
244 l_active_sessions: detachable LIST [ES_CLOUD_SESSION]
245 l_session: detachable ES_CLOUD_SESSION
246 l_user_plan: detachable ES_CLOUD_PLAN_SUBSCRIPTION
247 err: BOOLEAN
248 n, l_sess_limit, l_heartbeat: NATURAL
249 l_inst_location: STRING
250 do
251 if a_user.same_as (api.user) or else api.has_permission ("manage es accounts") then
252
253 create f.make (req.percent_encoded_path_info, "es-form")
254 f.extend_text_field ("installation_id", Void)
255 f.extend_text_field ("session_id", Void)
256 f.extend_text_field ("info", Void)
257 f.extend_text_field ("session_title", Void)
258 r := new_response (req, res)
259 f.process (r)
260 if
261 attached f.last_data as fd and then not fd.has_error
262 then
263 l_install_id := fd.string_item ("installation_id")
264 l_session_id := fd.string_item ("session_id")
265 if l_session_id /= Void and l_install_id /= Void then
266 l_installation := es_cloud_api.user_installation (a_user, l_install_id)
267 if l_installation /= Void then
268 l_installation.set_info (fd.string_item ("info"))
269 l_session := es_cloud_api.user_session (a_user, l_install_id, l_session_id)
270 if l_session /= Void then
271 l_active_sessions := es_cloud_api.user_active_concurrent_sessions (a_user, l_install_id, l_session)
272 end
273 if attached es_cloud_api.user_subscription (a_user) as l_plan then
274 l_sess_limit := l_plan.concurrent_sessions_limit
275 l_heartbeat := l_plan.heartbeat
276 else
277 l_sess_limit := es_cloud_api.default_concurrent_sessions_limit
278 l_heartbeat := es_cloud_api.default_heartbeat
279 end
280 if attached {WSF_STRING} req.form_parameter ("operation") as l_op then
281 if l_op.is_case_insensitive_equal ("ping") then
282 if l_session = Void then
283 create l_session.make (a_user, l_install_id, l_session_id, Void)
284 end
285 if
286 l_sess_limit > 0 and then
287 l_active_sessions /= Void and then
288 l_active_sessions.count.to_natural_32 >= l_sess_limit
289 then
290 -- Pause expired sessions or current session!
291 n := l_active_sessions.count.to_natural_32 - l_sess_limit + 1
292 from
293 l_active_sessions.finish
294 until
295 l_active_sessions.off or n = 0
296 loop
297 if l_active_sessions.item.is_expired (es_cloud_api) then
298 es_cloud_api.pause_session (a_user, l_active_sessions.item)
299 n := n - 1
300 l_active_sessions.remove
301 else
302 l_active_sessions.back
303 end
304 end
305 if n > 0 then
306 es_cloud_api.pause_session (a_user, l_session)
307 end
308 else
309 l_session.set_title (fd.string_item ("session_title"))
310 es_cloud_api.ping_installation (a_user, l_session)
311 end
312 elseif l_session /= Void then
313 if l_op.is_case_insensitive_equal ("end_session") then
314 es_cloud_api.end_session (a_user, l_session)
315 elseif l_op.is_case_insensitive_equal ("pause_session") then
316 es_cloud_api.pause_session (a_user, l_session)
317 elseif l_op.is_case_insensitive_equal ("resume_session") then
318 es_cloud_api.resume_session (a_user, l_session)
319 if l_active_sessions /= Void then
320 n := l_sess_limit
321 across
322 l_active_sessions as ic
323 loop
324 -- Keep first `l_sess_limit - 1` sessions active, and pause the others
325 if n > 1 then
326 n := n - 1
327 else
328 es_cloud_api.pause_session (a_user, ic.item)
329 end
330 end
331 end
332 else
333 -- default or error?
334 es_cloud_api.ping_installation (a_user, l_session)
335 end
336 else
337 err := True
338 end
339 else
340 -- default or error?
341 err := True
342 end
343 else
344 -- default or error?
345 -- Check for installation limit!
346 es_cloud_api.register_installation (a_user, l_install_id, fd.string_item ("info"))
347 end
348 err := es_cloud_api.has_error
349 end
350 else
351 err := True
352 end
353 if l_install_id = Void or l_session_id = Void or err then
354 r := new_error_response ("Error: missing installation information", req, res)
355 else
356 r := new_response (req, res)
357 if l_install_id /= Void then
358 --FIXME
359 if l_session_id /= Void then
360 l_inst_location := r.location.twin
361 remove_last_segment (l_inst_location, False)
362 remove_last_segment (l_inst_location, False)
363 r.add_link ("es:installation", "installation", (api.absolute_url (l_inst_location, Void)))
364 else
365 r.add_link ("es:installation", "installation", (api.absolute_url (r.location, Void) + "/" + api.url_encoded (l_install_id)))
366 end
367 end
368 if l_installation /= Void and then l_session /= Void then
369 inspect
370 l_session.state
371 when {ES_CLOUD_SESSION}.state_normal_id then
372 r.add_string_field ("es:session_state", "normal")
373 when {ES_CLOUD_SESSION}.state_paused_id then
374 r.add_string_field ("es:session_state", "paused")
375 when {ES_CLOUD_SESSION}.state_ended_id then
376 r.add_string_field ("es:session_state", "ended")
377 else
378 r.add_string_field ("es:session_state", "state#" + l_session.state.out)
379 end
380 if l_session.is_expired (es_cloud_api) then
381 r.add_boolean_field ("es:session_expired", True)
382 end
383 if l_heartbeat > 0 then
384 r.add_integer_64_field ("es:session_heartbeat", l_heartbeat)
385 end
386 r.add_link ("es:session", "session", api.absolute_url (r.location, Void)
387 + "/" + api.url_encoded (l_session.installation_id)
388 + "/session/" + api.url_encoded (l_session.id)
389 )
390 end
391 end
392 add_cloud_user_links_to (a_version, a_user, r)
393 add_user_links_to (a_user, r)
394 else
395 r := new_access_denied_error_response (Void, req, res)
396 end
397 r.execute
398 end
399
400 note
401 copyright: "2011-2017, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
402 license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
403 end
404

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23