Inscrivez-vous maintenant pour un meilleur devis personnalisé!

Nouvelles chaudes

Network Automation with Cisco DNA Center SDK -Part 1

Nov, 09, 2020 Hi-network.com

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 youon 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 willneed 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 requestsDNAC_URL =[DNA Center host url]DNAC_USER =[username]DNAC_PASS =[password]         defget_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/tokenendpoint. The result, if200OK, 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

Enterdnacentersdk(queue The Next Episode and drop the shades ? )
With dnacentersdk, the above Python code can be consolidated to the following:

fromdnacentersdkimportDNACenterAPIDNAC_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:

  1. Make the SDK available in your code by importing it
    from dnacentersdk import DNACenterAPI
  2. Define your DNA Center's host url, username and password
    DNAC_URL =[DNA Center host url]
    DNAC_USER =[username]
    DNAC_PASS =[password]
  3. 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


tag-icon Tags chauds: Cisco DevNet Centre d’adn Cisco network automation

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.