Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vrf_global resource module #1055

Merged
merged 28 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
903f5fc
Add vrf_global resource module - parsed state & gathered state working
Ruchip16 Apr 16, 2024
8fdd104
separate ios_vrf_af from vrf_global
Ruchip16 Apr 19, 2024
2445819
remove unwanted files
Ruchip16 Apr 19, 2024
f18dfdc
merged state working
Ruchip16 Apr 20, 2024
d178d83
add states & integration tests
Ruchip16 Apr 22, 2024
46cd6be
updated argspec
Ruchip16 May 2, 2024
238b6d0
updated code
Ruchip16 May 6, 2024
77bf263
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2024
066bca1
Update vrf_global.py
Ruchip16 May 6, 2024
1bb2758
complete work
Ruchip16 May 8, 2024
84c8746
some review comments
Ruchip16 May 8, 2024
d7f46f9
some review comments
Ruchip16 May 8, 2024
9be19a3
CI test failures
Ruchip16 May 8, 2024
6eccd0e
remove unused import
Ruchip16 May 8, 2024
01329ab
review changes
Ruchip16 May 16, 2024
9439a6c
use config module to reset the config
Ruchip16 May 17, 2024
2bbdc1f
some changes
Ruchip16 May 17, 2024
1d8addc
fix some typos
Ruchip16 May 22, 2024
8c869ec
review changes
Ruchip16 May 30, 2024
34e524b
pre-commit run
Ruchip16 May 30, 2024
4e22531
changes
Ruchip16 May 31, 2024
67bad21
add review changes
Ruchip16 Jun 3, 2024
904d701
doc changes
Ruchip16 Jun 4, 2024
5a85c35
some updates related to doc
Ruchip16 Jun 4, 2024
a88690c
Merge branch 'main' into feature_vrf
Ruchip16 Jun 4, 2024
63c3090
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 4, 2024
37a4e45
Merge branch 'main' into feature_vrf
roverflow Jun 6, 2024
828ce7c
Merge branch 'main' into feature_vrf
roverflow Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Name | Description
[cisco.ios.ios_user](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_user_module.rst)|Module to manage the aggregates of local users.
[cisco.ios.ios_vlans](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vlans_module.rst)|Resource module to configure VLANs.
[cisco.ios.ios_vrf](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vrf_module.rst)|Module to configure VRF definitions.
[cisco.ios.ios_vrf_global](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vrf_global_module.rst)|Resource module to configure global VRF definitions.
[cisco.ios.ios_vxlan_vtep](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vxlan_vtep_module.rst)|Resource module to configure VXLAN VTEP interface.

<!--end collection content-->
Expand Down
3 changes: 3 additions & 0 deletions changelogs/fragments/1055_add_vrf_global_module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
- Add ios_vrf_global resource module in favor of ios_vrf module (fixes - https://github.com/ansible-collections/cisco.ios/pull/1055)
1,253 changes: 1,253 additions & 0 deletions docs/cisco.ios.ios_vrf_global_module.rst

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ plugin_routing:
redirect: cisco.ios.ios_vrf
vxlan_vtep:
redirect: cisco.ios.ios_vxlan_vtep
vrf_global:
redirect: cisco.ios.ios_vrf_global
requires_ansible: ">=2.15.0"
1 change: 1 addition & 0 deletions plugins/action/vrf_global.py
Empty file.
99 changes: 99 additions & 0 deletions plugins/module_utils/network/ios/argspec/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
# Copyright 2024 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function


__metaclass__ = type

#############################################
# WARNING #
#############################################
#
# This file is auto generated by the
# ansible.content_builder.
#
# Manually editing this file is not advised.
#
# To update the argspec make the desired changes
# in the documentation in the module file and re-run
# ansible.content_builder commenting out
# the path to external 'docstring' in build.yaml.
#
##############################################

"""
The arg spec for the ios_vrf_global module
"""


class Vrf_globalArgs(object): # pylint: disable=R0903
"""The arg spec for the ios_vrf_global module"""

argument_spec = {
"config": {
"type": "dict",
"options": {
"vrfs": {
"type": "list",
"elements": "dict",
"options": {
"name": {"type": "str", "required": True},
"description": {"type": "str"},
"ipv4": {
"type": "dict",
"options": {
"multicast": {
"type": "dict",
"options": {"multitopology": {"type": "bool"}},
},
},
},
"ipv6": {
"type": "dict",
"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"}},
},
},
},
},
},
"running_config": {"type": "str"},
"state": {
"choices": [
"parsed",
"gathered",
"deleted",
"merged",
"replaced",
"rendered",
"overridden",
"purged",
],
"default": "merged",
"type": "str",
},
} # pylint: disable=C0301
Empty file.
117 changes: 117 additions & 0 deletions plugins/module_utils/network/ios/config/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# -*- coding: utf-8 -*-
# Copyright 2024 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#

