-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_device_config.py
66 lines (56 loc) · 1.87 KB
/
get_device_config.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
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)
# Export device configs to text files
def get_device_config(
token: AnyStr, ENV: Dict[AnyStr, Any]
) -> List[Dict[AnyStr, AnyStr]]:
"""Gets running configuration of network devices
Parameters
----------
token : AnyStr
Auth token
ENV : Dict[AnyStr, Any]
Environment variables
Returns
-------
List[Dict[AnyStr, AnyStr]]
Configuration of network devices
Raises
------
SystemExit
ConnectionError, HTTPError
SystemExit
KeyboardInterrupt
"""
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json",
"Accept": "application/json",
}
DEVICE_CONFIG_URL = "dna/intent/api/v1/network-device/config"
try:
cprint(text="Getting device configurations...", color="magenta")
response = requests.get(
url=f"{ENV['BASE_URL']}/{DEVICE_CONFIG_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"]