From dc1dd076b0f453376ab1246b12af4c684e647670 Mon Sep 17 00:00:00 2001 From: William Astorga Date: Fri, 11 Feb 2022 18:22:43 -0600 Subject: [PATCH] Add early check of status for SDA modules --- plugins/action/sda_fabric.py | 13 +++++++++++-- .../action/sda_fabric_authentication_profile.py | 13 +++++++++++-- plugins/action/sda_fabric_border_device.py | 13 +++++++++++-- plugins/action/sda_fabric_control_plane_device.py | 13 +++++++++++-- plugins/action/sda_fabric_edge_device.py | 13 +++++++++++-- plugins/action/sda_fabric_site.py | 13 +++++++++++-- plugins/action/sda_multicast.py | 13 +++++++++++-- .../action/sda_port_assignment_for_access_point.py | 13 +++++++++++-- .../action/sda_port_assignment_for_user_device.py | 13 +++++++++++-- plugins/action/sda_provision_device.py | 14 ++++++++++++-- plugins/action/sda_virtual_network.py | 13 +++++++++++-- plugins/action/sda_virtual_network_ip_pool.py | 13 +++++++++++-- plugins/action/sda_virtual_network_v2.py | 13 +++++++++++-- plugins/plugin_utils/exceptions.py | 5 +++++ 14 files changed, 149 insertions(+), 26 deletions(-) diff --git a/plugins/action/sda_fabric.py b/plugins/action/sda_fabric.py index ccd50e8a13..9df999f6d2 100644 --- a/plugins/action/sda_fabric.py +++ b/plugins/action/sda_fabric.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -138,6 +139,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -204,8 +210,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_fabric_authentication_profile.py b/plugins/action/sda_fabric_authentication_profile.py index f73541ac98..1dd163ecf3 100644 --- a/plugins/action/sda_fabric_authentication_profile.py +++ b/plugins/action/sda_fabric_authentication_profile.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -144,6 +145,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def update(self): @@ -226,8 +232,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() diff --git a/plugins/action/sda_fabric_border_device.py b/plugins/action/sda_fabric_border_device.py index cca062d58c..e1b12c2f50 100644 --- a/plugins/action/sda_fabric_border_device.py +++ b/plugins/action/sda_fabric_border_device.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -145,6 +146,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -211,8 +217,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_fabric_control_plane_device.py b/plugins/action/sda_fabric_control_plane_device.py index 66e3edf6ff..1989d290be 100644 --- a/plugins/action/sda_fabric_control_plane_device.py +++ b/plugins/action/sda_fabric_control_plane_device.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -139,6 +140,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -205,8 +211,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_fabric_edge_device.py b/plugins/action/sda_fabric_edge_device.py index cd53e095f8..9ee13afe67 100644 --- a/plugins/action/sda_fabric_edge_device.py +++ b/plugins/action/sda_fabric_edge_device.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -139,6 +140,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -205,8 +211,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_fabric_site.py b/plugins/action/sda_fabric_site.py index fd98e35524..2f4fd98854 100644 --- a/plugins/action/sda_fabric_site.py +++ b/plugins/action/sda_fabric_site.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -139,6 +140,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -205,8 +211,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_multicast.py b/plugins/action/sda_multicast.py index 2ab785c583..05624a783e 100644 --- a/plugins/action/sda_multicast.py +++ b/plugins/action/sda_multicast.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -142,6 +143,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -205,8 +211,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_port_assignment_for_access_point.py b/plugins/action/sda_port_assignment_for_access_point.py index 77076be7d0..5731f79874 100644 --- a/plugins/action/sda_port_assignment_for_access_point.py +++ b/plugins/action/sda_port_assignment_for_access_point.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -148,6 +149,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -214,8 +220,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_port_assignment_for_user_device.py b/plugins/action/sda_port_assignment_for_user_device.py index 4cf879ab65..59382ee768 100644 --- a/plugins/action/sda_port_assignment_for_user_device.py +++ b/plugins/action/sda_port_assignment_for_user_device.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -148,6 +149,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -214,8 +220,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_provision_device.py b/plugins/action/sda_provision_device.py index 2b2d476511..64bec7603a 100644 --- a/plugins/action/sda_provision_device.py +++ b/plugins/action/sda_provision_device.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -134,6 +135,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -197,8 +203,12 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) + elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_virtual_network.py b/plugins/action/sda_virtual_network.py index 0e734ae828..e4194c16c6 100644 --- a/plugins/action/sda_virtual_network.py +++ b/plugins/action/sda_virtual_network.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -144,6 +145,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -210,8 +216,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_virtual_network_ip_pool.py b/plugins/action/sda_virtual_network_ip_pool.py index eed9c00931..ba44a03c90 100644 --- a/plugins/action/sda_virtual_network_ip_pool.py +++ b/plugins/action/sda_virtual_network_ip_pool.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -150,6 +151,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def delete(self): @@ -216,8 +222,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() if obj_exists: diff --git a/plugins/action/sda_virtual_network_v2.py b/plugins/action/sda_virtual_network_v2.py index 2e2bb1d8f0..6cb906efbd 100644 --- a/plugins/action/sda_virtual_network_v2.py +++ b/plugins/action/sda_virtual_network_v2.py @@ -24,6 +24,7 @@ ) from ansible_collections.cisco.dnac.plugins.plugin_utils.exceptions import ( InconsistentParameters, + AnsibleSDAException, ) # Get common arguments specification @@ -150,6 +151,11 @@ def create(self): params=self.create_params(), op_modifies=True, ) + if isinstance(result, dict): + if 'response' in result: + result = result.get('response') + if isinstance(result, dict) and result.get("status") == "failed": + raise AnsibleSDAException(response=result) return result def update(self): @@ -226,8 +232,11 @@ def run(self, tmp=None, task_vars=None): response = prev_obj dnac.object_already_present() else: - response = obj.create() - dnac.object_created() + try: + response = obj.create() + dnac.object_created() + except AnsibleSDAException as e: + dnac.fail_json("Could not create object {e}".format(e=e._response)) elif state == "absent": (obj_exists, prev_obj) = obj.exists() diff --git a/plugins/plugin_utils/exceptions.py b/plugins/plugin_utils/exceptions.py index 562d6634e5..0ed417bed3 100644 --- a/plugins/plugin_utils/exceptions.py +++ b/plugins/plugin_utils/exceptions.py @@ -16,3 +16,8 @@ class AnsibleDNACException(Exception): class InconsistentParameters(AnsibleDNACException): """Provided parameters are not consistent.""" pass + + +class AnsibleSDAException(Exception): + def __init__(self, response): + self._response = response