Provide kubernetes definition diffs in check_mode (#41471)

Move dict_merge from azure_rm_resource module to
module_utils.common.dict_transformations and add tests.

Use dict_merge to provide a fairly realistic, reliable
diff output when k8s-based modules are run in check_mode.

Rename unit tests so that they actually run and reflect
the module_utils they're based on.
This commit is contained in:
Will Thames 2018-07-11 16:32:03 +10:00 committed by Jordan Borean
commit 42eaa00371
4 changed files with 78 additions and 25 deletions

View file

@ -8,6 +8,7 @@ __metaclass__ = type
import re
from copy import deepcopy
def camel_dict_to_snake_dict(camel_dict, reversible=False, ignore_list=()):
@ -105,3 +106,18 @@ def _camel_to_snake(name, reversible=False):
all_cap_pattern = r'([a-z0-9])([A-Z]+)'
s2 = re.sub(first_cap_pattern, r'\1_\2', s1)
return re.sub(all_cap_pattern, r'\1_\2', s2).lower()
def dict_merge(a, b):
'''recursively merges dicts. not just simple a['key'] = b['key'], if
both a and b have a key whose value is a dict then dict_merge is called
on both values and the result stored in the returned dictionary.'''
if not isinstance(b, dict):
return b
result = deepcopy(a)
for k, v in b.items():
if k in result and isinstance(result[k], dict):
result[k] = dict_merge(result[k], v)
else:
result[k] = deepcopy(v)
return result