From 4ac89b8ac7120f553c78eafb294c045f3baa8792 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Tue, 4 Feb 2020 15:14:04 -0500 Subject: [PATCH] Added Fix - Allow nxos_l2_interfaces to append the allowed vlans list (#66517) * Added Integration tests * Corrected lint errors * Added fix for bug # 54400 * Revert "Added fix for bug # 54400" This reverts commit bf42db42697d64abbfea4e546f890637d4a5175b. * Revert "Adding files for RM static_routes" This reverts commit dafdd92d4327cfc6dfcebb1e977454b2f77a1daa. * Revert "Added Integration tests" This reverts commit 129dc87682bba9292105fc3b642fdf3930ce79fd. * Bug Fix 65332 * Added testcase for #66517 * Removed unnecessary commit * fixing conflicts * fixing conflicts * addressed mikeweibe's comments * Corrected lint errors * Added idempotent tc for add vlans * Added replaced and overridded tcs for trunk vlan add --- .../config/l2_interfaces/l2_interfaces.py | 28 ++++++++++--- .../nxos_l2_interfaces/tests/cli/merged.yaml | 41 ++++++++++++++++++- .../tests/cli/overridden.yaml | 3 ++ .../tests/cli/replaced.yaml | 7 +++- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/lib/ansible/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py b/lib/ansible/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py index ad22ba0e356..9f17c4ba09d 100644 --- a/lib/ansible/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py +++ b/lib/ansible/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py @@ -151,12 +151,12 @@ class L2_interfaces(ConfigBase): diff = dict_diff(w, obj_in_have) else: diff = w - merged_commands = self.set_commands(w, have) + merged_commands = self.set_commands(w, have, True) if 'name' not in diff: diff['name'] = w['name'] wkeys = w.keys() dkeys = diff.keys() - for k in wkeys: + for k in w.copy(): if k in self.exclude_params and k in dkeys: del diff[k] replaced_commands = self.del_attribs(diff) @@ -192,7 +192,7 @@ class L2_interfaces(ConfigBase): del h[k] commands.extend(self.del_attribs(h)) for w in want: - commands.extend(self.set_commands(flatten_dict(w), have)) + commands.extend(self.set_commands(flatten_dict(w), have, True)) return commands def _state_merged(self, w, have): @@ -246,7 +246,7 @@ class L2_interfaces(ConfigBase): diff.update({'name': w['name']}) return diff - def add_commands(self, d): + def add_commands(self, d, vlan_exists=False): commands = [] if not d: return commands @@ -255,19 +255,35 @@ class L2_interfaces(ConfigBase): if 'vlan' in d: commands.append(cmd + 'access vlan ' + str(d['vlan'])) if 'allowed_vlans' in d: - commands.append(cmd + 'trunk allowed vlan ' + str(d['allowed_vlans'])) + if vlan_exists: + commands.append(cmd + 'trunk allowed vlan add ' + str(d['allowed_vlans'])) + else: + commands.append(cmd + 'trunk allowed vlan ' + str(d['allowed_vlans'])) if 'native_vlan' in d: commands.append(cmd + 'trunk native vlan ' + str(d['native_vlan'])) if commands: commands.insert(0, 'interface ' + d['name']) return commands - def set_commands(self, w, have): + def set_commands(self, w, have, replace=False): commands = [] + vlan_tobe_added = [] obj_in_have = flatten_dict(search_obj_in_list(w['name'], have, 'name')) if not obj_in_have: commands = self.add_commands(w) else: diff = self.diff_of_dicts(w, obj_in_have) + if diff and not replace: + if "allowed_vlans" in diff.keys() and diff["allowed_vlans"]: + vlan_tobe_added = diff["allowed_vlans"].split(',') + vlan_list = vlan_tobe_added[:] + have_vlans = obj_in_have["allowed_vlans"].split(',') + for w_vlans in vlan_list: + if w_vlans in have_vlans: + vlan_tobe_added.pop(vlan_tobe_added.index(w_vlans)) + if vlan_tobe_added: + diff.update({"allowed_vlans": ','.join(vlan_tobe_added)}) + commands = self.add_commands(diff, True) + return commands commands = self.add_commands(diff) return commands diff --git a/test/integration/targets/nxos_l2_interfaces/tests/cli/merged.yaml b/test/integration/targets/nxos_l2_interfaces/tests/cli/merged.yaml index 72aba5ebdac..3e563e59734 100644 --- a/test/integration/targets/nxos_l2_interfaces/tests/cli/merged.yaml +++ b/test/integration/targets/nxos_l2_interfaces/tests/cli/merged.yaml @@ -23,6 +23,8 @@ - name: "{{ test_int1 }}" access: vlan: 6 + trunk: + allowed_vlans: 200 state: merged register: result @@ -32,7 +34,7 @@ - "result.before|length == 0" - "'interface {{ test_int1 }}' in result.commands" - "'switchport access vlan 6' in result.commands" - - "result.commands|length == 2" + - "result.commands|length == 3" - name: Gather l2_interfaces facts nxos_facts: @@ -54,6 +56,43 @@ - "result.changed == false" - "result.commands|length == 0" + - name: Merge with existing vlans + nxos_l2_interfaces: &vlanadd + config: + - name: "{{ test_int1 }}" + trunk: + allowed_vlans: "10-12" + state: merged + register: result + + - assert: + that: + - "result.changed == true" + - "'interface {{ test_int1 }}' in result.commands" + - "'switchport trunk allowed vlan add 10,11,12' in result.commands" + - "result.commands|length == 2" + + - name: Gather l2_interfaces facts + nxos_facts: + gather_subset: + - '!all' + - '!min' + gather_network_resources: l2_interfaces + + - assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.after)|length == 0" + + - name: Idempotence - with newly added vlans + nxos_l2_interfaces: *vlanadd + register: result + + - assert: + that: + - "result.changed == false" + - "result.commands|length == 0" + + always: - name: teardown cli_config: *cleanup diff --git a/test/integration/targets/nxos_l2_interfaces/tests/cli/overridden.yaml b/test/integration/targets/nxos_l2_interfaces/tests/cli/overridden.yaml index 63f0d327e7b..af8a18ec450 100644 --- a/test/integration/targets/nxos_l2_interfaces/tests/cli/overridden.yaml +++ b/test/integration/targets/nxos_l2_interfaces/tests/cli/overridden.yaml @@ -35,6 +35,8 @@ - name: "{{ test_int2 }}" access: vlan: 6 + trunk: + allowed_vlans: "10-12" state: overridden register: result @@ -46,6 +48,7 @@ - "'no switchport trunk allowed vlan' in result.commands" - "'interface {{ test_int2 }}' in result.commands" - "'switchport access vlan 6' in result.commands" + - "'switchport trunk allowed vlan 10,11,12' in result.commands" - name: Gather l2_interfaces post facts nxos_facts: *facts diff --git a/test/integration/targets/nxos_l2_interfaces/tests/cli/replaced.yaml b/test/integration/targets/nxos_l2_interfaces/tests/cli/replaced.yaml index 0e98182a801..275ce8b6e71 100644 --- a/test/integration/targets/nxos_l2_interfaces/tests/cli/replaced.yaml +++ b/test/integration/targets/nxos_l2_interfaces/tests/cli/replaced.yaml @@ -22,6 +22,7 @@ interface {{ test_int2 }} switchport switchport trunk native vlan 15 + switchport trunk allowed vlan 25-27 - name: Gather l2_interfaces facts nxos_facts: &facts @@ -36,16 +37,18 @@ - name: "{{ test_int1 }}" access: vlan: 8 + trunk: + allowed_vlans: "10-12" state: replaced register: result - assert: that: - - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before)|length == 0" - "result.changed == true" - "'interface {{ test_int1 }}' in result.commands" - "'switchport access vlan 8' in result.commands" - - "result.commands|length == 2" + - "'switchport trunk allowed vlan 10,11,12' in result.commands" + - "result.commands|length == 3" - name: Gather l2_interfaces post facts nxos_facts: *facts