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_address_family module #489

Merged
merged 44 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c94fbb9
Add vrf module [WIP]
Ruchip16 Jan 24, 2024
3f15ecd
adds parsed state & gathered state
Ruchip16 Feb 5, 2024
a053529
minor changes in config
Ruchip16 Feb 5, 2024
ca39096
working state - parsed & merged
Ruchip16 Feb 15, 2024
54111bc
states implementation
Ruchip16 Feb 29, 2024
59a846c
change name vrf -> vrfs throughout
Ruchip16 Mar 6, 2024
2ab3671
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 6, 2024
07161a5
Merge branch 'main' into feature-vrf
Ruchip16 Mar 6, 2024
01de125
changes
Ruchip16 Mar 10, 2024
b590dd9
push changes
Ruchip16 Mar 21, 2024
c8da71f
unit tests running :D
Ruchip16 Mar 26, 2024
7e575c5
successful integration tests & unit tests
Ruchip16 Mar 31, 2024
3354229
Merge branch 'main' of https://github.com/ansible-collections/cisco.i…
Ruchip16 Mar 31, 2024
807b4ab
changes
Ruchip16 Mar 31, 2024
8efe0e8
changes
Ruchip16 Mar 31, 2024
3b319e9
changes
Ruchip16 Mar 31, 2024
b2124c3
sanity test fix
Ruchip16 Mar 31, 2024
04be084
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 31, 2024
573f87e
review changes
Ruchip16 Apr 4, 2024
03bf25a
Merge branch 'feature-vrf' of https://github.com/ansible-collections/…
Ruchip16 Apr 4, 2024
16c08a2
WIP vrf_address_family module
Ruchip16 May 13, 2024
b8d7a44
config states
Ruchip16 May 15, 2024
b12ef32
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
158861d
updates
Ruchip16 Jun 20, 2024
6a7df43
update config
Ruchip16 Jun 20, 2024
381eb00
states working
Ruchip16 Jun 27, 2024
e83b4c7
Merge branch 'main' into feature-vrf-af
Ruchip16 Jun 27, 2024
de6f483
chore: auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2024
81f3502
redundant code rm
Ruchip16 Jun 27, 2024
3319479
Merge branch 'feature-vrf-af' of https://github.com/ansible-collectio…
Ruchip16 Jun 27, 2024
f20e9ed
update docs
Ruchip16 Jul 1, 2024
0d98edb
update test
Ruchip16 Jul 1, 2024
550a1bb
fix lint errors
Ruchip16 Jul 1, 2024
3cbc5e2
review comments
Ruchip16 Jul 1, 2024
7bde76f
sanity errors fix
Ruchip16 Jul 1, 2024
79962d7
Update plugins/modules/iosxr_vrf_address_family.py
Ruchip16 Jul 3, 2024
0d600b3
review comments
Ruchip16 Jul 3, 2024
42365ec
Merge branch 'feature-vrf-af' of https://github.com/ansible-collectio…
Ruchip16 Jul 3, 2024
67aa72d
review comments
Ruchip16 Jul 4, 2024
7ad756c
Merge branch 'main' into feature-vrf-af
Ruchip16 Jul 4, 2024
c5b4f98
add meta redirect
Ruchip16 Jul 4, 2024
7757be5
Merge branch 'feature-vrf-af' of https://github.com/ansible-collectio…
Ruchip16 Jul 4, 2024
dbbb855
review changes
Ruchip16 Jul 26, 2024
9960ba3
update
Ruchip16 Jul 26, 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 @@ -69,6 +69,7 @@ Name | Description
[cisco.iosxr.iosxr_static_routes](https://github.com/ansible-collections/cisco.iosxr/blob/main/docs/cisco.iosxr.iosxr_static_routes_module.rst)|Resource module to configure static routes.
[cisco.iosxr.iosxr_system](https://github.com/ansible-collections/cisco.iosxr/blob/main/docs/cisco.iosxr.iosxr_system_module.rst)|Module to manage the system attributes.
[cisco.iosxr.iosxr_user](https://github.com/ansible-collections/cisco.iosxr/blob/main/docs/cisco.iosxr.iosxr_user_module.rst)|Module to manage the aggregates of local users.
[cisco.iosxr.iosxr_vrf_address_family](https://github.com/ansible-collections/cisco.iosxr/blob/main/docs/cisco.iosxr.iosxr_vrf_address_family_module.rst)|Resource module to configure VRF Address family.
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
[cisco.iosxr.iosxr_vrf_global](https://github.com/ansible-collections/cisco.iosxr/blob/main/docs/cisco.iosxr.iosxr_vrf_global_module.rst)|Manages global VRF configuration.

<!--end collection content-->
Expand Down
5 changes: 5 additions & 0 deletions changelogs/fragments/add_vrfs_module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
minor_changes:
- Adds a new module `iosxr_vrfs` to manage VRFs on Cisco IOS-XR devices.
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
- The module provides the ability to manage VRFs, VRF address families, and VRF VPNs.
- https://github.com/ansible-collections/cisco.iosxr/pull/467
1 change: 1 addition & 0 deletions plugins/action/vrf_address_family.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# -*- 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 iosxr_vrf_address_family module
"""


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

argument_spec = {
"config": {
"type": "list",
"elements": "dict",
"options": {
"name": {"type": "str", "required": True},
"address_families": {
"type": "list",
"elements": "dict",
"options": {
"afi": {"type": "str", "choices": ["ipv4", "ipv6"]},
"safi": {
"type": "str",
"choices": ["flowspec", "multicast", "unicast"],
},
"export": {
"type": "dict",
"options": {
"route_policy": {"type": "str"},
"route_target": {"type": "str"},
"to": {
"type": "dict",
"options": {
"default_vrf": {
"type": "dict",
"options": {
"route_policy": {"type": "str"},
},
},
"vrf": {
"type": "dict",
"options": {
"allow_imported_vpn": {
"type": "bool",
},
},
},
},
},
},
},
"import_config": {
"type": "dict",
"options": {
"route_policy": {"type": "str"},
"route_target": {"type": "str"},
"from_config": {
"type": "dict",
"options": {
"bridge_domain": {
"type": "dict",
"options": {
"advertise_as_vpn": {
"type": "bool",
},
},
},
"default_vrf": {
"type": "dict",
"options": {
"route_policy": {"type": "str"},
},
},
"vrf": {
"type": "dict",
"options": {
"advertise_as_vpn": {
"type": "bool",
},
},
},
},
},
},
},
"maximum": {
"type": "dict",
"options": {"prefix": {"type": "int"}},
},
},
},
},
},
"running_config": {"type": "str"},
"state": {
"choices": [
"parsed",
"gathered",
"deleted",
"merged",
"replaced",
"rendered",
"overridden",
],
"default": "merged",
"type": "str",
},
} # pylint: disable=C0301
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#
# -*- 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 iosxr_vrf_address_family 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.iosxr.plugins.module_utils.network.iosxr.facts.facts import Facts
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.rm_templates.vrf_address_family import (
Vrf_address_familyTemplate,
)


class Vrf_address_family(ResourceModule):
"""
The iosxr_vrf_address_family config class
"""

def __init__(self, module):
super(Vrf_address_family, self).__init__(
empty_fact_val={},
facts_module=Facts(module),
module=module,
resource="vrf_address_family",
tmplt=Vrf_address_familyTemplate(),
)
self.parsers = [
"address_family",
"export.route_policy",
"export.route_target",
"export.to.default_vrf.route_policy",
"export.to.vrf.allow_imported_vpn",
"import_config.route_target",
"import_config.route_policy",
"import_config.from_config.bridge_domain.advertise_as_vpn",
"import_config.from_config.default_vrf.route_policy",
"import_config.from_config.vrf.advertise_as_vpn",
"maximum.prefix",
]

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.
"""
wantd = self.want
haved = self.have

wantd = self._vrf_list_to_dict(wantd)
haved = self._vrf_list_to_dict(haved)

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

# if state is deleted, empty out wantd and set haved to wantd
if self.state == "deleted":
for vrfk, vrfv in iteritems(haved):
for afk, afv in iteritems(vrfv.get("address_families", {})):
adrf = wantd.get(vrfk, {}).get("address_families", {})
if afk in adrf or not adrf:
self.addcmd(
{"name": vrfk},
"name",
False,
)
self.addcmd(
{"afi": afv.get("afi"), "safi": afv.get("safi")},
"address_family",
True,
)

if self.state in ["overridden"]:
for vrfk, vrfv in iteritems(haved):
for k, have in iteritems(vrfv.get("address_families", {})):
wantx = wantd.get(vrfk, {}).get("address_families", {})
if k not in wantx:
self.addcmd(
{"name": vrfk},
"name",
False,
)
self.addcmd(
{"afi": have.get("afi"), "safi": have.get("safi")},
"address_family",
False,
)
self.compare(parsers=self.parsers, want={}, have=have)

if self.state != "deleted":
self._compare(want=wantd, have=haved)

def _compare(self, want, have):
"""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 network resource.
"""
for name, entry in iteritems(want):
begin = len(self.commands)
vrf_want = entry
vrf_have = have.pop(name, {})
self._compare_afs(vrf_want, vrf_have)
if len(self.commands) != begin:
self.commands.insert(begin, "vrf {0}".format(name))

def _compare_afs(self, want, have):
"""Custom handling of afs option
:params want: the want VRF dictionary
:params have: the have VRF dictionary
"""
waafs = want.get("address_families", {})
haafs = have.get("address_families", {})
for afk, afv in iteritems(waafs):
begin = len(self.commands)
self._compare_single_af(want=afv, have=haafs.get(afk, {}))
if len(self.commands) != begin:
self.commands.insert(begin, f"address-family {afv.get('afi')} {afv.get('safi')}")

def _compare_single_af(self, want, have):
"""Custom handling of single af option
:params want: the want VRF dictionary
:params have: the have VRF dictionary
"""
self.compare(parsers=self.parsers[1:], want=want, have=have)

def _vrf_list_to_dict(self, entry):
"""Convert list of items to dict of items
for efficient diff calculation.
:params entry: data dictionary
"""

for vrf in entry:
if "address_families" in vrf:
vrf["address_families"] = {
f"{x['afi']}_{x.get('safi')}": x for x in vrf["address_families"]
}
entry = {x["name"]: x for x in entry}
return entry
4 changes: 4 additions & 0 deletions plugins/module_utils/network/iosxr/facts/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.facts.static_routes.static_routes import (
Static_routesFacts,
)
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.facts.vrf_address_family.vrf_address_family import (
Vrf_address_familyFacts,
)
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.facts.vrf_global.vrf_global import (
Vrf_globalFacts,
)
Expand Down Expand Up @@ -129,6 +132,7 @@
snmp_server=Snmp_serverFacts,
hostname=HostnameFacts,
bgp_templates=Bgp_templatesFacts,
vrf_address_family=Vrf_address_familyFacts,
vrf_global=Vrf_globalFacts,
)

Expand Down
Empty file.
Loading
Loading