-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vrf_address_family
module (#489)
* 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
1 parent
4d95783
commit a7ecb28
Showing
30 changed files
with
2,917 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
iosxr.py |
Empty file.
132 changes: 132 additions & 0 deletions
132
plugins/module_utils/network/iosxr/argspec/vrf_address_family/vrf_address_family.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
169 changes: 169 additions & 0 deletions
169
plugins/module_utils/network/iosxr/config/vrf_address_family/vrf_address_family.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.