Skip to content

Commit

Permalink
Add vrf_address_family module (#489)
Browse files Browse the repository at this point in the history
* Add vrf module [WIP]

* adds parsed state & gathered state

* minor changes in config

* working state - parsed & merged

* states implementation

* change name vrf -> vrfs throughout

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* changes

* push changes

* unit tests running :D

* successful integration tests & unit tests

* changes

* changes

* changes

* sanity test fix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* review changes

* WIP vrf_address_family module

* config states

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* updates

* update config

* states working

* chore: auto fixes from pre-commit.com hooks

* redundant code rm

* update docs

* update test

* fix lint errors

* review comments

* sanity errors fix

* Update plugins/modules/iosxr_vrf_address_family.py

Co-authored-by: Rohit Thakur <[email protected]>

* review comments

* review comments

* add meta redirect

* review changes

* update

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Rohit Thakur <[email protected]>
  • Loading branch information
3 people authored Jul 26, 2024
1 parent 4d95783 commit a7ecb28
Show file tree
Hide file tree
Showing 30 changed files with 2,917 additions and 0 deletions.
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.
[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
3 changes: 3 additions & 0 deletions changelogs/fragments/add_vrfs_module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Adds a new module `iosxr_vrf_address_family` to manage VRFs address families on Cisco IOS-XR devices (https://github.com/ansible-collections/cisco.iosxr/pull/489).
2 changes: 2 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ plugin_routing:
redirect: cisco.iosxr.iosxr_hostname
vrf_global:
redirect: cisco.iosxr.iosxr_vrf_global
vrf_address_family:
redirect: cisco.iosxr.iosxr_vrf_address_family
1 change: 1 addition & 0 deletions plugins/action/vrf_address_family.py
Empty file.
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

0 comments on commit a7ecb28

Please sign in to comment.