floe-servicenow 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Floe
4
+ module ServiceNow
5
+ class Runner < Floe::BuiltinRunner::Runner
6
+ SCHEME = "servicenow"
7
+ SCHEME_PREFIX = "#{SCHEME}://".freeze
8
+ API_CLASSES = {
9
+ "cmdb" => Cmdb,
10
+ "table" => Table,
11
+ "incident" => Incident,
12
+ "oauth" => OAuth,
13
+ "service_catalog" => ServiceCatalog
14
+ }.freeze
15
+
16
+ def run_async!(resource, params, secrets, context)
17
+ raise ArgumentError, "Invalid resource" unless resource&.start_with?(SCHEME_PREFIX)
18
+
19
+ method_name = resource.sub(SCHEME_PREFIX, "")
20
+ runner_context = {"method" => method_name}
21
+
22
+ begin
23
+ api_class, api_method = resolve_api_method(method_name)
24
+ method_result = api_class.public_send(api_method, params, secrets, context)
25
+ method_result.merge(runner_context)
26
+ rescue NoMethodError
27
+ Floe::ServiceNow.error!(runner_context, :cause => "undefined method [#{method_name}]")
28
+ rescue => err
29
+ Floe::ServiceNow.error!(runner_context, :cause => err.to_s)
30
+ ensure
31
+ cleanup(runner_context)
32
+ end
33
+ end
34
+
35
+ def cleanup(runner_context)
36
+ method_name = runner_context["method"]
37
+ raise ArgumentError if method_name.nil?
38
+
39
+ api_class, api_method = resolve_api_method(method_name)
40
+ cleanup_method = :"#{api_method}_cleanup"
41
+ return unless api_class.respond_to?(cleanup_method, true)
42
+
43
+ api_class.send(cleanup_method, runner_context)
44
+ end
45
+
46
+ def status!(runner_context)
47
+ method_name = runner_context["method"]
48
+ raise ArgumentError if method_name.nil?
49
+ return if runner_context["running"] == false
50
+
51
+ api_class, api_method = resolve_api_method(method_name)
52
+ status_method = :"#{api_method}_status!"
53
+ return runner_context unless api_class.respond_to?(status_method, true)
54
+
55
+ api_class.send(status_method, runner_context)
56
+ end
57
+
58
+ private
59
+
60
+ def resolve_api_method(method_name)
61
+ api_name, api_method = method_name.split("/", 2)
62
+ raise NoMethodError if api_name.nil? || api_method.nil?
63
+
64
+ api_class = API_CLASSES[api_name]
65
+ raise NoMethodError if api_class.nil?
66
+
67
+ [api_class, api_method]
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ Floe::Runner.register_scheme(Floe::ServiceNow::Runner::SCHEME, -> { Floe::ServiceNow::Runner.new })
@@ -0,0 +1,446 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Floe
6
+ module ServiceNow
7
+ class ServiceCatalog < Floe::ServiceNow::Methods
8
+ def self.submit_catalog_item(params, secrets, _context)
9
+ error = verify_credentials(secrets)
10
+ error ||= verify_submit_catalog_item_params(params)
11
+ return ServiceNow.error!({}, :cause => error) if error
12
+
13
+ item_sys_id = params["item_sys_id"]
14
+
15
+ http_params = build_http_params(
16
+ "POST",
17
+ "/api/sn_sc/servicecatalog/items/#{item_sys_id}/order_now",
18
+ params,
19
+ secrets,
20
+ :body => build_submit_catalog_item_body(params)
21
+ )
22
+
23
+ http_request(http_params, {}, {})
24
+ end
25
+
26
+ def self.get_request(params, secrets, _context)
27
+ error = verify_credentials(secrets)
28
+ error ||= verify_request_params(params)
29
+ return ServiceNow.error!({}, :cause => error) if error
30
+
31
+ request_id = params["request_id"]
32
+
33
+ http_params = build_http_params(
34
+ "GET",
35
+ "/api/sn_sc/servicecatalog/requests/#{request_id}",
36
+ params,
37
+ secrets
38
+ )
39
+
40
+ http_request(http_params, {}, {})
41
+ end
42
+
43
+ def self.get_requested_item(params, secrets, _context)
44
+ error = verify_credentials(secrets)
45
+ error ||= verify_requested_item_params(params)
46
+ return ServiceNow.error!({}, :cause => error) if error
47
+
48
+ requested_item_id = params["requested_item_id"]
49
+
50
+ http_params = build_http_params(
51
+ "GET",
52
+ "/api/sn_sc/servicecatalog/items/#{requested_item_id}/get_item_summary",
53
+ params,
54
+ secrets
55
+ )
56
+
57
+ http_request(http_params, {}, {})
58
+ end
59
+
60
+ def self.get_catalogs(params, secrets, _context)
61
+ error = verify_credentials(secrets)
62
+ error ||= verify_get_catalogs_params(params)
63
+ return ServiceNow.error!({}, :cause => error) if error
64
+
65
+ http_params = build_http_params(
66
+ "GET",
67
+ "/api/sn_sc/servicecatalog/catalogs",
68
+ params,
69
+ secrets
70
+ )
71
+
72
+ http_request(http_params, {}, {})
73
+ end
74
+
75
+ def self.get_catalog(params, secrets, _context)
76
+ error = verify_credentials(secrets)
77
+ error ||= verify_get_catalog_params(params)
78
+ return ServiceNow.error!({}, :cause => error) if error
79
+
80
+ catalog_id = params["catalog_id"]
81
+
82
+ http_params = build_http_params(
83
+ "GET",
84
+ "/api/sn_sc/servicecatalog/catalogs/#{catalog_id}",
85
+ params,
86
+ secrets
87
+ )
88
+
89
+ http_request(http_params, {}, {})
90
+ end
91
+
92
+ def self.get_categories(params, secrets, _context)
93
+ error = verify_credentials(secrets)
94
+ error ||= verify_get_categories_params(params)
95
+ return ServiceNow.error!({}, :cause => error) if error
96
+
97
+ http_params = build_http_params(
98
+ "GET",
99
+ "/api/sn_sc/servicecatalog/categories",
100
+ params,
101
+ secrets
102
+ )
103
+
104
+ http_request(http_params, {}, {})
105
+ end
106
+
107
+ def self.get_category(params, secrets, _context)
108
+ error = verify_credentials(secrets)
109
+ error ||= verify_get_category_params(params)
110
+ return ServiceNow.error!({}, :cause => error) if error
111
+
112
+ category_id = params["category_id"]
113
+
114
+ http_params = build_http_params(
115
+ "GET",
116
+ "/api/sn_sc/servicecatalog/categories/#{category_id}",
117
+ params,
118
+ secrets
119
+ )
120
+
121
+ http_request(http_params, {}, {})
122
+ end
123
+
124
+ def self.get_items(params, secrets, _context)
125
+ error = verify_credentials(secrets)
126
+ error ||= verify_get_items_params(params)
127
+ return ServiceNow.error!({}, :cause => error) if error
128
+
129
+ http_params = build_http_params(
130
+ "GET",
131
+ "/api/sn_sc/servicecatalog/items",
132
+ params,
133
+ secrets
134
+ )
135
+
136
+ http_request(http_params, {}, {})
137
+ end
138
+
139
+ def self.get_item(params, secrets, _context)
140
+ error = verify_credentials(secrets)
141
+ error ||= verify_get_item_params(params)
142
+ return ServiceNow.error!({}, :cause => error) if error
143
+
144
+ item_id = params["item_id"]
145
+
146
+ http_params = build_http_params(
147
+ "GET",
148
+ "/api/sn_sc/servicecatalog/items/#{item_id}",
149
+ params,
150
+ secrets
151
+ )
152
+
153
+ http_request(http_params, {}, {})
154
+ end
155
+
156
+ def self.search_items(params, secrets, _context)
157
+ error = verify_credentials(secrets)
158
+ error ||= verify_search_items_params(params)
159
+ return ServiceNow.error!({}, :cause => error) if error
160
+
161
+ http_params = build_http_params(
162
+ "GET",
163
+ "/api/sn_sc/servicecatalog/items",
164
+ params,
165
+ secrets
166
+ )
167
+
168
+ http_request(http_params, {}, {})
169
+ end
170
+
171
+ def self.get_cart(params, secrets, _context)
172
+ error = verify_credentials(secrets)
173
+ error ||= verify_get_cart_params(params)
174
+ return ServiceNow.error!({}, :cause => error) if error
175
+
176
+ http_params = build_http_params(
177
+ "GET",
178
+ "/api/sn_sc/servicecatalog/cart",
179
+ params,
180
+ secrets
181
+ )
182
+
183
+ http_request(http_params, {}, {})
184
+ end
185
+
186
+ def self.add_to_cart(params, secrets, _context)
187
+ error = verify_credentials(secrets)
188
+ error ||= verify_add_to_cart_params(params)
189
+ return ServiceNow.error!({}, :cause => error) if error
190
+
191
+ cart_id = params["cart_id"] || "default"
192
+
193
+ http_params = build_http_params(
194
+ "POST",
195
+ "/api/sn_sc/servicecatalog/cart/#{cart_id}/add_item",
196
+ params,
197
+ secrets,
198
+ :body => build_add_to_cart_body(params)
199
+ )
200
+
201
+ http_request(http_params, {}, {})
202
+ end
203
+
204
+ def self.update_cart_item(params, secrets, _context)
205
+ error = verify_credentials(secrets)
206
+ error ||= verify_update_cart_item_params(params)
207
+ return ServiceNow.error!({}, :cause => error) if error
208
+
209
+ cart_id = params["cart_id"] || "default"
210
+
211
+ http_params = build_http_params(
212
+ "POST",
213
+ "/api/sn_sc/servicecatalog/cart/#{cart_id}/update_item",
214
+ params,
215
+ secrets,
216
+ :body => build_update_cart_item_body(params)
217
+ )
218
+
219
+ http_request(http_params, {}, {})
220
+ end
221
+
222
+ def self.remove_from_cart(params, secrets, _context)
223
+ error = verify_credentials(secrets)
224
+ error ||= verify_remove_from_cart_params(params)
225
+ return ServiceNow.error!({}, :cause => error) if error
226
+
227
+ cart_id = params["cart_id"] || "default"
228
+
229
+ http_params = build_http_params(
230
+ "DELETE",
231
+ "/api/sn_sc/servicecatalog/cart/#{cart_id}/remove_item",
232
+ params,
233
+ secrets,
234
+ :body => build_remove_from_cart_body(params)
235
+ )
236
+
237
+ http_request(http_params, {}, {})
238
+ end
239
+
240
+ def self.empty_cart(params, secrets, _context)
241
+ error = verify_credentials(secrets)
242
+ error ||= verify_empty_cart_params(params)
243
+ return ServiceNow.error!({}, :cause => error) if error
244
+
245
+ cart_id = params["cart_id"] || "default"
246
+
247
+ http_params = build_http_params(
248
+ "DELETE",
249
+ "/api/sn_sc/servicecatalog/cart/#{cart_id}/empty",
250
+ params,
251
+ secrets
252
+ )
253
+
254
+ http_request(http_params, {}, {})
255
+ end
256
+
257
+ def self.checkout_cart(params, secrets, _context)
258
+ error = verify_credentials(secrets)
259
+ error ||= verify_checkout_cart_params(params)
260
+ return ServiceNow.error!({}, :cause => error) if error
261
+
262
+ http_params = build_http_params(
263
+ "POST",
264
+ "/api/sn_sc/servicecatalog/cart/checkout",
265
+ params,
266
+ secrets,
267
+ :body => build_checkout_cart_body(params)
268
+ )
269
+
270
+ http_request(http_params, {}, {})
271
+ end
272
+
273
+ def self.submit_order(params, secrets, _context)
274
+ error = verify_credentials(secrets)
275
+ error ||= verify_submit_order_params(params)
276
+ return ServiceNow.error!({}, :cause => error) if error
277
+
278
+ http_params = build_http_params(
279
+ "POST",
280
+ "/api/sn_sc/servicecatalog/cart/submit_order",
281
+ params,
282
+ secrets,
283
+ :body => build_submit_order_body(params)
284
+ )
285
+
286
+ http_request(http_params, {}, {})
287
+ end
288
+
289
+ private_class_method def self.verify_submit_catalog_item_params(params)
290
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
291
+ return "Missing Parameter: item_sys_id" if params["item_sys_id"].nil?
292
+
293
+ nil
294
+ end
295
+
296
+ private_class_method def self.verify_request_params(params)
297
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
298
+ return "Missing Parameter: request_id" if params["request_id"].nil?
299
+
300
+ nil
301
+ end
302
+
303
+ private_class_method def self.verify_requested_item_params(params)
304
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
305
+ return "Missing Parameter: requested_item_id" if params["requested_item_id"].nil?
306
+
307
+ nil
308
+ end
309
+
310
+ private_class_method def self.verify_get_catalogs_params(params)
311
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
312
+
313
+ nil
314
+ end
315
+
316
+ private_class_method def self.verify_get_catalog_params(params)
317
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
318
+ return "Missing Parameter: catalog_id" if params["catalog_id"].nil?
319
+
320
+ nil
321
+ end
322
+
323
+ private_class_method def self.verify_get_categories_params(params)
324
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
325
+
326
+ nil
327
+ end
328
+
329
+ private_class_method def self.verify_get_category_params(params)
330
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
331
+ return "Missing Parameter: category_id" if params["category_id"].nil?
332
+
333
+ nil
334
+ end
335
+
336
+ private_class_method def self.verify_get_items_params(params)
337
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
338
+
339
+ nil
340
+ end
341
+
342
+ private_class_method def self.verify_get_item_params(params)
343
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
344
+ return "Missing Parameter: item_id" if params["item_id"].nil?
345
+
346
+ nil
347
+ end
348
+
349
+ private_class_method def self.verify_search_items_params(params)
350
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
351
+ return "Missing Parameter: search_term" if params["search_term"].nil?
352
+
353
+ nil
354
+ end
355
+
356
+ private_class_method def self.verify_get_cart_params(params)
357
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
358
+
359
+ nil
360
+ end
361
+
362
+ private_class_method def self.verify_add_to_cart_params(params)
363
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
364
+ return "Missing Parameter: item_id" if params["item_id"].nil?
365
+
366
+ nil
367
+ end
368
+
369
+ private_class_method def self.verify_update_cart_item_params(params)
370
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
371
+ return "Missing Parameter: cart_item_id" if params["cart_item_id"].nil?
372
+
373
+ nil
374
+ end
375
+
376
+ private_class_method def self.verify_remove_from_cart_params(params)
377
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
378
+ return "Missing Parameter: cart_item_id" if params["cart_item_id"].nil?
379
+
380
+ nil
381
+ end
382
+
383
+ private_class_method def self.verify_empty_cart_params(params)
384
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
385
+
386
+ nil
387
+ end
388
+
389
+ private_class_method def self.verify_checkout_cart_params(params)
390
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
391
+
392
+ nil
393
+ end
394
+
395
+ private_class_method def self.verify_submit_order_params(params)
396
+ return "Missing Parameter: instance_id" if params["instance_id"].nil?
397
+
398
+ nil
399
+ end
400
+
401
+ private_class_method def self.build_submit_catalog_item_body(params)
402
+ body = params.except("instance_id", "item_sys_id")
403
+ body["variables"] = params["variables"] if params.key?("variables")
404
+ body
405
+ end
406
+
407
+ private_class_method def self.build_add_to_cart_body(params)
408
+ body = {
409
+ "sysparm_item_id" => params["item_id"]
410
+ }
411
+ body["sysparm_quantity"] = params["quantity"] if params.key?("quantity")
412
+ body["variables"] = params["variables"] if params.key?("variables")
413
+ body
414
+ end
415
+
416
+ private_class_method def self.build_update_cart_item_body(params)
417
+ body = {
418
+ "sysparm_cart_item_id" => params["cart_item_id"]
419
+ }
420
+ body["sysparm_quantity"] = params["quantity"] if params.key?("quantity")
421
+ body["variables"] = params["variables"] if params.key?("variables")
422
+ body
423
+ end
424
+
425
+ private_class_method def self.build_remove_from_cart_body(params)
426
+ {
427
+ "sysparm_cart_item_id" => params["cart_item_id"]
428
+ }
429
+ end
430
+
431
+ private_class_method def self.build_checkout_cart_body(params)
432
+ body = {}
433
+ body["sysparm_cart_id"] = params["cart_id"] if params.key?("cart_id")
434
+ body["special_instructions"] = params["special_instructions"] if params.key?("special_instructions")
435
+ body
436
+ end
437
+
438
+ private_class_method def self.build_submit_order_body(params)
439
+ body = {}
440
+ body["sysparm_cart_id"] = params["cart_id"] if params.key?("cart_id")
441
+ body["requested_for"] = params["requested_for"] if params.key?("requested_for")
442
+ body
443
+ end
444
+ end
445
+ end
446
+ end