Skip to content

Commit

Permalink
Modify the fix to operation on individual requests in the enterprise_…
Browse files Browse the repository at this point in the history
…sonic httpapi plugin.
  • Loading branch information
kerry-meyer committed Aug 27, 2024
1 parent bf64770 commit 5655a00
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
7 changes: 6 additions & 1 deletion plugins/httpapi/sonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
16 changes: 12 additions & 4 deletions plugins/module_utils/network/sonic/facts/system/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
20 changes: 1 addition & 19 deletions plugins/module_utils/network/sonic/sonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down
6 changes: 0 additions & 6 deletions tests/unit/modules/network/sonic/test_sonic_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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'
Expand All @@ -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()
Expand Down

0 comments on commit 5655a00

Please sign in to comment.