cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1234
Views
2
Helpful
2
Replies

Cisco DNAC API Get Device - Limited to first 500 (?)

netmart2
Level 1
Level 1

Hello,

When using the following API string it seems that I do receive only the first five hundred devices.

 

dna/intent/api/v1/network-device
 
Is there way to retrieve all  devices.
 
Version 2.2.3.5
 
Please advise.
 
Thanks
1 Accepted Solution
2 Replies 2

Andrii Balevych
Cisco Employee
Cisco Employee

Unfortunately, as per the API documentation for DNA, there is no pagination mechanism provided, requiring manual incrementation of the offset parameter. Due to the link endpoint should look like:  /dna/intent/api/v1/network-device/{startIndex}/{recordsToReturn}, where we need to increment startIndex param which also cannot be 0 and should start from 1

 

 

def get_dnac_devices(server_ip, username, password):
# Authenticate and obtain the token
response = requests.post(f"https://{server_ip}/dna/system/api/v1/auth/token",
auth=(username, password), verify=False)
print(response.url)
try:
all_devices = []
token = response.json()['Token']
print(f"token: {token}")
headers = {'X-Auth-Token': token, 'Content-Type': 'application/json'}
url = f"https://{server_ip}/dna/intent/api/v1/network-device"
start_index = 1
records_to_return = 500
while True:
# Construct the URL with the current pagination parameters
current_url = f"{url}/{start_index}/{records_to_return}"
# Send the request
print(current_url)
dev_response = requests.get(current_url, headers=headers, verify=False)
response_json = dev_response.json()
devices = response_json.get('response', [])
#print(devices)
# If no devices are returned, break out of the loop
if len(devices) == 0:
break
# Add the devices from the current page to the list
all_devices.extend(devices)
# Increment the start index for the next page
start_index += records_to_return
print(f"incrementing index: {start_index}")
return all_devices
except Exception as e:
error = f'error occured: {e}'
print(error)
return error
get_dnac_devices('server_ip', 'username', 'password') # specify you server IP and credentials here