First let me say thank you, I am a network guy trying to learn Python.
I put together the follow ing code and I am having an issue with the DNA API rate limit. The error and the code is below. What I am trying to do is get a report/CSV that has the AP info and switch port it's connected to. I have a list of 150 IP address to loop over. Once I loop over 5 I get the limit error. Should I be using a different URI to pull the data I need? Am I going about this all wrong?
ERROR Message
{'error': "Rate Limit exceeded; BapiName: Get Device Enrichment Details, RateLimit config details: RateLimitContext{rate=5, windowUnit='minute', windowDuration=1, maxConc=5, windowUnit='minute', windowDuration=1, maxConcurrentExecutionsPermitted=0}", 'bapiExtendedStatusCode': 'REJECTED_ABO': 'For BAPI: Get Device Enrichment Details, maximVE_THROTTLE_LIMIT', 'bapiExtendedStatusDescription': 'For BAPI: Get Device Enrichment Details, maximum allowed BAPI instUTC 2020 and now'}ances per 1 minute is 5. The limit has been reached for the time-window between Wed Nov 11 18:44:46 UTC 2020 and now'}
Code
import requests
import http.client
import json
from pprint import pprint
from requests.auth import HTTPBasicAuth
from dnac_config import DNAC, DNAC_PORT, DNAC_USER, DNAC_PASSWORD, DNACU
import csv
DeviceData = []
a_file = open("aplist.txt", "r")
APList = [(line.strip()) for line in a_file]
a_file.close()
fields = ['Switch Name', 'Port Mode', 'Swtich Interface', 'VLAN', 'AP Address', 'AP Name', 'WLC']
url = DNAC + '/dna/system/api/v1/auth/token' # Endpoint URL
resp = requests.post(url, auth=HTTPBasicAuth(DNAC_USER, DNAC_PASSWORD)) # Make the POST Request
token = resp.json()['Token'] # Retrieve the Token from the returned JSON
#print("Token Retrieved: {}".format(token)) # Print out the Token
with open('GFG.csv', 'w') as f:
write = csv.writer(f)
write.writerow(fields)
for IP in APList:
conn = http.client.HTTPSConnection(DNACU)
headers = {
'X-Auth-Token': token,
'entity_type': "ip_address",
'entity_value': IP }
conn.request("GET", "/dna/intent/api/v1/device-enrichment-details", headers=headers)
res = conn.getresponse()
data = res.read()
device_data = json.loads(data)
print(device_data)
for item in device_data:
DeviceData.append(item['deviceDetails']['neighborTopology'][0]['nodes'][3]['name'])
for item in device_data:
DeviceData.append(item["deviceDetails"]['neighborTopology'][0]['nodes'][3]['role'])
for item in device_data:
DeviceData.append(item["deviceDetails"]['neighborTopology'][0]['links'][2]['targetInterfaceName'])
for item in device_data:
DeviceData.append('VLAN'+item["deviceDetails"]['neighborTopology'][0]['links'][2]['targetPortVLANInfo'])
for item in device_data:
DeviceData.append(item["deviceDetails"]['managementIpAddress'])
for item in device_data:
DeviceData.append(item["deviceDetails"]['hostname'])
for item in device_data:
DeviceData.append(item["deviceDetails"]['associatedWlcIp'])
with open('GFG.csv', 'a', newline='') as f:
write = csv.writer(f)
write.writerow(DeviceData)
DeviceData = []