From f77bb0ac285e2da519a81dcfd17b9fae9adcdd46 Mon Sep 17 00:00:00 2001
From: Peter Sprygada <psprygada@ansible.com>
Date: Mon, 4 Apr 2016 07:18:29 -0400
Subject: [PATCH] refactors eos_config to remove config diff functions

replaces with netcfg shared library for handling configuration diffs
---
 lib/ansible/modules/network/eos/eos_config.py | 74 ++++---------------
 1 file changed, 16 insertions(+), 58 deletions(-)

diff --git a/lib/ansible/modules/network/eos/eos_config.py b/lib/ansible/modules/network/eos/eos_config.py
index 66901fa99ea..78b38aac34d 100644
--- a/lib/ansible/modules/network/eos/eos_config.py
+++ b/lib/ansible/modules/network/eos/eos_config.py
@@ -19,7 +19,7 @@ DOCUMENTATION = """
 ---
 module: eos_config
 version_added: "2.1"
-author: "Peter sprygada (@privateip)"
+author: "Peter Sprygada (@privateip)"
 short_description: Manage Arista EOS configuration sections
 description:
   - Arista EOS configurations use a simple block indent file sytanx
@@ -146,8 +146,6 @@ responses:
   type: list
   sample: ['...', '...']
 """
-import re
-import itertools
 
 def get_config(module):
     config = module.params['config'] or dict()
@@ -155,34 +153,6 @@ def get_config(module):
         config = module.config
     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():
     """ main entry point for module execution
     """
@@ -210,47 +180,35 @@ def main():
     match = module.params['match']
     replace = module.params['replace']
 
-    contents = get_config(module)
-    config = module.parse_config(contents)
+    if not module.params['force']:
+        contents = get_config(module)
+        config = NetworkConfig(contents=contents, indent=3)
 
-    if parents:
-        for parent in parents:
-            for item in config:
-                if item.text == parent:
-                    config = item
-
-        try:
-            children = [c.text for c in config.children]
-        except AttributeError:
-            children = [c.text for c in config]
+        candidate = NetworkConfig(indent=3)
+        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 *