minor roll up of fixes to eos shared module

* added back import of Command object
* fixed eapi requests to handle output format correctly
* get_config() now honors include_defaults kwargs
pull/17431/head
Peter Sprygada 8 years ago
parent 37f721f315
commit ca95b2ae4f

@ -27,12 +27,14 @@
# #
import re import re
import time
from ansible.module_utils.basic import json from ansible.module_utils.basic import json
from ansible.module_utils.network import ModuleStub, NetworkError, NetworkModule from ansible.module_utils.network import ModuleStub, NetworkError, NetworkModule
from ansible.module_utils.network import add_argument, register_transport, to_list from ansible.module_utils.network import add_argument, register_transport, to_list
from ansible.module_utils.shell import CliBase from ansible.module_utils.shell import CliBase
from ansible.module_utils.urls import fetch_url, url_argument_spec from ansible.module_utils.urls import fetch_url, url_argument_spec
from ansible.module_utils.netcli import Command
EAPI_FORMATS = ['json', 'text'] EAPI_FORMATS = ['json', 'text']
@ -57,7 +59,7 @@ class EosConfigMixin(object):
cmd += ' all' cmd += ' all'
return self.execute([cmd])[0] return self.execute([cmd])[0]
def load_config(self, config, session, commit=False, replace=False, **kwargs): def load_config(self, config, session=None, commit=False, replace=False, **kwargs):
""" Loads the configuration into the remote device """ Loads the configuration into the remote device
This method handles the actual loading of the config This method handles the actual loading of the config
@ -69,6 +71,7 @@ class EosConfigMixin(object):
:returns list: ordered set of responses from device :returns list: ordered set of responses from device
""" """
session = session or 'ansible_%s' % int(time.time())
commands = ['configure session %s' % session] commands = ['configure session %s' % session]
if replace: if replace:
commands.append('rollback clean-config') commands.append('rollback clean-config')
@ -96,7 +99,12 @@ class EosConfigMixin(object):
commands = ['configure session %s' % session, commands = ['configure session %s' % session,
'show session-config diffs', 'show session-config diffs',
'end'] 'end']
if isinstance(self, Eapi):
response = self.execute(commands, output='text')
else:
response = self.execute(commands) response = self.execute(commands)
return response[-2] return response[-2]
def commit_config(self, session): def commit_config(self, session):
@ -119,16 +127,15 @@ class Eapi(EosConfigMixin):
def _error(self, msg): def _error(self, msg):
raise NetworkError(msg, url=self.url) raise NetworkError(msg, url=self.url)
def _get_body(self, commands, format, reqid=None): def _get_body(self, commands, output, reqid=None):
"""Create a valid eAPI JSON-RPC request message """Create a valid eAPI JSON-RPC request message
""" """
if output not in EAPI_FORMATS:
if format not in EAPI_FORMATS:
msg = 'invalid format, received %s, expected one of %s' % \ msg = 'invalid format, received %s, expected one of %s' % \
(format, ','.join(EAPI_FORMATS)) (output, ', '.join(EAPI_FORMATS))
self._error(msg=msg) self._error(msg=msg)
params = dict(version=1, cmds=commands, format=format) params = dict(version=1, cmds=commands, format=output)
return dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params) return dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params)
def connect(self, params, **kwargs): def connect(self, params, **kwargs):
@ -174,29 +181,7 @@ class Eapi(EosConfigMixin):
if self.enable is not None: if self.enable is not None:
commands.insert(0, self.enable) commands.insert(0, self.enable)
def run_commands(self, commands, **kwargs): data = self._get_body(commands, output)
output = None
cmds = list()
responses = list()
for cmd in commands:
if output and output != cmd.output:
responses.extend(self.execute(cmds, output=output))
cmds = list()
output = cmd.output
cmds.append(str(cmd))
if cmds:
responses.extend(self.execute(cmds, output=output))
for index, cmd in enumerate(commands):
if cmd.output == 'text':
responses[index] = responses[index].get('output')
return responses
data = self._get_body(commands, format)
data = json.dumps(data) data = json.dumps(data)
headers = {'Content-Type': 'application/json-rpc'} headers = {'Content-Type': 'application/json-rpc'}
@ -226,10 +211,36 @@ class Eapi(EosConfigMixin):
return response['result'] return response['result']
def run_commands(self, commands, **kwargs):
output = None
cmds = list()
responses = list()
for cmd in commands:
if output and output != cmd.output:
responses.extend(self.execute(cmds, output=output))
cmds = list()
output = cmd.output
cmds.append(str(cmd))
if cmds:
responses.extend(self.execute(cmds, output=output))
for index, cmd in enumerate(commands):
if cmd.output == 'text':
responses[index] = responses[index].get('output')
return responses
### Config methods ### ### Config methods ###
def get_config(self, **kwargs): def get_config(self, include_defaults=False):
return self.execute(['show running-config'], output='text')[0]['output'] cmd = 'show running-config'
if include_defaults:
cmd += ' all'
return self.execute([cmd], output='text')[0]['output']
Eapi = register_transport('eapi')(Eapi) Eapi = register_transport('eapi')(Eapi)

Loading…
Cancel
Save