mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-07-28 18:34:42 +00:00
Refactor k8s_service to use new module_utils code (#327)
Refactor k8s_service to use new module_utils code SUMMARY Refactor k8s_service to use new module_utils code ISSUE TYPE Feature Pull Request COMPONENT NAME k8s_service Reviewed-by: Mike Graves <mgraves@redhat.com> Reviewed-by: Alina Buzachis <None> Reviewed-by: None <None>
This commit is contained in:
committed by
Mike Graves
parent
61faa1079e
commit
349e9f473a
@@ -154,6 +154,19 @@ from ansible_collections.kubernetes.core.plugins.module_utils.args_common import
|
|||||||
COMMON_ARG_SPEC,
|
COMMON_ARG_SPEC,
|
||||||
RESOURCE_ARG_SPEC,
|
RESOURCE_ARG_SPEC,
|
||||||
)
|
)
|
||||||
|
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.kubernetes.core.plugins.module_utils.k8s.service import (
|
||||||
|
K8sService,
|
||||||
|
)
|
||||||
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import (
|
||||||
|
create_definitions,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
SERVICE_ARG_SPEC = {
|
SERVICE_ARG_SPEC = {
|
||||||
"apply": {"type": "bool", "default": False},
|
"apply": {"type": "bool", "default": False},
|
||||||
@@ -195,10 +208,35 @@ def argspec():
|
|||||||
return argument_spec
|
return argument_spec
|
||||||
|
|
||||||
|
|
||||||
def execute_module(module, k8s_ansible_mixin):
|
def perform_action(svc, resource, definition, params):
|
||||||
"""Module execution"""
|
state = params.get("state", None)
|
||||||
k8s_ansible_mixin.set_resource_definitions(module)
|
result = {}
|
||||||
|
|
||||||
|
existing = svc.retrieve(resource, definition)
|
||||||
|
|
||||||
|
if state == "absent":
|
||||||
|
result = svc.delete(resource, definition, existing)
|
||||||
|
result["method"] = "delete"
|
||||||
|
else:
|
||||||
|
if params.get("apply"):
|
||||||
|
result = svc.apply(resource, definition, existing)
|
||||||
|
result["method"] = "apply"
|
||||||
|
elif not existing:
|
||||||
|
result = svc.create(resource, definition)
|
||||||
|
result["method"] = "create"
|
||||||
|
elif params.get("force", False):
|
||||||
|
result = svc.replace(resource, definition, existing)
|
||||||
|
result["method"] = "replace"
|
||||||
|
else:
|
||||||
|
result = svc.update(resource, definition, existing)
|
||||||
|
result["method"] = "update"
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def execute_module(svc):
|
||||||
|
""" Module execution """
|
||||||
|
module = svc.module
|
||||||
api_version = "v1"
|
api_version = "v1"
|
||||||
selector = module.params.get("selector")
|
selector = module.params.get("selector")
|
||||||
service_type = module.params.get("type")
|
service_type = module.params.get("type")
|
||||||
@@ -218,28 +256,25 @@ def execute_module(module, k8s_ansible_mixin):
|
|||||||
def_meta["name"] = module.params.get("name")
|
def_meta["name"] = module.params.get("name")
|
||||||
def_meta["namespace"] = module.params.get("namespace")
|
def_meta["namespace"] = module.params.get("namespace")
|
||||||
|
|
||||||
# 'resource_definition:' has lower priority than module parameters
|
definitions = create_definitions(module.params)
|
||||||
definition = dict(
|
|
||||||
merge_dicts(k8s_ansible_mixin.resource_definitions[0], definition)
|
|
||||||
)
|
|
||||||
|
|
||||||
resource = k8s_ansible_mixin.find_resource("Service", api_version, fail=True)
|
# 'resource_definition:' has lower priority than module parameters
|
||||||
definition = k8s_ansible_mixin.set_defaults(resource, definition)
|
definition = dict(merge_dicts(definitions[0], definition))
|
||||||
result = k8s_ansible_mixin.perform_action(resource, definition)
|
resource = svc.find_resource("Service", api_version, fail=True)
|
||||||
|
|
||||||
|
result = perform_action(svc, resource, definition, module.params)
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
module = AnsibleModule(argument_spec=argspec(), supports_check_mode=True)
|
module = AnsibleK8SModule(
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
|
module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True,
|
||||||
K8sAnsibleMixin,
|
|
||||||
get_api_client,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
k8s_ansible_mixin = K8sAnsibleMixin(module)
|
client = get_api_client(module=module)
|
||||||
k8s_ansible_mixin.client = get_api_client(module=module)
|
svc = K8sService(client, module)
|
||||||
execute_module(module, k8s_ansible_mixin)
|
execute_module(svc)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user