Skip to content

Commit

Permalink
Add default cliconf plugin good enough to use cli_command most of the…
Browse files Browse the repository at this point in the history
… time (#569)

* Add default cliconf plugin good enough to use cli_command most of the time

* Delegate more functionality to CliconfBase

* Add tests for default
  • Loading branch information
Qalthos authored Jul 27, 2023
1 parent 3a1edc1 commit 07ef0c5
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 25 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Name | Description
--- | ---
[ansible.netcommon.enable](https://github.com/ansible-collections/ansible.netcommon/blob/main/docs/ansible.netcommon.enable_become.rst)|Switch to elevated permissions on a network device

### Cliconf plugins
Name | Description
--- | ---
[ansible.netcommon.default](https://github.com/ansible-collections/ansible.netcommon/blob/main/docs/ansible.netcommon.default_cliconf.rst)|General purpose cliconf plugin for new platforms

### Connection plugins
Name | Description
--- | ---
Expand Down
5 changes: 5 additions & 0 deletions changelogs/fragments/default-cliconf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
minor_changes:
- Add a new cliconf plugin ``default`` that can be used when no cliconf
plugin is found for a given network_os. This plugin only supports ``get()``.
(https://github.com/ansible-collections/ansible.netcommon/pull/569)
43 changes: 43 additions & 0 deletions docs/ansible.netcommon.default_cliconf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.. _ansible.netcommon.default_cliconf:


*************************
ansible.netcommon.default
*************************

**General purpose cliconf plugin for new platforms**


Version added: 5.2.0

.. contents::
:local:
:depth: 1


Synopsis
--------
- This plugin attemts to provide low level abstraction apis for sending and receiving CLI commands from arbitrary network devices.











Status
------


Authors
~~~~~~~

- Ansible Networking Team (@ansible-network)


.. hint::
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.
67 changes: 67 additions & 0 deletions plugins/cliconf/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# (c) 2023 Red Hat Inc.
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function


__metaclass__ = type

DOCUMENTATION = """
author: Ansible Networking Team (@ansible-network)
name: default
short_description: General purpose cliconf plugin for new platforms
description:
- This plugin attemts to provide low level abstraction apis for sending and receiving CLI
commands from arbitrary network devices.
version_added: 5.2.0
"""

import json

from ansible.errors import AnsibleConnectionFailure

from ansible_collections.ansible.netcommon.plugins.plugin_utils.cliconf_base import CliconfBase


class Cliconf(CliconfBase):
def __init__(self, *args, **kwargs):
super(Cliconf, self).__init__(*args, **kwargs)
self._device_info = {}

def get_device_info(self):
if not self._device_info:
device_info = {}

device_info["network_os"] = "default"
self._device_info = device_info

return self._device_info

def get_config(self, flags=None, format=None):
network_os = self.get_device_info()["network_os"]
raise AnsibleConnectionFailure("get_config is not supported by network_os %s" % network_os)

def edit_config(self, candidate=None, commit=True, replace=None, comment=None):
network_os = self.get_device_info()["network_os"]
raise AnsibleConnectionFailure("edit_config is not supported by network_os %s" % network_os)

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 {
"supports_diff_replace": False,
"supports_commit": False,
"supports_rollback": False,
"supports_defaults": False,
"supports_onbox_diff": False,
"supports_commit_comment": False,
"supports_multiline_delimiter": False,
"supports_diff_match": False,
"supports_diff_ignore_lines": False,
"supports_generate_diff": False,
"supports_replace": False,
}
38 changes: 21 additions & 17 deletions plugins/connection/network_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,26 +386,30 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
raise AnsibleConnectionFailure("network os %s is not supported" % self._network_os)

self.cliconf = cliconf_loader.get(self._network_os, self)
if self.cliconf:
self._sub_plugin = {
"type": "cliconf",
"name": self.cliconf._load_name,
"obj": self.cliconf,
}
if not self.cliconf:
self.queue_message(
"vvvv",
"loaded cliconf plugin %s from path %s for network_os %s"
% (
self.cliconf._load_name,
self.cliconf._original_path,
self._network_os,
),
)
else:
self.queue_message(
"vvvv",
"unable to load cliconf for network_os %s" % self._network_os,
"unable to load cliconf for network_os %s. Falling back to default"
% self._network_os,
)
self.cliconf = cliconf_loader.get("ansible.netcommon.default", self)
if not self.cliconf:
raise AnsibleConnectionFailure("Couldn't load fallback cliconf plugin")

self._sub_plugin = {
"type": "cliconf",
"name": self.cliconf._load_name,
"obj": self.cliconf,
}
self.queue_message(
"vvvv",
"loaded cliconf plugin %s from path %s for network_os %s"
% (
self.cliconf._load_name,
self.cliconf._original_path,
self._network_os,
),
)
else:
raise AnsibleConnectionFailure(
"Unable to automatically determine host network os. Please "
Expand Down
46 changes: 38 additions & 8 deletions plugins/plugin_utils/cliconf_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

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:
from scp import SCPClient
Expand Down Expand Up @@ -70,12 +73,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 +245,6 @@ def edit_config(
"""
pass

@abstractmethod
def get(
self,
command=None,
Expand All @@ -268,7 +271,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 @@ -340,7 +353,7 @@ def commit(self, comment=None):
:return: None
"""
return self._connection.method_not_found(
raise AnsibleConnectionFailure(
"commit is not supported by network_os %s" % self._play_context.network_os
)

Expand All @@ -353,7 +366,7 @@ def discard_changes(self):
:returns: None
"""
return self._connection.method_not_found(
raise AnsibleConnectionFailure(
"discard_changes is not supported by network_os %s" % self._play_context.network_os
)

Expand Down Expand Up @@ -478,7 +491,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
Loading

0 comments on commit 07ef0c5

Please sign in to comment.