Skip to content

Commit

Permalink
refined the parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
AAYUSH2091 committed Nov 11, 2024
1 parent 99bb654 commit 17e45c0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
5 changes: 4 additions & 1 deletion plugins/module_utils/network/ios/facts/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -143,6 +145,7 @@
evpn_global=Evpn_globalFacts,
evpn_evi=Evpn_eviFacts,
vrf_global=Vrf_globalFacts,
vrf_interfaces=Vrf_interfacesFacts
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
Expand Down
26 changes: 17 additions & 9 deletions plugins/module_utils/network/ios/rm_templates/vrf_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<key_a>\S+)
$""", re.VERBOSE,
^interface\s(?P<name>\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<key_b>\S+)
$""", re.VERBOSE,
\s*vrf\sforwarding\s(?P<vrf_name>\S+)$
""",
re.VERBOSE,
),
"setval": "",
"setval": "vrf forwarding {{ vrf_name }}",
"result": {
'{{ name }}': {
'vrf_name': '{{ vrf_name }}',
},
},
},
]
Expand Down

0 comments on commit 17e45c0

Please sign in to comment.