Skip to content

Commit

Permalink
Merge pull request #278 from MUTHU-RAKESH-27/main
Browse files Browse the repository at this point in the history
Added the maximum timeout for checking the execution of task id and execution id
  • Loading branch information
madhansansel authored Jun 7, 2024
2 parents 087b0a1 + e6de4ad commit d9b0823
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
37 changes: 28 additions & 9 deletions plugins/module_utils/dnac.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, module):
'parsed': self.verify_diff_parsed
}
self.dnac_log = dnac_params.get("dnac_log")
self.max_timeout = self.params.get('dnac_api_task_timeout')

if self.dnac_log and not DnacBase.__is_log_init:
self.dnac_log_level = dnac_params.get("dnac_log_level") or 'WARNING'
Expand Down Expand Up @@ -301,14 +302,16 @@ def get_task_details(self, task_id):

return result

def check_task_response_status(self, response, validation_string, data=False):
def check_task_response_status(self, response, validation_string, api_name, data=False):
"""
Get the site id from the site name.
Parameters:
self - The current object details.
response (dict) - API response.
validation_string (string) - String used to match the progress status.
validation_string (str) - String used to match the progress status.
api_name (str) - API name.
data (bool) - Set to True if the API is returning any information. Else, False.
Returns:
self
Expand All @@ -331,7 +334,15 @@ def check_task_response_status(self, response, validation_string, data=False):
return self

task_id = response.get("taskId")
start_time = time.time()
while True:
end_time = time.time()
if (end_time - start_time) >= self.max_timeout:
self.log("Max timeout of {0} sec has reached for the execution id '{1}'. "
"Exiting the loop due to unexpected API '{2}' status."
.format(self.max_timeout, task_id, api_name), "WARNING")
break

task_details = self.get_task_details(task_id)
self.log('Getting task details from task ID {0}: {1}'.format(task_id, task_details), "DEBUG")

Expand All @@ -350,7 +361,7 @@ def check_task_response_status(self, response, validation_string, data=False):
self.status = "success"
break

self.log("progress set to {0} for taskid: {1}".format(task_details.get('progress'), task_id), "DEBUG")
self.log("Progress is {0} for task ID: {1}".format(task_details.get('progress'), task_id), "DEBUG")

return self

Expand Down Expand Up @@ -380,12 +391,13 @@ def get_execution_details(self, execid):
self.log("Response for the current execution: {0}".format(response))
return response

def check_execution_response_status(self, response):
def check_execution_response_status(self, response, api_name):
"""
Checks the reponse status provided by API in the Cisco Catalyst Center
Parameters:
response (dict) - API response
api_name (str) - API name
Returns:
self
Expand All @@ -401,9 +413,17 @@ def check_execution_response_status(self, response):
self.status = "failed"
return self

executionid = response.get("executionId")
execution_id = response.get("executionId")
start_time = time.time()
while True:
execution_details = self.get_execution_details(executionid)
end_time = time.time()
if (end_time - start_time) >= self.max_timeout:
self.log("Max timeout of {0} sec has reached for the execution id '{1}'. "
"Exiting the loop due to unexpected API '{2}' status."
.format(self.max_timeout, execution_id, api_name), "WARNING")
break

execution_details = self.get_execution_details(execution_id)
if execution_details.get("status") == "SUCCESS":
self.result['changed'] = True
self.msg = "Successfully executed"
Expand Down Expand Up @@ -529,15 +549,14 @@ def check_status_api_events(self, status_execution_id):
response from the API. If the timeout is reached first, the method logs a warning and returns None.
"""

max_timeout = self.params.get('dnac_api_task_timeout')
events_response = None
start_time = time.time()

while True:
end_time = time.time()
if (end_time - start_time) >= max_timeout:
if (end_time - start_time) >= self.max_timeout:
self.log("""Max timeout of {0} sec has reached for the execution id '{1}' for the event and unexpected
api status so moving out of the loop.""".format(max_timeout, status_execution_id), "WARNING")
api status so moving out of the loop.""".format(self.max_timeout, status_execution_id), "WARNING")
break
# Now we check the status of API Events for configuring destination and notifications
response = self.dnac._exec(
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/device_credential_workflow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,7 @@ def create_device_credentials(self):
self.log("Received API response from 'create_global_credentials_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential addition performed"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "create_global_credentials_v2").check_return_status()
self.log("Global credential created successfully", "INFO")
result_global_credential.update({
"Creation": {
Expand Down Expand Up @@ -2288,7 +2288,7 @@ def update_device_credentials(self):
self.log("Received API response for 'update_global_credentials_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential update performed"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "update_global_credentials_v2").check_return_status()
self.log("Updating device credential API input parameters: {0}"
.format(final_response), "DEBUG")
self.log("Global device credential updated successfully", "INFO")
Expand Down Expand Up @@ -2344,7 +2344,7 @@ def assign_credentials_to_site(self):
self.log("Received API response for 'assign_device_credential_to_site_v2': {0}"
.format(response), "DEBUG")
validation_string = "desired common settings operation successful"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "assign_device_credential_to_site_v2").check_return_status()
self.log("Device credential assigned to site {0} is successfully."
.format(site_ids), "INFO")
self.log("Desired State for assign credentials to a site: {0}"
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/ise_radius_integration_workflow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ def update_auth_policy_server(self, ipAddress):
params=auth_server_params,
)
validation_string = "successfully updated aaa settings"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "edit_authentication_and_policy_server_access_configuration").check_return_status()
self.log("Authentication and Policy Server '{0}' updated successfully"
.format(ipAddress), "INFO")
result_auth_server.get("response").get(ipAddress) \
Expand Down Expand Up @@ -1399,7 +1399,7 @@ def delete_auth_policy_server(self, ipAddress):

# Check the task status
validation_string = "successfully deleted aaa settings"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "delete_authentication_and_policy_server_access_configuration").check_return_status()
taskid = response.get("response").get("taskId")

# Update result information
Expand Down
16 changes: 8 additions & 8 deletions plugins/modules/network_settings_workflow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ def get_have_network(self, network_details):

site_id = self.get_site_id(site_name)
if site_id is None:
self.msg = "Failed to get site id from {0}".format(site_name)
self.msg = "The site with the name '{0}' is not available in the Catalyst Center".format(site_name)
self.status = "failed"
return self

Expand Down Expand Up @@ -2223,7 +2223,7 @@ def update_global_pool(self, global_pool):
op_modifies=True,
params=pool_params,
)
self.check_execution_response_status(response).check_return_status()
self.check_execution_response_status(response, "create_global_pool").check_return_status()
self.log("Successfully created global pool successfully.", "INFO")
for item in pool_params.get("settings").get("ippool"):
name = item.get("ipPoolName")
Expand Down Expand Up @@ -2267,7 +2267,7 @@ def update_global_pool(self, global_pool):
params=pool_params,
)

