replaces NetCli in network with CliBase in shell

This doesn't change any of the behavior but moves NetCli to CliBase and
relcates the object to the shell module
This commit is contained in:
Peter Sprygada 2016-08-20 13:55:07 -04:00
parent 959a5e5fd1
commit 044a0533db
2 changed files with 81 additions and 98 deletions

View file

@ -32,7 +32,6 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback, get_exception
from ansible.module_utils.netcli import Cli, Command
from ansible.module_utils.netcfg import Config
from ansible.module_utils.shell import Shell, ShellError, HAS_PARAMIKO
NET_TRANSPORT_ARGS = dict(
host=dict(required=True),
@ -160,63 +159,6 @@ class NetworkModule(AnsibleModule):
exc = get_exception()
self.fail_json(msg=exc.message)
class NetCli(object):
"""Basic paramiko-based ssh transport any NetworkModule can use."""
def __init__(self):
if not HAS_PARAMIKO:
raise NetworkError(
msg='paramiko is required but does not appear to be installed. '
'It can be installed using `pip install paramiko`'
)
self.shell = None
self._connected = False
self.default_output = 'text'
def connect(self, params, kickstart=True, **kwargs):
host = params['host']
port = params.get('port') or 22
username = params['username']
password = params.get('password')
key_file = params.get('ssh_keyfile')
timeout = params['timeout']
try:
self.shell = Shell(
kickstart=kickstart,
prompts_re=self.CLI_PROMPTS_RE,
errors_re=self.CLI_ERRORS_RE,
)
self.shell.open(
host, port=port, username=username, password=password,
key_filename=key_file, timeout=timeout,
)
except ShellError:
exc = get_exception()
raise NetworkError(
msg='failed to connect to %s:%s' % (host, port), exc=str(exc)
)
self._connected = True
def disconnect(self, **kwargs):
self.shell.close()
self._connected = False
def authorize(self, params, **kwargs):
passwd = params['auth_pass']
self.execute(Command('enable', prompt=self.NET_PASSWD_RE, response=passwd))
def execute(self, commands, **kwargs):
try:
return self.shell.send(commands)
except ShellError:
exc = get_exception()
raise NetworkError(exc.message, commands=commands)
def register_transport(transport, default=False):
def register(cls):
NET_CONNECTIONS[transport] = cls