This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnac.py
77 lines (60 loc) · 2.51 KB
/
dnac.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
import requests
import json
import time
import logging
from dnac_config import DNAC, DNAC_PORT, DNAC_USER, DNAC_PASSWORD
from requests.auth import HTTPBasicAuth
requests.packages.urllib3.disable_warnings()
# -------------------------------------------------------------------
# Custom exception definitions
# -------------------------------------------------------------------
class TaskTimeoutError(Exception):
pass
class TaskError(Exception):
pass
# API ENDPOINTS
ENDPOINT_TICKET = "ticket"
ENDPOINT_TASK_SUMMARY ="task/%s"
RETRY_INTERVAL=2
# -------------------------------------------------------------------
# Helper functions
# -------------------------------------------------------------------
def create_url(path, controller_ip=DNAC):
""" Helper function to create a DNAC API endpoint URL
"""
return "https://%s:%s/api/v1/%s" % (controller_ip, DNAC_PORT, path)
def get_auth_token(controller_ip=DNAC, username=DNAC_USER, password=DNAC_PASSWORD):
""" Authenticates with controller and returns a token to be used in subsequent API invocations
"""
login_url = "https://{0}:{1}/api/system/v1/auth/token".format(controller_ip, DNAC_PORT)
result = requests.post(url=login_url, auth=HTTPBasicAuth(DNAC_USER, DNAC_PASSWORD), verify=False)
result.raise_for_status()
token = result.json()["Token"]
return {
"controller_ip": controller_ip,
"token": token
}
def wait_on_task(task_id, token, timeout=(5*RETRY_INTERVAL), retry_interval=RETRY_INTERVAL):
""" Waits for the specified task to complete
"""
task_url = create_url(ENDPOINT_TASK_SUMMARY % task_id, token["controller_ip"])
headers = {
"x-auth-token": token["token"]
}
start_time = time.time()
while True:
result = requests.get(url=task_url, headers=headers, verify=False)
result.raise_for_status()
response = result.json()["response"]
#print json.dumps(response)
if "endTime" in response:
return response
else:
if timeout and (start_time + timeout < time.time()):
raise TaskTimeoutError("Task %s did not complete within the specified timeout "
"(%s seconds)" % (task_id, timeout))
print("Task=%s has not completed yet. Sleeping %s seconds..." %(task_id, retry_interval))
time.sleep(retry_interval)
if response['isError'] == True:
raise TaskError("Task %s had error %s" % (task_id, response['progress']))
return response