Skip to content

Commit

Permalink
Delegate more functionality to CliconfBase
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Jul 24, 2023
1 parent 36306dc commit 8873ef7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 61 deletions.
64 changes: 9 additions & 55 deletions plugins/cliconf/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@

import json

from ansible.errors import AnsibleConnectionFailure
from ansible.module_utils.common._collections_compat import Mapping
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list
from ansible_collections.ansible.netcommon.plugins.plugin_utils.cliconf_base import CliconfBase


class Cliconf(CliconfBase):
__rpc__ = CliconfBase.__rpc__ + [
"run_commands",
]

def __init__(self, *args, **kwargs):
super(Cliconf, self).__init__(*args, **kwargs)
self._device_info = {}
Expand All @@ -41,52 +34,19 @@ def get_device_info(self):
return self._device_info

def get_config(self, flags=None, format=None):
raise NotImplementedError
return self._connection.method_not_found(
"commit is not supported by network_os %s" % self._play_context.network_os
)

def edit_config(self, candidate=None, commit=True, replace=None, comment=None):
raise NotImplementedError

def get(
self,
command=None,
prompt=None,
answer=None,
sendonly=False,
newline=True,
output=None,
check_all=False,
):
if not command:
raise ValueError("must provide value of command to execute")

return self.send_command(
command=command,
prompt=prompt,
answer=answer,
sendonly=sendonly,
newline=newline,
check_all=check_all,
return self._connection.method_not_found(
"commit is not supported by network_os %s" % self._play_context.network_os
)

def run_commands(self, commands=None, check_rc=True):
if commands is None:
raise ValueError("'commands' value is required")

responses = list()
for cmd in to_list(commands):
if not isinstance(cmd, Mapping):
cmd = {"command": cmd}

try:
out = self.send_command(**cmd)
except AnsibleConnectionFailure as e:
if check_rc:
raise
out = getattr(e, "err", e)

responses.append(out)

return responses
def get_capabilities(self):
result = super(Cliconf, self).get_capabilities()
result["device_operations"] = self.get_device_operations()
return json.dumps(result)

def get_device_operations(self):
return {
Expand All @@ -102,9 +62,3 @@ def get_device_operations(self):
"supports_generate_diff": False,
"supports_replace": False,
}

def get_capabilities(self):
result = super(Cliconf, self).get_capabilities()
result["device_operations"] = self.get_device_operations()
result.update(self.get_option_values())
return json.dumps(result)
41 changes: 35 additions & 6 deletions plugins/plugin_utils/cliconf_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

from ansible.errors import AnsibleConnectionFailure, AnsibleError
from ansible.module_utils._text import to_bytes, to_text
from ansible.module_utils.common._collections_compat import Mapping

# Needed to satisfy PluginLoader's required_base_class
from ansible.plugins.cliconf import CliconfBase as CliconfBaseBase
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list


try:
Expand Down Expand Up @@ -70,12 +72,13 @@ class CliconfBase(CliconfBaseBase):
"""

__rpc__ = [
"get_config",
"edit_config",
"get_capabilities",
"get",
"enable_response_logging",
"get",
"get_capabilities",
"get_config",
"disable_response_logging",
"run_commands",
]

def __init__(self, connection):
Expand Down Expand Up @@ -241,7 +244,6 @@ def edit_config(
"""
pass

@abstractmethod
def get(
self,
command=None,
Expand All @@ -268,7 +270,17 @@ def get(
given prompt.
:return: The output from the device after executing the command
"""
pass
if not command:
raise ValueError("must provide value of command to execute")

return self.send_command(
command=command,
prompt=prompt,
answer=answer,
sendonly=sendonly,
newline=newline,
check_all=check_all,
)

@abstractmethod
def get_capabilities(self):
Expand Down Expand Up @@ -478,7 +490,24 @@ def run_commands(self, commands=None, check_rc=True):
value is True an exception is raised.
:return: List of returned response
"""
pass
if commands is None:
raise ValueError("'commands' value is required")

responses = list()
for cmd in to_list(commands):
if not isinstance(cmd, Mapping):
cmd = {"command": cmd}

try:
out = self.send_command(**cmd)
except AnsibleConnectionFailure as e:
if check_rc:
raise
out = getattr(e, "err", e)

responses.append(out)

return responses

def check_edit_config_capability(
self,
Expand Down

0 comments on commit 8873ef7

Please sign in to comment.