diff --git a/lib/ansible/modules/network/openswitch/ops_template.py b/lib/ansible/modules/network/openswitch/ops_template.py index 644e103e2e..48f21c0ce9 100644 --- a/lib/ansible/modules/network/openswitch/ops_template.py +++ b/lib/ansible/modules/network/openswitch/ops_template.py @@ -97,40 +97,14 @@ responses: """ import copy - -def compare(this, other): - parents = [item.text for item in this.parents] - for entry in other: - if this == entry: - return None - return this - - -def expand(obj, queue): - block = [item.raw for item in obj.parents] - block.append(obj.raw) - - current_level = queue - for b in block: - if b not in current_level: - current_level[b] = collections.OrderedDict() - current_level = current_level[b] - for c in obj.children: - if c.raw not in current_level: - current_level[c.raw] = collections.OrderedDict() - - -def flatten(data, obj): - for k, v in data.items(): - obj.append(k) - flatten(v, obj) - return obj +from ansible.module_utils.netcfg import NetworkConfig, dumps +from ansible.module_utils.openswitch import NetworkModule def get_config(module): config = module.params['config'] or dict() if not config and not module.params['force']: - config = module.config + config = module.config.get_config() return config @@ -170,7 +144,6 @@ def merge(changeset, config=None): current_level[key] = value return config - def main(): """ main entry point for module execution """ @@ -184,18 +157,25 @@ def main(): mutually_exclusive = [('config', 'backup'), ('config', 'force')] - module = get_module(argument_spec=argument_spec, - mutually_exclusive=mutually_exclusive, - supports_check_mode=True) + module = NetworkModule(argument_spec=argument_spec, + mutually_exclusive=mutually_exclusive, + supports_check_mode=True) + + if not module.params['transport'] and not HAS_OPS: + module.fail_json(msg='unable to import ops.dc library') result = dict(changed=False) contents = get_config(module) - result['_backup'] = copy.deepcopy(module.config) + result['_backup'] = contents if module.params['transport'] in ['ssh', 'rest']: config = contents - src = module.from_json(module.params['src']) + + try: + src = module.from_json(module.params['src']) + except ValueError: + module.fail_json(msg='unable to load src due to json parsing error') changeset = diff(src, config) candidate = merge(changeset, config) @@ -208,45 +188,32 @@ def main(): if changeset: if not module.check_mode: - module.configure(config) + module.config(config) result['changed'] = True else: - config = module.parse_config(config) - candidate = module.parse_config(module.params['src']) + candidate = NetworkConfig(contents=module.params['src'], indent=4) - commands = collections.OrderedDict() - toplevel = [c.text for c in config] + if contents: + config = NetworkConfig(contents=contents, indent=4) - for line in candidate: - if line.text in ['!', '']: - continue - - if not line.parents: - if line.text not in toplevel: - expand(line, commands) - else: - item = compare(line, config) - if item: - expand(item, commands) - - commands = flatten(commands, list()) + if not module.params['force']: + commands = candidate.difference(config) + commands = dumps(commands, 'commands').split('\n') + commands = [str(c) for c in commands if c] + else: + commands = str(candidate).split('\n') if commands: if not module.check_mode: - commands = [str(c).strip() for c in commands] - response = module.configure(commands) + response = module.config(commands) result['responses'] = response result['changed'] = True + result['updates'] = commands module.exit_json(**result) -from ansible.module_utils.basic import * -from ansible.module_utils.urls import * -from ansible.module_utils.netcfg import * -from ansible.module_utils.shell import * -from ansible.module_utils.openswitch import * if __name__ == '__main__': main()