Skip to content

Commit

Permalink
updated argspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruchip16 committed May 2, 2024
1 parent d178d83 commit 46cd6be
Show file tree
Hide file tree
Showing 6 changed files with 566 additions and 144 deletions.
69 changes: 40 additions & 29 deletions plugins/module_utils/network/ios/argspec/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,51 @@ class Vrf_globalArgs(object): # pylint: disable=R0903

argument_spec = {
"config": {
"type": "list",
"elements": "dict",
"type": "dict",
"options": {
"name": {"type": "str", "required": True},
"description": {"type": "str"},
"ipv4": {
"type": "dict",
"vrfs": {
"type": "list",
"elements": "dict",
"options": {
"multicast": {
"name": {"type": "str", "required": True},
"description": {"type": "str"},
"ipv4": {
"type": "dict",
"options": {"multitopology": {"type": "bool"}},
}
},
},
"ipv6": {
"type": "dict",
"options": {
"multicast": {
"options": {
"multicast": {
"type": "dict",
"options": {"multitopology": {"type": "bool"}},
}
},
},
"ipv6": {
"type": "dict",
"options": {"multitopology": {"type": "bool"}},
}
},
},
"rd": {"type": "str"},
"route_target": {
"type": "dict",
"options": {
"export": {"type": "str"},
"import_config": {"type": "str"},
"both": {"type": "str"},
"options": {
"multicast": {
"type": "dict",
"options": {"multitopology": {"type": "bool"}},
}
},
},
"rd": {"type": "str"},
"route_target": {
"type": "dict",
"options": {
"export": {"type": "str"},
"import_config": {"type": "str"},
"both": {"type": "str"},
},
},
"vnet": {
"type": "dict",
"options": {"tag": {"type": "int"}},
},
"vpn": {
"type": "dict",
"options": {"id": {"type": "str"}},
},
},
},
"vnet": {"type": "dict", "options": {"tag": {"type": "int"}}},
"vpn": {"type": "dict", "options": {"id": {"type": "str"}}},
}
},
},
"running_config": {"type": "str"},
Expand Down
24 changes: 13 additions & 11 deletions plugins/module_utils/network/ios/config/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
necessary to bring the current configuration to its desired end-state is
created.
"""

from copy import deepcopy

import q
from ansible.module_utils.six import iteritems
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import (
ResourceModule,
Expand Down Expand Up @@ -75,21 +73,24 @@ def generate_commands(self):
"""Generate configuration commands to send based on
want, have and desired state.
"""

wantd = {entry["name"]: entry for entry in self.want}
q(wantd)
haved = {entry["name"]: entry for entry in self.have}

# if state is merged, merge want onto have and then compare
q(haved)
# if state is merged, merge want onto have
if self.state == "merged":
wantd = dict_merge(haved, wantd)

# if state is deleted, empty out wantd and set haved to wantd
# if state is deleted, limit the have to anything in want & set want to nothing
if self.state == "deleted":
haved = {k: v for k, v in iteritems(haved) if k in wantd or not wantd}
wantd = {}

if self.state == "purged":
haved = {k: v for k, v in iteritems(haved) if k in wantd or not wantd}
wantd = {}
# if self.state in ["overridden", "deleted"]:
# for k, have in iteritems(haved):
# if k not in wantd:
# self.addcmd(have, "name", True)

self._compare(want=wantd, have=haved)

Expand All @@ -99,6 +100,7 @@ def _compare(self, want, have):
the `want` and `have` data with the `parsers` defined
for the Vrf_global network resource.
"""

self._compare_vrf(want=want, have=have)

def _compare_vrf(self, want, have):
Expand All @@ -113,8 +115,8 @@ def _compare_vrf(self, want, have):

self.compare(parsers=self.parsers, want=entry, have=vrf_have)

# for deleted and overridden state
if self.state != "replaced":
# for purged state
if self.state == "purged":
begin = len(self.commands)
for name, entry in iteritems(have):
self.commands.insert(begin, "no vrf definition {0}".format(name))
Expand Down
34 changes: 26 additions & 8 deletions plugins/module_utils/network/ios/facts/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def get_config(self, connection):

return connection.get("show running-config vrf")

def dict_to_list(self, vrf_data):
"""Convert a dictionary to a list of dictionaries"""

facts_output = {"vrfs": []}

for vrf_entry in vrf_data.get("vrfs", []):
if "vrfs" in vrf_entry:
vrf_entry["vrfs"] = list(vrf_entry["vrfs"].values())
facts_output["vrfs"].append(vrf_entry)

return facts_output

def populate_facts(self, connection, ansible_facts, data=None):
""" Populate the facts for Vrf_global network resource
Expand All @@ -48,23 +60,29 @@ def populate_facts(self, connection, ansible_facts, data=None):
:rtype: dictionary
:returns: facts
"""
# import epdb; epdb.serve()
facts = {}
objs = []

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

# parse native config using the Vrf_global template
vrf_global_parser = Vrf_globalTemplate(lines=data.splitlines(), module=self._module)
objs = list(vrf_global_parser.parse().values())
objs = vrf_global_parser.parse()

ansible_facts['ansible_network_resources'].pop('vrf_global', None)

params = utils.remove_empties(
vrf_global_parser.validate_config(self.argument_spec, {"config": objs}, redact=True)
# Convert the dictionary to a list of dictionaries
objs["vrfs"] = (
objs["vrfs"].values() if "vrfs" in objs else []
)

facts['vrf_global'] = params.get("config", {})
ansible_facts['ansible_network_resources'].update(facts)
facts_output = self.dict_to_list(objs)

ansible_facts['ansible_network_resources'].pop('vrf_global', None)

if objs["vrfs"]:
params = vrf_global_parser.validate_config(self.argument_spec, {"config": facts_output}, redact=True)
params = utils.remove_empties(params)
facts['vrf_global'] = params["config"]
ansible_facts['ansible_network_resources'].update(facts)

return ansible_facts
96 changes: 58 additions & 38 deletions plugins/module_utils/network/ios/rm_templates/vrf_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ def __init__(self, lines=None, module=None):
),
"setval": "vrf definition {{ name }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
},
},
},
"shared": True,
Expand All @@ -55,9 +57,11 @@ def __init__(self, lines=None, module=None):
),
"setval": "description {{ description }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
'description': '{{ description }}',
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
'description': '{{ description }}',
},
},
},
},
Expand All @@ -70,11 +74,13 @@ def __init__(self, lines=None, module=None):
),
"setval": "ipv4 multicast multitopology",
"result": {
'{{ name }}': {
'name': '{{ name }}',
'ipv4': {
'multicast': {
'multitopology': "{{ true if multitopology is defined }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
'ipv4': {
'multicast': {
'multitopology': "{{ true if multitopology is defined }}",
},
},
},
},
Expand All @@ -89,11 +95,13 @@ def __init__(self, lines=None, module=None):
),
"setval": "ipv6 multicast multitopology",
"result": {
'{{ name }}': {
'name': '{{ name }}',
'ipv6': {
'multicast': {
'multitopology': "{{ true if multitopology is defined }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
'ipv6': {
'multicast': {
'multitopology': "{{ true if multitopology is defined }}",
},
},
},
},
Expand All @@ -108,9 +116,11 @@ def __init__(self, lines=None, module=None):
),
"setval": "rd {{ rd }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"rd": "{{ rd }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
"rd": "{{ rd }}",
},
},
},
},
Expand All @@ -123,10 +133,12 @@ def __init__(self, lines=None, module=None):
),
"setval": "route-target export {{ route_target.export }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"route_target": {
"export": "{{ route_target_export }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
"route_target": {
"export": "{{ route_target_export }}",
},
},
},
},
Expand All @@ -140,10 +152,12 @@ def __init__(self, lines=None, module=None):
),
"setval": "route-target import {{ route_target.import_config}}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"route_target": {
"import_config": "{{ route_target_import_config }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
"route_target": {
"import_config": "{{ route_target_import_config }}",
},
},
},
},
Expand All @@ -157,10 +171,12 @@ def __init__(self, lines=None, module=None):
),
"setval": "route-target both {{ route_target.both }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"route_target": {
"both": "{{ route_target_both }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
"route_target": {
"both": "{{ route_target_both }}",
},
},
},
},
Expand All @@ -174,10 +190,12 @@ def __init__(self, lines=None, module=None):
),
"setval": "vnet tag {{ vnet.tag }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"vnet": {
"tag": "{{ vnet_tag }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
"vnet": {
"tag": "{{ vnet_tag }}",
},
},
},
},
Expand All @@ -191,10 +209,12 @@ def __init__(self, lines=None, module=None):
),
"setval": "vpn id {{ vpn.id }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"vpn": {
"id": "{{ vpn_id }}",
"vrfs": {
'{{ name }}': {
'name': '{{ name }}',
"vpn": {
"id": "{{ vpn_id }}",
},
},
},
},
Expand Down
Loading

0 comments on commit 46cd6be

Please sign in to comment.