Skip to content

Commit

Permalink
Add eos_vrf_global resource module (#560)
Browse files Browse the repository at this point in the history
* Add eos_vrf_global resource module

* add action file

* changes

* rectify changes

* add redirect

* config side code

* add unit and integration tests and docs with examples

* add changelog

* sanity failures fix

* ci fix

* update

* updates

* review changes

* review comment change

* add privilege escalation

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

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

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Ruchip16 and pre-commit-ci[bot] authored Nov 22, 2024
1 parent e077133 commit 93c965a
Show file tree
Hide file tree
Showing 33 changed files with 1,629 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ venv.bak/

# mypy
.mypy_cache/
.vscode
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Name | Description
[arista.eos.eos_user](https://github.com/ansible-collections/arista.eos/blob/main/docs/arista.eos.eos_user_module.rst)|Manage the collection of local users on EOS devices
[arista.eos.eos_vlans](https://github.com/ansible-collections/arista.eos/blob/main/docs/arista.eos.eos_vlans_module.rst)|VLANs resource module
[arista.eos.eos_vrf](https://github.com/ansible-collections/arista.eos/blob/main/docs/arista.eos.eos_vrf_module.rst)|Manage VRFs on Arista EOS network devices
[arista.eos.eos_vrf_global](https://github.com/ansible-collections/arista.eos/blob/main/docs/arista.eos.eos_vrf_global_module.rst)|Resource module to configure VRF definitions.

<!--end collection content-->

Expand Down
3 changes: 3 additions & 0 deletions changelogs/fragments/add_vrf_global.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Adds a new module `eos_vrf_global` in favor of `eos_vrf` legacy module to manage VRF global configurations on Arista EOS devices.
2 changes: 2 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ plugin_routing:
redirect: arista.eos.eos_vlans
vrf:
redirect: arista.eos.eos_vrf
vrf_global:
redirect: arista.eos.eos_vrf_global
1 change: 1 addition & 0 deletions plugins/action/vrf_global.py
Empty file.
60 changes: 60 additions & 0 deletions plugins/module_utils/network/eos/argspec/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- 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 eos_vrf_global module
"""


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

argument_spec = {
"config": {
"type": "list",
"elements": "dict",
"options": {
"name": {"type": "str", "required": True},
"description": {"type": "str"},
"rd": {"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.
118 changes: 118 additions & 0 deletions plugins/module_utils/network/eos/config/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#
# -*- 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 eos_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.arista.eos.plugins.module_utils.network.eos.facts.facts import Facts
from ansible_collections.arista.eos.plugins.module_utils.network.eos.rm_templates.vrf_global import (
Vrf_globalTemplate,
)


class Vrf_global(ResourceModule):
"""
The eos_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",
"rd",
]

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 onto 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":
haved = {k: v for k, v in iteritems(haved) if k in wantd or not wantd}
wantd = {}

# remove superfluous config for overridden and deleted
if self.state in ["overridden", "deleted"]:
for k, have in iteritems(haved):
if k not in wantd:
self._compare(want={}, have=have, vrf=k)

if self.state == "purged":
for k, have in iteritems(haved):
self.purge(have)

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

def _compare(self, want, have, vrf):
"""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.
"""
begin = len(self.commands)
self.compare(self.parsers, want=want, have=have)
if len(self.commands) != begin:
self.commands.insert(begin, self._tmplt.render({"name": vrf}, "name", False))

def _vrf_list_to_dict(self, entry):
"""Convert list of items to dict of items
for efficient diff calculation.
:params entry: data dictionary
"""
entry = {x["name"]: x for x in entry}
return entry

def purge(self, have):
"""Purge the VRF configuration"""
self.commands.append("no vrf instance {0}".format(have["name"]))
4 changes: 4 additions & 0 deletions plugins/module_utils/network/eos/facts/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
from ansible_collections.arista.eos.plugins.module_utils.network.eos.facts.vlans.vlans import (
VlansFacts,
)
from ansible_collections.arista.eos.plugins.module_utils.network.eos.facts.vrf_global.vrf_global import (
Vrf_globalFacts,
)


FACT_LEGACY_SUBSETS = dict(
Expand Down Expand Up @@ -123,6 +126,7 @@
ntp_global=Ntp_globalFacts,
snmp_server=Snmp_serverFacts,
hostname=HostnameFacts,
vrf_global=Vrf_globalFacts,
)


Expand Down
Empty file.
69 changes: 69 additions & 0 deletions plugins/module_utils/network/eos/facts/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- 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 eos 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.arista.eos.plugins.module_utils.network.eos.argspec.vrf_global.vrf_global import (
Vrf_globalArgs,
)
from ansible_collections.arista.eos.plugins.module_utils.network.eos.rm_templates.vrf_global import (
Vrf_globalTemplate,
)


class Vrf_globalFacts(object):
"""The eos 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)

# 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())

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", [])
ansible_facts["ansible_network_resources"].update(facts)

return ansible_facts
79 changes: 79 additions & 0 deletions plugins/module_utils/network/eos/rm_templates/vrf_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- 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 Vrf_global parser templates file. This contains
a list of parser definitions and associated functions that
facilitates both facts gathering and native command generation for
the given network resource.
"""

import re

from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.network_template import (
NetworkTemplate,
)


class Vrf_globalTemplate(NetworkTemplate):
def __init__(self, lines=None, module=None):
super(Vrf_globalTemplate, self).__init__(lines=lines, tmplt=self, module=module)

# fmt: off
PARSERS = [
{
"name": "name",
"getval": re.compile(
r"""
^vrf\sinstance
(\s(?P<name>\S+))?
$""", re.VERBOSE,
),
"setval": "vrf instance {{ name }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
},
},
"shared": True,
},
{
"name": "description",
"getval": re.compile(
r"""
\s+description\s(?P<description>.+$)
$""", re.VERBOSE,
),
"setval": "description {{ description }}",
"result": {
'{{ name }}': {
'name': '{{ name }}',
'description': '{{ description }}',
},
},
},
{
"name": "rd",
"getval": re.compile(
r"""
\s+rd\s(?P<rd>\S+)
$""", re.VERBOSE,
),
"setval": "rd {{ rd }}",
"compval": "rd",
"result": {
'{{ name }}': {
'name': '{{ name }}',
"rd": "{{ rd }}",
},
},
},
]
# fmt: on
Loading

0 comments on commit 93c965a

Please sign in to comment.