forked from meraki/webhook-payload-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRUD_Webhooks.py
executable file
·483 lines (433 loc) · 14.3 KB
/
CRUD_Webhooks.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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#
# --------------------------------------------------------
#
__author__ = "Thomas Sterber"
__copyright__ = "October 2021"
__license__ = "GPL"
__email__ = "thomas.sterber@meraki.net"
__status__ = "demo"
# --------------------------------------------------------
# Import Modules
# --------------------------------------------------------
#
import time, os, sys, json
import warnings
#
try:
from requests.packages.urllib3 import exceptions
import requests
except:
os.system('pip3 install requests')
time.sleep(2)
from requests.packages.urllib3 import exceptions
import requests
# --------------------------------------------------------
# static variablen
# --------------------------------------------------------
#
api_url = 'https://api.meraki.com/api/v1'
line = 80*'-'
infotext = """
./(((((((((((((((((/.
*(((((((((((((((((((((((((((((
.(((((((((((((((((((((((((((((((((((/
((((((((((((((((((((((((((((((((((((((((/
,((((((((((((((((((((((((((((((((((((((((((((
.(((((((((((((((((((( ((((((/ ((((((((((,
(((((((((((((((((((((( ((((((/ (((((((((((
/((((((((((((((((((((((((((((((((((((((((((((((((((((
((((((((((((((((((((((((((((((((((((((((((((((((((((((*
((((((((((((((((((((((((((((((((((((((((((((((((((((((((
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((
(((((((((((((((((((((((( ((((((((((((((/ (((((((((
,(((((((((((((((((((((((( ((((((((((((((/ ((((((((/
((((((((((((((((((((((((( .//////////////* .((((((((
,(((((((((((((((((((((((((((((/ ((((((((((((.
((((((((((((((((((((((((((((((/ (((((((((((
(((((((((((((((((((((((((((((((((((((((((((((((((((((((*
.(((((((((((((((((((((((((((((((((((((((((((((((((((((*
/((((((((((((((((((((((((((((((((((((((((((((((((((*
(((((((((((((((((((((((((((((((((((((((((((((((*
(((((((((((/. ....
(((((((/
(((((
(((
/. Welcome to the power of the Meraki API
This is a demo on how to manage custom Webhook-payloadTemplates
If you like to upload your custom Webhook-payloadTemplate,
the headers.liquid and body.liquid file needs to be in the same dir as this script.
The use of this tool is on your own risk.
Please visit: https://meraki.io
"""
# -----------------------------------------------
# Functions
# -----------------------------------------------
def select_org():
"""get orgid's for given accesstoken and select one"""
#
url = api_url + '/organizations'
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
orgas = response.json()
# print(json.dumps(orgas, sort_keys=True, indent=4))
#
print('\n')
dic = {}
count=0
for n in orgas:
orgname = str(n['name'])
orgid = str(n['id'])
print(str(count).rjust(3,' '),' ', orgname.ljust(20, ' '), orgid)
dic[count] = orgid
count += 1
print('\n')
select = input('select org >')
organizationId = dic[int(select)]
#
return(organizationId)
def select_network(organizationId):
""" get all networks of organizationId and select one"""
#
resource = f'/organizations/{organizationId}/networks'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
# print(json.dumps(re, sort_keys=True, indent=4))
#
dic = {}
count=0
for n in re:
networkname = str(n['name'])
networkId = str(n['id'])
print(str(count).rjust(3,' '),' ', networkname.ljust(20, ' '), networkId)
dic[count] = networkId
count += 1
print('\n')
select = input('select network >')
# print(dic)
# print(dic[int(select)])
networkId = dic[int(select)]
#
return(networkId)
def end():
""" end script """
sys.exit()
def list_all_payloadTemplates():
""" get all payloadTemplates of network """
#
resource = f'/networks/{networkId}/webhooks/payloadTemplates'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
# print(json.dumps(re, sort_keys=True, indent=4))
print(line)
count=0
for n in re:
payloadTemplateName = str(n['name'])
payloadTemplateId = str(n['payloadTemplateId'])
payloadTemplateType = str(n["type"])
print(str(count).rjust(3,' '),' ', payloadTemplateName.ljust(20, ' '), payloadTemplateId.ljust(20, ' '), payloadTemplateType)
count += 1
print(line)
#
return()
def list_all_included_payloadTemplates():
""" get all payloadTemplates of the network """
#
resource = f'/networks/{networkId}/webhooks/payloadTemplates'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
# print(json.dumps(re, sort_keys=True, indent=4))
print(line)
count=0
for n in re:
if str(n["type"]) == "included":
payloadTemplateName = str(n['name'])
payloadTemplateId = str(n['payloadTemplateId'])
payloadTemplateType = str(n["type"])
print(str(count).rjust(3,' '),' ', payloadTemplateName.ljust(20, ' '), payloadTemplateId)
count += 1
print(line)
#
return()
def list_all_custom_payloadTemplates():
""" get all payloadTemplates of the network """
#
resource = f'/networks/{networkId}/webhooks/payloadTemplates'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
# print(json.dumps(re, sort_keys=True, indent=4))
print(line)
count=0
for n in re:
if str(n["type"]) == "custom":
payloadTemplateName = str(n['name'])
payloadTemplateId = str(n['payloadTemplateId'])
payloadTemplateType = str(n["type"])
print(str(count).rjust(3,' '),' ', payloadTemplateName.ljust(20, ' '), payloadTemplateId)
count += 1
print(line)
#
return()
def select_a_payloadTemplate():
""" get a payloadTemplate of the network """
#
resource = f'/networks/{networkId}/webhooks/payloadTemplates'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
# print(json.dumps(re, sort_keys=True, indent=4))
#
dic = {} # {count:'id'} create dic
count=0
for n in re:
payloadTemplateName = str(n['name'])
payloadTemplateId = str(n['payloadTemplateId'])
payloadTemplateType = str(n["type"])
print(str(count).rjust(3,' '),' ', payloadTemplateName.ljust(20, ' '), payloadTemplateId)
dic[count] = payloadTemplateId
count += 1
print('\n')
select = input('select template >')
payloadTemplateId = dic[int(select)]
#
return(payloadTemplateId)
def display_a_payloadTemplate():
""" display a selected payloadTemplate """
#
# select payloadTemplate
payloadTemplateId = select_a_payloadTemplate()
#
resource = f'/networks/{networkId}/webhooks/payloadTemplates/{payloadTemplateId}'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
# print(json.dumps(re, sort_keys=True, indent=4))
#
os.system('clear') # clear screen
print(line)
name = re['name']
print(f"Template '{name}' content :")
print(line)
for key in re:
print('\n')
print(key)
print('---------------------')
print(re[key])
print(line)
#
return()
def select_a_custom_payloadTemplate():
""" get custom payloadTemplates of the network """
#
resource = f'/networks/{networkId}/webhooks/payloadTemplates'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
# print(json.dumps(re, sort_keys=True, indent=4))
#
dic = {} # {count:'id'} create dic
count=0
for n in re:
if str(n["type"]) == "custom":
payloadTemplateName = str(n['name'])
payloadTemplateId = str(n['payloadTemplateId'])
payloadTemplateType = str(n["type"])
print(str(count).rjust(3,' '),' ', payloadTemplateName.ljust(20, ' '), payloadTemplateId)
dic[count] = payloadTemplateId
count += 1
print('\n')
select = input('select template >')
# print(dic)
# print(dic[int(select)])
payloadTemplateId = dic[int(select)]
#
return(payloadTemplateId)
def delete_a_custom_payloadTemplate():
""" delete a selected payloadTemplate """
#
# select custom payloadTemplate
payloadTemplateId = select_a_custom_payloadTemplate()
#
# delete payloadTemplateId in network
resource = f'/networks/{networkId}/webhooks/payloadTemplates/{payloadTemplateId}'
url = api_url + resource
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.delete(url, headers=headers, verify=False)
print('Status_code : ', response.status_code)
print('Status_text : ', response.text)
#
return()
def display_webhooks_alert_types():
""" download and display actual webhook alert types """
#
url = api_url + f'/organizations/{organizationId}/webhooks/alertTypes'
#
headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.get(url, headers=headers, verify=False)
re = response.json()
data = json.dumps(re, sort_keys=True, indent=4)
# display alert types
print(data)
# save re to file
print ("result will be stored in 'webhook_alert_types.txt'")
f = open('webhook_alert_types.txt', 'w')
f.write(data)
f.close
print("done")
#
return()
def upload_a_custom_payloadTemplate():
""" upload custom payloadTemplate """
#
# read *.liquid files in working dir
dirlist = os. listdir()
#print (dirlist)
#
# select headers.liquid file
dic = {}
count=0
print('\n')
for filename in dirlist:
#print(filename)
if (filename[-14:] == "headers.liquid"):
print(str(count).rjust(3,' '),' ', filename)
dic[count] = filename
count += 1
print('\n')
select = input('select headers.liquid file >')
header_file = dic[int(select)]
#
# select body.liquid file
dic = {}
count=0
print('\n')
for filename in dirlist:
if (filename[-11:] == "body.liquid"):
print(str(count).rjust(3,' '),' ', filename)
dic[count] = filename
count += 1
print('\n')
select = input('select body.liquid file >')
body_file = dic[int(select)]
#
print(line)
print('selected files: \n')
print('\tHeader file :', header_file)
print('\tBody file :', body_file)
check = input('\nPress Enter to upload')
#
# upload payloadTemplate
resource = f'/networks/{networkId}/webhooks/payloadTemplates'
url = api_url + resource
#
webhook_name = input('template_name :')
payload={'name': webhook_name}
#
files = [
('headersFile', ('customTemplate.headers.liquid', open(header_file, 'rb'),'application/octet-stream')),
('bodyFile', ('customTemplate.body.liquid', open(body_file,'rb'),'application/octet-stream'))
]
#
# headers = {'X-Cisco-Meraki-API-Key': accesstoken, 'Content-Type': 'application/json'}
headers = {'X-Cisco-Meraki-API-Key': accesstoken} # !! Content type
warnings.simplefilter("ignore", exceptions.InsecureRequestWarning)
response = requests.post(url, headers=headers, data=payload, files=files, verify=False)
print('response code : ' , response.status_code)
print('response text : ' , response.text)
#
return()
# Action Menue
def get_action():
# os.system('clear') # clear screen
breite = 55
rahmenh = '#'
rahmenv = '|'
menue = [
['list_all_payloadTemplates', list_all_payloadTemplates],
['list_all_custom_payloadTemplates', list_all_custom_payloadTemplates],
['list_all_included_payloadTemplates', list_all_included_payloadTemplates],
['display_a_payloadTemplate', display_a_payloadTemplate],
['display_webhooks_alert_types', display_webhooks_alert_types],
['upload_a_custom_payloadTemplate', upload_a_custom_payloadTemplate],
['delete_a_custom_payloadTemplate', delete_a_custom_payloadTemplate],
['quit', end]
]
print ('\n\n What do you like to do ? ')
print(breite * '-')
count = 0
for element in menue:
element = ' ' + element[0]
print (rahmenv + ' '.ljust(breite,' ') + rahmenv)
print (rahmenv + ' ' + str(count).ljust(4,' ') + ' ' + element.ljust(breite-6,' ') + rahmenv)
print (rahmenv + ' '.ljust(breite,' ') + rahmenv)
count +=1
print (''.ljust(breite + 2,rahmenh))
select = int(input (' >> '))
try:
action = menue[select][1]
return (action)
except:
return("error")
#
# --------------------------------------------------------
# Main
# --------------------------------------------------------
#
if __name__ == "__main__":
#workdir = os.path.abspath(os.curdir)
#
os.system('clear') # clear screen
print(infotext)
input('\n\tPress Enter to continue')
os.system('clear') # clear screen
#
# init
workdir = os.path.abspath(os.curdir)
accesstoken = input("\nAPI Token : ")
os.system('clear') # clear screen
organizationId = select_org()
networkId = select_network(organizationId)
os.system('clear') # clear screen
#
# action menue
endlosschleife = True
while endlosschleife:
os.system('clear') # clear screen
aktion = get_action()
if aktion == "error":
print("input error")
time.sleep(2)
continue
aktion()
wait = input('Press Enter to continue')