Api Example

In our last session on scripting we generated a script that read output from a Juniper switch and created output that we could paste into a Cisco switch to produce an identical port configuration. Today we will explore how to utilize the API provided by DNA Center to remove the step of having to paste the code in at the CLI.

I’m getting ahead of myself though. First we need to discuss what an API is and why they are used.

I found the following description of an API on a page on IBM’s website :

An API is a set of defined rules that explain how computers or applications communicate with one another. APIs sit between an application and the web server, acting as an intermediary layer that processes data transfer between systems.

Wait a second! To my network engineer ears that sounds like an API is basically just another protocol. I think that is a fair way to think of it. Just like TCP is encapsulated inside of IP, and https is encapsulated in TCP, you will normally find API’s are basically just another protocol normally found encapsulated inside of https.

The biggest difference in my mind is that API’s are normally updated and extended at a more rapid rate. The more that I learn about and trouble shoot application issues and voice issues I feel like SIP behaves and looks more like API traffic than a traditional protocol.

Ok, now it is time to REST. Wait, no keep paying attention I didn’t mean take a break or sleep. REST stands for REpresentational State Transfer and it is the type of API that we will most often interact with. Basically a RESTful protocol is a protocol in which you send structured data to a web service and receive structured data back.

I ask that you now open Cisco DNA Center API Quickstart in another window. Let’s look at the example in which they query information about the WLC. Don’t worry too much about the Python code. Basically that code just sends a web request to DNAC to a specific URL with the IP address of the device that they want to get information about as an argument. We will examine how this code works later. What I’d like you to more closely examine is the “example response””.

This is the structured data that I spoke of earlier. In this case the structure is a standard known as JSON. You can read that link for a more detailed description of what JSON is but it probably isn’t necessary for understanding what we are trying to accomplish.

Let’s look deeper into this example response. The API is specifically responsible for defining what this response looks like to us and the code that we can write so that we always can data that we can expect in a format that we expect.

Responses from DNAC (and Cisco APIC and probably most Cisco API’s) always start with:

{
'response': {

This is just letting you know that you got a valid response back from DNAC and you are going to get some interesting data that you can examine following.

Below that you find some stuff that you can really work with. These are organized in “name value pairs” and will always be in this format

'name': 'Value',

Think of name as the description or the property that you want to know the value of. The API defines all of the names that you can get back for any query which means that you as the person writing the script don’t have to determine which line contains the MAC address or software version of the device you can specifically ask or pull out a value associated with the name ‘softwareVersion’ for example and get back 8.2.166.0 specifically in this example. ‘softwareVersion’ is a name and ‘8.2.166.0’ is the value associated with that name. Also note something that is very important with JSON formatted data. If there is a another name-value pair to follow the current one the line must end with a comma in order to be considered valid and be parsable by most languages.

{
	'response': {
		...
		'softwareVersion': '8.2.166.0',
		'errorDescription': None,
		'serialNumber': 'FCH2207V0AT',
		'tagCount': '0',
		'upTime': '3 days, 7:51:38.64',
		'hostname': 'ITSM-WLC',
		...
	 },
	 'version': '1.0'
}

In our next session we actually write some code that will take the port config from that Juniper switch and the interface config we created in the previous sessions and install it on a switch using the command runner module of the DNAC API. In the mean time I encourage you to check out these resources for more information on scripting and API’s.

Learn Python with Socratica

Network Chuck’s Python Course

Thank you for joining me on this exploration of network automation.

Written on April 16, 2022