forked from signalfx/victorops-field-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usernameChange.py
321 lines (231 loc) · 8.49 KB
/
usernameChange.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
#!/usr/bin/python
#usernameChange
#
#this script is designed to take in a username and replace them with a new username with
#the same teams, personal paging policy, permissions and contact methods
#
#More information on the public API can be found at:
#
#https://portal.victorops.com/public/api-docs.html
#
#Note, there is no way to include mobile devices
#
#usage:
# ./usernameChange.py <apiId> <apiKey> <current_username> <new_username> <boolean_is_admin>
#
#for example:
# ./usernameChange.py 32caec21 2527dc8405e99eb0840995ea51c0276f dumbolduser newsmartuser True
#
#this line tests with org "cordis-testing-arena", as well the username(s) listed:
#
#./usernameChange.py 5f026e9a 190fb368cdd27f5a7bf25d32a4a1436e chall newusernameperson True
import datetime
import requests
import sys
import json
import time
class org:
"""This class contains the organization information and operates as the public api in this script"""
#all org objects must have an apiId and apiKey
def __init__(self, apiId, apiKey):
self.apiId = apiId
self.apiKey = apiKey
self.genericHeader = {
'Content-Type': 'application/json',
'X-VO-Api-Id': apiId,
'X-VO-Api-Key': apiKey,
'Accept': 'application/json'
}
self.baseURL = 'https://api.victorops.com/api-public'
#print org info
def printOrg(self):
print '\n--------------------------------------------------\n ORG INFO:\n\n API-ID:\t' + self.apiId + '\n' + ' API-Key:\t' + self.apiKey + '\n\n--------------------------------------------------\n\n'
pass
def rmUser(self, newUsername, oldUsername):
url = self.baseURL + '/v1/user/' + oldUsername
body = {
"replacement":newUsername
}
req = requests.delete(url, headers = self.genericHeader, data = json.dumps(body))
if req.status_code != 200: #ERROR
print "\n********\n","ERROR(",req.status_code,"): ", "\n\t",req.text,"\n********\n"
else: #SUCCESS
print "SUCCESS (", req.status_code, "\n"
pass
def postUser(self, first, last, username, email, admin):
url = self.baseURL + '/v1/user'
body = {
"firstName": first,
"lastName": last,
"username": username,
"email": "chall+ieurw3b@victorops.com",
"admin": admin,
"expirationHours": 24
}
req = requests.request('POST', url, headers = self.genericHeader, data = json.dumps(body))
if req.status_code != 200: #ERROR
print "\n********\n","ERROR(",req.status_code,"): ", "\n\t",req.text,"\n********\n"
else: #SUCCESS
print "SUCCESS (", req.status_code, "\n"
pass
def postPhones(self, username, phones):
ids = []
url = self.baseURL +'/v1/user/' + username + '/contact-methods/phones'
for x in xrange(0, len(phones)):
body = {
"label": phones[x][0],
"phone": phones[x][1],
"rank": 0
}
print body
req = requests.request('POST', url, headers = self.genericHeader, data = json.dumps(body))
if req.status_code != 200: #ERROR
print "\n********\n","ERROR(",req.status_code,"): ", "\n\t",req.text,"\n********\n"
else: #SUCCESS
print "SUCCESS (", req.status_code, "\n"
#log contact id
ids.append(json.loads(req.text)['id'])
print ids
return ids
def postEmails(self, username, emails):
url = self.baseURL +'/v1/user/' + username + '/contact-methods/emails'
#check if only one email. If so, exit as this should already be included with the user creation
#this is also why the index of emails starts at 1 instead of 0
for x in xrange(1, len(emails)):
body = {
"label": emails[x][0],
"email": emails[x][1],
"rank": 0
}
req = requests.request('POST', url, headers = self.genericHeader, data = json.dumps(body))
if req.status_code != 200: #ERROR
print "\n********\n","ERROR(",req.status_code,"): ", "\n\t",req.text,"\n********\n"
else: #SUCCESS
print "SUCCESS (", req.status_code, "\n"
pass
def getUserInfo(self, username):
print 'Getting User Information'
url = self.baseURL+'/v1/user/' + username
print url
req = requests.get(url, headers = self.genericHeader)
#print json.loads(req.text)
return json.loads(req.text)
def getContactInfo(self, username):
print 'Getting user contact information'
url = self.baseURL + '/v1/user/' + username + '/contact-methods'
req = requests.get(url, headers=self.genericHeader)
contactInfo = self.formatContactMethods(json.loads(req.text))
#print json.loads(req.text)
return contactInfo
def formatContactMethods(self, contactInfo):
phones = []
emails = []
#devices = [] can't post to user, not worth having right now
for x in xrange(0,len(contactInfo['phones']['contactMethods'])):
phones.append([contactInfo['phones']['contactMethods'][x]['label'],contactInfo['phones']['contactMethods'][x]['value']])
for x in xrange(0,len(contactInfo['emails']['contactMethods'])):
emails.append([contactInfo['emails']['contactMethods'][x]['label'],contactInfo['emails']['contactMethods'][x]['value']])
# for x in xrange(0,len(contactInfo['devices']['contactMethods'])):
# print contactInfo['devices']['contactMethods'][x]['label']
# print contactInfo['devices']['contactMethods'][x]['value']
devices = {"phones": phones, "emails": emails}
return devices
# def getEmail(self, username):
# url = self.baseURL + '/v1/'
# pass
# def getContactID(self, username, method, labels):
# url = self.baseURL + '/v1/user/' + username + '/contact-methods/' + method
# id = []
# for x in xrange(0,len(labels)):
# req = requests.get(url, headers = self.genericHeader)
# tmp = json.loads(req.text)['contactMethods']
# for y in xrange(0, len(tmp)):
# if labels[x][0] == tmp[y]['label']:
# print tmp[y]['id']
# pass
def allTeamsHelper(self):
print 'Getting list of all teams . . .\n'
teamsArray = []
url = self.baseURL + '/v1/team'
req = requests.get(url, headers = self.genericHeader)
temp = json.loads(req.text)
for element in json.loads(req.text):
if element['memberCount'] != 0:
teamsArray.append(element['slug'])
#print teamsArray[-1], " \n"
return teamsArray
def getTeamInfo(self):
print 'Getting team members for all teams . . . \n'
teamsArray = self.allTeamsHelper()
url = self.baseURL+'/v1/team/'
for x in xrange(0,len(teamsArray)):
req = requests.get(url + teamsArray[x] + '/members', headers = self.genericHeader)
#print json.loads(req.text)
def getPolicy(self):
pass
class user:
#All user information is initialized to blank values
pagingPolicy = [] #will be built as an array of JSON steps
teams = []
isAdmin = False
phones = []
emails = []
first = ''
last = ''
#Initialization must have both the new and old usernames
def __init__(self, username):
self.username = username
isAdmin = True
#print all user information
def printUser(self):
print '--------------------------------------------------\n\n USER INFO\n\n username:\t' + self.username + '\n\n--------------------------------------------------\n'
pass
def setTeams(self, teamArray):
pass
def setEmail(self, email):
self.emails = email
print 'user email set to ' , self.emails, '\n'
def setPhones(self, phones):
self.phones = phones
print 'user phones set to ', self.phones, '\n'
def setName(self, first, last):
self.first = first
self.last = last
print 'user\'s name is ' + self.first, self.last + '\n'
def setPolicy(self):
pass
def main():
#build objects and print them out
organization = org(sys.argv[1], sys.argv[2])
newUser = user(sys.argv[4])
oldUser = user(sys.argv[3])
#print initialized objects
organization.printOrg()
newUser.printUser()
oldUser.printUser()
#user information
userInfo = organization.getUserInfo(oldUser.username)
print userInfo
#set userInfo
#myUser.setEmail(userInfo['email'])
newUser.setName(userInfo['firstName'], userInfo['lastName'])
#get user contact information
oldContactInfo = organization.getContactInfo(sys.argv[3])
#set user contact information
newUser.setEmail(oldContactInfo['emails'])
newUser.setPhones(oldContactInfo['phones'])
#get and print paging policy
#organization.getContactID(newUser.username, 'emails', newUser.emails)
#Change condition to acually post stuff, keep at false for testing
if True:
#create new user
organization.postUser(newUser.first, newUser.last, newUser.username, newUser.emails[0][1], newUser.isAdmin)
#post user contact information
organization.postEmails(newUser.username, newUser.emails)
organization.postPhones(newUser.username, newUser.phones)
#Now get the id's
#remove old user
#organization.rmUser(oldUser.newName, oldUser.oldName)
#organization.getTeamInfo()
if __name__ == "__main__":
main()