Skip to content

Commit

Permalink
[WIP] Add spanning tree resource module
Browse files Browse the repository at this point in the history
  • Loading branch information
roverflow committed Feb 27, 2024
1 parent fdbfe11 commit 330d402
Show file tree
Hide file tree
Showing 15 changed files with 505 additions and 0 deletions.
Empty file.
Empty file added plugins/cache/__init__.py
Empty file.
Empty file added plugins/lookup/__init__.py
Empty file.
Empty file.
Empty file.
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

#############################################
# 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 nxos_spanning_tree_global module
"""


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

argument_spec = {
"config": {
"type": "dict",
"options": {
"bridge_assurance": {"type": "bool"},
"bridge_domain": {"type": "str"},
"fcoe": {"type": "bool"},
"lc_issu": {
"type": "str",
"choices": ["auto", "disruptive", "non-disruptive"],
},
"loopguard_default": {"type": "bool"},
"mode": {"type": "str", "choices": ["mst", "rapid-pvst"]},
"pathcost_method": {"type": "str", "choices": ["long", "short"]},
"port_type": {
"type": "dict",
"mutually_exclusive": [["edge", "network", "default"]],
"options": {
"edge": {
"type": "str",
"choices": ["bpdufilter", "bpduguard", "default"],
},
"network": {"type": "bool"},
"default": {"type": "bool"},
},
},
"vlan": {"type": "str"},
},
},
"running_config": {"type": "str"},
"state": {
"choices": [
"merged",
"replaced",
"overridden",
"deleted",
"rendered",
"gathered",
"purged",
"parsed",
],
"default": "merged",
"type": "str",
},
} # pylint: disable=C0301
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#
# -*- 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 nxos_spanning_tree_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 copy import deepcopy

from ansible.module_utils.six import iteritems
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import (
dict_merge,
)
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import (
ResourceModule,
)
from ansible_collections.cisco.nxos.nxos.plugins.module_utils.network.nxos.facts.facts import (
Facts,
)
from ansible_collections.cisco.nxos.nxos.plugins.module_utils.network.nxos.rm_templates.spanning_tree_global import (
Spanning_tree_globalTemplate,
)


class Spanning_tree_global(ResourceModule):
"""
The nxos_spanning_tree_global config class
"""

def __init__(self, module):
super(Spanning_tree_global, self).__init__(
empty_fact_val={},
facts_module=Facts(module),
module=module,
resource="spanning_tree_global",
tmplt=Spanning_tree_globalTemplate(),
)
self.parsers = [
]

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 = {entry['name']: entry for entry in self.want}
haved = {entry['name']: entry for entry in self.have}

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

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

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 Spanning_tree_global network resource.
"""
self.compare(parsers=self.parsers, want=want, have=have)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- 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 nxos spanning_tree_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 copy import deepcopy

from ansible.module_utils.six import iteritems
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import (
utils,
)
from ansible_collections.cisco.nxos.nxos.plugins.module_utils.network.nxos.rm_templates.spanning_tree_global import (
Spanning_tree_globalTemplate,
)
from ansible_collections.cisco.nxos.nxos.plugins.module_utils.network.nxos.argspec.spanning_tree_global.spanning_tree_global import (
Spanning_tree_globalArgs,
)

class Spanning_tree_globalFacts(object):
""" The nxos spanning_tree_global facts class
"""

def __init__(self, module, subspec='config', options='options'):
self._module = module
self.argument_spec = Spanning_tree_globalArgs.argument_spec

def populate_facts(self, connection, ansible_facts, data=None):
""" Populate the facts for Spanning_tree_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 = connection.get()

# parse native config using the Spanning_tree_global template
spanning_tree_global_parser = Spanning_tree_globalTemplate(lines=data.splitlines(), module=self._module)
objs = list(spanning_tree_global_parser.parse().values())

ansible_facts['ansible_network_resources'].pop('spanning_tree_global', None)

params = utils.remove_empties(
spanning_tree_global_parser.validate_config(self.argument_spec, {"config": objs}, redact=True)
)

facts['spanning_tree_global'] = params['config']
ansible_facts['ansible_network_resources'].update(facts)

return ansible_facts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- 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 Spanning_tree_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 Spanning_tree_globalTemplate(NetworkTemplate):
def __init__(self, lines=None, module=None):
super(Spanning_tree_globalTemplate, self).__init__(lines=lines, tmplt=self, module=module)

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

0 comments on commit 330d402

Please sign in to comment.