-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_now_apis.py
191 lines (159 loc) · 7.23 KB
/
service_now_apis.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
#!/usr/bin/env python3
# developed by Gabi Zapodeanu, TSA, GPO, Cisco Systems
# This file contains the ServiceNow functions to be used during the demo
import requests
import json
import utils
from config import SNOW_ADMIN, SNOW_DEV, SNOW_PASS, SNOW_URL
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # Disable insecure https warnings
# users roles :
# SNOW_ADMIN = Application Admin
# SNOW_DEV = Device REST API Calls
def get_last_incidents_list(incident_count):
"""
This function will return the numbers for the last {incident_count} number of incidents
:param incident_count: number of incidents
:return: incident list - list with all incidents numbers
"""
url = SNOW_URL + '/table/incident?sysparm_limit=' + str(incident_count)
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.get(url, auth=(SNOW_ADMIN, SNOW_PASS), headers=headers)
incident_json = response.json()
incident_info = incident_json['result']
incident_list = []
for incident in incident_info:
incident_list.append(incident['number'])
return incident_list
def get_last_incidents_info(incident_count):
"""
This function will return the info for the last {incident_count} number of incidents
:param incident_count: number of incidents
:return: incident info - info for all incidents
"""
url = SNOW_URL + '/table/incident?sysparm_limit=' + str(incident_count)
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.get(url, auth=(SNOW_ADMIN, SNOW_PASS), headers=headers)
incident_json = response.json()
incident_info = incident_json['result']
return incident_info
def get_incident_detail(incident):
"""
This function will return the incident information for the incident with the number {incident}
:param incident: incident number
:return: incident info
"""
incident_sys_id = get_incident_sys_id(incident)
url = SNOW_URL + '/table/incident/' + incident_sys_id
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.get(url, auth=(SNOW_ADMIN, SNOW_PASS), headers=headers)
incident_json = response.json()
return incident_json['result']
def create_incident(description, comment, username, severity):
"""
This function will create a new incident with the {description}, {comments} for the {user}
:param description: incident short description
:param comment: comment with incident details
:param username: caller username
:param severity: urgency level
:return: incident number
"""
caller_sys_id = get_user_sys_id(username)
url = SNOW_URL + '/table/incident'
payload = {'short_description': description,
'comments': (comment + '\n\nCreated using APIs by caller: ' + username),
'caller_id': caller_sys_id,
'urgency': severity,
'priority': severity
}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.post(url, auth=(username, SNOW_PASS), data=json.dumps(payload), headers=headers)
incident_json = response.json()
return incident_json['result']['number']
def update_incident(incident, comment, username):
"""
:param incident: incident number
:param comment: comment with incident details
:param username: caller username
:return:
"""
caller_sys_id = get_user_sys_id(username)
incident_sys_id = get_incident_sys_id(incident)
url = SNOW_URL + '/table/incident/' + incident_sys_id
payload = {'comments': (comment + ',\n\nUpdated using APIs by caller: ' + username),
'caller_id': caller_sys_id}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.patch(url, auth=(username, SNOW_PASS), data=json.dumps(payload), headers=headers)
def get_incident_sys_id(incident):
"""
This function will find the incident sys_id for the incident with the number {incident}
:param incident: incident number
:return: incident sys_id
"""
url = SNOW_URL + '/table/incident?sysparm_limit=1&number=' + incident
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.get(url, auth=(SNOW_ADMIN, SNOW_PASS), headers=headers)
incident_json = response.json()
return incident_json['result'][0]['sys_id']
def close_incident(incident, username):
"""
This function will close the incident with the number {incident}
:param incident: incident number
:param username: user that calls in to close ticket
:return: status code
"""
incident_id = get_incident_sys_id(incident)
caller_id = get_user_sys_id(username)
url = SNOW_URL + '/table/incident/' + incident_id
payload = {'close_code': 'Closed/Resolved by Caller',
'state': '7',
'caller_id': caller_id,
'close_notes': ('Closed using APIs by caller: ' + username)}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.put(url, auth=(username, SNOW_PASS), data=json.dumps(payload), headers=headers)
def get_user_sys_id(username):
"""
This function will retrieve the user sys_id for the user with the name {username}
:param username: the username
:return: user sys_id
"""
url = SNOW_URL + '/table/sys_user?sysparm_limit=1&name=' + username
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.get(url, auth=(username, SNOW_PASS), headers=headers)
user_json = response.json()
return user_json['result'][0]['sys_id']
def get_incident_comments(incident):
"""
This function will return the comments for an incident with the number {incident_number}
:param incident: incident_number
:return: incident comments
"""
incident_sys_id = get_incident_sys_id(incident)
url = SNOW_URL + '/table/sys_journal_field?sysparm_query=element_id=' + incident_sys_id
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.get(url, auth=(SNOW_ADMIN, SNOW_PASS), headers=headers)
comments_json = response.json()['result']
return comments_json
def delete_incident(incident):
"""
This function will delete the incident with the number {incident}
:param incident: incident number
:return: status code
"""
incident_id = get_incident_sys_id(incident)
url = SNOW_URL + '/table/incident/' + incident_id
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
response = requests.delete(url, auth=(SNOW_ADMIN, SNOW_PASS), headers=headers)
return response.status_code
def find_comment(incident, comment):
"""
Find if any of the existing comments from the {incident} matches exactly the {comment}
:param incident: incident number
:param comment: comment string to serach for
:return: {True} if comment exist, {False} if not
"""
comments_list = get_incident_comments(incident)
for comment_info in comments_list:
if comment == comment_info['value']:
return True
return False