Migrate json_patch to use new refactored code (#339)

Migrate json_patch to use new refactored code

SUMMARY

Migrate json_patch to use new refactored code

ISSUE TYPE

COMPONENT NAME

k8s_json_patch
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
This commit is contained in:
Mike Graves
2022-01-18 15:51:17 -05:00
parent e2e3f71ecf
commit afa6a74178

View File

@@ -136,10 +136,19 @@ from ansible_collections.kubernetes.core.plugins.module_utils.args_common import
AUTH_ARG_SPEC, AUTH_ARG_SPEC,
WAIT_ARG_SPEC, WAIT_ARG_SPEC,
) )
from ansible_collections.kubernetes.core.plugins.module_utils.common import ( from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client, get_api_client,
K8sAnsibleMixin,
) )
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import (
get_waiter,
)
try: try:
from kubernetes.dynamic.exceptions import DynamicApiError from kubernetes.dynamic.exceptions import DynamicApiError
@@ -185,7 +194,7 @@ def json_patch(existing, patch):
return None, error return None, error
def execute_module(k8s_module, module): def execute_module(module, svc):
kind = module.params.get("kind") kind = module.params.get("kind")
api_version = module.params.get("api_version") api_version = module.params.get("api_version")
name = module.params.get("name") name = module.params.get("name")
@@ -200,19 +209,15 @@ def execute_module(k8s_module, module):
"type" "type"
): ):
wait_condition = module.params["wait_condition"] wait_condition = module.params["wait_condition"]
# definition is needed for wait
definition = {
"kind": kind,
"metadata": {"name": name, "namespace": namespace},
}
def build_error_msg(kind, name, msg): def build_error_msg(kind, name, msg):
return "%s %s: %s" % (kind, name, msg) return "%s %s: %s" % (kind, name, msg)
resource = k8s_module.find_resource(kind, api_version, fail=True) client = svc.client
resource = svc.find_resource(kind, api_version, fail=True)
try: try:
existing = resource.get(name=name, namespace=namespace) existing = client.get(resource, name=name, namespace=namespace)
except DynamicApiError as exc: except DynamicApiError as exc:
msg = "Failed to retrieve requested object: {0}".format(exc.body) msg = "Failed to retrieve requested object: {0}".format(exc.body)
module.fail_json( module.fail_json(
@@ -227,7 +232,7 @@ def execute_module(k8s_module, module):
msg=build_error_msg(kind, name, msg), error="", status="", reason="" msg=build_error_msg(kind, name, msg), error="", status="", reason=""
) )
if module.check_mode and not k8s_module.supports_dry_run: if module.check_mode and not client.dry_run:
obj, error = json_patch(existing.to_dict(), patch) obj, error = json_patch(existing.to_dict(), patch)
if error: if error:
module.fail_json(**error) module.fail_json(**error)
@@ -236,7 +241,8 @@ def execute_module(k8s_module, module):
if module.check_mode: if module.check_mode:
params["dry_run"] = "All" params["dry_run"] = "All"
try: try:
obj = resource.patch( obj = client.patch(
resource,
patch, patch,
name=name, name=name,
namespace=namespace, namespace=namespace,
@@ -255,10 +261,11 @@ def execute_module(k8s_module, module):
success = True success = True
result = {"result": obj} result = {"result": obj}
if wait and not module.check_mode: if wait and not module.check_mode:
success, result["result"], result["duration"] = k8s_module.wait( waiter = get_waiter(client, resource, condition=wait_condition)
resource, definition, wait_sleep, wait_timeout, condition=wait_condition success, result["result"], result["duration"] = waiter.wait(
wait_timeout, wait_sleep, name, namespace
) )
match, diffs = k8s_module.diff_objects(existing.to_dict(), obj) match, diffs = svc.diff_objects(existing.to_dict(), obj)
result["changed"] = not match result["changed"] = not match
if module._diff: if module._diff:
result["diff"] = diffs result["diff"] = diffs
@@ -274,13 +281,12 @@ def main():
args = copy.deepcopy(AUTH_ARG_SPEC) args = copy.deepcopy(AUTH_ARG_SPEC)
args.update(copy.deepcopy(WAIT_ARG_SPEC)) args.update(copy.deepcopy(WAIT_ARG_SPEC))
args.update(JSON_PATCH_ARGS) args.update(JSON_PATCH_ARGS)
module = AnsibleModule(argument_spec=args, supports_check_mode=True) module = AnsibleK8SModule(
k8s_module = K8sAnsibleMixin(module) module_class=AnsibleModule, argument_spec=args, supports_check_mode=True
k8s_module.params = module.params )
k8s_module.check_library_version()
client = get_api_client(module) client = get_api_client(module)
k8s_module.client = client svc = K8sService(client, module)
execute_module(k8s_module, module) execute_module(module, svc)
if __name__ == "__main__": if __name__ == "__main__":