from __future__ import absolute_import, division, print_function


__metaclass__ = type

"""
The ios_vrf_global config file.
It is in this file where the current configuration (as dict)
is compared to the provided configuration (as dict) and the command set
necessary to bring the current configuration to its desired end-state is
created.
"""

from ansible.module_utils.six import iteritems
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import (
ResourceModule,
)
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import (
dict_merge,
)

from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.facts import Facts
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.vrf_global import (
Vrf_globalTemplate,
)


class Vrf_global(ResourceModule):
"""
The ios_vrf_global config class
"""

def __init__(self, module):
super(Vrf_global, self).__init__(
empty_fact_val={},
facts_module=Facts(module),
module=module,
resource="vrf_global",
tmplt=Vrf_globalTemplate(),
)
self.parsers = [
"description",
"ipv4.multicast.multitopology",
"ipv6.multicast.multitopology",
"rd",
"route_target.export",
"route_target.import_config",
"route_target.both",
"vnet.tag",
"vpn.id",
]

def execute_module(self):
"""Execute the module

:rtype: A dictionary
:returns: The result from module execution
"""
if self.state not in ["parsed", "gathered"]:
self.generate_commands()
self.run_commands()
return self.result

def generate_commands(self):
"""Generate configuration commands to send based on
want, have and desired state.
"""
haved, wantd = dict(), dict()

if self.want:
for entry in self.want.get("vrfs", []):
wantd.update({(entry["name"]): entry})

if self.have:
for entry in self.have.get("vrfs", []):
haved.update({(entry["name"]): entry})

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

# if state is deleted, limit the have to anything in want & set want to nothing
if self.state == "deleted":
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
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._compare(want={}, have=have)

if self.state == "purged":
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
for k, have in iteritems(haved):
self.purge(have)

for k, want in iteritems(wantd):
self._compare(want=want, have=haved.pop(k, {}))

def _compare(self, want, have):
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
"""Leverages the base class `compare()` method and
populates the list of commands to be run by comparing
the `want` and `have` data with the `parsers` defined
for the Vrf_global network resource.
"""
if want != have:
self.addcmd(want or have, "name", False)
self.compare(self.parsers, want, have)

def purge(self, have):
"""Purge the VRF configuration"""
self.commands.append("no vrf definition {0}".format(have["name"]))
4 changes: 4 additions & 0 deletions plugins/module_utils/network/ios/facts/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vlans.vlans import (
VlansFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vrf_global.vrf_global import (
Vrf_globalFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vxlan_vtep.vxlan_vtep import (
Vxlan_vtepFacts,
)
Expand Down Expand Up @@ -139,6 +142,7 @@
vxlan_vtep=Vxlan_vtepFacts,
evpn_global=Evpn_globalFacts,
evpn_evi=Evpn_eviFacts,
vrf_global=Vrf_globalFacts,
)


Expand Down
Empty file.
72 changes: 72 additions & 0 deletions plugins/module_utils/network/ios/facts/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
# Copyright 2024 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function


__metaclass__ = type

"""
The ios vrf_global fact class
It is in this file the configuration is collected from the device
for a given resource, parsed, and the facts tree is populated
based on the configuration.
"""


from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils

from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.vrf_global.vrf_global import (
Vrf_globalArgs,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.vrf_global import (
Vrf_globalTemplate,
)


class Vrf_globalFacts(object):
"""The ios vrf_global facts class"""

def __init__(self, module, subspec="config", options="options"):
self._module = module
self.argument_spec = Vrf_globalArgs.argument_spec

def get_config(self, connection):
"""Get the configuration from the device"""

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

def populate_facts(self, connection, ansible_facts, data=None):
"""Populate the facts for Vrf_global network resource

:param connection: the device connection
:param ansible_facts: Facts dictionary
:param data: previously collected conf

:rtype: dictionary
:returns: facts
"""

facts = {}
objs = []

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

vrf_global_parser = Vrf_globalTemplate(lines=data.splitlines(), module=self._module)
objs = vrf_global_parser.parse()

# Convert the dictionary to a list of dictionaries
objs["vrfs"] = list(objs["vrfs"].values()) if "vrfs" in objs else []

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),
)

facts["vrf_global"] = params.get("config", {})
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
ansible_facts["ansible_network_resources"].update(facts)

return ansible_facts
Loading
Loading