mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Add support for cliconf and netconf plugin (#25093)
* ansible-connection refactor and action plugin changes * Add cliconf plugin for eos, ios, iosxr, junos, nxos, vyos * Add netconf plugin for junos * Add jsonrpc support * Modify network_cli and netconf connection plugin * Fix py3 unit test failure * Fix review comment * Minor fixes * Fix ansible-connection review comments * Fix CI issue * platform_agnostic related changes
This commit is contained in:
parent
c20285782d
commit
6215922889
32 changed files with 1542 additions and 585 deletions
|
@ -29,9 +29,13 @@
|
|||
import signal
|
||||
import socket
|
||||
import struct
|
||||
import os
|
||||
import uuid
|
||||
|
||||
from functools import partial
|
||||
|
||||
from ansible.module_utils.basic import get_exception
|
||||
from ansible.module_utils._text import to_bytes, to_native
|
||||
from ansible.module_utils._text import to_bytes, to_native, to_text
|
||||
|
||||
|
||||
def send_data(s, data):
|
||||
|
@ -75,4 +79,63 @@ def exec_command(module, command):
|
|||
|
||||
sf.close()
|
||||
|
||||
return (rc, to_native(stdout), to_native(stderr))
|
||||
return rc, to_native(stdout), to_native(stderr)
|
||||
|
||||
|
||||
class Connection:
|
||||
|
||||
def __init__(self, module):
|
||||
self._module = module
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
return self.__dict__[name]
|
||||
except KeyError:
|
||||
if name.startswith('_'):
|
||||
raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
|
||||
return partial(self.__rpc__, name)
|
||||
|
||||
def __rpc__(self, name, *args, **kwargs):
|
||||
"""Executes the json-rpc and returns the output received
|
||||
from remote device.
|
||||
:name: rpc method to be executed over connection plugin that implements jsonrpc 2.0
|
||||
:args: Ordered list of params passed as arguments to rpc method
|
||||
:kwargs: Dict of valid key, value pairs passed as arguments to rpc method
|
||||
|
||||
For usage refer the respective connection plugin docs.
|
||||
"""
|
||||
|
||||
reqid = str(uuid.uuid4())
|
||||
req = {'jsonrpc': '2.0', 'method': name, 'id': reqid}
|
||||
|
||||
params = list(args) or kwargs or None
|
||||
if params:
|
||||
req['params'] = params
|
||||
|
||||
if not self._module._socket_path:
|
||||
self._module.fail_json(msg='provider support not available for this host')
|
||||
|
||||
if not os.path.exists(self._module._socket_path):
|
||||
self._module.fail_json(msg='provider socket does not exist, is the provider running?')
|
||||
|
||||
try:
|
||||
data = self._module.jsonify(req)
|
||||
rc, out, err = exec_command(self._module, data)
|
||||
|
||||
except socket.error:
|
||||
exc = get_exception()
|
||||
self._module.fail_json(msg='unable to connect to socket', err=str(exc))
|
||||
|
||||
try:
|
||||
response = self._module.from_json(to_text(out, errors='surrogate_then_replace'))
|
||||
except ValueError as exc:
|
||||
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
if response['id'] != reqid:
|
||||
self._module.fail_json(msg='invalid id received')
|
||||
|
||||
if 'error' in response:
|
||||
msg = response['error'].get('data') or response['error']['message']
|
||||
self._module.fail_json(msg=to_text(msg, errors='surrogate_then_replace'))
|
||||
|
||||
return response['result']
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue