mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Move missing library abort to use rather than import for netconf (#55384)
This commit is contained in:
@@ -24,13 +24,17 @@ from functools import wraps
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins import AnsiblePlugin
|
||||
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.basic import missing_required_lib
|
||||
|
||||
try:
|
||||
from ncclient.operations import RPCError
|
||||
from ncclient.xml_ import to_xml, to_ele
|
||||
except ImportError:
|
||||
raise AnsibleError("ncclient is not installed")
|
||||
HAS_NCCLIENT = True
|
||||
NCCLIENT_IMP_ERR = None
|
||||
except (ImportError, AttributeError) as err: # paramiko and gssapi are incompatible and raise AttributeError not ImportError
|
||||
HAS_NCCLIENT = False
|
||||
NCCLIENT_IMP_ERR = err
|
||||
|
||||
try:
|
||||
from lxml.etree import Element, SubElement, tostring, fromstring
|
||||
@@ -47,6 +51,15 @@ def ensure_connected(func):
|
||||
return wrapped
|
||||
|
||||
|
||||
def ensure_ncclient(func):
|
||||
@wraps(func)
|
||||
def wrapped(self, *args, **kwargs):
|
||||
if not HAS_NCCLIENT:
|
||||
raise AnsibleError("%s: %s" % (missing_required_lib('ncclient'), to_native(NCCLIENT_IMP_ERR)))
|
||||
return func(self, *args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
class NetconfBase(AnsiblePlugin):
|
||||
"""
|
||||
A base class for implementing Netconf connections
|
||||
@@ -107,6 +120,7 @@ class NetconfBase(AnsiblePlugin):
|
||||
def m(self):
|
||||
return self._connection._manager
|
||||
|
||||
@ensure_ncclient
|
||||
@ensure_connected
|
||||
def rpc(self, name):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user