cleanup(info): Extract execute_module

Extract the execute_module function from the kubevirt_vm_info module to
make it available for other modules.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2024-08-14 16:34:55 +02:00
parent 079a8066fb
commit cd270e271b
3 changed files with 70 additions and 54 deletions

View File

@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
# Copyright 2024 Red Hat, Inc.
# Apache License 2.0 (see LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
)
INFO_ARG_SPEC = {
"api_version": {"default": "kubevirt.io/v1"},
"name": {},
"namespace": {},
"label_selectors": {"type": "list", "elements": "str", "default": []},
"field_selectors": {"type": "list", "elements": "str", "default": []},
"wait": {"type": "bool"},
"wait_sleep": {"type": "int", "default": 5},
"wait_timeout": {"type": "int", "default": 120},
}
def execute_info_module(module, kind, wait_condition):
"""
execute_info_module runs the lookup of resources.
"""
try:
client = get_api_client(module)
svc = K8sService(client, module)
facts = svc.find(
kind=kind,
api_version=module.params["api_version"],
name=module.params["name"],
namespace=module.params["namespace"],
label_selectors=module.params["label_selectors"],
field_selectors=module.params["field_selectors"],
wait=module.params["wait"],
wait_sleep=module.params["wait_sleep"],
wait_timeout=module.params["wait_timeout"],
condition=wait_condition,
)
module.exit_json(changed=False, **facts)
except CoreException as exc:
module.fail_from_exception(exc)

View File

@@ -161,46 +161,11 @@ from ansible_collections.kubernetes.core.plugins.module_utils.args_common import
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client,
from ansible_collections.kubevirt.core.plugins.module_utils.info import (
INFO_ARG_SPEC,
execute_info_module,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
)
def execute_module(module, svc):
"""
execute_module defines the kind and wait_condition and runs the lookup
of resources.
"""
# Set kind to query for VirtualMachines
KIND = "VirtualMachine"
# Set wait_condition to allow waiting for the ready state of the
# VirtualMachine based on the running parameter.
if module.params["running"] is None or module.params["running"]:
wait_condition = {"type": "Ready", "status": True}
else:
wait_condition = {"type": "Ready", "status": False, "reason": "VMINotExists"}
facts = svc.find(
kind=KIND,
api_version=module.params["api_version"],
name=module.params["name"],
namespace=module.params["namespace"],
label_selectors=module.params["label_selectors"],
field_selectors=module.params["field_selectors"],
wait=module.params["wait"],
wait_sleep=module.params["wait_sleep"],
wait_timeout=module.params["wait_timeout"],
condition=wait_condition,
)
module.exit_json(changed=False, **facts)
def arg_spec():
@@ -208,16 +173,9 @@ def arg_spec():
arg_spec defines the argument spec of this module.
"""
spec = {
"api_version": {"default": "kubevirt.io/v1"},
"name": {},
"namespace": {},
"label_selectors": {"type": "list", "elements": "str", "default": []},
"field_selectors": {"type": "list", "elements": "str", "default": []},
"running": {"type": "bool"},
"wait": {"type": "bool"},
"wait_sleep": {"type": "int", "default": 5},
"wait_timeout": {"type": "int", "default": 120},
}
spec.update(deepcopy(INFO_ARG_SPEC))
spec.update(deepcopy(AUTH_ARG_SPEC))
return spec
@@ -234,12 +192,17 @@ def main():
supports_check_mode=True,
)
try:
client = get_api_client(module)
svc = K8sService(client, module)
execute_module(module, svc)
except CoreException as exc:
module.fail_from_exception(exc)
# Set kind to query for VirtualMachines
kind = "VirtualMachine"
# Set wait_condition to allow waiting for the ready state of the
# VirtualMachine based on the running parameter.
if module.params["running"] is None or module.params["running"]:
wait_condition = {"type": "Ready", "status": True}
else:
wait_condition = {"type": "Ready", "status": False, "reason": "VMINotExists"}
execute_info_module(module, kind, wait_condition)
if __name__ == "__main__":

View File

@@ -15,6 +15,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import
from ansible_collections.kubevirt.core.plugins.modules import (
kubevirt_vm_info,
)
from ansible_collections.kubevirt.core.plugins.module_utils import (
info,
)
from ansible_collections.kubevirt.core.tests.unit.utils.ansible_module_mock import (
AnsibleExitJson,
AnsibleFailJson,
@@ -81,7 +84,7 @@ FIND_ARGS_STOPPED = FIND_ARGS_DEFAULT | {
)
def test_module(mocker, module_args, find_args):
mocker.patch.object(AnsibleModule, "exit_json", exit_json)
mocker.patch.object(kubevirt_vm_info, "get_api_client")
mocker.patch.object(info, "get_api_client")
find = mocker.patch.object(
K8sService,