Use recursive_diff for kubernetes (#45645)
Move recursive_diff from cloudfront_distribution to common.dict_transformations and reuse it in k8s modules
This commit is contained in:
parent
c20433e5e4
commit
411397a975
3 changed files with 26 additions and 30 deletions
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue