adds module create function for eapi.py shared module

This commit changes the way modules create an instance of AnsibleModule to
now use a common function, eapi_module.  This function will now automatically
append the common argument spec to the module argument_spec.  Module
arguments can override common module arguments
pull/13354/head
Peter Sprygada 9 years ago
parent 3e01516783
commit 93cd7a2a5f

@ -33,58 +33,75 @@ from ansible.module_utils.eapi import *
The eapi module provides the following common argument spec: The eapi module provides the following common argument spec:
* host (str) - [Required] The IPv4 address or FQDN of the network device * host (str) - [Required] The IPv4 address or FQDN of the network device
* port (str) - Overrides the default port to use for the HTTP/S * port (str) - Overrides the default port to use for the HTTP/S
connection. The default values are 80 for HTTP and connection. The default values are 80 for HTTP and
443 for HTTPS 443 for HTTPS
* username (str) - [Required] The username to use to authenticate
* url_username (str) - [Required] The username to use to authenticate
the HTTP/S connection. Aliases: username the HTTP/S connection. Aliases: username
* password (str) - [Required] The password to use to authenticate
* url_password (str) - [Required] The password to use to authenticate
the HTTP/S connection. Aliases: password the HTTP/S connection. Aliases: password
* use_ssl (bool) - Specifies whether or not to use an encrypted (HTTPS) * use_ssl (bool) - Specifies whether or not to use an encrypted (HTTPS)
connection or not. The default value is False. connection or not. The default value is False.
* enable_mode (bool) - Specifies whether or not to enter `enable` mode * enable_mode (bool) - Specifies whether or not to enter `enable` mode
prior to executing the command list. The default value is True prior to executing the command list. The default value is True
* enable_password (str) - The password for entering `enable` mode * enable_password (str) - The password for entering `enable` mode
on the switch if configured. on the switch if configured.
* device (dict) - Used to send the entire set of connectin parameters
as a dict object. This argument is mutually exclusive with the
host argument
In order to communicate with Arista EOS devices, the eAPI feature In order to communicate with Arista EOS devices, the eAPI feature
must be enabled and configured on the device. must be enabled and configured on the device.
""" """
def eapi_argument_spec(spec=None): EAPI_COMMON_ARGS = dict(
"""Creates an argument spec for working with eAPI host=dict(),
port=dict(),
username=dict(),
password=dict(no_log=True),
use_ssl=dict(default=True, type='bool'),
enable_mode=dict(default=True, type='bool'),
enable_password=dict(no_log=True)
enable_password=dict(no_log=True),
device=dict()
)
def eapi_module(**kwargs):
"""Append the common args to the argument_spec
""" """
arg_spec = url_argument_spec() spec = kwargs.get('argument_spec') or dict()
arg_spec.update(dict(
host=dict(required=True), argument_spec = url_argument_spec()
port=dict(), argument_spec.update(EAPI_COMMON_ARGS)
url_username=dict(required=True, aliases=['username']), if kwargs.get('argument_spec'):
url_password=dict(required=True, aliases=['password']), argument_spec.update(kwargs['argument_spec'])
use_ssl=dict(default=True, type='bool'), kwargs['argument_spec'] = argument_spec
enable_mode=dict(default=True, type='bool'),
enable_password=dict() module = AnsibleModule(**kwargs)
))
if spec: device = module.params.get('device') or dict()
arg_spec.update(spec) for key, value in device.iteritems():
return arg_spec if key in EAPI_COMMON_ARGS:
module.params[key] = value
def eapi_url(module):
"""Construct a valid Arist eAPI URL params = json_dict_unicode_to_bytes(json.loads(MODULE_COMPLEX_ARGS))
for key, value in params.iteritems():
if key != 'device':
module.params[key] = value
return module
def eapi_url(params):
"""Construct a valid Arista eAPI URL
""" """
if module.params['use_ssl']: if params['use_ssl']:
proto = 'https' proto = 'https'
else: else:
proto = 'http' proto = 'http'
host = module.params['host'] host = params['host']
url = '{}://{}'.format(proto, host) url = '{}://{}'.format(proto, host)
if module.params['port']: if params['port']:
url = '{}:{}'.format(url, module.params['port']) url = '{}:{}'.format(url, params['port'])
return '{}/command-api'.format(url) return '{}/command-api'.format(url)
def to_list(arg): def to_list(arg):
@ -103,11 +120,11 @@ def eapi_body(commands, encoding, reqid=None):
params = dict(version=1, cmds=to_list(commands), format=encoding) params = dict(version=1, cmds=to_list(commands), format=encoding)
return dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params) return dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params)
def eapi_enable_mode(module): def eapi_enable_mode(params):
"""Build commands for entering `enable` mode on the switch """Build commands for entering `enable` mode on the switch
""" """
if module.params['enable_mode']: if params['enable_mode']:
passwd = module.params['enable_password'] passwd = params['enable_password']
if passwd: if passwd:
return dict(cmd='enable', input=passwd) return dict(cmd='enable', input=passwd)
else: else:
@ -117,9 +134,9 @@ def eapi_command(module, commands, encoding='json'):
"""Send an ordered list of commands to the device over eAPI """Send an ordered list of commands to the device over eAPI
""" """
commands = to_list(commands) commands = to_list(commands)
url = eapi_url(module) url = eapi_url(module.params)
enable = eapi_enable_mode(module) enable = eapi_enable_mode(module.params)
if enable: if enable:
commands.insert(0, enable) commands.insert(0, enable)
@ -128,6 +145,9 @@ def eapi_command(module, commands, encoding='json'):
headers = {'Content-Type': 'application/json-rpc'} headers = {'Content-Type': 'application/json-rpc'}
module.params['url_username'] = module.params['username']
module.params['url_password'] = module.params['password']
response, headers = fetch_url(module, url, data=data, headers=headers, response, headers = fetch_url(module, url, data=data, headers=headers,
method='POST') method='POST')

Loading…
Cancel
Save