Skip to content

Commit

Permalink
Fix vlans context gathered operation (#1151)
Browse files Browse the repository at this point in the history
* Fix vlans context gathered operation

Signed-off-by: rohitthakur2590 <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: rohitthakur2590 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
rohitthakur2590 and pre-commit-ci[bot] authored Jan 16, 2025
1 parent eefccd0 commit a07e958
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
6 changes: 6 additions & 0 deletions changelogs/fragments/ana_602_fix_vlan_context_gathering.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bugfixes:
- Fixed an issue with VLAN configuration gathering where pre-filled data was blocking proper fetching of dynamic VLAN details. Now VLAN facts are populated correctly for all cases.
- Cleaned up unit tests that were passing for the wrong reasons. The updated tests now ensure the right config sections are verified for VLAN configurations.
- Added a test to validate the gathered state for VLAN configuration context, improving reliability.
- Made improvements to ensure VLAN facts are gathered properly, both for specific configurations and general VLAN settings.
26 changes: 11 additions & 15 deletions plugins/module_utils/network/ios/facts/vlans/vlans.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,13 @@ def populate_facts(self, connection, ansible_facts, data=None):
remote_objs = []
final_objs = []
pvlan_objs = []
conf_data = {}

if not data:
data = self.get_vlans_data(connection)

# deals with vlan configuration config only
conf_data = self.populate_vlans_config_facts(connection, data)

# Determine if we need to collect vlan data or config data
vlan_data = data if data else self.get_vlans_data(connection)
vlan_conf_data = None
vlan_conf_data = self.populate_vlans_config_facts(connection, data)
# operate on a collection of resource x
config = data.split("\n")
# Get individual vlan configs separately
config = vlan_data.split("\n") if vlan_data else []

vlan_info = ""
temp = ""
vlan_name = True
Expand Down Expand Up @@ -190,14 +186,14 @@ def populate_facts(self, connection, ansible_facts, data=None):

facts = {}

if conf_data:
if vlan_conf_data:
for vlan in objs:
if conf_data.get(vlan.get("vlan_id")):
member_data = conf_data.pop(vlan.get("vlan_id"))
if vlan_conf_data.get(vlan.get("vlan_id")):
member_data = vlan_conf_data.pop(vlan.get("vlan_id"))
vlan.update(member_data)

if conf_data: # if any vlan configuration data is pending add it to facts
for vlanid, conf in conf_data.items():
if vlan_conf_data: # if any vlan configuration data is pending add it to facts
for vlanid, conf in vlan_conf_data.items():
objs.append(conf)

if objs:
Expand Down
25 changes: 5 additions & 20 deletions tests/unit/modules/network/ios/test_ios_vlans.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,23 +1113,8 @@ def test_ios_vlans_config_merged(self):

def test_ios_vlans_config_merged_idempotent(self):
self.mock_l2_device_command.side_effect = True
self.mock_execute_show_command_conf.side_effect = dedent(
"""\
vlan configuration 101
member evpn-instance 101 vni 10101
vlan configuration 102
member evpn-instance 102 vni 10102
vlan configuration 201
member evpn-instance 201 vni 10201
vlan configuration 202
member evpn-instance 202 vni 10202
vlan configuration 901
member vni 50901
vlan configuration 902
member vni 50902
""",
)
self.execute_show_command.return_value = dedent(
self.mock_execute_show_command_conf.side_effect = ""
self.execute_show_command_conf.return_value = dedent(
"""\
vlan configuration 101
member evpn-instance 101 vni 10101
Expand Down Expand Up @@ -1164,7 +1149,7 @@ def test_ios_vlans_config_merged_idempotent(self):
def test_ios_vlans_config_overridden(self):
self.mock_l2_device_command.side_effect = True
self.mock_execute_show_command_conf.side_effect = ""
self.execute_show_command.return_value = dedent(
self.execute_show_command_conf.return_value = dedent(
"""\
vlan configuration 101
member evpn-instance 101 vni 10101
Expand Down Expand Up @@ -1221,7 +1206,7 @@ def test_ios_vlans_config_overridden(self):
def test_ios_delete_vlans_config_2(self):
self.mock_l2_device_command.side_effect = True
self.mock_execute_show_command_conf.side_effect = ""
self.execute_show_command.return_value = dedent(
self.execute_show_command_conf.return_value = dedent(
"""\
vlan configuration 101
member evpn-instance 101 vni 10101
Expand Down Expand Up @@ -1252,7 +1237,7 @@ def test_ios_delete_vlans_config_2(self):
def test_ios_purged_vlans_config(self):
self.mock_l2_device_command.side_effect = True
self.mock_execute_show_command_conf.side_effect = ""
self.execute_show_command.return_value = dedent(
self.execute_show_command_conf.return_value = dedent(
"""\
vlan configuration 101
member evpn-instance 101 vni 10101
Expand Down

0 comments on commit a07e958

Please sign in to comment.