Infrastructure and Automation · Topic 4 of 8
Identifying the Workflow in a Python API Script
Learn to read a Python script that calls Cisco APIs on ACI, Meraki, Catalyst Center and RESTCONF, and identify the workflow it automates from its calls.
- 5 min
- Read in order: imports, authentication, method and path, body, then what happens to the reply.
- GET reads, POST creates, PUT or PATCH changes and DELETE removes.
- The
X-Auth-Tokenheader and the/dna/path suggest Catalyst Center.
What it is
Reading a script means working out from its imports, URLs, methods and loops what job the code performs.
The exam shows a Python script that calls a Cisco API, and asks what it does. The answer comes from a few clues: the authentication step, the HTTP method, the path, the data sent and what is done with the reply.
You do not need to run the code. You need to read it in the same order each time.
How it works
Read the script from the top, and find the method, the path and the data.
A reading routine
- Check the imports.
requestsmeans REST.ncclientmeans NETCONF. A platform SDK names the platform. - Find the authentication. An
X-Auth-Tokenheader suggests Catalyst Center. AnX-Cisco-Meraki-API-Keyheader suggests Meraki. A call toaaaLoginsuggests ACI. A RESTCONF path withapplication/yang-data+jsonsuggests a device. - Find the HTTP method. GET reads, POST creates, PUT or PATCH changes and DELETE removes.
- Read the path. It names the object that is being used.
- Check the body. The fields show what is created or changed.
- See what happens to the reply. A loop with a print means reporting, and a second call that uses a field from the first is a chained workflow.
Clues by platform
| Clue in the script | Platform |
|---|---|
/dna/intent/api/v1/network-device | Catalyst Center device inventory |
X-Auth-Token header | Catalyst Center token |
/organizations and /networks paths | Meraki |
X-Cisco-Meraki-API-Key header | Meraki |
/api/aaaLogin.json | ACI login |
/api/node/class/ or /api/node/mo/ | ACI query |
/restconf/data/ | RESTCONF on a device |
Example 1: Meraki
headers = {"X-Cisco-Meraki-API-Key": API_KEY}
orgs = requests.get(BASE_URL + "/organizations", headers=headers).json()
org_id = orgs[0]["id"]
nets = requests.get(
BASE_URL + "/organizations/" + org_id + "/networks", headers=headers
).json()
for net in nets:
print(net["name"])
The key header and the paths show Meraki. The script reads the first organisation, takes its ID, and then lists the networks of that organisation. The workflow is list the networks in an organisation.
Example 2: Catalyst Center
auth = requests.post(
BASE_URL + "/dna/system/api/v1/auth/token", auth=("admin", PASSWORD)
)
token = auth.json()["Token"]
headers = {"X-Auth-Token": token}
devices = requests.get(
BASE_URL + "/dna/intent/api/v1/network-device", headers=headers
).json()["response"]
for d in devices:
if d["reachabilityStatus"] != "Reachable":
print(d["hostname"], d["managementIpAddress"])
The script gets a token, reads the device inventory, and prints devices that are not reachable. The workflow is report unreachable devices.
Example 3: ACI
session = requests.Session()
login = {"aaaUser": {"attributes": {"name": "admin", "pwd": PASSWORD}}}
session.post(BASE_URL + "/api/aaaLogin.json", json=login)
tenant = {"fvTenant": {"attributes": {"name": "Demo"}}}
session.post(BASE_URL + "/api/node/mo/uni.json", json=tenant)
The script logs in to the APIC and posts a fvTenant object under uni, which is the root of the policy tree. The workflow is create a tenant named Demo.
Example 4: RESTCONF
url = BASE_URL + "/restconf/data/ietf-interfaces:interfaces/interface=Loopback100"
headers = {
"Content-Type": "application/yang-data+json",
"Accept": "application/yang-data+json",
}
body = {
"ietf-interfaces:interface": {
"name": "Loopback100",
"type": "iana-if-type:softwareLoopback",
"enabled": True,
}
}
requests.put(url, headers=headers, json=body, auth=("admin", PASSWORD))
A PUT on a path that names an interface, with a body that names a software loopback, creates or replaces Loopback100 on the device. The workflow is configure a loopback interface.
Other things to notice
- A
whileloop that reads a task status points to an asynchronous workflow. - A
forloop over a list of devices means a bulk operation. - A
tryblock withraise_for_status()adds error handling. - Values read from
os.environshow that secrets are kept out of the code. verify=Falsesuggests a lab device with a self signed certificate.
Stating the workflow
Give the answer as one sentence with a verb and an object, such as list, create, update or delete plus the object and the platform. If a script has several steps, describe the order: log in, read, filter, report.
Follow the HTTP method and the path. They tell you the verb and the object of the workflow.
What to remember for the exam
Six facts cover this topic.
- Read in order: imports, authentication, method and path, body, then what happens to the reply.
- GET reads, POST creates, PUT or PATCH changes and DELETE removes.
- The
X-Auth-Tokenheader and the/dna/path suggest Catalyst Center. - The
X-Cisco-Meraki-API-Keyheader suggests Meraki. aaaLoginand paths under/api/node/suggest ACI./restconf/data/withapplication/yang-data+jsonsuggests RESTCONF on a device.
Common mistakes
Four mistakes are typical.
Reading only the last line. The workflow depends on the whole sequence, including the login and the filter.
Ignoring the HTTP method. A GET and a POST to the same path do different jobs.
Missing a condition. An if inside a loop may mean that only some devices are processed.
Mixing up PUT and PATCH. PUT replaces the resource, while PATCH changes part of it.
Do not assume the script is safe because it looks like a read. Check for POST, PUT, PATCH or DELETE calls.
Check yourself
Work through these three questions.
A script sends an
X-Auth-Tokenheader to a path under/dna/intent/api/v1/. Which platform is it using?Cisco Catalyst Center.
How can you tell that a script creates something rather than reads it?
It uses POST, or PUT with a body that describes a new object, instead of GET.
What does a POST of an
fvTenantobject to the APIC do?It creates a tenant on the ACI fabric.
Topic 4 of 8 in Infrastructure and Automation
Test yourself on Infrastructure and Automation
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.