forked from signalfx/victorops-field-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org_build_and_scrub.py
193 lines (161 loc) · 5.21 KB
/
org_build_and_scrub.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
#!/usr/bin/python
#python3 - updated 4.5.19
import requests
import sys
import json
import time
def getUsers(orgHead):
t = time.perf_counter()
req = requests.get('https://api.victorops.com/api-public/v1/user', headers = orgHead)
#RATE LIMITS
while req.status_code == 403:
#wait until minute has passed
print(('waiting ', str(t + 60 - time.perf_counter()), 'seconds. . .'))
time.sleep(t + 60 - time.perf_counter())
#reset timer to current time
t = time.perf_counter()
#retry
req = requests.get('https://api.victorops.com/api-public/v1/user', headers=orgHead)
if req.status_code == 200:
#return list of usernames
usernames = []
get = json.loads(req.text)['users'][0]
for k in get:
usernames.append(k['username'])
return usernames
#NON-RATE LIMIT FAILURE
else:
print('Failed to get usernames . . .')
def deleteAllUsers(orgHead, users, repU):
for u in users:
if u == repU:
continue
else:
deleteUser(orgHead, u, repU)
def deleteUser(orgHead, delU, repU):
t = time.perf_counter()
body = {
'replacement':repU
}
req = requests.delete('https://api.victorops.com/api-public/v1/user/' + delU, headers=orgHead, data = json.dumps(body))
#RATE LIMITS
while req.status_code == 403:
#wait until minute has passed
print(('waiting ', str(t + 60 - time.perf_counter()), 'seconds. . .'))
time.sleep(t + 60 - time.perf_counter())
#reset timer to current time
t = time.perf_counter()
#retry
req = requests.delete('https://api.victorops.com/api-public/v1/user/' + delU, headers=orgHead, data = json.dumps(body))
if req.status_code == 200:
print(('\t 200 - ' + delU + ' was deleted'))
#NON-RATE LIMIT FAILURE
else:
print(('Failed to get usernames . . .\n\t\t' + str(json.loads(req.text))))
def getTeams(orgHead):
t = time.perf_counter()
req = requests.get('https://api.victorops.com/api-public/v1/team', headers = orgHead)
#RATE LIMITS
while req.status_code == 403:
#wait until minute has passed
print(('waiting ', str(t + 60 - time.perf_counter()), 'seconds. . .'))
time.sleep(t + 60 - time.perf_counter())
#reset timer to current time
t = time.perf_counter()
#retry
req = requests.get('https://api.victorops.com/api-public/v1/team', headers=orgHead)
if req.status_code == 200:
#return list of usernames
teams = []
get = json.loads(req.text)
for k in get:
teams.append([k['slug'], k['name']])
return teams
#NON-RATE LIMIT FAILURE
else:
print('Failed to get usernames . . .')
def deleteTeam(orgHead, team):
t = time.perf_counter()
req = requests.delete('https://api.victorops.com/api-public/v1/team/' + team[0], headers=orgHead)
#RATE LIMITS
while req.status_code == 403:
#wait until minute has passed
print(('waiting ', str(t + 60 - time.perf_counter()), 'seconds. . .'))
time.sleep(t + 60 - time.perf_counter())
#reset timer to current time
t = time.perf_counter()
#retry
req = requests.delete('https://api.victorops.com/api-public/v1/team/' + team[0], headers=orgHead)
if req.status_code == 200:
print(('\t 200 - ' + team[1] + ' was deleted'))
#NON-RATE LIMIT FAILURE
else:
print(('Failed to delete ' + team[1] + ' . . .\n\t\t' + str(json.loads(req.text))))
def deleteAllTeams(orgHead, teams):
for t in teams:
deleteTeam(orgHead, t)
def printList(l, num=False):
for i, val in enumerate(l):
print((str(i) + '. ' + str(val)))
def main():
userList = []
userIn = int(eval(input('Enter \'0\' to use vops-jdsouza, enter anything else to set other VO account.\n')))
# Set Default VictorOps Account (i.e. Test Account)
apiAuth = {
'Content-Type': 'application/json',
'X-VO-Api-Id': '[API ID]',
'X-VO-Api-Key': '[API KEY]',
'Accept': 'application/json'
}
if userIn == 0:
apiAuth = {
'Content-Type': 'application/json',
'X-VO-Api-Id': '[API ID]',
'X-VO-Api-Key': '[API KEY]',
'Accept': 'application/json'
}
else:
apiAuth = {
'Content-Type': 'application/json',
'X-VO-Api-Id': str(userIn),
'X-VO-Api-Key': '',
'Accept': 'application/json'
}
#userIn = input('Enter the api key:\n')
#apiAuth['X-VO-Api-Key'] = str(userIn)
while True:
userIn = int(eval(input('\nEnter a number for,\n\t1. Get and Print Users\n\t2. Delete Users\n\t3. Get and Print Teams\n\t4. Delete Teams\n\t5. Nuke\n\t6. SUPERNUKE\n\t0. Exit\n>')))
#GET USERS
if userIn == 1:
userList = getUsers(apiAuth)
printList(userList)
#DELETE ALL USERS
elif userIn == 2:
printList(userList)
userIn = eval(input('\nSelect one user from above to keep:'))
deleteAllUsers(apiAuth, userList, userList[userIn])
#GET TEAMS
elif userIn == 3:
teamList = getTeams(apiAuth)
printList(teamList)
#DELETE TEAMS
elif userIn == 4:
deleteAllTeams(apiAuth, teamList)
elif userIn ==5:
printList(userList)
userIn = int(eval(input('\nSelect one user from above to keep:')))
deleteAllUsers(apiAuth, userList, userList[userIn])
deleteAllTeams(apiAuth, teamList)
elif userIn == 6:
userList = getUsers(apiAuth)
teamList = getTeams(apiAuth)
printList(userList)
userIn = int(eval(input('\nSelect one user from above to keep:')))
deleteAllTeams(apiAuth, teamList)
deleteAllUsers(apiAuth, userList, userList[userIn])
elif userIn == 0:
sys.exit()
else:
print('invalid input')
if __name__ == '__main__':
main()