diff --git a/plugins/httpapi/sonic.py b/plugins/httpapi/sonic.py index eb9eda949..b35b3005a 100644 --- a/plugins/httpapi/sonic.py +++ b/plugins/httpapi/sonic.py @@ -39,6 +39,7 @@ import json import time +import re from ansible.module_utils._text import to_text from ansible.module_utils.connection import ConnectionError @@ -78,7 +79,11 @@ def edit_config(self, requests): try: response = self.send_request(**req) except ConnectionError as exc: - raise ConnectionError(to_text(exc, errors='surrogate_then_replace')) + if req.get('method') == 'get' and re.search("code.*404", str(exc)): + # 'code': 404, 'error-message': 'Resource not found' + response = [{}, {}] + else: + raise ConnectionError(to_text(exc, errors='surrogate_then_replace')) responses.append(response) return responses diff --git a/plugins/module_utils/network/sonic/facts/system/system.py b/plugins/module_utils/network/sonic/facts/system/system.py index cc6ed352b..39a56ab8f 100644 --- a/plugins/module_utils/network/sonic/facts/system/system.py +++ b/plugins/module_utils/network/sonic/facts/system/system.py @@ -16,10 +16,12 @@ from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import ( utils, ) + +from ansible.module_utils.connection import ConnectionError + from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.sonic import ( to_request, - edit_config, - edit_config_catch + edit_config ) from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.system.system import SystemArgs @@ -91,7 +93,10 @@ def get_anycast_addr(self): def get_load_share_hash_algo(self): """Get load share hash algorithm""" request = [{"path": "data/openconfig-loadshare-mode-ext:loadshare/hash-algorithm/config", "method": GET}] - response = edit_config_catch(self._module, to_request(self._module, request)) + try: + response = edit_config(self._module, to_request(self._module, request)) + except ConnectionError as exc: + self._module.fail_json(msg=str(exc), code=exc.code) if ('openconfig-loadshare-mode-ext:config' in response[0][1]): data = response[0][1]['openconfig-loadshare-mode-ext:config'] else: @@ -101,7 +106,10 @@ def get_load_share_hash_algo(self): def get_auditd_rules(self): """Get auditd rules configuration available in chassis""" request = [{"path": "data/openconfig-system:system/openconfig-system-ext:auditd-system", "method": GET}] - response = edit_config_catch(self._module, to_request(self._module, request)) + try: + response = edit_config(self._module, to_request(self._module, request)) + except ConnectionError as exc: + self._module.fail_json(msg=str(exc), code=exc.code) data = {} if response and response[0]: if len(response[0]) > 1: diff --git a/plugins/module_utils/network/sonic/sonic.py b/plugins/module_utils/network/sonic/sonic.py index 4022ae282..2075e2332 100644 --- a/plugins/module_utils/network/sonic/sonic.py +++ b/plugins/module_utils/network/sonic/sonic.py @@ -130,7 +130,7 @@ def edit_config(module, commands, skip_code=None): # Start: This is to convert interface name from Eth1/1 to Eth1%2f1 for request in commands: - # This check is to differenciate between requests and commands + # This check is to differentiate between requests and commands if isinstance(request, dict): url = request.get("path", None) if url: @@ -139,24 +139,6 @@ def edit_config(module, commands, skip_code=None): return connection.edit_config(commands) -def edit_config_catch(module, commands, skip_code=None): - connection = get_connection(module) - - # Start: This is to convert interface name from Eth1/1 to Eth1%2f1 - for request in commands: - # This check is to differenciate between requests and commands - if isinstance(request, dict): - url = request.get("path", None) - if url: - request["path"] = update_url(url) - # End - try: - response = connection.edit_config(commands) - except ConnectionError: - response = [[{}, {}]] - return response - - def edit_config_reboot(module, commands, skip_code=None): connection = get_connection(module) diff --git a/tests/unit/modules/network/sonic/test_sonic_system.py b/tests/unit/modules/network/sonic/test_sonic_system.py index c21d328a1..a63e5bf26 100644 --- a/tests/unit/modules/network/sonic/test_sonic_system.py +++ b/tests/unit/modules/network/sonic/test_sonic_system.py @@ -22,9 +22,6 @@ def setUpClass(cls): cls.mock_facts_edit_config = patch( "ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.system.system.edit_config" ) - cls.mock_facts_edit_config_catch = patch( - "ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.system.system.edit_config_catch" - ) cls.mock_config_edit_config = patch( "ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.system.system.edit_config" ) @@ -39,10 +36,8 @@ def setUpClass(cls): def setUp(self): super(TestSonicSystemModule, self).setUp() self.facts_edit_config = self.mock_facts_edit_config.start() - self.facts_edit_config_catch = self.mock_facts_edit_config_catch.start() self.config_edit_config = self.mock_config_edit_config.start() self.facts_edit_config.side_effect = self.facts_side_effect - self.facts_edit_config_catch.side_effect = self.facts_side_effect self.config_edit_config.side_effect = self.config_side_effect self.get_interface_naming_mode = self.mock_get_interface_naming_mode.start() self.get_interface_naming_mode.return_value = 'standard' @@ -52,7 +47,6 @@ def setUp(self): def tearDown(self): super(TestSonicSystemModule, self).tearDown() self.mock_facts_edit_config.stop() - self.mock_facts_edit_config_catch.stop() self.mock_config_edit_config.stop() self.mock_get_interface_naming_mode.stop() self.mock_utils_edit_config.stop()