Forum Discussion
Lately I've found myself using the Cisco Catalyst Center APIs for one-off tasks. This week's fun side project is building out a templated approach to accessing the Cisco Catalyst Center. Yesterday one of my guys came to me with automate needs and we were able to use this template to throw together a script to do it in about 5 minutes.
The goal of this template is to write API calls with exceptionally high velocity using the Catalyst Center Developer Toolkit. It's a swagger-like GUI explorer hosted on the Catalyst Center that you can copy and paste out of into the function skeleton I have outlined below.
Yall go easy on me, though! I'm a network/storage guy who pretends he's a developer sometimes. I'm already working on version 2 that will introduce a couple of new features. The first enhancement will be an additional flag on the function skeleton to request if the function should hold until execution completes (for those long running calls) or just carry on if you want to go asynchronous. The second enhancement will be actually hashing the passwords instead of just encoding them. What can I say, we're already using secured connections with enhanced cert validation but I'm paranoid!
import requests
import base64
import json
import certifi
import getpass
#Generate an auth token to be reused for all other API calls
#Auth token valid for 1 hour
def get_auth_token():
resource = '/api/system/v1/auth/token'
url = cace + resource
byte_string = (f'{user}:{password}').encode("ascii")
authorizationBase64 = base64.b64encode(byte_string).decode()
headers = {
'content-type': 'application/json',
'Authorization': 'Basic ' + authorizationBase64
}
#verify uses the certify library for verification. Load your enterprise root CA into the file at print(certifi.where()).
response = requests.request('POST', url, headers=headers, verify=certifi.where())
token = json.loads(response.text)
token = (token["Token"])
return token
#This function will actually execute the API call for your logic function.
#Make sure you have a token value in your headers variable when you call the api_call function.
#Make sure to null any unusaed variables when calling function.
def api_call(resource, params, body, method):
url = cace + resource
headers = {
'content-type': 'application/json',
'x-auth-token': token
}
#All API calls will funnel through this request.
response = requests.request(method, url, params=params, headers=headers, json=body, verify=certifi.where())
#Use these commented out lines for troubleshooting
#print(json.loads(response.text))
#print(response.status_codes)
#print('\n')
return response
#Generic skeleton of functions to access CaCe
#https://developer.cisco.com/docs/dna-center/cisco-catalyst-center-2-3-7-9-api-overview/
def cace_function():
#some endpoints require appending details to the resouce. Simple concatenation works here.
resource = '/whatever/you/need'
params = None #Fill if the resource needs it
body = None #Fill if the resource needs it
method = 'GET|POST|PUT|DELTE'
response = api_call(resource, params, body, method)
#returns only message response
function_output = json.loads(response.text)
return function_output
#credentials and CaCe
user = input("Please enter username: ")
password = getpass.getpass("Please enter password: ")
#Catalyst Center to access
cace = 'https://YourCayalystCenter.domain'
token = ''
token = get_auth_token()
#Call your function
cace_function()
- catud2 months agoCommunity Manager
That's absolutely amazing, Mike_Dehaan!
Thank you so much for being the very first one to participate in our Weekly Creative Corner series! You've set the bar and shown us exactly what we hoped this series would bring!
The template you've built for accessing the Cisco Catalyst Center sounds incredibly powerful, as well as practical. That's the kind of real world efficiency we love to see!
We can't wait to see more of your fun side projects, especially those version 2 enhancements. The two added features you're working on are genius!
Keep these awesome creations coming everybody!