Move errors from import to connect (#50034)

This commit is contained in:
Nathaniel Case 2018-12-17 12:33:44 -05:00 committed by GitHub
parent d4ee599fe9
commit bf4ad56479
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View file

@ -138,10 +138,7 @@ try:
from napalm.base import ModuleImportError from napalm.base import ModuleImportError
HAS_NAPALM = True HAS_NAPALM = True
except ImportError: except ImportError:
raise AnsibleError( HAS_NAPALM = False
'Napalm is required to use the napalm connection type.\n'
'Please run pip install napalm'
)
display = Display() display = Display()
@ -158,6 +155,11 @@ class Connection(NetworkConnectionBase):
self.napalm = None self.napalm = None
def _connect(self): def _connect(self):
if not HAS_NAPALM:
raise AnsibleError(
'Napalm is required to use the napalm connection type.\n'
'Please run pip install napalm'
)
super(Connection, self)._connect() super(Connection, self)._connect()
if not self.connected: if not self.connected:

View file

@ -188,8 +188,9 @@ try:
from ncclient.operations import RPCError from ncclient.operations import RPCError
from ncclient.transport.errors import SSHUnknownHostError from ncclient.transport.errors import SSHUnknownHostError
from ncclient.xml_ import to_ele, to_xml from ncclient.xml_ import to_ele, to_xml
HAS_NCCLIENT = True
except ImportError: except ImportError:
raise AnsibleError("ncclient is not installed") HAS_NCCLIENT = False
display = Display() display = Display()
@ -252,6 +253,12 @@ class Connection(NetworkConnectionBase):
return super(Connection, self).exec_command(cmd, in_data, sudoable) return super(Connection, self).exec_command(cmd, in_data, sudoable)
def _connect(self): def _connect(self):
if not HAS_NCCLIENT:
raise AnsibleError(
'ncclient is required to use the netconf connection type.\n'
'Please run pip install ncclient'
)
display.display('ssh connection done, starting ncclient', log_only=True) display.display('ssh connection done, starting ncclient', log_only=True)
allow_agent = True allow_agent = True