Move missing library abort to use rather than import for netconf (#55384)

This commit is contained in:
Nathaniel Case 2019-04-23 09:19:47 -04:00 committed by GitHub
parent 780ee45819
commit b442706b54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 52 deletions

View file

@ -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):
"""