From 639e324fdb57bd15ff1757c1c0926daac37b5de5 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Mon, 4 Apr 2016 08:13:36 -0400 Subject: [PATCH] refactors nxos_config and removes diff functions this refactors the nxos_config module removing the diff functions and replacing with the netcfg shared module --- network/nxos/nxos_config.py | 70 ++++++++----------------------------- 1 file changed, 15 insertions(+), 55 deletions(-) diff --git a/network/nxos/nxos_config.py b/network/nxos/nxos_config.py index de7d949315d..ec578eaaa17 100644 --- a/network/nxos/nxos_config.py +++ b/network/nxos/nxos_config.py @@ -154,8 +154,6 @@ responses: type: list sample: ['...', '...'] """ -import re -import itertools def get_config(module): config = module.params['config'] or dict() @@ -164,34 +162,6 @@ def get_config(module): return config -def build_candidate(lines, parents, config, strategy): - candidate = list() - - if strategy == 'strict': - for index, cmd in enumerate(lines): - try: - if cmd != config[index]: - candidate.append(cmd) - except IndexError: - candidate.append(cmd) - - elif strategy == 'exact': - if len(lines) != len(config): - candidate = list(lines) - else: - for cmd, cfg in itertools.izip(lines, config): - if cmd != cfg: - candidate = list(lines) - break - - else: - for cmd in lines: - if cmd not in config: - candidate.append(cmd) - - return candidate - - def main(): argument_spec = dict( @@ -220,45 +190,35 @@ def main(): contents = get_config(module) config = module.parse_config(contents) - if parents: - _config = list() - for item in config: - p = [p.text for p in item.parents] - if parents == p: - _config.append(item) + if not module.params['force']: + contents = get_config(module) + config = NetworkConfig(contents=contents, indent=2) - try: - children = [c.text for c in _config.children] - except AttributeError: - children = [c.text for c in _config] + candidate = NetworkConfig(indent=2) + candidate.add(lines, parents=parents) + commands = candidate.difference(config, path=parents, match=match, replace=replace) else: - children = [c.text for c in config if not c.parents] + commands = parents + commands.extend(lines) result = dict(changed=False) - candidate = build_candidate(lines, parents, children, match) - - if candidate: - if replace == 'line': - candidate[:0] = parents - else: - candidate = list(parents) - candidate.extend(lines) - + if commands: if before: - candidate[:0] = before + commands[:0] = before if after: - candidate.extend(after) + commands.extend(after) if not module.check_mode: - response = module.configure(candidate) + commands = [str(c).strip() for c in commands] + response = module.configure(commands) result['responses'] = response result['changed'] = True - result['updates'] = candidate - return module.exit_json(**result) + result['updates'] = commands + module.exit_json(**result) from ansible.module_utils.basic import * from ansible.module_utils.urls import *