mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-07-29 02:44:35 +00:00
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:
50
plugins/module_utils/info.py
Normal file
50
plugins/module_utils/info.py
Normal 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)
|
||||||
@@ -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 (
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
|
||||||
AnsibleK8SModule,
|
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():
|
def arg_spec():
|
||||||
@@ -208,16 +173,9 @@ def arg_spec():
|
|||||||
arg_spec defines the argument spec of this module.
|
arg_spec defines the argument spec of this module.
|
||||||
"""
|
"""
|
||||||
spec = {
|
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"},
|
"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))
|
spec.update(deepcopy(AUTH_ARG_SPEC))
|
||||||
|
|
||||||
return spec
|
return spec
|
||||||
@@ -234,12 +192,17 @@ def main():
|
|||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
# Set kind to query for VirtualMachines
|
||||||
client = get_api_client(module)
|
kind = "VirtualMachine"
|
||||||
svc = K8sService(client, module)
|
|
||||||
execute_module(module, svc)
|
# Set wait_condition to allow waiting for the ready state of the
|
||||||
except CoreException as exc:
|
# VirtualMachine based on the running parameter.
|
||||||
module.fail_from_exception(exc)
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import
|
|||||||
from ansible_collections.kubevirt.core.plugins.modules import (
|
from ansible_collections.kubevirt.core.plugins.modules import (
|
||||||
kubevirt_vm_info,
|
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 (
|
from ansible_collections.kubevirt.core.tests.unit.utils.ansible_module_mock import (
|
||||||
AnsibleExitJson,
|
AnsibleExitJson,
|
||||||
AnsibleFailJson,
|
AnsibleFailJson,
|
||||||
@@ -81,7 +84,7 @@ FIND_ARGS_STOPPED = FIND_ARGS_DEFAULT | {
|
|||||||
)
|
)
|
||||||
def test_module(mocker, module_args, find_args):
|
def test_module(mocker, module_args, find_args):
|
||||||
mocker.patch.object(AnsibleModule, "exit_json", exit_json)
|
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(
|
find = mocker.patch.object(
|
||||||
K8sService,
|
K8sService,
|
||||||
|
|||||||
Reference in New Issue
Block a user