From 5b461af7f4e7d5fc38365db15055370c32e3a2b6 Mon Sep 17 00:00:00 2001 From: Nilashish Chakraborty Date: Wed, 22 May 2024 17:14:03 +0530 Subject: [PATCH] [l3_interfaces] fail if encapsulation exists on a different sub-interface. (#857) * [l3_interfaces] fail if encapsulation exists on a different sub-interface --------- Signed-off-by: NilashishC --- changelogs/fragments/nxos_l3_interfaces.yaml | 3 + plugins/cliconf/nxos.py | 65 +++++++++++++------ .../config/l3_interfaces/l3_interfaces.py | 4 +- 3 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 changelogs/fragments/nxos_l3_interfaces.yaml diff --git a/changelogs/fragments/nxos_l3_interfaces.yaml b/changelogs/fragments/nxos_l3_interfaces.yaml new file mode 100644 index 000000000..b516aa859 --- /dev/null +++ b/changelogs/fragments/nxos_l3_interfaces.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: + - "`nxos_l3_interfaces` - fail if encapsulation exists on a different sub-interface." diff --git a/plugins/cliconf/nxos.py b/plugins/cliconf/nxos.py index 3a1ae1c6c..b3e15b80e 100644 --- a/plugins/cliconf/nxos.py +++ b/plugins/cliconf/nxos.py @@ -210,38 +210,65 @@ def get_config(self, source="running", flags=None, format="text"): return self.send_command(cmd) - def edit_config(self, candidate=None, commit=True, replace=None, comment=None): + def edit_config( + self, + candidate=None, + commit=True, + replace=None, + comment=None, + err_responses=None, + ): resp = {} operations = self.get_device_operations() - self.check_edit_config_capability(operations, candidate, commit, replace, comment) + self.check_edit_config_capability( + operations, + candidate, + commit, + replace, + comment, + ) results = [] requests = [] + if err_responses: + # update platform default stderr regexes to include modules specific ones + err_responses = [re.compile(to_bytes(err_re)) for err_re in err_responses] + current_stderr_re = self._connection._get_terminal_std_re( + "terminal_stderr_re", + ) + current_stderr_re.extend(err_responses) + if replace: - device_info = self.get_device_info() # not all NX-OS versions support `config replace` # we let the device throw the invalid command error - candidate = "config replace {0}".format(replace) + candidate = f"config replace {replace}" - if commit: - self.send_command("configure terminal") + try: + if commit: + self.send_command("configure terminal") - for line in to_list(candidate): - if not isinstance(line, Mapping): - line = {"command": line} + for line in to_list(candidate): + if not isinstance(line, Mapping): + line = {"command": line} - cmd = line["command"] - if cmd != "end": - results.append(self.send_command(**line)) - requests.append(cmd) + cmd = line["command"] + if cmd != "end": + results.append(self.send_command(**line)) + requests.append(cmd) - self.send_command("end") - else: - raise ValueError("check mode is not supported") + self.send_command("end") + else: + raise ValueError("check mode is not supported") + + resp["request"] = requests + resp["response"] = results + return resp - resp["request"] = requests - resp["response"] = results - return resp + finally: + # always reset terminal regexes to platform default + if err_responses: + for x in err_responses: + current_stderr_re.remove(x) def get( self, diff --git a/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py b/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py index d4da881f5..e9aa2ee02 100644 --- a/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py +++ b/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py @@ -46,6 +46,8 @@ class L3_interfaces(ConfigBase): exclude_params = [] + err_responses = [r"encap in use by another sub-interface"] + def __init__(self, module): super(L3_interfaces, self).__init__(module) @@ -70,7 +72,7 @@ def get_l3_interfaces_facts(self, data=None): return l3_interfaces_facts def edit_config(self, commands): - return self._connection.edit_config(commands) + return self._connection.edit_config(candidate=commands, err_responses=self.err_responses) def execute_module(self): """Execute the module