-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdna-put-devices.py
90 lines (65 loc) · 2.46 KB
/
dna-put-devices.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
#!/usr/bin/env python
"""
Author: Nick Russo Purpose: Demonstrate Python "requests" to get
the list of devices from Cisco DNA Center using the REST API.
"""
import requests
from auth_token import get_token
import time
def main():
"""
Execution begins here.
"""
# Reuse the get_token() function from before. If it fails
# allow exception to crash program
token = get_token()
# Declare useful local variables to simplify request process
api_path = "https://sandboxdnac2.cisco.com"
headers = {"Content-Type": "application/json", "X-Auth-Token": token}
# Issue HTTP GET request to get list of network devices
get_resp = requests.get(
f"{api_path}/api/v1/network-device", headers=headers
)
# adding a devices dictionary
new_device_dict = {
"ipAddress": ["1.1.1.1"],
"snmpVersion": "v2",
"snmpROCommunity": "readonly",
"snmpRWCommunity": "readwrite",
"snmpRetry": "1",
"snmpTimeout": "60",
"cliTransport": "ssh",
"userName": "imejia",
"password": "cisco",
"enablePassword": "sercret123",
}
# Debugging output to learn the JSON structure, then quit import json; print(json.dumps(get_resp.json(), indent=2)) Iterate over list of dictionaries and print device ID and management IP
add_resp = requests.post(
f"{api_path}/dna/intent/api/v1/network-device",
json=new_device_dict,
headers=headers
)
if add_resp.ok:
# Wait 10 seconds after server responds
print(f"Request accepted: status code {add_resp.status_code}")
time.sleep(10)
# Query DNA center for the status of the specific task ID
task = add_resp.json()["response"]["taskId"]
task_resp = requests.get(
f"{api_path}/intent/api/v1/task/{task}", headers=headers
)
# See if the task was completed successfully or not
if task_resp.ok:
task_data = task_resp.json()["response"]
if not task_data["isError"]:
print("New device successfully added")
else:
print(f"Async task error seen: {task_data['progress']}")
else:
print(f"Async GET failed: status code {task_resp.status_code}")
else:
# The initial HTTP POST failed; print details
print(f"Device addition failed with code {add_resp.status_code}")
print(f"Failure body: {add_resp.text}")
if __name__ == "__main__":
main()