Skip to content

Commit

Permalink
Refactor k8s_service to use new module_utils code (ansible-collection…
Browse files Browse the repository at this point in the history
…s#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 <[email protected]>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
  • Loading branch information
alinabuzachis authored and gravesm committed May 24, 2022
1 parent 61faa10 commit 349e9f4
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions plugins/modules/k8s_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@
COMMON_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 = {
"apply": {"type": "bool", "default": False},
Expand Down Expand Up @@ -195,10 +208,35 @@ def argspec():
return argument_spec


def execute_module(module, k8s_ansible_mixin):
"""Module execution"""
k8s_ansible_mixin.set_resource_definitions(module)
def perform_action(svc, resource, definition, params):
state = params.get("state", None)
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"
selector = module.params.get("selector")
service_type = module.params.get("type")
Expand All @@ -218,28 +256,25 @@ def execute_module(module, k8s_ansible_mixin):
def_meta["name"] = module.params.get("name")
def_meta["namespace"] = module.params.get("namespace")

definitions = create_definitions(module.params)

# 'resource_definition:' has lower priority than module parameters
definition = dict(
merge_dicts(k8s_ansible_mixin.resource_definitions[0], definition)
)
definition = dict(merge_dicts(definitions[0], definition))
resource = svc.find_resource("Service", api_version, fail=True)

resource = k8s_ansible_mixin.find_resource("Service", api_version, fail=True)
definition = k8s_ansible_mixin.set_defaults(resource, definition)
result = k8s_ansible_mixin.perform_action(resource, definition)
result = perform_action(svc, resource, definition, module.params)

module.exit_json(**result)


def main():
module = AnsibleModule(argument_spec=argspec(), supports_check_mode=True)
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
K8sAnsibleMixin,
get_api_client,
module = AnsibleK8SModule(
module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True,
)

k8s_ansible_mixin = K8sAnsibleMixin(module)
k8s_ansible_mixin.client = get_api_client(module=module)
execute_module(module, k8s_ansible_mixin)
client = get_api_client(module=module)
svc = K8sService(client, module)
execute_module(svc)


if __name__ == "__main__":
Expand Down

0 comments on commit 349e9f4

Please sign in to comment.