diff --git a/ansible_collections/arista/cvp/plugins/module_utils/change_tools.py b/ansible_collections/arista/cvp/plugins/module_utils/change_tools.py index be36b4f70..fef39d7ba 100644 --- a/ansible_collections/arista/cvp/plugins/module_utils/change_tools.py +++ b/ansible_collections/arista/cvp/plugins/module_utils/change_tools.py @@ -654,7 +654,12 @@ def module_action(self, change: dict, name: str = None, state: str = "show", cha MODULE_LOGGER.debug("Successfully deleted: %s", change_id) changed = True except Exception as e: - self.__ansible.fail_json(msg="{0}".format(e)) + if "Forbidden" in str(e): + message = "Failed to delete Change Control. User is unauthorized!" + else: + message = str(e) + logging.error(message) + self.__ansible.fail_json(msg="{0}".format(message)) return changed, {'remove': []}, warnings @@ -704,7 +709,12 @@ def module_action(self, change: dict, name: str = None, state: str = "show", cha data = cc_structure['key'] except Exception as e: - self.__ansible.fail_json(msg="{0}".format(e)) + if "Forbidden" in str(e): + message = "Failed to create Change Control. User is unauthorized!" + else: + message = str(e) + logging.error(message) + self.__ansible.fail_json(msg="{0}".format(message)) elif state in ['approve', 'unapprove', 'execute', 'schedule', 'approve_and_execute', 'schedule_and_approve'] and self.__check_mode is False: MODULE_LOGGER.debug("Change control state: %s", state) @@ -764,9 +774,14 @@ def module_action(self, change: dict, name: str = None, state: str = "show", cha e = "Change control {0} id not found".format(cc_id) self.__ansible.fail_json(msg="{0}".format(e)) return changed, {"approve": change_id}, warnings - except CvpRequestError: + except CvpRequestError as e: # Skip this - covers the case where an approved CC is approved again - pass + if "Forbidden" in str(e): + message = "Failed to approve Change Control. User is unauthorized!" + self.__ansible.fail_json(msg="{0}".format(message)) + logging.error(str(message)) + else: + pass except Exception as e: self.__ansible.fail_json(msg="{0}".format(e)) diff --git a/ansible_collections/arista/cvp/plugins/module_utils/container_tools.py b/ansible_collections/arista/cvp/plugins/module_utils/container_tools.py index c8ce49c86..b6535b2d4 100644 --- a/ansible_collections/arista/cvp/plugins/module_utils/container_tools.py +++ b/ansible_collections/arista/cvp/plugins/module_utils/container_tools.py @@ -32,7 +32,7 @@ from ansible_collections.arista.cvp.plugins.module_utils.tools_schema import validate_json_schema from ansible_collections.arista.cvp.plugins.module_utils.resources.exceptions import AnsibleCVPApiError, AnsibleCVPNotFoundError, CVPRessource try: - from cvprac.cvp_client_errors import CvpClientError, CvpApiError + from cvprac.cvp_client_errors import CvpClientError, CvpApiError, CvpRequestError HAS_CVPRAC = True except ImportError: HAS_CVPRAC = False @@ -356,6 +356,13 @@ def __configlet_add(self, container: dict, configlets: list, save_topology: bool container=container, create_task=save_topology ) + except CvpRequestError as e: + if "Forbidden" in str(e): + message = "Error configuring configlets. User is unauthorized!" + else: + message = "Error configuring configlets " + str(configlets) + " to container " + str(container) + ". Exception: " + str(e) + MODULE_LOGGER.error(message) + self.__ansible.fail_json(msg=message) except CvpApiError as e: message = "Error configuring configlets " + str(configlets) + " to container " + str(container) + ". Exception: " + str(e) MODULE_LOGGER.error(message) @@ -425,6 +432,13 @@ def __configlet_del(self, container: dict, configlets: list, save_topology: bool container=container, create_task=save_topology ) + except CvpRequestError as e: + if "Forbidden" in str(e): + message = "Error removing configlets. User is unauthorized!" + else: + message = "Error removing configlets " + str(configlets) + " to container " + str(container) + ". Exception: " + str(e) + MODULE_LOGGER.error(message) + self.__ansible.fail_json(msg=message) except CvpApiError as e: message = "Error removing configlets " + str(configlets) + " from container " + str(container) + ". Exception: " + str(e) MODULE_LOGGER.error(message) @@ -507,6 +521,13 @@ def __image_bundle_add(self, container: dict, image_bundle: str): container[Api.generic.NAME], 'container' ) + except CvpRequestError as e: + if "Forbidden" in str(e): + message = "Error applying bundle to container. User is unauthorized!" + else: + message = "Error applying bundle to container " + str(container[Api.generic.NAME]) + ". Exception: " + str(e) + MODULE_LOGGER.error(message) + self.__ansible.fail_json(msg=message) except CvpApiError as catch_error: MODULE_LOGGER.error('Error applying bundle to device: %s', str(catch_error)) self.__ansible.fail_json(msg='Error applying bundle to container' + container[Api.generic.NAME] + ': ' + catch_error) @@ -567,6 +588,13 @@ def __image_bundle_del(self, container: dict): container[Api.generic.NAME], 'container' ) + except CvpRequestError as e: + if "Forbidden" in str(e): + message = "Error removing bundle from container. User is unauthorized!" + else: + message = "Error removing bundle from container " + str(container[Api.generic.NAME]) + ". Exception: " + str(e) + MODULE_LOGGER.error(message) + self.__ansible.fail_json(msg=message) except CvpApiError as catch_error: MODULE_LOGGER.error('Error removing bundle from container: %s', str(catch_error)) self.__ansible.fail_json(msg='Error removing bundle from container: ' + container[Api.generic.NAME] + ': ' + catch_error) @@ -839,6 +867,13 @@ def create_container(self, container: str, parent: str): try: resp = self.__cvp_client.api.add_container( container_name=container, parent_key=parent_id, parent_name=parent) + except CvpRequestError as e: + if "Forbidden" in str(e): + message = "Error creating container. User is unauthorized!" + else: + message = "Error creating container " + str(container) + ". Exception: " + str(e) + MODULE_LOGGER.error(message) + self.__ansible.fail_json(msg=message) except CvpApiError as e: # Add Ansible error management message = "Error creating container " + str(container) + " on CV. Exception: " + str(e) @@ -915,6 +950,13 @@ def delete_container(self, container: str, parent: str): try: resp = self.__cvp_client.api.delete_container( container_name=container, container_key=container_id, parent_key=parent_id, parent_name=parent) + except CvpRequestError as e: + if "Forbidden" in str(e): + message = "Error deleting container. User is unauthorized!" + else: + message = "Error deleting container " + str(container) + ". Exception: " + str(e) + MODULE_LOGGER.error(message) + self.__ansible.fail_json(msg=message) except CvpApiError as e: # Add Ansible error management message = "Error deleting container " + str(container) + " on CV. Exception: " + str(e) diff --git a/ansible_collections/arista/cvp/plugins/module_utils/task_tools.py b/ansible_collections/arista/cvp/plugins/module_utils/task_tools.py index 8919c9230..47ceb2e02 100644 --- a/ansible_collections/arista/cvp/plugins/module_utils/task_tools.py +++ b/ansible_collections/arista/cvp/plugins/module_utils/task_tools.py @@ -137,7 +137,15 @@ def tasker(self, taskIds_list: list, state: str = 'executed'): api_result = CvApiResult(action_name='task_' + str(task_id)) if self.is_actionable(task_data=self.__get_task_data(task_id)): if self.__ansible.check_mode is False: - self.__cv_client.api.add_note_to_task(task_id, "Executed by Ansible") + try: + self.__cv_client.api.add_note_to_task(task_id, "Executed by Ansible") + except CvpRequestError as e: + if "Forbidden" in str(e): + message = "Error while adding note and executing task. User is unauthorized!" + else: + message = "Error while adding note to task: {}".format(str(e)) + logging.error(message) + self.__ansible.fail_json(msg=message) if state == "executed": api_result.add_entry(self.execute_task(task_id)) api_result.changed = True