-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDnac_auth.py
86 lines (74 loc) · 2.7 KB
/
Dnac_auth.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
import requests
from requests.auth import HTTPBasicAuth
import time
import urllib3
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from pprint import pprint
import credentials
BASE_URL = 'https://64.100.11.142'
AUTH_URL = '/dna/system/api/v1/auth/token'
USERNAME = credentials.DNAC_USERNAME
PASSWORD = credentials.DNAC_PASSWORD
def get_data(uri,header=None, query=None):
"""
Get data from Dnac using the uri and query provided
params:
uri (str): uri to get data from
header (dict): header to be passed to the uri (optional)
query (dict): query to be passed to the uri (optional)
Returns:
json: json response from Dnac
"""
response = requests.post(BASE_URL + AUTH_URL, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False)
token = response.json()['Token']
headers = {'X-Auth-Token': token, 'Content-Type': 'application/json'}
while True:
url = BASE_URL + uri
if(header):
headers.update(header)
response = requests.get(url, headers=headers, params=query, verify=False)
if response.status_code == 200:
try:
return response.json()
except Exception as e:
print("Exception raised : ",e)
continue
elif(response.status_code == 429):
print("got response 429 from Dnac, retrying in 60 seconds..")
time.sleep(60)
continue
else:
print(response, response.text)
continue
def post_data(uri, body=None):
"""
Post data to Dnac using the uri and body provided
params:
uri (str): uri to post data to
body (dict): body to be passed to the uri (optional)
Returns:
json: json response from Dnac
"""
response = requests.post(BASE_URL + AUTH_URL, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False)
token = response.json()['Token']
headers = {'X-Auth-Token': token, 'Content-Type': 'application/json'}
url = BASE_URL + uri
response = requests.post(url,headers=headers,json=body,verify=False)
if response.status_code == 202:
return response.json()
else:
print("error while posting data : ", response, response.json())
return False
if __name__ == "__main__":
print("device-health : ")
data = get_data(uri="/dna/intent/api/v1/device-health")['response']
pprint(data)
print("device-details : ")
for item in data:
query = {
"searchBy": item["macAddress"],
"identifier": "macAddress"
}
print(query)
pprint(get_data(uri="/dna/intent/api/v1/device-detail", query=query))