From 17e45c04ab455b49a59b7d115ccad2d325929b68 Mon Sep 17 00:00:00 2001 From: AAYUSH ANAND Date: Mon, 11 Nov 2024 21:57:49 +0530 Subject: [PATCH] refined the parsers --- .../argspec/vrf_interfaces/vrf_interfaces.py | 2 +- .../config/vrf_interfaces/vrf_interfaces.py | 7 ++++- .../module_utils/network/ios/facts/facts.py | 5 +++- .../facts/vrf_interfaces/vrf_interfaces.py | 15 ++--------- .../ios/rm_templates/vrf_interfaces.py | 26 ++++++++++++------- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/plugins/module_utils/network/ios/argspec/vrf_interfaces/vrf_interfaces.py b/plugins/module_utils/network/ios/argspec/vrf_interfaces/vrf_interfaces.py index e5aeb26e7..7a5a1bbe7 100644 --- a/plugins/module_utils/network/ios/argspec/vrf_interfaces/vrf_interfaces.py +++ b/plugins/module_utils/network/ios/argspec/vrf_interfaces/vrf_interfaces.py @@ -38,7 +38,7 @@ class Vrf_interfacesArgs(object): # pylint: disable=R0903 "elements": "dict", "options": { "name": {"type": "str", "required": True}, - "vrf": {"type": "str"}, + "vrf_name": {"type": "str"}, }, }, "running_config": {"type": "str"}, diff --git a/plugins/module_utils/network/ios/config/vrf_interfaces/vrf_interfaces.py b/plugins/module_utils/network/ios/config/vrf_interfaces/vrf_interfaces.py index 288c1c061..7ff5f2d62 100644 --- a/plugins/module_utils/network/ios/config/vrf_interfaces/vrf_interfaces.py +++ b/plugins/module_utils/network/ios/config/vrf_interfaces/vrf_interfaces.py @@ -47,7 +47,9 @@ def __init__(self, module): resource="vrf_interfaces", tmplt=Vrf_interfacesTemplate(), ) - self.parsers = [] + self.parsers = ["interface", + "vrf_name" + ] def execute_module(self): """Execute the module @@ -91,4 +93,7 @@ def _compare(self, want, have): the `want` and `have` data with the `parsers` defined for the Vrf_interfaces network resource. """ + begin = len(self.commands) self.compare(parsers=self.parsers, want=want, have=have) + if len(self.commands) != begin: + self.commands.insert(begin, self._tmplt.render(want or have, "interface", False)) diff --git a/plugins/module_utils/network/ios/facts/facts.py b/plugins/module_utils/network/ios/facts/facts.py index 7e7266b76..74a7a7340 100644 --- a/plugins/module_utils/network/ios/facts/facts.py +++ b/plugins/module_utils/network/ios/facts/facts.py @@ -105,7 +105,9 @@ from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vxlan_vtep.vxlan_vtep import ( Vxlan_vtepFacts, ) - +from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vrf_interfaces.vrf_interfaces import( + Vrf_interfacesFacts +) FACT_LEGACY_SUBSETS = dict( default=Default, @@ -143,6 +145,7 @@ evpn_global=Evpn_globalFacts, evpn_evi=Evpn_eviFacts, vrf_global=Vrf_globalFacts, + vrf_interfaces=Vrf_interfacesFacts ) diff --git a/plugins/module_utils/network/ios/facts/vrf_interfaces/vrf_interfaces.py b/plugins/module_utils/network/ios/facts/vrf_interfaces/vrf_interfaces.py index 771d80591..7565fa61e 100644 --- a/plugins/module_utils/network/ios/facts/vrf_interfaces/vrf_interfaces.py +++ b/plugins/module_utils/network/ios/facts/vrf_interfaces/vrf_interfaces.py @@ -62,25 +62,14 @@ def populate_facts(self, connection, ansible_facts, data=None): module=self._module, ) - objs = vrf_interfaces_parser.parse() - final_objs = [] - - for key, value in objs.items(): - # Adjust VRF data if necessary, similar to how address families were handled - if "vrf" in value: - value["vrf"] = value["vrf"] # Could be expanded if more processing is needed - - if value: - value = utils.remove_empties(value) - final_objs.append(value) - + objs = list(vrf_interfaces_parser.parse().values()) # Ensure previous facts are removed to avoid duplication ansible_facts["ansible_network_resources"].pop("vrf_interfaces", None) params = utils.remove_empties( vrf_interfaces_parser.validate_config( self.argument_spec, - {"config": final_objs}, + {"config": objs}, redact=True, ), ) diff --git a/plugins/module_utils/network/ios/rm_templates/vrf_interfaces.py b/plugins/module_utils/network/ios/rm_templates/vrf_interfaces.py index 8fe8dd4a8..be127c05b 100644 --- a/plugins/module_utils/network/ios/rm_templates/vrf_interfaces.py +++ b/plugins/module_utils/network/ios/rm_templates/vrf_interfaces.py @@ -29,26 +29,34 @@ def __init__(self, lines=None, module=None): # fmt: off PARSERS = [ { - "name": "key_a", + "name": "interface", "getval": re.compile( r""" - ^key_a\s(?P\S+) - $""", re.VERBOSE, + ^interface\s(?P\S+)$""", + re.VERBOSE, ), - "setval": "", - "result": { + "setval": "interface {{ name }}", + 'result': { + '{{ name }}': { + 'name': '{{ name }}', + }, }, "shared": True, + "shared": True, }, { - "name": "key_b", + "name": "vrf_name", "getval": re.compile( r""" - \s+key_b\s(?P\S+) - $""", re.VERBOSE, + \s*vrf\sforwarding\s(?P\S+)$ + """, + re.VERBOSE, ), - "setval": "", + "setval": "vrf forwarding {{ vrf_name }}", "result": { + '{{ name }}': { + 'vrf_name': '{{ vrf_name }}', + }, }, }, ]