mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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:
|
else:
|
||||||
result[k] = deepcopy(v)
|
result[k] = deepcopy(v)
|
||||||
return result
|
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.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.common.dict_transformations import recursive_diff
|
||||||
from ansible.module_utils.six import iteritems, string_types
|
from ansible.module_utils.six import iteritems, string_types
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -40,12 +41,6 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_YAML = False
|
HAS_YAML = False
|
||||||
|
|
||||||
try:
|
|
||||||
import dictdiffer
|
|
||||||
HAS_DICTDIFFER = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_DICTDIFFER = False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib3
|
import urllib3
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
|
@ -226,12 +221,12 @@ class K8sAnsibleMixin(object):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def diff_objects(existing, new):
|
def diff_objects(existing, new):
|
||||||
if not HAS_DICTDIFFER:
|
result = dict()
|
||||||
return False, []
|
diff = recursive_diff(existing, new)
|
||||||
|
if diff:
|
||||||
diffs = list(dictdiffer.diff(new, existing))
|
result['before'] = diff[0]
|
||||||
match = len(diffs) == 0
|
result['after'] = diff[1]
|
||||||
return match, diffs
|
return not diff, result
|
||||||
|
|
||||||
|
|
||||||
class KubernetesAnsibleModule(AnsibleModule, K8sAnsibleMixin):
|
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._text import to_text, to_native
|
||||||
from ansible.module_utils.aws.core import AnsibleAWSModule
|
from ansible.module_utils.aws.core import AnsibleAWSModule
|
||||||
from ansible.module_utils.aws.cloudfront_facts import CloudFrontFactsServiceManager
|
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 get_aws_connection_info
|
||||||
from ansible.module_utils.ec2 import ec2_argument_spec, boto3_conn, compare_aws_tags
|
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
|
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
|
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):
|
def create_distribution(client, module, config, tags):
|
||||||
try:
|
try:
|
||||||
if not tags:
|
if not tags:
|
||||||
|
|
Loading…
Reference in a new issue