self.check_execution_response_status(response).check_return_status()
self.check_execution_response_status(response, "update_global_pool").check_return_status()
for item in pool_params.get("settings").get("ippool"):
name = item.get("ipPoolName")
self.log("Global pool '{0}' Updated successfully.".format(name), "INFO")
Expand Down Expand Up @@ -2316,7 +2316,7 @@ def update_reserve_pool(self, reserve_pool):
op_modifies=True,
params=reserve_params,
)
self.check_execution_response_status(response).check_return_status()
self.check_execution_response_status(response, "reserve_ip_subpool").check_return_status()
self.log("Successfully created IP subpool reservation '{0}'.".format(name), "INFO")
result_reserve_pool.get("response") \
.update({name: self.want.get("wantReserve")[reserve_pool_index]})
Expand Down Expand Up @@ -2347,7 +2347,7 @@ def update_reserve_pool(self, reserve_pool):
op_modifies=True,
params=reserve_params,
)
self.check_execution_response_status(response).check_return_status()
self.check_execution_response_status(response, "update_reserve_ip_subpool").check_return_status()
self.log("Reserved ip subpool '{0}' updated successfully.".format(name), "INFO")
result_reserve_pool.get("response") \
.update({name: reserve_params})
Expand Down Expand Up @@ -2402,7 +2402,7 @@ def update_network(self, config):
)
self.log("Received API response of 'update_network_v2': {0}".format(response), "DEBUG")
validation_string = "desired common settings operation successful"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "update_network_v2").check_return_status()
self.log("Network has been changed successfully", "INFO")
result_network.get("msg") \
.update({site_name: "Network Updated successfully"})
Expand Down Expand Up @@ -2469,7 +2469,7 @@ def delete_reserve_pool(self, reserve_pool_details):
op_modifies=True,
params={"id": _id},
)
self.check_execution_response_status(response).check_return_status()
self.check_execution_response_status(response, "release_reserve_ip_subpool").check_return_status()
executionid = response.get("executionId")
result_reserve_pool = self.result.get("response")[1].get("reservePool")
result_reserve_pool.get("response").update({name: {}})
Expand Down Expand Up @@ -2513,7 +2513,7 @@ def delete_global_pool(self, global_pool_details):
)

# Check the execution status
self.check_execution_response_status(response).check_return_status()
self.check_execution_response_status(response, "delete_global_ip_pool").check_return_status()
executionid = response.get("executionId")

# Update result information
Expand Down
6 changes: 4 additions & 2 deletions plugins/modules/template_workflow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,7 @@ def handle_export(self, export):
validation_string = "successfully exported project"
self.check_task_response_status(response,
validation_string,
"export_projects",
True).check_return_status()
self.result['response'][1].get("export").get("response").update({"exportProject": self.msg})

Expand All @@ -2200,6 +2201,7 @@ def handle_export(self, export):
validation_string = "successfully exported template"
self.check_task_response_status(response,
validation_string,
"export_templates",
True).check_return_status()
self.result['response'][1].get("export").get("response").update({"exportTemplate": self.msg})

Expand Down Expand Up @@ -2248,7 +2250,7 @@ def handle_import(self, _import):
params=_import_project,
)
validation_string = "successfully imported project"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "imports_the_projects_provided").check_return_status()
self.result['response'][2].get("import").get("response").update({"importProject": validation_string})
else:
self.msg = "Projects '{0}' already available.".format(payload)
Expand Down Expand Up @@ -2334,7 +2336,7 @@ def handle_import(self, _import):
params=import_template
)
validation_string = "successfully imported template"
self.check_task_response_status(response, validation_string).check_return_status()
self.check_task_response_status(response, validation_string, "imports_the_templates_provided").check_return_status()
self.result['response'][2].get("import").get("response") \
.update({"importTemplate": "Successfully imported the templates"})

Expand Down

0 comments on commit d9b0823

Please sign in to comment.