|
|
|
@ -19,15 +19,28 @@
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
import collections
|
|
|
|
|
import json
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
from itertools import chain
|
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils._text import to_text
|
|
|
|
|
from ansible.module_utils.network.common.utils import to_list
|
|
|
|
|
from ansible.plugins.cliconf import CliconfBase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def configure(func):
|
|
|
|
|
@wraps(func)
|
|
|
|
|
def wrapped(self, *args, **kwargs):
|
|
|
|
|
prompt = self._connection.get_prompt()
|
|
|
|
|
if not to_text(prompt, errors='surrogate_or_strict').strip().endswith('#'):
|
|
|
|
|
self.send_command('configure')
|
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Cliconf(CliconfBase):
|
|
|
|
|
|
|
|
|
|
def get_text(self, ele, tag):
|
|
|
|
@ -56,48 +69,148 @@ class Cliconf(CliconfBase):
|
|
|
|
|
device_info['network_os_hostname'] = match.group(1)
|
|
|
|
|
return device_info
|
|
|
|
|
|
|
|
|
|
def get_config(self, source='running', format='text'):
|
|
|
|
|
def get_config(self, source='running', format='text', flags=None):
|
|
|
|
|
if source != 'running':
|
|
|
|
|
return self.invalid_params("fetching configuration from %s is not supported" % source)
|
|
|
|
|
raise ValueError("fetching configuration from %s is not supported" % source)
|
|
|
|
|
|
|
|
|
|
options_values = self.get_option_values()
|
|
|
|
|
if format not in options_values['format']:
|
|
|
|
|
raise ValueError("'format' value %s is invalid. Valid values are %s" % (format, ','.join(options_values['format'])))
|
|
|
|
|
|
|
|
|
|
if format == 'text':
|
|
|
|
|
cmd = 'show configuration'
|
|
|
|
|
else:
|
|
|
|
|
cmd = 'show configuration | display %s' % format
|
|
|
|
|
|
|
|
|
|
cmd += ' '.join(to_list(flags))
|
|
|
|
|
cmd = cmd.strip()
|
|
|
|
|
return self.send_command(cmd)
|
|
|
|
|
|
|
|
|
|
def edit_config(self, command):
|
|
|
|
|
for cmd in chain(['configure'], to_list(command)):
|
|
|
|
|
self.send_command(cmd)
|
|
|
|
|
@configure
|
|
|
|
|
def edit_config(self, candidate=None, commit=True, replace=None, comment=None):
|
|
|
|
|
|
|
|
|
|
operations = self.get_device_operations()
|
|
|
|
|
self.check_edit_config_capabiltiy(operations, candidate, commit, replace, comment)
|
|
|
|
|
|
|
|
|
|
resp = {}
|
|
|
|
|
results = []
|
|
|
|
|
requests = []
|
|
|
|
|
|
|
|
|
|
if replace:
|
|
|
|
|
candidate = 'load replace {0}'.format(replace)
|
|
|
|
|
|
|
|
|
|
for line in to_list(candidate):
|
|
|
|
|
if not isinstance(line, collections.Mapping):
|
|
|
|
|
line = {'command': line}
|
|
|
|
|
cmd = line['command']
|
|
|
|
|
results.append(self.send_command(**line))
|
|
|
|
|
requests.append(cmd)
|
|
|
|
|
|
|
|
|
|
diff = self.compare_configuration()
|
|
|
|
|
if diff:
|
|
|
|
|
resp['diff'] = diff
|
|
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
|
self.commit(comment=comment)
|
|
|
|
|
else:
|
|
|
|
|
self.discard_changes()
|
|
|
|
|
|
|
|
|
|
resp['request'] = requests
|
|
|
|
|
resp['response'] = results
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
def get(self, command, prompt=None, answer=None, sendonly=False):
|
|
|
|
|
def get(self, command, prompt=None, answer=None, sendonly=False, output=None):
|
|
|
|
|
if output:
|
|
|
|
|
command = self._get_command_with_output(command, output)
|
|
|
|
|
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly)
|
|
|
|
|
|
|
|
|
|
def commit(self, *args, **kwargs):
|
|
|
|
|
"""Execute commit command on remote device.
|
|
|
|
|
:kwargs:
|
|
|
|
|
comment: Optional commit description.
|
|
|
|
|
@configure
|
|
|
|
|
def commit(self, comment=None, confirmed=False, at_time=None, synchronize=False):
|
|
|
|
|
"""
|
|
|
|
|
Execute commit command on remote device.
|
|
|
|
|
:param comment: Comment to be associated with commit
|
|
|
|
|
:param confirmed: Boolean flag to indicate if the previous commit should confirmed
|
|
|
|
|
:param at_time: Time at which to activate configuration changes
|
|
|
|
|
:param synchronize: Boolean flag to indicate if commit should synchronize on remote peers
|
|
|
|
|
:return: Command response received from device
|
|
|
|
|
"""
|
|
|
|
|
comment = kwargs.get('comment', None)
|
|
|
|
|
command = 'commit'
|
|
|
|
|
if comment:
|
|
|
|
|
command += ' comment {0}'.format(comment)
|
|
|
|
|
if confirmed:
|
|
|
|
|
command += ' confirmed'
|
|
|
|
|
if at_time:
|
|
|
|
|
command += ' {0}'.format(at_time)
|
|
|
|
|
if synchronize:
|
|
|
|
|
command += ' peers-synchronize'
|
|
|
|
|
|
|
|
|
|
command += ' and-quit'
|
|
|
|
|
return self.send_command(command)
|
|
|
|
|
|
|
|
|
|
@configure
|
|
|
|
|
def discard_changes(self):
|
|
|
|
|
command = 'rollback 0'
|
|
|
|
|
for cmd in chain(to_list(command), 'exit'):
|
|
|
|
|
self.send_command(cmd)
|
|
|
|
|
|
|
|
|
|
@configure
|
|
|
|
|
def validate(self):
|
|
|
|
|
return self.send_command('commit check')
|
|
|
|
|
|
|
|
|
|
@configure
|
|
|
|
|
def compare_configuration(self, rollback_id=None):
|
|
|
|
|
command = 'show | compare'
|
|
|
|
|
if rollback_id is not None:
|
|
|
|
|
command += ' rollback %s' % int(rollback_id)
|
|
|
|
|
resp = self.send_command(command)
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
def get_diff(self, rollback_id=None):
|
|
|
|
|
return self.compare_configuration(rollback_id=rollback_id)
|
|
|
|
|
|
|
|
|
|
def get_device_operations(self):
|
|
|
|
|
return {
|
|
|
|
|
'supports_diff_replace': False,
|
|
|
|
|
'supports_commit': True,
|
|
|
|
|
'supports_rollback': True,
|
|
|
|
|
'supports_defaults': False,
|
|
|
|
|
'supports_onbox_diff': True,
|
|
|
|
|
'supports_commit_comment': True,
|
|
|
|
|
'supports_multiline_delimiter': False,
|
|
|
|
|
'supports_diff_match': False,
|
|
|
|
|
'supports_diff_ignore_lines': False,
|
|
|
|
|
'supports_generate_diff': False,
|
|
|
|
|
'supports_replace': True
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get_option_values(self):
|
|
|
|
|
return {
|
|
|
|
|
'format': ['text', 'set', 'xml', 'json'],
|
|
|
|
|
'diff_match': [],
|
|
|
|
|
'diff_replace': [],
|
|
|
|
|
'output': ['text', 'set', 'xml', 'json']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get_capabilities(self):
|
|
|
|
|
result = dict()
|
|
|
|
|
result['rpc'] = self.get_base_rpc() + ['commit', 'discard_changes']
|
|
|
|
|
result['rpc'] = self.get_base_rpc() + ['commit', 'discard_changes', 'run_commands', 'compare_configuration', 'validate', 'get_diff']
|
|
|
|
|
result['network_api'] = 'cliconf'
|
|
|
|
|
result['device_info'] = self.get_device_info()
|
|
|
|
|
result['device_operations'] = self.get_device_operations()
|
|
|
|
|
result.update(self.get_option_values())
|
|
|
|
|
return json.dumps(result)
|
|
|
|
|
|
|
|
|
|
def compare_configuration(self, rollback_id=None):
|
|
|
|
|
command = 'show | compare'
|
|
|
|
|
if rollback_id is not None:
|
|
|
|
|
command += ' rollback %s' % int(rollback_id)
|
|
|
|
|
return self.send_command(command)
|
|
|
|
|
def _get_command_with_output(self, command, output):
|
|
|
|
|
options_values = self.get_option_values()
|
|
|
|
|
if output not in options_values['output']:
|
|
|
|
|
raise ValueError("'output' value %s is invalid. Valid values are %s" % (output, ','.join(options_values['output'])))
|
|
|
|
|
|
|
|
|
|
if output == 'json' and not command.endswith('| display json'):
|
|
|
|
|
cmd = '%s | display json' % command
|
|
|
|
|
elif output == 'xml' and not command.endswith('| display xml'):
|
|
|
|
|
cmd = '%s | display xml' % command
|
|
|
|
|
elif output == 'text' and (command.endswith('| display json') or command.endswith('| display xml')):
|
|
|
|
|
cmd = command.rsplit('|', 1)[0]
|
|
|
|
|
else:
|
|
|
|
|
cmd = command
|
|
|
|
|
return cmd
|
|
|
|
|