-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnac_apis.py
1098 lines (956 loc) · 39.8 KB
/
dnac_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
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
# developed by Gabi Zapodeanu, TME, ENB, Cisco Systems
import requests
import json
import time
import os
import os.path
import urllib3
import socket
import re
import utils
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
from requests.auth import HTTPBasicAuth # for Basic Auth
from config import DNAC_URL, DNAC_PASS, DNAC_USER
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
DNAC_AUTH = HTTPBasicAuth(DNAC_USER, DNAC_PASS)
def pprint(json_data):
"""
Pretty print JSON formatted data
:param json_data: data to pretty print
:return:
"""
print(json.dumps(json_data, indent=4, separators=(' , ', ' : ')))
def get_dnac_jwt_token(dnac_auth):
"""
Create the authorization token required to access DNA C
Call to DNA C - /api/system/v1/auth/login
:param dnac_auth - DNA C Basic Auth string
:return: DNA C JWT token
"""
url = DNAC_URL + '/dna/system/api/v1/auth/token'
print(url)
header = {'content-type': 'application/json'}
response = requests.post(url, auth=dnac_auth, headers=header, verify=False)
dnac_jwt_token = response.json()['Token']
return dnac_jwt_token
def get_all_device_info(dnac_jwt_token):
"""
The function will return all network devices info
:param dnac_jwt_token: DNA C token
:return: DNA C device inventory info
"""
url = DNAC_URL + '/api/v1/network-device'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
all_device_response = requests.get(url, headers=header, verify=False)
all_device_info = all_device_response.json()
return all_device_info['response']
def get_device_info(device_id, dnac_jwt_token):
"""
This function will retrieve all the information for the device with the DNA C device id
:param device_id: DNA C device_id
:param dnac_jwt_token: DNA C token
:return: device info
"""
url = DNAC_URL + '/api/v1/network-device?id=' + device_id
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
device_response = requests.get(url, headers=header, verify=False)
device_info = device_response.json()
return device_info['response'][0]
def get_project_id(project_name, dnac_jwt_token):
"""
This function will retrieve the CLI templates project id for the project with the name {project_name}
:param project_name: CLI project template
:param dnac_jwt_token: DNA token
:return: project id
"""
url = DNAC_URL + '/api/v1/template-programmer/project?name=' + project_name
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
proj_json = response.json()
proj_id = proj_json[0]['id']
return proj_id
def get_project_info(project_name, dnac_jwt_token):
"""
This function will retrieve all templates associated with the project with the name {project_name}
:param project_name: project name
:param dnac_jwt_token: DNA C token
:return: list of all templates, including names and ids
"""
url = DNAC_URL + '/api/v1/template-programmer/project?name=' + project_name
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
project_json = response.json()
template_list = project_json[0]['templates']
return template_list
def create_commit_template(template_name, project_name, cli_template, dnac_jwt_token):
"""
This function will create and commit a CLI template, under the project with the name {project_name}, with the the text content
{cli_template}
:param template_name: CLI template name
:param project_name: Project name
:param cli_template: CLI template text content
:param dnac_jwt_token: DNA C token
:return:
"""
project_id = get_project_id(project_name, dnac_jwt_token)
# prepare the template param to sent to DNA C
payload = {
"name": template_name,
"description": "Remote router configuration",
"tags": [],
"author": "admin",
"deviceTypes": [
{
"productFamily": "Routers"
},
{
"productFamily": "Switches and Hubs"
}
],
"softwareType": "IOS-XE",
"softwareVariant": "XE",
"softwareVersion": "",
"templateContent": cli_template,
"rollbackTemplateContent": "",
"templateParams": [],
"rollbackTemplateParams": [],
"parentTemplateId": project_id
}
# check and delete older versions of the template
# template_id = get_template_id(template_name, project_name, dnac_jwt_token)
# if template_id:
# delete_template(template_name, project_name, dnac_jwt_token)
# create the new template
url = DNAC_URL + '/api/v1/template-programmer/project/' + project_id + '/template'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
# get the template id
template_id = get_template_id(template_name, project_name, dnac_jwt_token)
# commit template
commit_template(template_id, 'committed by Python script', dnac_jwt_token)
def commit_template(template_id, comments, dnac_jwt_token):
"""
This function will commit the template with the template id {template_id}
:param template_id: template id
:param comments: text with comments
:param dnac_jwt_token: DNA C token
:return:
"""
url = DNAC_URL + '/api/v1/template-programmer/template/version'
payload = {
"templateId": template_id,
"comments": comments
}
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
def update_commit_template(template_name, project_name, cli_template, dnac_jwt_token):
"""
This function will update an existing template
:param template_name: template name
:param project_name: project name
:param cli_template: CLI template text content
:param dnac_jwt_token: DNA C token
:return:
"""
# get the project id
project_id = get_project_id(project_name, dnac_jwt_token)
# get the template id
template_id = get_template_id(template_name, project_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/template-programmer/template'
# prepare the template param to sent to DNA C
payload = {
"name": template_name,
"description": "Remote router configuration",
"tags": [],
"id": template_id,
"author": "admin",
"deviceTypes": [
{
"productFamily": "Routers"
},
{
"productFamily": "Switches and Hubs"
}
],
"softwareType": "IOS-XE",
"softwareVariant": "XE",
"softwareVersion": "",
"templateContent": cli_template,
"rollbackTemplateContent": "",
"templateParams": [],
"rollbackTemplateParams": [],
"parentTemplateId": project_id
}
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.put(url, data=json.dumps(payload), headers=header, verify=False)
# commit template
commit_template(template_id, 'committed by Python script', dnac_jwt_token)
def upload_template(template_name, project_name, cli_template, dnac_jwt_token):
"""
This function will create and deploy a new template if not existing, or will update an existing template.
:param template_name: template name
:param project_name: project name
:param cli_template: CLI template text content
:param dnac_jwt_token: DNA C token
:return:
"""
template_id = get_template_id(template_name, project_name, dnac_jwt_token)
if template_id:
update_commit_template(template_name, project_name, cli_template, dnac_jwt_token)
else:
create_commit_template(template_name, project_name, cli_template, dnac_jwt_token)
def delete_template(template_name, project_name, dnac_jwt_token):
"""
This function will delete the template with the name {template_name}
:param template_name: template name
:param project_name: Project name
:param dnac_jwt_token: DNA C token
:return:
"""
template_id = get_template_id(template_name, project_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/template-programmer/template/' + template_id
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.delete(url, headers=header, verify=False)
def get_all_template_info(dnac_jwt_token):
"""
This function will return the info for all CLI templates existing on DNA C, including all their versions
:param dnac_jwt_token: DNA C token
:return: all info for all templates
"""
url = DNAC_URL + '/api/v1/template-programmer/template'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
all_template_list = response.json()
return all_template_list
def get_template_name_info(template_name, project_name, dnac_jwt_token):
"""
This function will return the info for the CLI template with the name {template_name}
:param template_name: template name
:param project_name: Project name
:param dnac_jwt_token: DNA C token
:return: all info for all templates
"""
template_id = get_template_id(template_name, project_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/template-programmer/template/' + template_id
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
template_json = response.json()
return template_json
def get_template_id(template_name, project_name, dnac_jwt_token):
"""
This function will return the latest version template id for the DNA C template with the name {template_name},
part of the project with the name {project_name}
:param template_name: name of the template
:param project_name: Project name
:param dnac_jwt_token: DNA C token
:return: DNA C template id
"""
template_list = get_project_info(project_name, dnac_jwt_token)
template_id = None
for template in template_list:
if template['name'] == template_name:
template_id = template['id']
return template_id
def get_template_id_version(template_name, project_name, dnac_jwt_token):
"""
This function will return the latest version template id for the DNA C template with the name {template_name},
part of the project with the name {project_name}
:param template_name: name of the template
:param project_name: Project name
:param dnac_jwt_token: DNA C token
:return: DNA C template id for the last version
"""
project_id = get_project_id(project_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/template-programmer/template?projectId=' + project_id + '&includeHead=false'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
project_json = response.json()
for template in project_json:
if template['name'] == template_name:
version = 0
versions_info = template['versionsInfo']
for ver in versions_info:
if int(ver['version']) > version:
template_id_ver = ver['id']
version = int(ver['version'])
return template_id_ver
def deploy_template(template_name, project_name, device_name, dnac_jwt_token):
"""
This function will deploy the template with the name {template_name} to the network device with the name
{device_name}
:param template_name: template name
:param project_name: project name
:param device_name: device hostname
:param dnac_jwt_token: DNA C token
:return: the deployment task id
"""
device_management_ip = get_device_management_ip(device_name, dnac_jwt_token)
template_id = get_template_id_version(template_name, project_name, dnac_jwt_token)
payload = {
"templateId": template_id,
"targetInfo": [
{
"id": device_management_ip,
"type": "MANAGED_DEVICE_IP",
"params": {}
}
]
}
url = DNAC_URL + '/api/v1/template-programmer/template/deploy'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.post(url, headers=header, data=json.dumps(payload), verify=False)
depl_task_id = (response.json())["deploymentId"]
return depl_task_id
def check_template_deployment_status(depl_task_id, dnac_jwt_token):
"""
This function will check the result for the deployment of the CLI template with the id {depl_task_id}
:param depl_task_id: template deployment id
:param dnac_jwt_token: DNA C token
:return: status - {SUCCESS} or {FAILURE}
"""
url = DNAC_URL + '/api/v1/template-programmer/template/deploy/status/' + depl_task_id
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
response_json = response.json()
deployment_status = response_json["status"]
return deployment_status
def get_client_info(client_ip, dnac_jwt_token):
"""
This function will retrieve all the information from the client with the IP address
:param client_ip: client IPv4 address
:param dnac_jwt_token: DNA C token
:return: client info, or {None} if client does not found
"""
url = DNAC_URL + '/api/v1/host?hostIp=' + client_ip
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
client_json = response.json()
try:
client_info = client_json['response'][0]
return client_info
except:
return None
def locate_client_ip(client_ip, dnac_jwt_token):
"""
Locate a wired client device in the infrastructure by using the client IP address
Call to DNA C - api/v1/host?hostIp={client_ip}
:param client_ip: Client IP Address
:param dnac_jwt_token: DNA C token
:return: hostname, interface_name, vlan_id, or None, if the client does not exist
"""
client_info = get_client_info(client_ip, dnac_jwt_token)
if client_info is not None:
hostname = client_info['connectedNetworkDeviceName']
interface_name = client_info['connectedInterfaceName']
vlan_id = client_info['vlanId']
return hostname, interface_name, vlan_id
else:
return None
def get_device_id_name(device_name, dnac_jwt_token):
"""
This function will find the DNA C device id for the device with the name {device_name}
:param device_name: device hostname
:param dnac_jwt_token: DNA C token
:return:
"""
device_id = None
device_list = get_all_device_info(dnac_jwt_token)
for device in device_list:
if device['hostname'] == device_name:
device_id = device['id']
return device_id
def get_device_status(device_name, dnac_jwt_token):
"""
This function will return the reachability status for the network device with the name {device_name}
:param device_name: device name
:param dnac_jwt_token: DNA C token
:return: status - {UNKNOWN} to locate a device in the database,
{SUCCESS} device reachable
{FAILURE} device not reachable
"""
device_id = get_device_id_name(device_name, dnac_jwt_token)
if device_id is None:
return 'UNKNOWN'
else:
device_info = get_device_info(device_id, dnac_jwt_token)
if device_info['reachabilityStatus'] == 'Reachable':
return 'SUCCESS'
else:
return 'FAILURE'
def get_device_management_ip(device_name, dnac_jwt_token):
"""
This function will find out the management IP address for the device with the name {device_name}
:param device_name: device name
:param dnac_jwt_token: DNA C token
:return: the management ip address
"""
device_ip = None
device_list = get_all_device_info(dnac_jwt_token)
for device in device_list:
if device['hostname'] == device_name:
device_ip = device['managementIpAddress']
return device_ip
def get_device_id_sn(device_sn, dnac_jwt_token):
"""
The function will return the DNA C device id for the device with serial number {device_sn}
:param device_sn: network device SN
:param dnac_jwt_token: DNA C token
:return: DNA C device id
"""
url = DNAC_URL + '/api/v1/network-device/serial-number/' + device_sn
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
device_response = requests.get(url, headers=header, verify=False)
device_info = device_response.json()
device_id = device_info['response']['id']
return device_id
def get_device_location(device_name, dnac_jwt_token):
"""
This function will find the location for the device with the name {device_name}
:param device_name: device name
:param dnac_jwt_token: DNA C token
:return: the location
"""
device_id = get_device_id_name(device_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/group/member/' + device_id + '?groupType=SITE'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
device_response = requests.get(url, headers=header, verify=False)
device_info = (device_response.json())['response']
device_location = device_info[0]['groupNameHierarchy']
return device_location
def create_site(site_name, dnac_jwt_token):
"""
The function will create a new site with the name {site_name}
:param site_name: DNA C site name
:param dnac_jwt_token: DNA C token
:return: none
"""
payload = {
"additionalInfo": [
{
"nameSpace": "Location",
"attributes": {
"type": "area"
}
}
],
"groupNameHierarchy": "Global/" + site_name,
"groupTypeList": [
"SITE"
],
"systemGroup": False,
"parentId": "",
"name": site_name,
"id": ""
}
url = DNAC_URL + '/api/v1/group'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
requests.post(url, data=json.dumps(payload), headers=header, verify=False)
def get_site_id(site_name, dnac_jwt_token):
"""
The function will get the DNA C site id for the site with the name {site_name}
:param site_name: DNA C site name
:param dnac_jwt_token: DNA C token
:return: DNA C site id
"""
site_id = None
url = DNAC_URL + '/api/v1/group?groupType=SITE'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
site_response = requests.get(url, headers=header, verify=False)
site_json = site_response.json()
site_list = site_json['response']
for site in site_list:
if site_name == site['name']:
site_id = site['id']
return site_id
def create_building(site_name, building_name, address, dnac_jwt_token):
"""
The function will create a new building with the name {building_name}, part of the site with the name {site_name}
:param site_name: DNA C site name
:param building_name: DNA C building name
:param address: building address
:param dnac_jwt_token: DNA C token
:return: none
"""
# get the site id for the site name
site_id = get_site_id(site_name, dnac_jwt_token)
# get the geolocation info for address
geo_info = get_geo_info(address, GOOGLE_API_KEY)
print('\nGeolocation info for the address ', address, ' is:')
pprint(geo_info)
payload = {
"additionalInfo": [
{
"nameSpace": "Location",
"attributes": {
"country": "United States",
"address": address,
"latitude": geo_info['lat'],
"type": "building",
"longitude": geo_info['lng']
}
}
],
"groupNameHierarchy": "Global/" + site_name + '/' + building_name,
"groupTypeList": [
"SITE"
],
"systemGroup": False,
"parentId": site_id,
"name": building_name,
"id": ""
}
url = DNAC_URL + '/api/v1/group'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
requests.post(url, data=json.dumps(payload), headers=header, verify=False)
def get_building_id(building_name, dnac_jwt_token):
"""
The function will get the DNA C building id for the building with the name {building_name}
:param building_name: building name
:param dnac_jwt_token: DNA C token
:return: DNA C building id
"""
building_id = None
url = DNAC_URL + '/api/v1/group?groupType=SITE'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
building_response = requests.get(url, headers=header, verify=False)
building_json = building_response.json()
building_list = building_json['response']
for building in building_list:
if building_name == building['name']:
building_id = building['id']
return building_id
def create_floor(building_name, floor_name, floor_number, dnac_jwt_token):
"""
The function will create a floor in the building with the name {site_name}
:param building_name: DNA C site name
:param floor_name: floor name
:param floor_number: floor number
:param dnac_jwt_token: DNA C token
:return: none
"""
# get the site id
building_id = get_building_id(building_name, dnac_jwt_token)
payload = {
"additionalInfo": [
{
"nameSpace": "Location",
"attributes": {
"type": "floor"
}
},
{
"nameSpace": "mapGeometry",
"attributes": {
"offsetX": "0.0",
"offsetY": "0.0",
"width": "200.0",
"length": "100.0",
"geometryType": "DUMMYTYPE",
"height": "20.0"
}
},
{
"nameSpace": "mapsSummary",
"attributes": {
"floorIndex": floor_number
}
}
],
"groupNameHierarchy": "",
"groupTypeList": [
"SITE"
],
"name": floor_name,
"parentId": building_id,
"systemGroup": False,
"id": ""
}
url = DNAC_URL + '/api/v1/group'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
requests.post(url, data=json.dumps(payload), headers=header, verify=False)
def get_floor_id(building_name, floor_name, dnac_jwt_token):
"""
This function will return the floor id for the floor with the name {floor_name} located in the building with the
name {building_name}
:param building_name: building name
:param floor_name: floor name
:param dnac_jwt_token: DNA C token
:return: floor_id
"""
floor_id = None
building_id = get_building_id(building_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/group' + building_id + '/child?level=1'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
building_response = requests.get(url, headers=header, verify=False)
building_json = building_response.json()
floor_list = building_json['response']
for floor in floor_list:
if floor['name'] == floor_name:
floor_id = floor['id']
return floor_id
def assign_device_sn_building(device_sn, building_name, dnac_jwt_token):
"""
This function will assign a device with the specified SN to a building with the name {building_name}
:param device_sn: network device SN
:param building_name: DNA C building name
:param dnac_jwt_token: DNA C token
:return:
"""
# get the building and device id's
building_id = get_building_id(building_name, dnac_jwt_token)
device_id = get_device_id_sn(device_sn, dnac_jwt_token)
url = DNAC_URL + '/api/v1/group/' + building_id + '/member'
payload = {"networkdevice": [device_id]}
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
print('\nDevice with the SN: ', device_sn, 'assigned to building: ', building_name)
def assign_device_name_building(device_name, building_name, dnac_jwt_token):
"""
This function will assign a device with the specified name to a building with the name {building_name}
:param device_name: network device name
:param building_name: DNA C building name
:param dnac_jwt_token: DNA C token
:return:
"""
# get the building and device id's
building_id = get_building_id(building_name, dnac_jwt_token)
device_id = get_device_id_name(device_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/group/' + building_id + '/member'
payload = {"networkdevice": [device_id]}
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
print('\nDevice with the name: ', device_name, 'assigned to building: ', building_name)
def get_geo_info(address, google_key):
"""
The function will access Google Geolocation API to find the longitude/latitude for a address
:param address: address, including ZIP and Country
:param google_key: Google API Key
:return: longitude/latitude
"""
url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&key=' + google_key
header = {'content-type': 'application/json'}
response = requests.get(url, headers=header, verify=False)
response_json = response.json()
location_info = response_json['results'][0]['geometry']['location']
return location_info
def sync_device(device_name, dnac_jwt_token):
"""
This function will sync the device configuration from the device with the name {device_name}
:param device_name: device hostname
:param dnac_jwt_token: DNA C token
:return: the response status code, 202 if sync initiated, and the task id
"""
device_id = get_device_id_name(device_name, dnac_jwt_token)
param = [device_id]
url = DNAC_URL + '/api/v1/network-device/sync?forceSync=true'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
sync_response = requests.put(url, data=json.dumps(param), headers=header, verify=False)
task = sync_response.json()['response']['taskId']
return sync_response.status_code, task
def check_task_id_status(task_id, dnac_jwt_token):
"""
This function will check the status of the task with the id {task_id}
:param task_id: task id
:param dnac_jwt_token: DNA C token
:return: status - {SUCCESS} or {FAILURE}
"""
url = DNAC_URL + '/api/v1/task/' + task_id
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
task_response = requests.get(url, headers=header, verify=False)
task_json = task_response.json()
task_status = task_json['response']['isError']
if not task_status:
task_result = 'SUCCESS'
else:
task_result = 'FAILURE'
return task_result
def check_task_id_output(task_id, dnac_jwt_token):
"""
This function will check the status of the task with the id {task_id}. Loop one seconds increments until task is completed
:param task_id: task id
:param dnac_jwt_token: DNA C token
:return: status - {SUCCESS} or {FAILURE}
"""
url = DNAC_URL + '/api/v1/task/' + task_id
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
completed = 'no'
while completed == 'no':
try:
task_response = requests.get(url, headers=header, verify=False)
task_json = task_response.json()
task_output = task_json['response']
task_output['endTime']
completed = 'yes'
except:
time.sleep(1)
return task_output
def create_path_trace(src_ip, dest_ip, dnac_jwt_token):
"""
This function will create a new Path Trace between the source IP address {src_ip} and the
destination IP address {dest_ip}
:param src_ip: Source IP address
:param dest_ip: Destination IP address
:param dnac_jwt_token: DNA C token
:return: DNA C path visualisation id
"""
param = {
'destIP': dest_ip,
'periodicRefresh': False,
'sourceIP': src_ip
}
url = DNAC_URL + '/api/v1/flow-analysis'
header = {'accept': 'application/json', 'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
path_response = requests.post(url, data=json.dumps(param), headers=header, verify=False)
path_json = path_response.json()
path_id = path_json['response']['flowAnalysisId']
return path_id
def get_path_trace_info(path_id, dnac_jwt_token):
"""
This function will return the path trace details for the path visualisation {id}
:param path_id: DNA C path visualisation id
:param dnac_jwt_token: DNA C token
:return: Path visualisation status, and the details in a list [device,interface_out,interface_in,device...]
"""
url = DNAC_URL + '/api/v1/flow-analysis/' + path_id
header = {'accept': 'application/json', 'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
path_response = requests.get(url, headers=header, verify=False)
path_json = path_response.json()
path_info = path_json['response']
path_status = path_info['request']['status']
path_list = []
if path_status == 'COMPLETED':
network_info = path_info['networkElementsInfo']
path_list.append(path_info['request']['sourceIP'])
for elem in network_info:
try:
path_list.append(elem['ingressInterface']['physicalInterface']['name'])
except:
pass
try:
path_list.append(elem['name'])
except:
pass
try:
path_list.append(elem['egressInterface']['physicalInterface']['name'])
except:
pass
path_list.append(path_info['request']['destIP'])
return path_status, path_list
def check_ipv4_network_interface(ip_address, dnac_jwt_token):
"""
This function will check if the provided IPv4 address is configured on any network interfaces
:param ip_address: IPv4 address
:param dnac_jwt_token: DNA C token
:return: None, or device_hostname and interface_name
"""
url = DNAC_URL + '/api/v1/interface/ip-address/' + ip_address
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
response_json = response.json()
try:
response_info = response_json['response'][0]
interface_name = response_info['portName']
device_id = response_info['deviceId']
device_info = get_device_info(device_id, dnac_jwt_token)
device_hostname = device_info['hostname']
return device_hostname, interface_name
except:
device_info = get_device_info_ip(ip_address, dnac_jwt_token) # required for AP's
device_hostname = device_info['hostname']
return device_hostname, ''
def get_device_info_ip(ip_address, dnac_jwt_token):
"""
This function will retrieve the device information for the device with the management IPv4 address {ip_address}
:param ip_address: device management ip address
:param dnac_jwt_token: DNA C token
:return: device information, or None
"""
url = DNAC_URL + '/api/v1/network-device/ip-address/' + ip_address
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
response_json = response.json()
device_info = response_json['response']
if 'errorCode' == 'Not found':
return None
else:
return device_info
def get_legit_cli_command_runner(dnac_jwt_token):
"""
This function will get all the legit CLI commands supported by the {command runner} APIs
:param dnac_jwt_token: DNA C token
:return: list of CLI commands
"""
url = DNAC_URL + '/api/v1/network-device-poller/cli/legit-reads'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
response_json = response.json()
cli_list = response_json['response']
return cli_list
def get_content_file_id(file_id, dnac_jwt_token):
"""
This function will download a file specified by the {file_id}
:param file_id: file id
:param dnac_jwt_token: DNA C token
:return: file
"""
url = DNAC_URL + '/api/v1/file/' + file_id
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False, stream=True)
response_json = response.json()
return response_json
def get_output_command_runner(command, device_name, dnac_jwt_token):
"""
This function will return the output of the CLI command specified in the {command}, sent to the device with the
hostname {device}
:param command: CLI command
:param device_name: device hostname
:param dnac_jwt_token: DNA C token
:return: file with the command output
"""
# get the DNA C device id
device_id = get_device_id_name(device_name, dnac_jwt_token)
# get the DNA C task id that will process the CLI command runner
payload = {
"commands": [command],
"deviceUuids": [device_id],
"timeout": 0
}
url = DNAC_URL + '/api/v1/network-device-poller/cli/read-request'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
response_json = response.json()
task_id = response_json['response']['taskId']
# get task id status
task_result = check_task_id_output(task_id, dnac_jwt_token)
file_info = json.loads(task_result['progress'])
file_id = file_info['fileId']
# get output from file
time.sleep(2) # wait for a second for the file to be ready
file_output = get_content_file_id(file_id, dnac_jwt_token)
command_responses = file_output[0]['commandResponses']
if command_responses['SUCCESS'] is not {}:
command_output = command_responses['SUCCESS'][command]
elif command_responses['FAILURE'] is not {}:
command_output = command_responses['FAILURE'][command]
else:
command_output = command_responses['BLACKLISTED'][command]
return command_output
def get_all_configs(dnac_jwt_token):
"""
This function will retrieve all the devices configurations
:param dnac_jwt_token: DNA C token
:return: Return all config files in a list
"""
url = DNAC_URL + '/api/v1/network-device/config'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
config_json = response.json()
config_files = config_json['response']
return config_files
def get_device_config(device_name, dnac_jwt_token):
"""
This function will get the configuration file for the device with the name {device_name}
:param device_name: device hostname
:param dnac_jwt_token: DNA C token
:return: configuration file
"""
device_id = get_device_id_name(device_name, dnac_jwt_token)
url = DNAC_URL + '/api/v1/network-device/' + device_id + '/config'
header = {'content-type': 'application/json', 'x-auth-token': dnac_jwt_token}
response = requests.get(url, headers=header, verify=False)
config_json = response.json()
config_file = config_json['response']
return config_file
def check_ipv4_address(ipv4_address, dnac_jwt_token):
"""
This function will find if the IPv4 address is configured on any network interfaces or used by any hosts.
:param ipv4_address: IPv4 address
:param dnac_jwt_token: DNA C token
:return: True/False
"""
# check against network devices interfaces
try:
device_info = check_ipv4_network_interface(ipv4_address, dnac_jwt_token)
return True
except:
# check against any hosts
try: