From orchestrating deployment automation to network management and assurance, Cisco DNA Center controller is the brain of network automation. With Cisco’s API-first approach in mind, Cisco DNA Center enables developers to build network applications on top of DNA Center.
Over the next few months I will be posting a series of blogs that take you on a journey of network programmability and automation using the Cisco DNA Center SDK. I’ll be showing some cool things you can do along the way. But first, we’re going to get you set up and started.
Let’s start from the beginning
For this blog, I’m going to start from the very beginning:
- you don’t need prior knowledge of Cisco DNA Center
- you don’t need to know advanced coding and networking concepts
- you will need basic Python and API knowledge to understand the utilization of the SDK
If you do not have basic Python and API knowledge … don’t worry. DevNet has some great resources to get you started with the basics. Check out DevNet Start Now to not only learn the basics of coding but also learn how to get your developer environment setup.
Start Now – Cisco DNA Center SDK
At this point you should have you developer environment ready to go, if you don’t, this DevNet module should help
Now let’s make sure we install our SDK before we start using it.
DNA Center SDK is available via PIP and the Python Package Index (PyPI). To install it simply run:
$ pip install dnacentersdk
Working with the DNA Center APIs directly without the help of the Python SDK is pretty straight forward, however, when you are looking to write a network automation, the code can become rather repetitive.
import requests DNAC_URL = [DNA Center host url] DNAC_USER = [username] DNAC_PASS = [password] def get_auth_token(): """ Building out Auth request. Using requests.post to make a call to the Auth Endpoint """ url = 'https://{}/dna/system/api/v1/auth/token'.format(DNAC_URL) hdr = {'content-type' : 'application/json'} resp = requests.post(url, auth=HTTPBasicAuth(DNAC_USER, DNAC_PASS), headers=hdr) token = resp.json()['Token'] print("Token Retrieved: {}".format(token)) return token
Using the `requests` library we make a POST call to the /auth/token endpoint. The result, if 200OK, will return an authentication token that will need to be utilized for all subsequent API calls as part of `X-Auth-Token` header call. Which means every time we have to call the get_auth_token() function to refresh the token. This is what we are trying to avoid as you can see how repetitive this could get.
Life with Cisco DNAC Center SDK
The DNA Center SDK saves you an insane amount of time by not requiring you to:
- Setup the environment every time
- Remember URLs, request parameters and JSON formats
- Parse the returned JSON and work with multiple layers of list and dictionary indexes
Enter dnacentersdk (queue The Next Episode and drop the shades ? )
With dnacentersdk, the above Python code can be consolidated to the following:
from dnacentersdk import DNACenterAPI DNAC_URL = [DNA Center host url] DNAC_USER = [username] DNAC_PASS = [password] dnac = DNACenterAPI(username= DNAC_USER, password= DNAC_PASS, base_url= DNAC_URL)
Let’s dig into the code here to see how simple it is:
- Make the SDK available in your code by importing it
from dnacentersdk import DNACenterAPI - Define your DNA Center’s host url, username and password
DNAC_URL = [DNA Center host url]
DNAC_USER = [username]
DNAC_PASS = [password] - Create a DNACenterAPI connection object and save it
dnac = DNACenterAPI(username= DNAC_USER, password= DNAC_PASS, base_url= DNAC_URL)
From this point on, in order to access the subsequent API calls, you don’t have to worry about managing your token validity, API headers or Rate-Limit handling. The SDK does that for you.
Another great feature about the SDK is that it represents all returned JSON objects as native Python objects so you can access all of the object’s attributes using native dot.syntax!
Congratulations!
At this point you have a working developer environment and a python project that leverages the DNA Center SDK. For the next installment of this series, I’ll walk you through building on top of the code we are building together here, and will start exploring how to leverage the SDK to automate some of the tasks within Cisco DNA Center. For future reference, everything I have mentioned here – from SDK documentation to code – can be found on Cisco DevNet.
See you in a couple of weeks with Part 2! And please don’t hesitate to drop me a comment (below) if you have comments or questions.
Resources
- Github Code Repo
- Cisco DNA Center Dev Center
- DevNet Network Automation Exchange
- Cisco DevNet Certification
We’d love to hear what you think. Ask a question or leave a comment below.
And stay connected with Cisco DevNet on social!
Twitter @CiscoDevNet | Facebook | LinkedIn
Visit the new Developer Video Channel
Once I tried out the Cisco DNA Center SDK, there was no way back..
The SDK made my life waaaay more simple encapsulating the various API calls, and returning Python objects.
As an additional benefit – the SDK gets updated from one release to the next, and if some API have changed – I don’t need to update my code as the SDK takes care of it for me.
Sir, I would like to know whether the SDK would be just installed only by typing and running the “pip install dnacentersdk” or should it downloaded from some other source before typing the command, could you please tell?
yes, just by “pip install dnacentersdk” in you rpython env will install it.
Do we need any special package in DNAC to try out this one?