Introduction

Work with the DNA Center APIs in Native Python!

Sure, working with the DNA Center APIs is easy (see api_docs). They are RESTful, naturally structured, require only a simple Access Token for authentication, and the data is elegantly represented in intuitive JSON. What could be easier?

import requests

URL = 'https://sandboxdnac.cisco.com:443/dna/intent/api/v1/network-device'
ACCESS_TOKEN = '<your_access_token>'

family = '<family_name>'
headers = {'X-Auth-Token': ACCESS_TOKEN,
           'Content-type': 'application/json;charset=utf-8'}
params_data = { 'family': family }
response = requests.get(URL, params=params_data, headers=headers)
if response.status_code == 200:
    device_response = response.json['response']
    for device in device_response:
        print('{:20s}{}'.format(device['hostname'], device['upTime']))
else:
    # Oops something went wrong...  Better do something about it.
    print(response.status_code, response.text)

Like I said, EASY. However, in use, the code can become rather repetitive…

  • You have to setup the environment every time

  • You have to remember URLs, request parameters and JSON formats (or reference the docs)

  • You have to parse the returned JSON and work with multiple layers of list and dictionary indexes

Enter dnacentersdk, a simple API wrapper that wraps all of the DNA Center API calls and returned JSON objects within native Python objects and methods.

With dnacentersdk, the above Python code can be consolidated to the following:

from dnacentersdk import api

api_ = api.DNACenterAPI(base_url='https://sandboxdnac.cisco.com:443', version='2.3.5.3')
# Or even just api_ = api.DNACenterAPI() as base_url and version have those values.
try:
    devices = api_.devices.get_device_list(family='Switches and Hubs')
    for device in devices.response:
        print('{:20s}{}'.format(device.hostname, device.upTime))
except ApiError as e:
    print(e)

dnacentersdk handles all of this for you:

  • Reads your DNA Center credentials from environment variables (DNA_CENTER_ENCODED_AUTH, DNA_CENTER_USERNAME, DNA_CENTER_PASSWORD)

  • Reads your DNA Center API version from environment variable DNA_CENTER_VERSION. Supported versions: 1.2.10, 1.3.0, 1.3.1, 1.3.3, 2.1.1, 2.1.2, 2.2.1, 2.2.2.3, 2.2.3.3, 2.3.3.0 and 2.3.5.3. Now with version and base_url, you have more control.

  • Controls whether to verify the server’s TLS certificate or not according to the verify parameter.

  • Reads your DNA Center debug from environment variable DNA_CENTER_DEBUG. Boolean, it controls whether to log information about DNA Center APIs’ request and response process.

  • Wraps and represents all DNA Center API calls as a simple hierarchical tree of native-Python methods (with default arguments provided everywhere possible!)

  • If your Python IDE supports auto-completion (like PyCharm), you can navigate the available methods and object attributes right within your IDE

  • Represents all returned JSON objects as native Python objects - you can access all of the object’s attributes using native dot.syntax

  • Automatic Rate-Limit Handling Sending a lot of requests to DNA Center? Don’t worry; we have you covered. DNA Center will respond with a rate-limit response, which will automatically be caught and “handled” for you. Your requests and script will automatically be “paused” for the amount of time specified by DNA Center, while we wait for the DNA Center rate-limit timer to cool down. After the cool-down, your request will automatically be retried, and your script will continue to run as normal. Handling all of this requires zero (0) changes to your code - you’re welcome. 😎

  • Refresh token Each time the token becomes invalid, the SDK will generate a new valid token for you.

    Just know that if you are are sending a lot of requests, your script might take longer to run if your requests are getting rate limited.

All of this, combined, lets you do powerful things simply:

from dnacentersdk import api

# Create a DNACenterAPI connection object; it uses DNA Center sandbox URL, username and password, with DNA Center API version 2.3.5.3.
api_ = api.DNACenterAPI(username="devnetuser", password="Cisco123!", base_url="https://sandboxdnac.cisco.com:443", version='2.3.5.3')

# Find all devices that have 'Switches and Hubs' in their family
devices = api_.devices.get_device_list(family='Switches and Hubs')

# Print all of demo devices
for device in devices.response:
    print('{:20s}{}'.format(device.hostname, device.upTime))

# Find all tags
all_tags = api_.tag.get_tag(sort_by='name', order='des')
demo_tags = [tag for tag in all_tags.response if 'Demo' in tag.name ]

#  Delete all of the demo tags
for tag in demo_tags:
    api_.tag.delete_tag(tag.id)

# Create a new demo tag
demo_tag = api_.tag.create_tag(name='dna Demo')
task_demo_tag = api_.task.get_task_by_id(task_id=demo_tag.response.taskId)

if not task_demo_tag.response.isError:
    # Retrieve created tag
    created_tag = api_.tag.get_tag(name='dna Demo')

    # Update tag
    update_tag = api_.tag.update_tag(id=created_tag.response[0].id,
                                     name='Updated ' + created_tag.response[0].name,
                                     description='DNA Center demo tag')

    print(api_.task.get_task_by_id(task_id=update_tag.response.taskId).response.progress)

    # Retrieved updated
    updated_tag = api_.tag.get_tag(name='Updated dna Demo')
    print(updated_tag)
else:
    # Get task error details
    print('Unfortunately ', task_demo_tag.response.progress)
    print('Reason: ', task_demo_tag.response.failureReason)

Head over to the Quickstart page to begin working with the DNA Center APIs in native Python!

MIT License

dnacentersdk is currently licensed under the MIT Open Source License, and distributed as a source distribution (no binaries) via PyPI, and the complete source code is available on GitHub.

dnacentersdk License

MIT License

Copyright (c) 2019-2021 Cisco Systems.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Copyright (c) 2019-2021 Cisco Systems.