add provider argument to eos shared module

Adds a new argument `provider` to the eos shared module and updates the
eos doc fragment.  This commit includes some additional minor fixes and
code refactors for naming conventions.  The `provider` argument allows the
shared module arguments to be passed as a dict object instead of having
to pass each argument invididually.
This commit is contained in:
Peter Sprygada 2016-01-19 07:26:19 -05:00
parent d07d974ad8
commit e2ff26a5cf
2 changed files with 31 additions and 12 deletions
lib/ansible
module_utils
utils/module_docs_fragments

View file

@ -26,7 +26,8 @@ NET_COMMON_ARGS = dict(
authorize=dict(default=False, type='bool'),
auth_pass=dict(no_log=True),
transport=dict(choices=['cli', 'eapi']),
use_ssl=dict(default=True, type='bool')
use_ssl=dict(default=True, type='bool'),
provider=dict()
)
def to_list(val):
@ -129,10 +130,10 @@ class Cli(object):
def send(self, commands, encoding='text'):
return self.shell.send(commands)
class EosModule(AnsibleModule):
class NetworkModule(AnsibleModule):
def __init__(self, *args, **kwargs):
super(EosModule, self).__init__(*args, **kwargs)
super(NetworkModule, self).__init__(*args, **kwargs)
self.connection = None
self._config = None
@ -142,6 +143,14 @@ class EosModule(AnsibleModule):
self._config = self.get_config()
return self._config
def _load_params(self):
params = super(NetworkModule, self)._load_params()
provider = params.get('provider') or dict()
for key, value in provider.items():
if key in NET_COMMON_ARGS.keys():
params[key] = value
return params
def connect(self):
if self.params['transport'] == 'eapi':
self.connection = Eapi(self)
@ -163,8 +172,18 @@ class EosModule(AnsibleModule):
commands.insert(0, 'configure terminal')
responses = self.execute(commands)
responses.pop(0)
return responses
def config_replace(self, commands):
if self.params['transport'] == 'cli':
self.fail_json(msg='config replace only supported over eapi')
cmd = 'configure replace terminal:'
commands = '\n'.join(to_list(commands))
command = dict(cmd=cmd, input=commands)
self.execute(command)
def execute(self, commands, **kwargs):
try:
return self.connection.send(commands, **kwargs)
@ -189,26 +208,19 @@ class EosModule(AnsibleModule):
def get_module(**kwargs):
"""Return instance of EosModule
"""Return instance of NetworkModule
"""
argument_spec = NET_COMMON_ARGS.copy()
if kwargs.get('argument_spec'):
argument_spec.update(kwargs['argument_spec'])
kwargs['argument_spec'] = argument_spec
kwargs['check_invalid_arguments'] = False
module = EosModule(**kwargs)
module = NetworkModule(**kwargs)
# HAS_PARAMIKO is set by module_utils/shell.py
if module.params['transport'] == 'cli' and not HAS_PARAMIKO:
module.fail_json(msg='paramiko is required but does not appear to be installed')
# copy in values from local action.
params = json_dict_unicode_to_bytes(json.loads(MODULE_COMPLEX_ARGS))
for key, value in params.iteritems():
module.params[key] = value
module.connect()
return module

View file

@ -80,5 +80,12 @@ options:
required: false
default: true
choices: BOOLEANS
provider:
description:
- Convience method that allows all M(eos) arguments to be passed as
a dict object. All constraints (required, choices, etc) must be
met either by individual arguments or values in this dict.
required: false
default: null
"""