mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 14:41:23 -07:00
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:
parent
cf7a42b4f4
commit
42eaa00371
4 changed files with 78 additions and 25 deletions
|
@ -21,6 +21,7 @@ from __future__ import absolute_import, division, print_function
|
|||
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils.k8s.common import KubernetesAnsibleModule
|
||||
from ansible.module_utils.common.dict_transformations import dict_merge
|
||||
|
||||
|
||||
try:
|
||||
|
@ -143,7 +144,9 @@ class KubernetesRawModule(KubernetesAnsibleModule):
|
|||
return result
|
||||
else:
|
||||
if not existing:
|
||||
if not self.check_mode:
|
||||
if self.check_mode:
|
||||
k8s_obj = definition
|
||||
else:
|
||||
try:
|
||||
k8s_obj = resource.create(definition, namespace=namespace)
|
||||
except ConflictError:
|
||||
|
@ -153,7 +156,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
|
|||
self.warn("{0} was not found, but creating it returned a 409 Conflict error. This can happen \
|
||||
if the resource you are creating does not directly create a resource of the same kind.".format(name))
|
||||
return result
|
||||
result['result'] = k8s_obj.to_dict()
|
||||
result['result'] = k8s_obj.to_dict()
|
||||
result['changed'] = True
|
||||
result['method'] = 'create'
|
||||
return result
|
||||
|
@ -162,28 +165,32 @@ class KubernetesRawModule(KubernetesAnsibleModule):
|
|||
diffs = []
|
||||
|
||||
if existing and force:
|
||||
if not self.check_mode:
|
||||
if self.check_mode:
|
||||
k8s_obj = definition
|
||||
else:
|
||||
try:
|
||||
k8s_obj = resource.replace(definition, name=name, namespace=namespace).to_dict()
|
||||
match, diffs = self.diff_objects(existing.to_dict(), k8s_obj)
|
||||
result['result'] = k8s_obj
|
||||
except DynamicApiError as exc:
|
||||
self.fail_json(msg="Failed to replace object: {0}".format(exc.body),
|
||||
error=exc.status, status=exc.status, reason=exc.reason)
|
||||
match, diffs = self.diff_objects(existing.to_dict(), k8s_obj)
|
||||
result['result'] = k8s_obj
|
||||
result['changed'] = not match
|
||||
result['method'] = 'replace'
|
||||
result['diff'] = diffs
|
||||
return result
|
||||
|
||||
# Differences exist between the existing obj and requested params
|
||||
if not self.check_mode:
|
||||
if self.check_mode:
|
||||
k8s_obj = dict_merge(existing.to_dict(), definition)
|
||||
else:
|
||||
try:
|
||||
k8s_obj = resource.patch(definition, name=name, namespace=namespace).to_dict()
|
||||
match, diffs = self.diff_objects(existing.to_dict(), k8s_obj)
|
||||
result['result'] = k8s_obj
|
||||
except DynamicApiError as exc:
|
||||
self.fail_json(msg="Failed to patch object: {0}".format(exc.body),
|
||||
error=exc.status, status=exc.status, reason=exc.reason)
|
||||
match, diffs = self.diff_objects(existing.to_dict(), k8s_obj)
|
||||
result['result'] = k8s_obj
|
||||
result['changed'] = not match
|
||||
result['method'] = 'patch'
|
||||
result['diff'] = diffs
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue