From 411397a975441d28c3193733303981a7f5b729a9 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 2 Oct 2018 02:24:52 -0500 Subject: [PATCH] Use recursive_diff for kubernetes (#45645) Move recursive_diff from cloudfront_distribution to common.dict_transformations and reuse it in k8s modules --- .../common/dict_transformations.py | 18 ++++++++++++++++++ lib/ansible/module_utils/k8s/common.py | 19 +++++++------------ .../cloud/amazon/cloudfront_distribution.py | 19 +------------------ 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/ansible/module_utils/common/dict_transformations.py b/lib/ansible/module_utils/common/dict_transformations.py index c6d7c3b42d6..26ecbcf51fa 100644 --- a/lib/ansible/module_utils/common/dict_transformations.py +++ b/lib/ansible/module_utils/common/dict_transformations.py @@ -121,3 +121,21 @@ def dict_merge(a, b): else: result[k] = deepcopy(v) return result + + +def recursive_diff(dict1, dict2): + left = dict((k, v) for (k, v) in dict1.items() if k not in dict2) + right = dict((k, v) for (k, v) in dict2.items() if k not in dict1) + for k in (set(dict1.keys()) & set(dict2.keys())): + if isinstance(dict1[k], dict) and isinstance(dict2[k], dict): + result = recursive_diff(dict1[k], dict2[k]) + if result: + left[k] = result[0] + right[k] = result[1] + elif dict1[k] != dict2[k]: + left[k] = dict1[k] + right[k] = dict2[k] + if left or right: + return left, right + else: + return None diff --git a/lib/ansible/module_utils/k8s/common.py b/lib/ansible/module_utils/k8s/common.py index 17be0b297ff..9d8f81f6bbb 100644 --- a/lib/ansible/module_utils/k8s/common.py +++ b/lib/ansible/module_utils/k8s/common.py @@ -23,6 +23,7 @@ import os from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.common.dict_transformations import recursive_diff from ansible.module_utils.six import iteritems, string_types try: @@ -40,12 +41,6 @@ try: except ImportError: HAS_YAML = False -try: - import dictdiffer - HAS_DICTDIFFER = True -except ImportError: - HAS_DICTDIFFER = False - try: import urllib3 urllib3.disable_warnings() @@ -226,12 +221,12 @@ class K8sAnsibleMixin(object): @staticmethod def diff_objects(existing, new): - if not HAS_DICTDIFFER: - return False, [] - - diffs = list(dictdiffer.diff(new, existing)) - match = len(diffs) == 0 - return match, diffs + result = dict() + diff = recursive_diff(existing, new) + if diff: + result['before'] = diff[0] + result['after'] = diff[1] + return not diff, result class KubernetesAnsibleModule(AnsibleModule, K8sAnsibleMixin): diff --git a/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py b/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py index 63145bede2e..a6cb8ac9d76 100644 --- a/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py +++ b/lib/ansible/modules/cloud/amazon/cloudfront_distribution.py @@ -1092,6 +1092,7 @@ web_acl_id: from ansible.module_utils._text import to_text, to_native from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.cloudfront_facts import CloudFrontFactsServiceManager +from ansible.module_utils.common.dict_transformations import recursive_diff from ansible.module_utils.ec2 import get_aws_connection_info from ansible.module_utils.ec2 import ec2_argument_spec, boto3_conn, compare_aws_tags from ansible.module_utils.ec2 import camel_dict_to_snake_dict, ansible_dict_to_boto3_tag_list @@ -1146,24 +1147,6 @@ def ansible_list_to_cloudfront_list(list_items=None, include_quantity=True): return result -def recursive_diff(dict1, dict2): - left = dict((k, v) for (k, v) in dict1.items() if k not in dict2) - right = dict((k, v) for (k, v) in dict2.items() if k not in dict1) - for k in (set(dict1.keys()) & set(dict2.keys())): - if isinstance(dict1[k], dict) and isinstance(dict2[k], dict): - result = recursive_diff(dict1[k], dict2[k]) - if result: - left[k] = result[0] - right[k] = result[1] - elif dict1[k] != dict2[k]: - left[k] = dict1[k] - right[k] = dict2[k] - if left or right: - return left, right - else: - return None - - def create_distribution(client, module, config, tags): try: if not tags: