-
Notifications
You must be signed in to change notification settings - Fork 0
/
restconf_service.py
121 lines (97 loc) · 3.3 KB
/
restconf_service.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import urllib3
import requests
from bs4 import BeautifulSoup
from requests.auth import HTTPBasicAuth
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
BASE_URL = "http://localhost:8080"
USERNAME = "admin"
PASSWORD = "admin"
def establish_restconf_connection() -> requests.Session:
http_headers = {
"Accept": "application/yang-data+xml",
"Content-Type": "application/yang-data+json",
}
session = requests.session()
session.auth = HTTPBasicAuth(USERNAME, PASSWORD)
session.headers.update(http_headers)
session.verify = False
return session
def send_restconf_request(
restconf_session: requests.Session, request_path: str, request_data: str
) -> str:
response = restconf_session.patch(
BASE_URL + request_path, json=request_data
)
response.raise_for_status()
return response.text, response.status_code
def add_dns_server_dry_run(
service_name: str, device_name: str, server_address: str
) -> tuple[str, dict]:
path = "/restconf/data?dry-run"
data = _get_dns_server_payload(service_name, device_name, server_address)
return path, data
def add_dns_server(
service_name: str, device_name: str, server_address: str
) -> tuple[str, dict]:
path = "/restconf/data"
data = _get_dns_server_payload(service_name, device_name, server_address)
return path, data
# TIP: Verify the RESTCONF data using the NSO CLI
def _get_dns_server_payload(
service_name: str, device_name: str, server_address: str
) -> str:
"""
To get the data payload, add first an example manually,
commit it, and then use 'display json' as below
show running-config router | display json
"""
return {
"route": [
{
"name": service_name,
"device": [device_name],
"sys": {"ntp": {"server": [{"name": server_address}]}},
}
]
}
def display_parsed_response(
parsed_data: BeautifulSoup, http_status_code: str
) -> None:
print(f"{'#' * 20} Response received: {'#' * 20}")
print(parsed_data.prettify())
print(f"http status code: {http_status_code}")
def parse_xml(xml_data: str) -> BeautifulSoup:
return BeautifulSoup(xml_data, "xml")
def main() -> None:
SERVICE_NAME = "core"
DEVICE_NAME = "core-rtr0"
SERVER_ADDRESS = "9.9.9.9"
session = establish_restconf_connection()
#### Dry run
path, data = add_dns_server_dry_run(
service_name=SERVICE_NAME,
device_name=DEVICE_NAME,
server_address=SERVER_ADDRESS,
)
response, status_code = send_restconf_request(
restconf_session=session, request_path=path, request_data=data
)
parsed_response = parse_xml(xml_data=response)
display_parsed_response(
parsed_data=parsed_response, http_status_code=status_code
)
# #### Apply
# path, data = add_dns_server(
# service_name=SERVICE_NAME,
# device_name=DEVICE_NAME,
# server_address=SERVER_ADDRESS,
# )
# response, status_code = send_restconf_request(
# restconf_session=session, request_path=path, request_data=data
# )
# parsed_response = parse_xml(xml_data=response)
# display_parsed_response(
# parsed_data=parsed_response, http_status_code=status_code
# )
if __name__ == "__main__":
main()