roll up updates to junos_config module

* remove 'update' argument
* update doc strings
* reorder functions
reviewable/pr18780/r1
Peter Sprygada 8 years ago
parent 0c05f0dfa4
commit 42856d9949

@ -177,30 +177,6 @@ from ansible.module_utils.junos import NetworkModule, NetworkError
DEFAULT_COMMENT = 'configured by junos_config' DEFAULT_COMMENT = 'configured by junos_config'
def invoke(name, *args, **kwargs):
func = globals().get(name)
if func:
return func(*args, **kwargs)
def run(module, result):
if module.params['rollback']:
action = 'rollback_config'
elif module.params['zeroize']:
action = 'zeroize_config'
else:
action = 'load_config'
return invoke(action, module, result)
def check_args(module, warnings):
if module.params['replace'] is True:
warnings.append('The replace argument is deprecated, please use '
'update=replace instead. This argument will be '
'removed in the future')
if module.params['lines'] and module.params['update'] == 'replace':
module.fail_json(msg='config replace is only allowed when src is specified')
def guess_format(config): def guess_format(config):
try: try:
json.loads(config) json.loads(config)
@ -219,23 +195,21 @@ def guess_format(config):
return 'text' return 'text'
def backup_config(module, result):
result['__backup__'] = module.config.get_config()
def load_config(module, result): def load_config(module, result):
comment = module.params['comment']
confirm = module.params['confirm']
update = module.params['update']
candidate = module.params['lines'] or module.params['src'] candidate = module.params['lines'] or module.params['src']
commit = not module.check_mode
config_format = module.params['src_format'] or guess_format(candidate) kwargs = dict()
kwargs['comment'] = module.params['comment']
kwargs['confirm'] = module.params['confirm']
kwargs['commit'] = not module.check_mode
diff = module.config.load_config(candidate, update=update, comment=comment, if module.params['src']:
format=config_format, commit=commit, config_format = module.params['src_format'] or guess_format(candidate)
confirm=confirm) elif module.params['lines']:
config_format = 'set'
kwargs['config_format'] = config_format
diff = module.config.load_config(candidate, **kwargs)
if diff: if diff:
result['changed'] = True result['changed'] = True
@ -243,12 +217,11 @@ def load_config(module, result):
def rollback_config(module, result): def rollback_config(module, result):
rollback = module.params['rollback'] rollback = module.params['rollback']
comment = module.params['comment']
commit = not module.check_mode kwargs = dict(comment=module.param['comment'],
commit=not module.check_mode)
diff = module.connection.rollback_config(rollback, commit=commit, diff = module.connection.rollback_config(rollback, **kwargs)
comment=comment)
if diff: if diff:
result['changed'] = True result['changed'] = True
@ -259,9 +232,18 @@ def zeroize_config(module, result):
module.cli.run_commands('request system zeroize') module.cli.run_commands('request system zeroize')
result['changed'] = True result['changed'] = True
def run(module, result):
if module.params['rollback']:
return rollback_config(module, result)
elif module.params['zeroize']:
return zeroize_config(module, result)
else:
return load_config(module, result)
def main():
def main():
""" main entry point for module execution
"""
argument_spec = dict( argument_spec = dict(
lines=dict(type='list'), lines=dict(type='list'),
@ -269,7 +251,7 @@ def main():
src_format=dict(choices=['xml', 'text', 'set', 'json']), src_format=dict(choices=['xml', 'text', 'set', 'json']),
# update operations # update operations
update=dict(default='merge', choices=['merge', 'replace']), replace=dict(default=False, type='bool'),
confirm=dict(default=0, type='int'), confirm=dict(default=0, type='int'),
comment=dict(default=DEFAULT_COMMENT), comment=dict(default=DEFAULT_COMMENT),
@ -278,10 +260,6 @@ def main():
rollback=dict(type='int'), rollback=dict(type='int'),
zeroize=dict(default=False, type='bool'), zeroize=dict(default=False, type='bool'),
# this argument is deprecated in favor of setting update=replace
# and will be removed in a future version
replace=dict(default=False, type='bool'),
transport=dict(default='netconf', choices=['netconf']) transport=dict(default='netconf', choices=['netconf'])
) )
@ -289,17 +267,17 @@ def main():
('rollback', 'zeroize'), ('lines', 'src'), ('rollback', 'zeroize'), ('lines', 'src'),
('src', 'zeroize'), ('src', 'rollback')] ('src', 'zeroize'), ('src', 'rollback')]
required_if = [('replace', True, ['src'])]
module = NetworkModule(argument_spec=argument_spec, module = NetworkModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive, mutually_exclusive=mutually_exclusive,
required_if=required_if,
supports_check_mode=True) supports_check_mode=True)
warnings = list() result = dict(changed=False)
check_args(module, warnings)
if module.params['replace'] is True:
module.params['update'] = 'replace'
result = dict(changed=False, warnings=warnings) if module.params['backup']:
result['__backup__'] = module.config.get_config()
try: try:
run(module, result) run(module, result)

Loading…
Cancel
Save