network command modules cleanup (#45242)

* Standardize _command modules

* Centralize command transformation process

* Fix check mode error test
This commit is contained in:
Nathaniel Case 2018-09-13 08:55:35 -04:00 committed by GitHub
commit 285b927889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 115 additions and 204 deletions

View file

@ -153,42 +153,24 @@ failed_conditions:
"""
import time
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible.module_utils.six import string_types
from ansible.module_utils.network.common.parsing import Conditional
from ansible.module_utils.network.common.utils import ComplexList
from ansible.module_utils.network.common.utils import transform_commands, to_lines
from ansible.module_utils.network.eos.eos import run_commands
from ansible.module_utils.network.eos.eos import eos_argument_spec, check_args
VALID_KEYS = ['command', 'output', 'prompt', 'response']
def to_lines(stdout):
lines = list()
for item in stdout:
if isinstance(item, string_types):
item = str(item).split('\n')
lines.append(item)
return lines
def parse_commands(module, warnings):
spec = dict(
command=dict(key=True),
output=dict(),
prompt=dict(),
answer=dict()
)
transform = ComplexList(spec, module)
commands = transform(module.params['commands'])
commands = transform_commands(module)
if module.check_mode:
for item in list(commands):
if not item['command'].startswith('show'):
warnings.append(
'Only show commands are supported when using check_mode, not '
'Only show commands are supported when using check mode, not '
'executing %s' % item['command']
)
commands.remove(item)
@ -221,20 +203,16 @@ def main():
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
result = {'changed': False}
warnings = list()
result = {'changed': False, 'warnings': warnings}
check_args(module, warnings)
commands = parse_commands(module, warnings)
if warnings:
result['warnings'] = warnings
wait_for = module.params['wait_for'] or list()
try:
conditionals = [Conditional(c) for c in wait_for]
except AttributeError as exc:
module.fail_json(msg=to_native(exc))
module.fail_json(msg=to_text(exc))
retries = module.params['retries']
interval = module.params['interval']
@ -262,9 +240,8 @@ def main():
module.fail_json(msg=msg, failed_conditions=failed_conditions)
result.update({
'changed': False,
'stdout': responses,
'stdout_lines': to_lines(responses)
'stdout_lines': list(to_lines(responses)),
})
module.exit_json(**result)