eos_eapi module allows independent configuration of protocol and port

The eos_eapi module would not configure the port if the protocol wasn't
configured as reported in #4905.  This changes the behavior to now allow
the port to be configured independently

fixes #4905
This commit is contained in:
Peter Sprygada 2016-09-19 10:53:43 -04:00
parent a8a4d88b53
commit 4ff0fd3910

View file

@ -182,9 +182,11 @@ urls:
import re import re
import time import time
from ansible.module_utils.netcfg import NetworkConfig, dumps import ansible.module_utils.eos
from ansible.module_utils.eos import NetworkModule, NetworkError
from ansible.module_utils.basic import get_exception from ansible.module_utils.basic import get_exception
from ansible.module_utils.network import NetworkModule, NetworkError
from ansible.module_utils.netcfg import NetworkConfig, dumps
PRIVATE_KEYS_RE = re.compile('__.+__') PRIVATE_KEYS_RE = re.compile('__.+__')
@ -194,7 +196,24 @@ def invoke(name, *args, **kwargs):
if func: if func:
return func(*args, **kwargs) return func(*args, **kwargs)
def started(module, commands): def get_instance(module):
try:
resp = module.cli('show management api http-commands', 'json')
return dict(
http=resp[0]['httpServer']['configured'],
http_port=resp[0]['httpServer']['port'],
https=resp[0]['httpsServer']['configured'],
https_port=resp[0]['httpsServer']['port'],
local_http=resp[0]['localHttpServer']['configured'],
local_http_port=resp[0]['localHttpServer']['port'],
socket=resp[0]['unixSocketServer']['configured'],
vrf=resp[0]['vrf']
)
except NetworkError:
exc = get_exception()
module.fail_json(msg=str(exc), **exc.kwargs)
def started(module, instance, commands):
commands.append('no shutdown') commands.append('no shutdown')
setters = set() setters = set()
for key, value in module.argument_spec.iteritems(): for key, value in module.argument_spec.iteritems():
@ -202,45 +221,45 @@ def started(module, commands):
setter = value.get('setter') or 'set_%s' % key setter = value.get('setter') or 'set_%s' % key
if setter not in setters: if setter not in setters:
setters.add(setter) setters.add(setter)
invoke(setter, module, commands) invoke(setter, module, instance, commands)
def stopped(module, commands): def stopped(module, instance, commands):
commands.append('shutdown') commands.append('shutdown')
def set_protocol_http(module, commands): def set_protocol_http(module, instance, commands):
port = module.params['http_port'] port = module.params['http_port']
if not 1 <= port <= 65535: if not 1 <= port <= 65535:
module.fail_json(msg='http_port must be between 1 and 65535') module.fail_json(msg='http_port must be between 1 and 65535')
elif module.params['http'] is True: elif any((module.params['http'], instance['http'])):
commands.append('protocol http port %s' % port) commands.append('protocol http port %s' % port)
elif module.params['http'] is False: elif module.params['http'] is False:
commands.append('no protocol http') commands.append('no protocol http')
def set_protocol_https(module, commands): def set_protocol_https(module, instance, commands):
port = module.params['https_port'] port = module.params['https_port']
if not 1 <= port <= 65535: if not 1 <= port <= 65535:
module.fail_json(msg='https_port must be between 1 and 65535') module.fail_json(msg='https_port must be between 1 and 65535')
elif module.params['https'] is True: elif any((module.params['https'], instance['https'])):
commands.append('protocol https port %s' % port) commands.append('protocol https port %s' % port)
elif module.params['https'] is False: elif module.params['https'] is False:
commands.append('no protocol https') commands.append('no protocol https')
def set_local_http(module, commands): def set_local_http(module, instance, commands):
port = module.params['local_http_port'] port = module.params['local_http_port']
if not 1 <= port <= 65535: if not 1 <= port <= 65535:
module.fail_json(msg='local_http_port must be between 1 and 65535') module.fail_json(msg='local_http_port must be between 1 and 65535')
elif module.params['local_http'] is True: elif any((module.params['local_http'], instance['local_http'])):
commands.append('protocol http localhost port %s' % port) commands.append('protocol http localhost port %s' % port)
elif module.params['local_http'] is False: elif module.params['local_http'] is False:
commands.append('no protocol http localhost port 8080') commands.append('no protocol http localhost port 8080')
def set_socket(module, commands): def set_socket(module, instance, commands):
if module.params['socket'] is True: if any((module.params['socket'], instance['socket'])):
commands.append('protocol unix-socket') commands.append('protocol unix-socket')
elif module.params['socket'] is False: elif module.params['socket'] is False:
commands.append('no protocol unix-socket') commands.append('no protocol unix-socket')
def set_vrf(module, commands): def set_vrf(module, instance, commands):
vrf = module.params['vrf'] vrf = module.params['vrf']
if vrf != 'default': if vrf != 'default':
resp = module.cli(['show vrf']) resp = module.cli(['show vrf'])
@ -256,14 +275,14 @@ def get_config(module):
config = NetworkConfig(indent=3, contents=contents[0]) config = NetworkConfig(indent=3, contents=contents[0])
return config return config
def load_config(module, commands, result): def load_config(module, instance, commands, result):
commit = not module.check_mode commit = not module.check_mode
diff = module.config.load_config(commands, commit=commit) diff = module.config.load_config(commands, commit=commit)
if diff: if diff:
result['diff'] = dict(prepared=diff) result['diff'] = dict(prepared=diff)
result['changed'] = True result['changed'] = True
def load(module, commands, result): def load(module, instance, commands, result):
candidate = NetworkConfig(indent=3) candidate = NetworkConfig(indent=3)
candidate.add(commands, parents=['management api http-commands']) candidate.add(commands, parents=['management api http-commands'])
@ -273,7 +292,7 @@ def load(module, commands, result):
if configobjs: if configobjs:
commands = dumps(configobjs, 'commands').split('\n') commands = dumps(configobjs, 'commands').split('\n')
result['updates'] = commands result['updates'] = commands
load_config(module, commands, result) load_config(module, instance, commands, result)
def clean_result(result): def clean_result(result):
# strip out any keys that have two leading and two trailing # strip out any keys that have two leading and two trailing
@ -326,15 +345,15 @@ def main():
state = module.params['state'] state = module.params['state']
warnings = list() result = dict(changed=False)
result = dict(changed=False, warnings=warnings)
commands = list() commands = list()
invoke(state, module, commands) instance = get_instance(module)
invoke(state, module, instance, commands)
try: try:
load(module, commands, result) load(module, instance, commands, result)
except NetworkError: except NetworkError:
exc = get_exception() exc = get_exception()
module.fail_json(msg=str(exc), **exc.kwargs) module.fail_json(msg=str(exc), **exc.kwargs)