Skip to content

Commit

Permalink
Add function/class wrapper for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
juztas committed Jul 12, 2024
1 parent 19b1927 commit 682abed
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion plugins/action/cisconx9.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible_collections.sense.cisconx9.plugins.module_utils.network.cisconx9 import cisconx9_provider_spec
from ansible_collections.sense.cisconx9.plugins.module_utils.runwrapper import classwrapper

display = Display()

display = Display()

@classwrapper
class ActionModule(ActionNetworkModule):
""" Ansible Action Module"""

Expand Down
2 changes: 2 additions & 0 deletions plugins/cliconf/cisconx9.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from ansible.module_utils._text import to_text
from ansible.plugins.cliconf import CliconfBase, enable_mode
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list
from ansible_collections.sense.cisconx9.plugins.module_utils.runwrapper import classwrapper

@classwrapper
class Cliconf(CliconfBase):

def get_device_info(self):
Expand Down
12 changes: 8 additions & 4 deletions plugins/module_utils/network/cisconx9.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ansible.module_utils.connection import exec_command
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list, ComplexList
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import NetworkConfig, ConfigLine
from ansible_collections.sense.cisconx9.plugins.module_utils.runwrapper import functionwrapper

_DEVICE_CONFIGS = {}

Expand All @@ -31,17 +32,20 @@
'provider': {'type': 'dict', 'options': cisconx9_provider_spec}
}

@functionwrapper
def to_json(out):
"""Check and change output to dict if possible"""
try:
return json.loads(out)
except ValueError:
return out

@functionwrapper
def check_args(module, warnings):
"""Check args pass"""
pass

@functionwrapper
def get_config(module, flags=None):
"""Get running config"""
flags = [] if flags is None else flags
Expand All @@ -59,7 +63,7 @@ def get_config(module, flags=None):
_DEVICE_CONFIGS[cmd] = cfg
return cfg


@functionwrapper
def to_commands(module, commands):
"""Transform commands"""
spec = {
Expand All @@ -70,7 +74,7 @@ def to_commands(module, commands):
transform = ComplexList(spec, module)
return transform(commands)


@functionwrapper
def run_commands(module, commands, check_rc=True):
"""Run Commands"""
responses = []
Expand All @@ -83,7 +87,7 @@ def run_commands(module, commands, check_rc=True):
responses.append(to_json(to_text(out, errors='surrogate_or_strict')))
return responses


@functionwrapper
def load_config(module, commands):
"""Load config"""
ret, _out, err = exec_command(module, 'configure terminal')
Expand All @@ -99,7 +103,7 @@ def load_config(module, commands):

exec_command(module, 'end')


@functionwrapper
def get_sublevel_config(running_config, module):
"""Get sublevel config"""
contents = []
Expand Down
6 changes: 4 additions & 2 deletions plugins/modules/cisconx9_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
from ansible_collections.sense.cisconx9.plugins.module_utils.network.cisconx9 import cisconx9_argument_spec, check_args
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ComplexList
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.parsing import Conditional
from ansible_collections.sense.cisconx9.plugins.module_utils.runwrapper import functionwrapper

display = Display()

@functionwrapper
def toLines(stdout):
for item in stdout:
if isinstance(item, string_types):
item = str(item).split('\n')
yield item


@functionwrapper
def parse_commands(module, warnings):
command = ComplexList({'command': {'key':True}, 'prompt': {}, 'answer':{}}, module)
commands = command(module.params['commands'])
Expand All @@ -32,7 +34,7 @@ def parse_commands(module, warnings):
module.fail_json(msg='cisconx9_command does not support running config mode commands. Please use cisconx9_config instead')
return commands


@functionwrapper
def main():
"""main entry point for module execution
"""
Expand Down
6 changes: 4 additions & 2 deletions plugins/modules/cisconx9_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
from ansible_collections.sense.cisconx9.plugins.module_utils.network.cisconx9 import cisconx9_argument_spec, check_args
from ansible_collections.sense.cisconx9.plugins.module_utils.network.cisconx9 import load_config, run_commands
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import NetworkConfig, dumps

from ansible_collections.sense.cisconx9.plugins.module_utils.runwrapper import functionwrapper

display = Display()


@functionwrapper
def get_candidate(module):
candidate = NetworkConfig(indent=1)
if module.params['src']:
Expand All @@ -41,13 +41,15 @@ def get_candidate(module):
return candidate


@functionwrapper
def get_running_config(module):
contents = module.params['config']
if not contents:
contents = get_config(module)
return contents


@functionwrapper
def main():
backup_spec = dict(
filename=dict(),
Expand Down
9 changes: 7 additions & 2 deletions plugins/modules/cisconx9_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from ansible.utils.display import Display
from ansible_collections.sense.cisconx9.plugins.module_utils.network.cisconx9 import (
check_args, cisconx9_argument_spec, run_commands)
from ansible_collections.sense.cisconx9.plugins.module_utils.runwrapper import classwrapper, functionwrapper

display = Display()


@classwrapper
class FactsBase:
"""Base class for Facts"""

Expand All @@ -32,7 +34,7 @@ def run(self, cmd):
"""Run commands"""
return run_commands(self.module, cmd, check_rc=False)


@classwrapper
class Default(FactsBase):
"""Default Class to get basic info"""

Expand All @@ -52,6 +54,7 @@ def populate(self):
self.facts[outkey] = data[key]


@classwrapper
class Config(FactsBase):
"""Default Class to get basic info"""

Expand All @@ -64,6 +67,7 @@ def populate(self):
self.facts["config"] = self.responses[0]


@classwrapper
class Interfaces(FactsBase):
"""All Interfaces Class"""

Expand Down Expand Up @@ -204,6 +208,7 @@ def populate(self):
self.populate_lldp()


@classwrapper
class Routing(FactsBase):
"""Routing Information Class"""

Expand Down Expand Up @@ -257,7 +262,7 @@ def populate(self):

VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())


@functionwrapper
def main():
"""main entry point for module execution"""
argument_spec = {"gather_subset": {"default": ["!config"], "type": "list"}}
Expand Down
3 changes: 2 additions & 1 deletion plugins/terminal/cisconx9.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from ansible.module_utils._text import to_text, to_bytes
from ansible.plugins.terminal import TerminalBase
from ansible.errors import AnsibleConnectionFailure
from ansible_collections.sense.cisconx9.plugins.module_utils.runwrapper import classwrapper


@classwrapper
class TerminalModule(TerminalBase):

terminal_stdout_re = [
Expand Down

0 comments on commit 682abed

Please sign in to comment.