-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_app_creator.py
1027 lines (900 loc) · 51.1 KB
/
custom_app_creator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 3 13:51:07 2016
@author: Daniel Koohmarey
@company: Pericror
Copyright (c) Pericror 2016
Dependencies:
sudo pip install requests
"""
import json
import pickle
import requests
import sys
import os
import getpass
from app_creator import AppCreator
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings() # if python version < 2.7.9
class CustomAppCreator(AppCreator):
def __init__(self, instance_prefix, user, pwd, app_name, app_prefix,
prev_state = {}):
self.app_name = app_name
self.app_prefix = app_prefix
self.table_name = 'u_'+ app_name.replace(" ","_").lower()
auth_pair = user, pwd
run_variables = {}
run_variables['app_name'] = app_name
run_variables['app_prefix'] = app_prefix
run_variables['table_name'] = self.table_name
AppCreator.__init__(self, instance_prefix, auth_pair, run_variables,
prev_state)
self.state_map = {
1: (self.check_login_credentials,
"Check if the login credentials are valid."),
2: (self.check_for_custom_table,
"Check if the custom app table already exists."),
3: (self.create_custom_table,
"Create the custom app table & retrieve the app sys id."),
4: (self.setup_custom_app_role,
"Create custom app role and apply to app."),
5: (self.set_role_permissions,
"Add delete role to custom role."),
6: (self.set_custom_group_role,
"Create the group record & assign custom role to group."),
7: (self.create_live_feed_group,
"Create the live feed group."),
8: (self.create_knowledge_base,
"Create the knowledge base & retrieve the knowledge sys id."),
9: (self.create_user_criteria_record,
"Create a user criteria record & retrieve the criteria sys id."),
10: (self.create_can_contribute_record,
"Create a can contribute record."),
11: (self.create_email_notification_records,
"Create email notification records."),
12: (self.create_inbound_email_actions,
"Retrieve the plus sys id & create inbound email actions."),
13: (self.create_modules,
"Retrieve the page sys id & create modules."),
14: (self.create_reports,
"Create reports."),
15: (self.create_assignment_rules,
"Create assignment rules."),
16: (self.setup_slas,
"Create SLAs & save P1-P4 sla sys ids & create escalation rule."),
17: (self.create_catalog_category,
"Create catalog category & save category sys id."),
18: (self.create_record_producer,
"Create record producer & save producer sys id."),
19: (self.create_catalog_item,
"Create catalog item & save item sys id."),
20: (self.add_reports,
"Add reports to overview."),
}
def check_for_custom_table(self):
return self.check_for_table(self.table_name)
def create_custom_table(self):
# Create the custom table
success, log = self.web_driver.create_table(self.app_name,
self.app_prefix,
self.app_name)
if not success:
return success, log
else:
self.log(log)
# Save the created applications sys_id field
url = "https://{}.service-now.com/api/now/table/sys_app_application?" \
"sysparm_query=titleSTARTSWITH{}&sysparm_limit=1".format(
self.instance_prefix, self.app_name)
app_sys_id, log = self.get_json_response_key('sys_id', url)
if app_sys_id:
self.state_variables['app_sys_id'] = app_sys_id
return app_sys_id, log
def setup_custom_app_role(self):
# Create the custom application role
url = "https://{}.service-now.com/api/now/table/sys_user_role".format(self.instance_prefix)
post_data = json.dumps({
'name': self.app_name,
'description': "This role is required for access "\
"to the {} application.".format(self.app_name)
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Update the new application to require the new role
url = "https://{}.service-now.com/api/now/table/sys_app_application/{}".format(
self.instance_prefix, self.state_variables['app_sys_id'])
put_data = json.dumps({
'roles': self.app_name
})
return self.verify_put_data(url, put_data)
def set_role_permissions(self):
# Get table dictionary record sys id
url = "https://{}.service-now.com/api/now/table/sys_dictionary?sysparm_query="\
"name%3D{}%5Einternal_type%3Dcollection&sysparm_limit=1".format(self.instance_prefix, self.table_name)
role_sys_id, log = self.get_json_response_key('sys_id', url)
if not role_sys_id:
return role_sys_id, log
else:
self.state_variables['role_sys_id'] = role_sys_id
self.log(log)
url = "https://{}.service-now.com/api/now/table/sys_dictionary/{}".format(self.instance_prefix,
self.state_variables['role_sys_id'])
put_data = json.dumps({
'delete_roles': self.app_name
})
return self.verify_put_data(url, put_data)
def set_custom_group_role(self):
# Check if the group record already exists
url = "https://{}.service-now.com/api/now/table/sys_user_group?"\
"sysparm_query=nameSTARTSWITH{}&sysparm_limit=1".format(self.instance_prefix, self.app_name)
response = requests.get(url, auth=self.auth_pair, headers=self.json_headers)
if response.status_code == 200:
if response.json()['result']:
return True, "The {} group already exists, skipping creation.".format(self.app_name)
# Create the group record
url = "https://{}.service-now.com/api/now/table/sys_user_group".format(self.instance_prefix)
post_data = json.dumps({
'name': self.app_name,
'description': "This group contains the fulfillers working "\
"in the {} application.".format(self.app_name)
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Assign custom role to group
url = "https://{}.service-now.com/api/now/table/sys_group_has_role".format(self.instance_prefix)
post_data = json.dumps({
'group': self.app_name,
'role': self.app_name
})
return self.verify_post_data(url, post_data)
def create_live_feed_group(self):
# Create the live feed group
url = "https://{}.service-now.com/api/now/table/live_group_profile".format(self.instance_prefix)
post_data = json.dumps({
'name': self.app_name,
'public_group': False,
'visible_group': True,
'short_description': "Discuss and collaborate on topics "\
"regarding {}".format(self.app_name)
})
return self.verify_post_data(url, post_data)
def create_knowledge_base(self):
# Create the knowledgebase and save the sys_id
url = 'https://{}.service-now.com/api/now/table/kb_knowledge_base'.format(self.instance_prefix)
post_data = json.dumps({
'description': "Read self-help articles and learn more about {}".format(self.app_name),
'active': False,
'retire_workflow': '3d18ef12c30031000096dfdc64d3aeb6',
'workflow': 'fbe441019f112100d8f8700c267fcf1a',
'title': self.app_name,
'retire_workflow_type': 'INSTANT',
'workflow_type': 'INSTANT'
})
knowledge_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if knowledge_sys_id:
self.state_variables['knowledge_sys_id'] = knowledge_sys_id
return knowledge_sys_id, log
def create_user_criteria_record(self):
# Create a user criteria record for the new role and save the sys_id
url = "https://{}.service-now.com/api/now/table/user_criteria".format(self.instance_prefix)
post_data = json.dumps({
'role': self.app_name,
'name': self.app_name
})
criteria_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if criteria_sys_id:
self.state_variables['criteria_sys_id'] = criteria_sys_id
return criteria_sys_id, log
def create_can_contribute_record(self):
# Create a can contribute record for the custom role on the knowledgebase
url = "https://{}.service-now.com/api/now/table/kb_uc_can_contribute_mtom".format(self.instance_prefix)
post_data = json.dumps({
'kb_knowledge_base': self.state_variables['knowledge_sys_id'],
'user_criteria': self.app_name
})
return self.verify_post_data(url, post_data)
def create_email_notification_records(self):
# Create email notification record for when custom app commented
url = "https://{}.service-now.com/api/now/table/sysevent_email_action".format(self.instance_prefix)
post_data = json.dumps({
'sysevent_email_action': 'INSERT_OR_UPDATE',
'action_update': True,
'send_self': False,
'name': '{} Commented'.format(self.app_name),
'recipient_fields': "opened_by,assigned_to,watch_list",
'collection': self.table_name,
'condition': 'commentsVALCHANGES^EQ',
'subject': "{} ${{number}} -- comments added".format(self.app_name),
'message_html': """<div>Short Description: ${{short_description}}</div>
<div>Click here to view {}: ${{URI_REF}}</div>
<div><hr/></div>
<div>Priority: ${{priority}}</div>
<div>Comments:</div>
<div>${{comments}}</div>
<div> </div>""".format(self.app_name)
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create email notification record for when custom app closed
post_data = json.dumps({
'sysevent_email_action': 'INSERT_OR_UPDATE',
'action_update': True,
'send_self': False,
'name': "{} Closed".format(self.app_name),
'recipient_fields': "opened_by,watch_list",
'collection': self.table_name,
'condition': 'activeCHANGESTOfalse^EQ',
'subject': "Your {} ${{number}} has been closed".format(self.app_name),
'message_html': """<div>Your {} ${{number}} has been closed."""\
"""Please contact the service desk if you have any questions.</div>
<div>Closed by: ${{closed_by}}</div>
<div> </div>
<div>Short description: ${{short_description}}</div>
<div>Click here to view: ${{URI_REF}}</div>
<div><hr/></div>
<div>Comments:</div>
<div>${{comments}}</div>
<div> </div>""".format(self.app_name)
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create email notification record for when custom app assigned to my group
post_data = json.dumps({
'sysevent_email_action': 'INSERT_OR_UPDATE',
'action_insert': True,
'action_update': True,
'name': "{} assigned to my group".format(self.app_name),
'recipient_fields': 'assignment_group',
'collection': self.table_name,
'condition': 'assigned_toISEMPTY^assignment_groupVALCHANGES^EQ',
'subject': "{} ${{number}} has been assigned to group "\
"${{assignment_group}}".format(self.app_name),
'message_html': """<div>Short Description: ${{short_description}}</div>
<div>Click here to view {}: ${{URI_REF}}</div>
<div><hr/></div>
<div>Priority: ${{priority}}</div>
<div>Comments:</div>
<div>${{comments}}</div>
<div> </div>""".format(self.app_name)
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create email notification record for when custom app assigned to me
post_data = json.dumps({
'sysevent_email_action': 'INSERT_OR_UPDATE',
'action_update': True,
'action_insert': True,
'send_self': False,
'name': "{} assigned to me".format(self.app_name),
'recipient_fields':'assigned_to',
'collection': self.table_name,
'condition': 'assigned_toVALCHANGES^assigned_toISNOTEMPTY^EQ',
'subject': "{} ${{number}} has been assigned to you".format(self.app_name),
'message_html': """<div>Short Description: ${{short_description}}</div>
<div>Click here to view {}: ${{URI_REF}}</div>
<div><hr /></div>
<div>Priority: ${{priority}}</div>
<div>Comments:</div>
<div>${{comments}}</div>
<div> </div>""".format(self.app_name)
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create email notification record for when custom app opened for me
post_data = json.dumps({
'sysevent_email_action': 'INSERT_OR_UPDATE',
'action_insert': True,
'send_self': True,
'name': "{} opened for me".format(self.app_name),
'recipient_fields': 'opened_by',
'collection': self.table_name,
'condition': 'active=true^EQ',
'subject': "{} ${{number}} -- opened on your behalf".format(self.app_name),
'message_html': """<div>The service desk has received your request for help"""\
""" and will respond shortly. If you would like to provide further"""\
""" information about the issue, you may simply reply to this"""\
""" email with any updates.</div>
<div> </div>
<div>Click here to view: ${{URI_REF}}</div>
<div><hr /></div>
<div>Description: ${{short_description}}</div>
<div>Comments:</div>
<div>${{comments}}</div>
<div> </div>""".format(self.app_name)
})
return self.verify_post_data(url, post_data)
def create_inbound_email_actions(self):
# Create subaddress needed for creating new inbound email records
url = "https://{}.service-now.com/api/now/table/email_plus_address".format(self.instance_prefix)
post_data = json.dumps({
'name': self.app_name,
'plus_address': self.app_prefix
})
plus_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if not plus_sys_id:
return plus_sys_id, log
else:
self.state_variables['plus_sys_id'] = plus_sys_id
self.log(log)
# Create custom app inbound email action record
url = "https://{}.service-now.com/api/now/table/sysevent_in_email_action".format(self.instance_prefix)
post_data = json.dumps({
'name': "Create {} Record".format(self.app_name),
'type': 'new',
'table': self.table_name,
'active': True,
'stop_processing': True,
'template': 'contact_type=email^short_descriptionDYNAMICb637bd21ef3221002841f'\
'7f775c0fbb6^commentsDYNAMIC367bf121ef3221002841f7f775c0fbe2^EQ',
'plus_address': self.state_variables['plus_sys_id']
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create custom app from Forward record
post_data = json.dumps({
'name': "Create {} Record (Forwarded)".format(self.app_name),
'type': 'forward',
'table': self.table_name,
'active': True,
'order': '95',
'stop_processing': True,
'filter_condition': 'recipientsLIKE{}+{}@service-now.com^EQ'.format(
self.instance_prefix, self.app_prefix),
'template': 'contact_type=email^short_descriptionDYNAMICb637bd21ef3221002841f7f775c0fbb6'\
'^commentsDYNAMIC367bf121ef3221002841f7f775c0fbe2^EQ'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create custom app from Reply record
post_data = json.dumps({
'name': "Update {} Record (Reply)".format(self.app_name),
'type': 'reply',
'table': self.table_name,
'active': 'true',
'stop_processing': 'true',
'template': 'commentsDYNAMIC367bf121ef3221002841f7f775c0fbe2^EQ'
})
return self.verify_post_data(url, post_data)
def create_modules(self):
# Create sys_portal_page for overview model
url = "https://{}.service-now.com/api/now/table/sys_portal_page".format(self.instance_prefix)
post_data = json.dumps({
'title': "{} Overview".format(self.app_name),
'selectable': False,
'view': '{}_overview'.format(self.app_name),
'roles': 'admin'
})
page_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if not page_sys_id:
return page_sys_id, log
else:
self.state_variables['page_sys_id'] = page_sys_id
self.log(log)
# Create 'Overview' model
url = "https://{}.service-now.com/api/now/table/sys_app_module".format(self.instance_prefix)
post_data = json.dumps({
'title': 'Overview',
'active': True,
'order': '100',
'link_type': 'HOMEPAGE',
'homepage': self.state_variables['page_sys_id'],
'application': self.app_name
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create 'Create New' module
post_data = json.dumps({
'title': "Create New",
'active': True,
'order': '200',
'link_type': 'NEW',
'name': self.table_name,
'application': self.app_name
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create 'Open' module
post_data = json.dumps({
'title': 'Open',
'active': True,
'order': '300',
'link_type': 'LIST',
'name': self.table_name,
'application': self.app_name,
'filter': 'active=true^EQ'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create 'Open - Unassigned' module
post_data = json.dumps({
'title': "Open - Unassigned",
'active': True,
'order': '400',
'link_type': 'LIST',
'name': self.table_name,
'application': self.app_name,
'filter': 'active=true^assigned_toISEMPTY^EQ'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create 'Assigned to me' module
post_data = json.dumps({
'title': "Assigned to me",
'active': True,
'order': '500',
'link_type': 'LIST',
'name': self.table_name,
'application': self.app_name,
'filter': 'active=true^assigned_to=javascript:getMyAssignments()^EQ'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create 'Closed' module
post_data = json.dumps({
'title': 'Closed',
'active': True,
'order': '600',
'link_type': 'LIST',
'name': self.table_name,
'application': self.app_name,
'filter': 'active=false'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create 'All' module
post_data = json.dumps({
'title': 'All',
'active': True,
'order':'700',
'link_type': 'LIST',
'name': self.table_name,
'application': self.app_name
})
return self.verify_post_data(url, post_data)
def create_reports(self):
# Create My Open Custom App Records report
url = "https://{}.service-now.com/api/now/table/sys_report".format(self.instance_prefix)
post_data = json.dumps({
'title': "My Open {} Issues".format(self.app_name),
'table': self.table_name,
'field': '',
'type': 'list',
'filter': 'opened_byDYNAMIC90d1921e5f510100a9ad2572f2b477fe^active=true',
'user': 'GLOBAL'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create Open Custom App Records by Assignment report
post_data = json.dumps({
'title': "Open {} Records by Assignment".format(self.app_name),
'table': self.table_name,
'field': 'assigned_to',
'type': 'bar',
'filter': 'active=true^EQ',
'user': 'GLOBAL'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create Open Custom app Records by Priority report
post_data = json.dumps({
'title': "Opened {} Records this month by Priority".format(self.app_name),
'table': self.table_name,
'field': 'priority',
'type': 'bar',
'filter':'opened_atONThis month@javascript:gs.beginningOfThisMonth()'\
'@javascript:gs.endOfThisMonth()',
'user': 'GLOBAL'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create Open Custom app Records by State report
post_data = json.dumps({
'title': 'Open {} Records by State'.format(self.app_name),
'table': self.table_name,
'field': 'state',
'type': 'pie',
'filter': 'active=true^EQ',
'user': 'GLOBAL'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create Open Custom app Records by Escalation report
post_data = json.dumps({
'title':'Open {} Records by Escalation'.format(self.app_name),
'table': self.table_name,
'field': 'escalation',
'type': 'bar',
'filter': 'active=true^EQ',
'user': 'GLOBAL'
})
return self.verify_post_data(url, post_data)
def add_reports(self):
# Add all created reports to overview page
return self.web_driver.add_reports(self.app_name, [self.app_name], 4, 1) # 4 reports expected, skip the 1st one
def create_assignment_rules(self):
# Create default assignment for all records on new table, new group
url = "https://{}.service-now.com/api/now/table/sysrule_assignment".format(self.instance_prefix)
post_data = json.dumps({
'active': True,
'name': "{} Group Assignment".format(self.app_name),
'table': self.table_name,
'group': self.app_name
})
return self.verify_post_data(url, post_data)
def setup_slas(self):
# Create sysrule_escalate for Custom App Priority 1
url = "https://{}.service-now.com/api/now/table/sysrule_escalate".format(self.instance_prefix)
post_data = json.dumps({
'name': "{} Priority 1".format(self.app_name),
'table': self.table_name,
'description': "Priority 1 should be solved within eight hours"\
" on a 24X7 basis (no calendar).",
'condition': 'priority=1^EQ',
'pause_condition': 'active=false^EQ'
})
p1_sla_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if not p1_sla_sys_id:
return p1_sla_sys_id, log
else:
self.state_variables['p1_sla_sys_id'] = p1_sla_sys_id
self.log(log)
# Create sysrule_escalate for Custom App Priority 2 and save the sla sys_id
post_data = json.dumps({
'name': "{} Priority 2".format(self.app_name),
'table': self.table_name,
'description': "Priority 2 should be solved within tweny four hours"\
" on a 24X7 basis (no calendar).",
'condition': 'priority=2^EQ',
'pause_condition': 'active=false^EQ'
})
p2_sla_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if not p2_sla_sys_id:
return p2_sla_sys_id, log
else:
self.state_variables['p2_sla_sys_id'] = p2_sla_sys_id
self.log(log)
# Create sysrule_escalate for Custom App Priority 3 and save the sla sys_id
post_data = json.dumps({
'name': "{} Priority 3".format(self.app_name),
'table': self.table_name,
'description': "Priority 3 should be solved within three business days"\
" on a 24X7 basis (no calendar).",
'condition': 'priority=3^EQ',
'pause_condition': 'active=false^EQ'
})
p3_sla_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if not p3_sla_sys_id:
return p3_sla_sys_id, log
else:
self.state_variables['p3_sla_sys_id'] = p3_sla_sys_id
self.log(log)
# Create sysrule_escalate for Custom App Priority 4 and save the sla sys_id
post_data = json.dumps({
'name': "{} Priority 4".format(self.app_name),
'table': self.table_name,
'description': "Priority 4 should be solved within five business days on a 24X7 basis (no calendar).",
'condition': 'priority=4^EQ',
'pause_condition': 'active=false^EQ'
})
p4_sla_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if not p4_sla_sys_id:
return p4_sla_sys_id, log
else:
self.state_variables['p4_sla_sys_id'] = p4_sla_sys_id
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 1 Moderate Escalation
url = "https://{}.service-now.com/api/now/table/sysrule_escalate_interval".format(self.instance_prefix)
post_data = json.dumps({
'escalation': self.state_variables['p1_sla_sys_id'],
'escalation_level': '1',
'wait': '1970-01-01 00:15:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 1 High Escalation
post_data = json.dumps({
'escalation': self.state_variables['p1_sla_sys_id'],
'escalation_level': '2',
'wait': '1970-01-01 02:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 1 Overdue Escalation
post_data = json.dumps({
'escalation': self.state_variables['p1_sla_sys_id'],
'escalation_level': '3',
'wait': '1970-01-01 02:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 2 Moderate Escalation
post_data = json.dumps({
'escalation': self.state_variables['p2_sla_sys_id'],
'escalation_level': '1',
'wait': '1970-01-01 08:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 2 High Escalation
post_data = json.dumps({
'escalation': self.state_variables['p2_sla_sys_id'],
'escalation_level': '2',
'wait': '1970-01-01 08:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 2 Overdue Escalation
post_data = json.dumps({
'escalation': self.state_variables['p2_sla_sys_id'],
'escalation_level': '3',
'wait': '1970-01-01 08:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 3 Moderate Escalation
post_data = json.dumps({
'escalation': self.state_variables['p3_sla_sys_id'],
'escalation_level': '1',
'wait': '1970-01-01 24:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 3 High Escalation
post_data = json.dumps({
'escalation': self.state_variables['p3_sla_sys_id'],
'escalation_level': '2',
'wait': '1970-01-01 24:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 3 Overdue Escalation
post_data = json.dumps({
'escalation': self.state_variables['p3_sla_sys_id'],
'escalation_level': '3',
'wait': '1970-01-01 24:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 4 Moderate Escalation
post_data = json.dumps({
'escalation': self.state_variables['p4_sla_sys_id'],
'escalation_level': '1',
'wait': '1970-01-01 48:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 4 High Escalation
post_data = json.dumps({
'escalation': self.state_variables['p4_sla_sys_id'],
'escalation_level': '2',
'wait': '1970-01-01 48:00:00'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create sysrule_escalate_interval for Custom App Priority 4 Overdue Escalation
post_data = json.dumps({
'escalation': self.state_variables['p4_sla_sys_id'],
'escalation_level': '3',
'wait': '1970-01-01 24:00:00'
})
return self.verify_post_data(url, post_data)
def create_catalog_category(self):
# Create catalog category for Custom App Services and save category sys_id
url = "https://{}.service-now.com/api/now/table/sc_category".format(self.instance_prefix)
post_data = json.dumps({
'active': True,
'sc_catalog': 'e0d08b13c3330100c8b837659bba8fb4',
'title': "{} Services".format(self.app_name),
'description': "Request goods and services from the {} group.".format(self.app_name)
})
catalog_category_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if catalog_category_sys_id:
self.state_variables['catalog_category_sys_id'] = catalog_category_sys_id
return catalog_category_sys_id, log
def create_record_producer(self):
# Create Custom App Record Producer in created catalog category and save record producer sys_id
url = "https://{}.service-now.com/api/now/table/sc_cat_item_producer".format(self.instance_prefix)
post_data = json.dumps({
'name': "{} Record Producer".format(self.app_name),
'table_name': self.table_name,
'active': True,
'category': self.state_variables['catalog_category_sys_id'],
'short_description': "Get {} help".format(self.app_name),
'description': "Submit an issue or question regarding {} services.".format(self.app_name)
})
record_producer_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if not record_producer_sys_id:
return record_producer_sys_id, log
else:
self.state_variables['record_producer_sys_id'] = record_producer_sys_id
self.log(log)
# Create short description variable
url = "https://{}.service-now.com/api/now/table/item_option_new".format(self.instance_prefix)
post_data = json.dumps({
'map_to_field': True,
'field': 'short_description',
'type': "Single Line Text",
'mandatory': True,
'cat_item': self.state_variables['record_producer_sys_id'],
'question_text': "What is your question or issue?",
'order': '10'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create description variable
post_data = json.dumps({
'map_to_field': True,
'field': 'additional_comments',
'type': "Multi Line Text",
'mandatory': False,
'cat_item': self.state_variables['record_producer_sys_id'],
'question_text': "Please provide any additional information below:",
'order': '20'
})
return self.verify_post_data(url, post_data)
def create_catalog_item(self):
# Create Custom App catalog item in created catalog category
url = "https://{}.service-now.com/api/now/table/sc_cat_item".format(self.instance_prefix)
post_data = json.dumps({
'name': "{} Item".format(self.app_name),
'active': True,
'category': self.state_variables['catalog_category_sys_id'],
'delivery_plan': '523da512c611228900811a37c97c2014',
'short_description': "Request item or service from {}".format(self.app_name),
'description': "Submitting this form will begin the standard "\
"request fulfillment process for a {} item.".format(self.app_name),
'sc_catalogs': 'e0d08b13c3330100c8b837659bba8fb4'
})
catalog_item_sys_id, log = self.get_json_response_key('sys_id', url, post_data)
if catalog_item_sys_id:
self.state_variables['catalog_item_sys_id'] = catalog_item_sys_id
url = "https://{}.service-now.com/api/now/table/item_option_new".format(self.instance_prefix)
# Create short description variable
post_data = json.dumps({
'map_to_field': True,
'type': "Single Line Text",
'mandatory': True,
'cat_item': self.state_variables['catalog_item_sys_id'],
'question_text': "What item or service are you requesting?",
'order': '10'
})
success, log = self.verify_post_data(url, post_data)
if not success:
return success, log
else:
self.log(log)
# Create description variable
post_data = json.dumps({
'map_to_field': True,
'type': "Multi Line Text",
'mandatory': False,
'cat_item': self.state_variables['catalog_item_sys_id'],
'question_text': "Please provide any additional information below:",
'order': '20'
})
return self.verify_post_data(url, post_data)
if __name__ == '__main__':
state_data = {}
if(len(sys.argv) == 2 and sys.argv[1] == 'interactive'):
instance_prefix = raw_input("Enter Instance Prefix:\n").strip()
user = raw_input("Enter User:\n").strip()
password = getpass.getpass("Enter Password:\n")
app_name = raw_input("Enter App Name:\n").strip()
app_prefix = raw_input("Enter App Prefix:\n").strip()
print "Running Custom App Creator with the following settings:\n"\
"Instance Prefix: {0}\nUser: {1}\nPass: {2}\nApp Name: {3}\n"\
"App Prefix: {4}".format(instance_prefix, user, "*"*len(password),
app_name, app_prefix)
app_creator = CustomAppCreator(instance_prefix, user, password, app_name,