Move Conditional class to netcfg. Added error handling for connect and execute methods.

Fix comments
This commit is contained in:
chouseknecht 2016-02-09 14:59:44 -05:00
commit 5a45ca8bb1
4 changed files with 147 additions and 34 deletions

View file

@ -29,6 +29,7 @@ NET_COMMON_ARGS = dict(
provider=dict()
)
def to_list(val):
if isinstance(val, (list, tuple)):
return list(val)
@ -37,6 +38,7 @@ def to_list(val):
else:
return list()
class Cli(object):
def __init__(self, module):
@ -51,7 +53,11 @@ class Cli(object):
password = self.module.params['password']
self.shell = Shell()
self.shell.open(host, port=port, username=username, password=password)
try:
self.shell.open(host, port=port, username=username, password=password)
except Exception, exc:
self.module.fail_json('Failed to connect to {0}:{1} - {2}'.format(host, port, str(exc)))
def authorize(self):
passwd = self.module.params['auth_pass']
@ -60,6 +66,7 @@ class Cli(object):
def send(self, commands):
return self.shell.send(commands)
class NetworkModule(AnsibleModule):
def __init__(self, *args, **kwargs):
@ -101,7 +108,10 @@ class NetworkModule(AnsibleModule):
return responses
def execute(self, commands, **kwargs):
return self.connection.send(commands)
try:
return self.connection.send(commands, **kwargs)
except Exception, exc:
self.fail_json(msg=exc.message, commands=commands)
def disconnect(self):
self.connection.close()
@ -115,6 +125,7 @@ class NetworkModule(AnsibleModule):
cmd += ' all'
return self.execute(cmd)[0]
def get_module(**kwargs):
"""Return instance of NetworkModule
"""