From dcc4e0f220d79bf1860ec17833e58fadd937b87f Mon Sep 17 00:00:00 2001 From: Wojciech Wypior Date: Tue, 19 Mar 2019 04:26:04 +0100 Subject: [PATCH] Moving comparision functions to compare.py from common.py (#53946) --- .../module_utils/network/f5/compare.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/lib/ansible/module_utils/network/f5/compare.py b/lib/ansible/module_utils/network/f5/compare.py index ca0ead7975..f45dabe940 100644 --- a/lib/ansible/module_utils/network/f5/compare.py +++ b/lib/ansible/module_utils/network/f5/compare.py @@ -6,6 +6,8 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +from ansible.module_utils.six import iteritems + def cmp_simple_list(want, have): if want is None: @@ -28,3 +30,55 @@ def cmp_str_with_none(want, have): return None if want != have: return want + + +def compare_complex_list(want, have): + """Performs a complex list comparison + + A complex list is a list of dictionaries + + Args: + want (list): List of dictionaries to compare with second parameter. + have (list): List of dictionaries compare with first parameter. + + Returns: + bool: + """ + if want == [] and have is None: + return None + if want is None: + return None + w = [] + h = [] + for x in want: + tmp = [(str(k), str(v)) for k, v in iteritems(x)] + w += tmp + for x in have: + tmp = [(str(k), str(v)) for k, v in iteritems(x)] + h += tmp + if set(w) == set(h): + return None + else: + return want + + +def compare_dictionary(want, have): + """Performs a dictionary comparison + + Args: + want (dict): Dictionary to compare with second parameter. + have (dict): Dictionary to compare with first parameter. + + Returns: + bool: + """ + if want == {} and have is None: + return None + if want is None: + return None + w = [(str(k), str(v)) for k, v in iteritems(want)] + h = [(str(k), str(v)) for k, v in iteritems(have)] + if set(w) == set(h): + return None + else: + return want