mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #14432 from chouseknecht/net_common_cleanup_2
Cleanup for networking common bits.
This commit is contained in:
commit
09a6bf5c15
4 changed files with 147 additions and 34 deletions
|
@ -38,6 +38,7 @@ def to_list(val):
|
||||||
else:
|
else:
|
||||||
return list()
|
return list()
|
||||||
|
|
||||||
|
|
||||||
class Eapi(object):
|
class Eapi(object):
|
||||||
|
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
|
@ -107,6 +108,7 @@ class Eapi(object):
|
||||||
|
|
||||||
return response['result']
|
return response['result']
|
||||||
|
|
||||||
|
|
||||||
class Cli(object):
|
class Cli(object):
|
||||||
|
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
|
@ -121,15 +123,20 @@ class Cli(object):
|
||||||
password = self.module.params['password']
|
password = self.module.params['password']
|
||||||
|
|
||||||
self.shell = Shell()
|
self.shell = Shell()
|
||||||
|
|
||||||
|
try:
|
||||||
self.shell.open(host, port=port, username=username, password=password)
|
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):
|
def authorize(self):
|
||||||
passwd = self.module.params['auth_pass']
|
passwd = self.module.params['auth_pass']
|
||||||
self.send(Command('enable', prompt=NET_PASSWD_RE, response=passwd))
|
self.send(Command('enable', prompt=NET_PASSWD_RE, response=passwd))
|
||||||
|
|
||||||
def send(self, commands, encoding='text'):
|
def send(self, commands):
|
||||||
return self.shell.send(commands)
|
return self.shell.send(commands)
|
||||||
|
|
||||||
|
|
||||||
class NetworkModule(AnsibleModule):
|
class NetworkModule(AnsibleModule):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
|
@ -29,6 +29,7 @@ NET_COMMON_ARGS = dict(
|
||||||
provider=dict()
|
provider=dict()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def to_list(val):
|
def to_list(val):
|
||||||
if isinstance(val, (list, tuple)):
|
if isinstance(val, (list, tuple)):
|
||||||
return list(val)
|
return list(val)
|
||||||
|
@ -37,6 +38,7 @@ def to_list(val):
|
||||||
else:
|
else:
|
||||||
return list()
|
return list()
|
||||||
|
|
||||||
|
|
||||||
class Cli(object):
|
class Cli(object):
|
||||||
|
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
|
@ -51,7 +53,11 @@ class Cli(object):
|
||||||
password = self.module.params['password']
|
password = self.module.params['password']
|
||||||
|
|
||||||
self.shell = Shell()
|
self.shell = Shell()
|
||||||
|
|
||||||
|
try:
|
||||||
self.shell.open(host, port=port, username=username, password=password)
|
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):
|
def authorize(self):
|
||||||
passwd = self.module.params['auth_pass']
|
passwd = self.module.params['auth_pass']
|
||||||
|
@ -60,6 +66,7 @@ class Cli(object):
|
||||||
def send(self, commands):
|
def send(self, commands):
|
||||||
return self.shell.send(commands)
|
return self.shell.send(commands)
|
||||||
|
|
||||||
|
|
||||||
class NetworkModule(AnsibleModule):
|
class NetworkModule(AnsibleModule):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -101,7 +108,10 @@ class NetworkModule(AnsibleModule):
|
||||||
return responses
|
return responses
|
||||||
|
|
||||||
def execute(self, commands, **kwargs):
|
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):
|
def disconnect(self):
|
||||||
self.connection.close()
|
self.connection.close()
|
||||||
|
@ -115,6 +125,7 @@ class NetworkModule(AnsibleModule):
|
||||||
cmd += ' all'
|
cmd += ' all'
|
||||||
return self.execute(cmd)[0]
|
return self.execute(cmd)[0]
|
||||||
|
|
||||||
|
|
||||||
def get_module(**kwargs):
|
def get_module(**kwargs):
|
||||||
"""Return instance of NetworkModule
|
"""Return instance of NetworkModule
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -38,6 +38,7 @@ class ConfigLine(object):
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
return not self.__eq__(other)
|
return not self.__eq__(other)
|
||||||
|
|
||||||
|
|
||||||
def parse(lines, indent):
|
def parse(lines, indent):
|
||||||
toplevel = re.compile(r'\S')
|
toplevel = re.compile(r'\S')
|
||||||
childline = re.compile(r'^\s*(.+)$')
|
childline = re.compile(r'^\s*(.+)$')
|
||||||
|
@ -83,3 +84,87 @@ def parse(lines, indent):
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
class Conditional(object):
|
||||||
|
'''
|
||||||
|
Used in command modules to evaluate waitfor conditions
|
||||||
|
'''
|
||||||
|
|
||||||
|
OPERATORS = {
|
||||||
|
'eq': ['eq', '=='],
|
||||||
|
'neq': ['neq', 'ne', '!='],
|
||||||
|
'gt': ['gt', '>'],
|
||||||
|
'ge': ['ge', '>='],
|
||||||
|
'lt': ['lt', '<'],
|
||||||
|
'le': ['le', '<='],
|
||||||
|
'contains': ['contains']
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, conditional):
|
||||||
|
self.raw = conditional
|
||||||
|
|
||||||
|
key, op, val = shlex.split(conditional)
|
||||||
|
self.key = key
|
||||||
|
self.func = self.func(op)
|
||||||
|
self.value = self._cast_value(val)
|
||||||
|
|
||||||
|
def __call__(self, data):
|
||||||
|
try:
|
||||||
|
value = self.get_value(dict(result=data))
|
||||||
|
return self.func(value)
|
||||||
|
except Exception:
|
||||||
|
raise ValueError(self.key)
|
||||||
|
|
||||||
|
def _cast_value(self, value):
|
||||||
|
if value in BOOLEANS_TRUE:
|
||||||
|
return True
|
||||||
|
elif value in BOOLEANS_FALSE:
|
||||||
|
return False
|
||||||
|
elif re.match(r'^\d+\.d+$', value):
|
||||||
|
return float(value)
|
||||||
|
elif re.match(r'^\d+$', value):
|
||||||
|
return int(value)
|
||||||
|
else:
|
||||||
|
return unicode(value)
|
||||||
|
|
||||||
|
def func(self, oper):
|
||||||
|
for func, operators in self.OPERATORS.items():
|
||||||
|
if oper in operators:
|
||||||
|
return getattr(self, func)
|
||||||
|
raise AttributeError('unknown operator: %s' % oper)
|
||||||
|
|
||||||
|
def get_value(self, result):
|
||||||
|
for key in self.key.split('.'):
|
||||||
|
match = re.match(r'^(.+)\[(\d+)\]', key)
|
||||||
|
if match:
|
||||||
|
key, index = match.groups()
|
||||||
|
result = result[key][int(index)]
|
||||||
|
else:
|
||||||
|
result = result.get(key)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def number(self, value):
|
||||||
|
if '.' in str(value):
|
||||||
|
return float(value)
|
||||||
|
else:
|
||||||
|
return int(value)
|
||||||
|
|
||||||
|
def eq(self, value):
|
||||||
|
return value == self.value
|
||||||
|
|
||||||
|
def neq(self, value):
|
||||||
|
return value != self.value
|
||||||
|
|
||||||
|
def gt(self, value):
|
||||||
|
return self.number(value) > self.value
|
||||||
|
|
||||||
|
def ge(self, value):
|
||||||
|
return self.number(value) >= self.value
|
||||||
|
|
||||||
|
def lt(self, value):
|
||||||
|
return self.number(value) < self.value
|
||||||
|
|
||||||
|
def le(self, value):
|
||||||
|
return self.number(value) <= self.value
|
||||||
|
|
||||||
|
def contains(self, value):
|
||||||
|
return self.value in value
|
||||||
|
|
|
@ -128,6 +128,7 @@ class Nxapi(object):
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
class Cli(object):
|
class Cli(object):
|
||||||
|
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
|
@ -142,11 +143,16 @@ class Cli(object):
|
||||||
password = self.module.params['password']
|
password = self.module.params['password']
|
||||||
|
|
||||||
self.shell = Shell()
|
self.shell = Shell()
|
||||||
|
|
||||||
|
try:
|
||||||
self.shell.open(host, port=port, username=username, password=password)
|
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 send(self, commands, encoding='text'):
|
def send(self, commands, encoding='text'):
|
||||||
return self.shell.send(commands)
|
return self.shell.send(commands)
|
||||||
|
|
||||||
|
|
||||||
class NetworkModule(AnsibleModule):
|
class NetworkModule(AnsibleModule):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -190,7 +196,10 @@ class NetworkModule(AnsibleModule):
|
||||||
return responses
|
return responses
|
||||||
|
|
||||||
def execute(self, commands, **kwargs):
|
def execute(self, commands, **kwargs):
|
||||||
|
try:
|
||||||
return self.connection.send(commands, **kwargs)
|
return self.connection.send(commands, **kwargs)
|
||||||
|
except Exception, exc:
|
||||||
|
self.fail_json(msg=exc.message, commands=commands)
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
self.connection.close()
|
self.connection.close()
|
||||||
|
@ -206,6 +215,7 @@ class NetworkModule(AnsibleModule):
|
||||||
response = self.execute(cmd)
|
response = self.execute(cmd)
|
||||||
return response[0]
|
return response[0]
|
||||||
|
|
||||||
|
|
||||||
def get_module(**kwargs):
|
def get_module(**kwargs):
|
||||||
"""Return instance of NetworkModule
|
"""Return instance of NetworkModule
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue