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

DNAC SDK Question - Understanding get_task_tree

evanm
Level 1
Level 1

Hello Cisco community,

I am trying to understand how to best proceed with validating device additions within DNAC via the SDK. Any tips or tricks would be greatly appreciated. Here's the process that I am following:

  1. Python script authenticates via authentication_api().
  2. Python script creates the payload dictionary and utilizes add_device(payload=device).
  3. Python script utilizes the taskId in get_task_tree()to determine the status.
  4. Python scripts calls assign_device_to_site()with the buildingid to accomplish the correct assignment.

I struggle to understand whether I need to be adding a 10-30 second delay between each addition or even the execution of the "assign_device_to_site()" command. Or are people using a form of event handling to monitor the progress of the "get_task_tree"? If the latter, could you please share what you are using? I understand that DNAC SDK provides a rate limiter but still fuzzy how it would affect my process. Would the SDK api call just hang until it completes or is there a risk that the device does not get added and my script calls the assign_device_to_site() prematurely.

Thanks in advance.

-E

1 Accepted Solution

Accepted Solutions

nirraman
Cisco Employee
Cisco Employee

One way to monitor the progress of the "get_task_tree" is to use a loop that checks the status of the task at regular intervals until it is completed. You can use the `get_task_by_id` method to check the status of the task and then use a conditional statement to determine if the task is completed or not. If the task is not completed, you can use the `time.sleep` method to add a delay before checking the status again.

 

Regarding the rate limiter provided by the DNAC SDK, it is designed to help prevent your script from exceeding the rate limits set by Cisco DNA Center. If your script exceeds the rate limit, the SDK will automatically retry the request after a certain amount of time has passed. This means that you do not need to worry about adding delays between each addition as the SDK will handle this for you.

 

Here is an example of how you could implement this in your script:

 

```python

-------------------------------------------------------------------------------

from dnacentersdk import api

import time

 

dnac = api.DNACenterAPI(username='username', password='password', base_url='https://your-dnac-url')

device = {'hostname': 'device-hostname', 'ip': 'device-ip', 'type': 'device-type'}

response = dnac.devices.add_device(**device)

task_id = response.response.taskId

 

while True:

  task = dnac.task.get_task_by_id(task_id)

  if task.response.isError:

    print(f'Task failed: {task.response.failureReason}')

    break

  elif task.response.progress == 'COMPLETED':

    print('Task completed successfully')

    break

  else:

    print(f'Task in progress: {task.response.progress}')

    time.sleep(10)

 

-------------------------------------------------------------------------------

 

This script authenticates with Cisco DNA Center and adds a device using the `add_device` method. It then uses a loop to check the status of the task at regular intervals until it is completed. If the task fails, it prints an error message and exits the loop. If the task is completed successfully, it prints a success message and exits the loop. Otherwise, it prints the current progress of the task and waits for 10 seconds before checking again.

 

If you find my reply solved your question or issue, kindly click the 'Accept as Solution' button and vote it as helpful.

You can also learn more about Cisco DNA Center through our live Ask the Experts (ATXs) session. Check out Cisco DNA Center ATXs Resources [https://community.cisco.com/t5/networking-knowledge-base/cisco-dna-center-ask-the-experts-resources/ta-p/4394489] to view the latest schedule for upcoming sessions, as well as the useful references, e.g. online guides, FAQs.

View solution in original post

2 Replies 2

nirraman
Cisco Employee
Cisco Employee

One way to monitor the progress of the "get_task_tree" is to use a loop that checks the status of the task at regular intervals until it is completed. You can use the `get_task_by_id` method to check the status of the task and then use a conditional statement to determine if the task is completed or not. If the task is not completed, you can use the `time.sleep` method to add a delay before checking the status again.

 

Regarding the rate limiter provided by the DNAC SDK, it is designed to help prevent your script from exceeding the rate limits set by Cisco DNA Center. If your script exceeds the rate limit, the SDK will automatically retry the request after a certain amount of time has passed. This means that you do not need to worry about adding delays between each addition as the SDK will handle this for you.

 

Here is an example of how you could implement this in your script:

 

```python

-------------------------------------------------------------------------------

from dnacentersdk import api

import time

 

dnac = api.DNACenterAPI(username='username', password='password', base_url='https://your-dnac-url')

device = {'hostname': 'device-hostname', 'ip': 'device-ip', 'type': 'device-type'}

response = dnac.devices.add_device(**device)

task_id = response.response.taskId

 

while True:

  task = dnac.task.get_task_by_id(task_id)

  if task.response.isError:

    print(f'Task failed: {task.response.failureReason}')

    break

  elif task.response.progress == 'COMPLETED':

    print('Task completed successfully')

    break

  else:

    print(f'Task in progress: {task.response.progress}')

    time.sleep(10)

 

-------------------------------------------------------------------------------

 

This script authenticates with Cisco DNA Center and adds a device using the `add_device` method. It then uses a loop to check the status of the task at regular intervals until it is completed. If the task fails, it prints an error message and exits the loop. If the task is completed successfully, it prints a success message and exits the loop. Otherwise, it prints the current progress of the task and waits for 10 seconds before checking again.

 

If you find my reply solved your question or issue, kindly click the 'Accept as Solution' button and vote it as helpful.

You can also learn more about Cisco DNA Center through our live Ask the Experts (ATXs) session. Check out Cisco DNA Center ATXs Resources [https://community.cisco.com/t5/networking-knowledge-base/cisco-dna-center-ask-the-experts-resources/ta-p/4394489] to view the latest schedule for upcoming sessions, as well as the useful references, e.g. online guides, FAQs.

Thank you Nirraman. I was able to write something similar but not as eloquent as yours. Much appreciated.

Kind regards.

-E