-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_device_list.py
64 lines (53 loc) · 1.79 KB
/
get_device_list.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
from distutils.util import strtobool
from typing import Any, AnyStr, Dict, List
import requests
from requests.exceptions import ConnectionError, HTTPError
from requests.packages import urllib3
from termcolor import colored, cprint
from urllib3.exceptions import InsecureRequestWarning
# Disable SSL warnings. Not needed in production environments with valid certificates
# (REMOVE if you are not sure of its purpose)
urllib3.disable_warnings(category=InsecureRequestWarning)
def get_device_list(token: AnyStr, ENV: Dict[AnyStr, Any]) -> List[Dict[AnyStr, Any]]:
"""Gets device list of a Cisco DNA Center
Parameters
----------
token : AnyStr
Auth token
ENV : Dict[AnyStr, Any]
Environment variables
Returns
-------
List[Dict[AnyStr, Any]]
List of network devices
Raises
------
SystemExit
ConnectionError, HTTPError
SystemExit
KeyboardInterrupt
"""
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json",
"Accept": "application/json",
}
DEVICE_LIST_URL = "dna/intent/api/v1/network-device/"
try:
cprint(text="Getting device list...", color="magenta")
response = requests.get(
url=f"{ENV['BASE_URL']}/{DEVICE_LIST_URL}",
headers=headers,
data=None,
verify=bool(strtobool(val=ENV["SSL_CERTIFICATE"])),
)
response.raise_for_status()
except (ConnectionError, HTTPError) as e:
raise SystemExit(colored(text=e, color="red"))
except KeyboardInterrupt:
raise SystemExit(
colored(text="Process interrupted by the user", color="yellow")
)
else:
cprint(text="The request was successful.\n", color="green")
return response.json()["response"]