This module will complete the onboarding of your new L2VNI into your Tenant-1 overlay. This module will introduce you to a Python script using RESTCONF, which will almost be no different to the Python you used in the NX-API CLI and NX-API REST Python Requests sections.
Create a new Python file.
code -r /home/cisco/Documents/nxapilab/nx_restconf.py
Import the same libraries you used previously with Python Requests. Additionally, import specifically HTTPBasicAuth to used for the username and password later.
import requests
import json
# Using python request library for REST operations
from requests.auth import HTTPBasicAuth
# disable warnings from SSL/TLS certificates
requests.packages.urllib3.disable_warnings()
This section of the Python code is the data payload of the REST request. Much like we've done through this lab, you can derive this directly from the NX-API Sandbox. Feel free to give this a try by using your Sandbox, setting the Method to RESTCONF, and typing the configuration for creating an interface VLAN (SVI). Otherwise, copy the payload below into you script.
payload = {
"ipv4-items": {
"inst-items": {
"dom-items": {
"Dom-list": [
{
"name": "Tenant-1",
"if-items": {
"If-list": [
{
"id": "vlan16",
"addr-items": {
"Addr-list": [
{
"addr": "10.0.16.1/24"
}
]
}
}
]
}
}
]
}
}
},
"intf-items": {
"svi-items": {
"If-list": [
{
"adminSt": "up",
"id": "vlan16",
"rtvrfMbr-items": {
"tDn": "/System/inst-items/Inst-list[name='Tenant-1']"
}
}
]
}
},
"hmm-items": {
"fwdinst-items": {
"if-items": {
"FwdIf-list": [
{
"id": "vlan16",
"mode": "anycastGW"
}
]
}
}
}
}
For this section of the script, like other scripts, you need to define a list of the devices to interact with. Then, per RFC, you need to specify the
HTTP headers; here this is not just json, but application/yang.data+json
. Lastly, we're going to perform a patch as the parent YANG container
already exists; this can be normal. The URI or URL is going to be derived from the Sandbox or over time, you'll come to realize it is pretty standard using
/restconf/data/ followed by the YANG container paths. Once the request is performed, there is a simple check to determine if it was successful or not.
# Device dictionary to use
devices = [
{"ip": "10.15.1.12"},
{"ip": "10.15.1.13"}
]
headers = {"Content-Type": "application/yang.data+json", "Accept": "application/yang.data+json"}
for device in devices:
# Patch cisco-nx-os-device-system with the updated ipv4-items, hmm-items, and intf-items configuration
response = requests.patch("https://" + device['ip'] + "/restconf/data/Cisco-NX-OS-device:System/",
auth=HTTPBasicAuth('admin', 'cisco.123'), headers=headers, json=payload,
verify=False)
if response.status_code == 204:
print("Configuration applied to " + device['ip'] + "! Status return code is " + str(response.status_code))
Be sure to save your file! Not saving will result in your code not executing.
Once you have finished with your code, execute your RESTCONF Python script:
python nx_restconf.py
Upon a successful execution, you should see two print messages with confirming success and the response code of 204.
Configuration applied to 10.15.1.12! Status return code is 204 Configuration applied to 10.15.1.13! Status return code is 204
This completes the entire lab. Thank you signing up for and completing this lab!