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

@ -95,40 +95,25 @@ stdout_lines:
type: list
sample: [['...', '...'], ['...'], ['...']]
"""
import time
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule
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.edgeos.edgeos import run_commands
from ansible.module_utils.six import string_types
def to_lines(stdout):
for item in stdout:
if isinstance(item, string_types):
item = to_text(item).split('\n')
yield item
def parse_commands(module, warnings):
spec = dict(
command=dict(key=True),
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 executing %s' % item['command'])
'Only show commands are supported when using check mode, not '
'executing %s' % item['command']
)
commands.remove(item)
return commands
@ -146,14 +131,14 @@ def main():
module = AnsibleModule(argument_spec=spec, supports_check_mode=True)
warnings = list()
result = {'changed': False}
result = {'changed': False, 'warnings': warnings}
commands = parse_commands(module, warnings)
wait_for = module.params['wait_for'] or list()
try:
conditionals = [Conditional(c) for c in wait_for]
except AttributeError as e:
module.fail_json(msg=to_text(e))
except AttributeError as exc:
module.fail_json(msg=to_text(exc))
retries = module.params['retries']
interval = module.params['interval']
@ -180,12 +165,10 @@ def main():
msg = 'One or more conditional statements have not been satisfied'
module.fail_json(msg=msg, failed_conditions=failed_conditions)
result = {
'changed': False,
result.update({
'stdout': responses,
'warnings': warnings,
'stdout_lines': list(to_lines(responses))
}
'stdout_lines': list(to_lines(responses)),
})
module.exit_json(**result)