From b3178ba8400d4114a72134ec714b6f7f95914a55 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Tue, 20 Aug 2024 15:48:47 +0200 Subject: [PATCH] Cleanups Signed-off-by: Alina Buzachis --- plugins/module_utils/common.py | 32 +++++-- plugins/module_utils/controller.py | 88 ++++++++------------ plugins/modules/activation.py | 47 +++++------ plugins/modules/activation_info.py | 5 +- plugins/modules/controller_token.py | 2 +- plugins/modules/credential.py | 74 ++++++++-------- plugins/modules/credential_info.py | 3 +- plugins/modules/credential_type.py | 2 +- plugins/modules/credential_type_info.py | 3 +- plugins/modules/decision_environment.py | 22 +++-- plugins/modules/decision_environment_info.py | 3 +- plugins/modules/project.py | 25 ++---- plugins/modules/project_info.py | 3 +- plugins/modules/user.py | 2 +- 14 files changed, 147 insertions(+), 164 deletions(-) diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index f4263052..ad241b8f 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -5,16 +5,30 @@ from __future__ import absolute_import, division, print_function -from typing import Any, Optional, Union - __metaclass__ = type -def to_list_of_dict( - result: Optional[Union[dict[str, bool], list[dict[Any, Any]]]] -) -> list[dict]: - if result is None: - return [] - if not isinstance(result, list): - return [result] +from typing import Any, Optional + +from ansible.module_utils.basic import AnsibleModule + +from ..module_utils.controller import Controller +from ..module_utils.errors import EDAError + + +def lookup_resource_id( + module: AnsibleModule, + controller: Controller, + endpoint: str, + name: str, + params: Optional[dict[str, Any]] = None, +): + result = None + + try: + result = controller.resolve_name_to_id( + endpoint, name, **params if params is not None else {} + ) + except EDAError as e: + module.fail_json(msg=f"Failed to lookup resource: {e}") return result diff --git a/plugins/module_utils/controller.py b/plugins/module_utils/controller.py index a96a3b8a..8e0026db 100644 --- a/plugins/module_utils/controller.py +++ b/plugins/module_utils/controller.py @@ -4,14 +4,14 @@ # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) from __future__ import absolute_import, annotations, division, print_function -from typing import Any, List, NoReturn, Optional, Union +__metaclass__ = type -from ansible.module_utils.basic import AnsibleModule -from .client import Client +from typing import Any, List, NoReturn, Optional -__metaclass__ = type +from ansible.module_utils.basic import AnsibleModule +from .client import Client from .errors import EDAError @@ -76,43 +76,49 @@ def get_item_name(self, item): raise EDAError(msg) def fail_wanted_one(self, response, endpoint, query_params) -> NoReturn: - sample = response.json.copy() - if len(sample["results"]) > 1: - sample["results"] = sample["results"][:2] + ["...more results snipped..."] url = self.client.build_url(endpoint, query_params) host_length = len(self.client.host) display_endpoint = url.geturl()[ host_length: ] # truncate to not include the base URL - msg = f"Request to {display_endpoint} returned {response.json['count']} items, expected 1" + msg = ( + f"Request to {display_endpoint} returned {len(response)} items, expected 1" + ) raise EDAError(msg) - def get_exactly_one( - self, endpoint, name=None, **kwargs - ) -> Optional[dict[str, bool]]: - result = self.get_one_or_many( - endpoint, name=name, allow_none=True, want_one=True, **kwargs - ) - # typing: needed as get_one_or_many can also return lists, not expected - # to reach this ever. - if isinstance(result, list): # pragma: no cover - self.fail_wanted_one(result, endpoint, {}) - return result + def get_exactly_one(self, endpoint, name=None, **kwargs) -> dict[str, bool]: + new_kwargs = kwargs.copy() + + result = self.get_one_or_many(endpoint, name=name, **kwargs) + + if len(result) == 0: + return {} + + if len(result) > 1: + if name: + # Since we did a name or ID search and got > 1 return + # something if the id matches + for asset in result: + if str(asset["id"]) == name: + return asset + # We got > 1 and either didn't find something by ID (which means + # multiple names) + # Or we weren't running with a or search and just got back too + # many to begin with. + self.fail_wanted_one(result, endpoint, new_kwargs.get("data")) - def resolve_name_to_id(self, endpoint, name): - return self.get_exactly_one(endpoint, name)["id"] + return result[0] + + def resolve_name_to_id(self, endpoint, name, **kwargs): + return self.get_exactly_one(endpoint, name, **kwargs)["id"] def get_one_or_many( self, endpoint: str, name: Optional[str] = None, - allow_none: bool = True, - check_exists: bool = False, - want_one: bool = True, **kwargs: Any, - ) -> Union[None, dict[str, bool], List]: + ) -> List[Any]: new_kwargs = kwargs.copy() - response = None if name: name_field = self.get_name_field_from_endpoint(endpoint) @@ -133,31 +139,9 @@ def get_one_or_many( raise EDAError("The endpoint did not provide count, results") if response.json["count"] == 0: - if allow_none: - return None - self.fail_wanted_one(response, endpoint, new_kwargs.get("data")) - if response.json["count"] == 1: - return response.json["results"][0] - if response.json["count"] > 1: - if want_one: - if name: - # Since we did a name or ID search and got > 1 return - # something if the id matches - for asset in response.json["results"]: - if str(asset["id"]) == name: - return asset - # We got > 1 and either didn't find something by ID (which means - # multiple names) - # Or we weren't running with a or search and just got back too - # many to begin with. - self.fail_wanted_one(response, endpoint, new_kwargs.get("data")) - else: - return response.json["results"] + return [] - if check_exists: - self.result["id"] = response.json["results"][0]["id"] - return self.result - return None + return response.json["results"] def create_if_needed( self, @@ -304,7 +288,7 @@ def update_if_needed( # to you to process the response and exit from the module. # Note: common error codes from the EDA API can cause the module to fail response = None - if existing_item is None: + if not existing_item: raise RuntimeError( "update_if_needed called incorrectly without existing_item" ) @@ -387,7 +371,7 @@ def create_or_update_if_needed( ) def delete_if_needed(self, existing_item, endpoint, on_delete=None): - if existing_item is None: + if not existing_item: return self.result # If we have an item, we can try to delete it diff --git a/plugins/modules/activation.py b/plugins/modules/activation.py index 6cfe0c05..7f815711 100644 --- a/plugins/modules/activation.py +++ b/plugins/modules/activation.py @@ -151,50 +151,43 @@ from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client +from ..module_utils.common import lookup_resource_id from ..module_utils.controller import Controller from ..module_utils.errors import EDAError -def lookup(module: AnsibleModule, controller: Controller, endpoint, name): - result = None - try: - result = controller.resolve_name_to_id(endpoint, name) - except EDAError as e: - module.fail_json(msg=f"Failed to lookup resource: {e}") - return result - - def create_params(module: AnsibleModule, controller: Controller) -> dict[str, Any]: activation_params: dict[str, Any] = {} # Get the project id project_id = None if module.params.get("project_name"): - project_id = lookup( + project_id = lookup_resource_id( module, controller, "projects", module.params["project_name"] ) if project_id is not None: activation_params["project_id"] = project_id # Get the rulebook id - rulebook = None + rulebook_id = None params = {} if project_id is not None: params = {"data": {"project_id": project_id}} if module.params.get("rulebook_name"): - try: - rulebook = controller.get_exactly_one( - "rulebooks", name=module.params["rulebook_name"], **params - ) - except EDAError as e: - module.fail_json(msg=f"Failed to lookup rulebook: {e}") - if rulebook is not None: - activation_params["rulebook_id"] = rulebook["id"] + rulebook_id = lookup_resource_id( + module, + controller, + "rulebooks", + module.params["rulebook_name"], + params, + ) + if rulebook_id is not None: + activation_params["rulebook_id"] = rulebook_id # Get the decision environment id decision_environment_id = None if module.params.get("decision_environment_name"): - decision_environment_id = lookup( + decision_environment_id = lookup_resource_id( module, controller, "decision-environments", @@ -206,7 +199,7 @@ def create_params(module: AnsibleModule, controller: Controller) -> dict[str, An # Get the organization id organization_id = None if module.params.get("organization_name"): - organization_id = lookup( + organization_id = lookup_resource_id( module, controller, "organizations", module.params["organization_name"] ) if organization_id is not None: @@ -221,7 +214,7 @@ def create_params(module: AnsibleModule, controller: Controller) -> dict[str, An # Get the AWX token id awx_token_id = None if module.params.get("awx_token_name"): - awx_token_id = lookup( + awx_token_id = lookup_resource_id( module, controller, "/users/me/awx-tokens/", module.params["awx_token_name"] ) if awx_token_id is not None: @@ -241,7 +234,7 @@ def create_params(module: AnsibleModule, controller: Controller) -> dict[str, An if module.params.get("eda_credentials"): eda_credential_ids = [] for item in module.params["eda_credentials"]: - cred_id = lookup(module, controller, "eda-credentials", item) + cred_id = lookup_resource_id(module, controller, "eda-credentials", item) if cred_id is not None: eda_credential_ids.append(cred_id) @@ -256,7 +249,7 @@ def create_params(module: AnsibleModule, controller: Controller) -> dict[str, An if module.params.get("webhooks"): webhooks_ids = [] for item in module.params["webhooks"]: - webhook_id = lookup(module, controller, "webhooks", item) + webhook_id = lookup_resource_id(module, controller, "webhooks", item) if webhook_id is not None: webhooks_ids.append(webhook_id) if webhooks_ids is not None: @@ -303,7 +296,11 @@ def main() -> None: argument_spec.update(AUTH_ARGSPEC) required_if = [ - ("state", "present", ("name", "rulebook_name", "decision_environment_name", "organization_name")) + ( + "state", + "present", + ("name", "rulebook_name", "decision_environment_name", "organization_name"), + ) ] module = AnsibleModule( diff --git a/plugins/modules/activation_info.py b/plugins/modules/activation_info.py index d3b4493c..d440dd0d 100644 --- a/plugins/modules/activation_info.py +++ b/plugins/modules/activation_info.py @@ -82,7 +82,6 @@ from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client -from ..module_utils.common import to_list_of_dict from ..module_utils.controller import Controller from ..module_utils.errors import EDAError @@ -109,11 +108,11 @@ def main() -> None: # Attempt to look up credential based on the provided name try: - result = controller.get_one_or_many("activations", name=name, want_one=False) + result = controller.get_one_or_many("activations", name=name) except EDAError as e: module.fail_json(msg=f"Failed to get rulebook activations: {e}") - module.exit_json(activations=to_list_of_dict(result)) + module.exit_json(activations=result) if __name__ == "__main__": diff --git a/plugins/modules/controller_token.py b/plugins/modules/controller_token.py index 729a2022..caaae635 100644 --- a/plugins/modules/controller_token.py +++ b/plugins/modules/controller_token.py @@ -119,7 +119,7 @@ def main() -> None: ret = {} try: - token_type = controller.get_one_or_many(token_endpoint, name=token_name) + token_type = controller.get_exactly_one(token_endpoint, name=token_name) except EDAError as eda_err: module.fail_json(msg=str(eda_err)) diff --git a/plugins/modules/credential.py b/plugins/modules/credential.py index fc1ddc58..58ef47aa 100644 --- a/plugins/modules/credential.py +++ b/plugins/modules/credential.py @@ -85,21 +85,49 @@ """ +from typing import Any + from ansible.module_utils.basic import AnsibleModule from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client +from ..module_utils.common import lookup_resource_id from ..module_utils.controller import Controller from ..module_utils.errors import EDAError -def lookup(module, controller, endpoint, name): - result = None - try: - result = controller.resolve_name_to_id(endpoint, name) - except EDAError as e: - module.fail_json(msg=f"Failed to lookup resource: {e}") - return result +def create_params(module: AnsibleModule, controller: Controller) -> dict[str, Any]: + credential_params: dict[str, Any] = {} + + credential_params = {} + if module.params.get("description"): + credential_params["description"] = module.params["description"] + + if module.params.get("inputs"): + credential_params["inputs"] = module.params["inputs"] + + credential_type_id = None + if module.params.get("credential_type_name"): + credential_type_id = lookup_resource_id( + module, + controller, + "credential-types", + module.params["credential_type_name"], + ) + + if credential_type_id: + credential_params["credential_type_id"] = credential_type_id + + organization_id = None + if module.params.get("organization_name"): + organization_id = lookup_resource_id( + module, controller, "organizations", module.params["organization_name"] + ) + + if organization_id: + credential_params["organization_id"] = organization_id + + return credential_params def main() -> None: @@ -141,37 +169,9 @@ def main() -> None: new_name = module.params.get("new_name") state = module.params.get("state") - credential_params = {} - if module.params.get("description"): - credential_params["description"] = module.params["description"] - - if module.params.get("inputs"): - credential_params["inputs"] = module.params["inputs"] - - credential_type_id = None - if module.params.get("credential_type_name"): - credential_type_id = lookup( - module, - controller, - "credential-types", - module.params["credential_type_name"], - ) - - if credential_type_id: - credential_params["credential_type_id"] = credential_type_id - - organization_id = None - if module.params.get("organization_name"): - organization_id = lookup( - module, controller, "organizations", module.params["organization_name"] - ) - - if organization_id: - credential_params["organization_id"] = organization_id - # Attempt to look up credential based on the provided name try: - credential = controller.get_one_or_many("eda-credentials", name=name) + credential = controller.get_exactly_one("eda-credentials", name=name) except EDAError as e: module.fail_json(msg=f"Failed to get credential: {e}") @@ -183,6 +183,8 @@ def main() -> None: except EDAError as e: module.fail_json(msg=f"Failed to delete credential: {e}") + # Activation Data that will be sent for create/update + credential_params = create_params(module, controller) credential_params["name"] = ( new_name if new_name diff --git a/plugins/modules/credential_info.py b/plugins/modules/credential_info.py index b5c86638..2a2e7c74 100644 --- a/plugins/modules/credential_info.py +++ b/plugins/modules/credential_info.py @@ -78,7 +78,6 @@ from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client -from ..module_utils.common import to_list_of_dict from ..module_utils.controller import Controller from ..module_utils.errors import EDAError @@ -111,7 +110,7 @@ def main() -> None: except EDAError as e: module.fail_json(msg=f"Failed to get credential: {e}") - module.exit_json(credentials=to_list_of_dict(result)) + module.exit_json(credentials=result) if __name__ == "__main__": diff --git a/plugins/modules/credential_type.py b/plugins/modules/credential_type.py index 28bbd27d..33737f28 100644 --- a/plugins/modules/credential_type.py +++ b/plugins/modules/credential_type.py @@ -135,7 +135,7 @@ def main() -> None: # Attempt to look up credential_type based on the provided name try: - credential_type = controller.get_one_or_many("credential-types", name=name) + credential_type = controller.get_exactly_one("credential-types", name=name) except EDAError as e: module.fail_json(msg=f"Failed to get credential type: {e}") diff --git a/plugins/modules/credential_type_info.py b/plugins/modules/credential_type_info.py index e8f744a0..1bf1290a 100644 --- a/plugins/modules/credential_type_info.py +++ b/plugins/modules/credential_type_info.py @@ -76,7 +76,6 @@ from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client -from ..module_utils.common import to_list_of_dict from ..module_utils.controller import Controller from ..module_utils.errors import EDAError @@ -109,7 +108,7 @@ def main() -> None: except EDAError as e: module.fail_json(msg=f"Failed to get credential type: {e}") - module.exit_json(credential_types=to_list_of_dict(result)) + module.exit_json(credential_types=result) if __name__ == "__main__": diff --git a/plugins/modules/decision_environment.py b/plugins/modules/decision_environment.py index 6166f399..33f6e9f3 100644 --- a/plugins/modules/decision_environment.py +++ b/plugins/modules/decision_environment.py @@ -100,6 +100,7 @@ from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client +from ..module_utils.common import lookup_resource_id from ..module_utils.controller import Controller from ..module_utils.errors import EDAError @@ -143,7 +144,7 @@ def main() -> None: ret = {} try: - decision_environment_type = controller.get_one_or_many( + decision_environment_type = controller.get_exactly_one( decision_environment_endpoint, name=decision_environment_name ) except EDAError as eda_err: @@ -165,24 +166,21 @@ def main() -> None: if image_url: decision_environment_type_params["image_url"] = image_url - credential_type = None + credential_type_id = None if credential: - try: - credential_type = controller.get_exactly_one( - "eda-credentials", name=credential - ) - except EDAError as eda_err: - module.fail_json(msg=str(eda_err)) + credential_type_id = lookup_resource_id( + module, controller, "eda-credentials", name=credential + ) - if credential_type is not None: + if credential_type_id is not None: # this is resolved earlier, so save an API call and don't do it again # in the loop above - decision_environment_type_params["credential"] = credential_type["id"] + decision_environment_type_params["credential"] = credential_type_id organization_id = None if module.params.get("organization_name"): - organization_id = controller.resolve_name_to_id( - "organizations", module.params["organization_name"] + organization_id = lookup_resource_id( + module, controller, "organizations", module.params["organization_name"] ) if organization_id: diff --git a/plugins/modules/decision_environment_info.py b/plugins/modules/decision_environment_info.py index 5c380ce2..5b26bb7c 100644 --- a/plugins/modules/decision_environment_info.py +++ b/plugins/modules/decision_environment_info.py @@ -66,7 +66,6 @@ from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client -from ..module_utils.common import to_list_of_dict from ..module_utils.controller import Controller from ..module_utils.errors import EDAError @@ -103,7 +102,7 @@ def main() -> None: module.fail_json(msg=str(eda_err)) raise # https://github.com/ansible/ansible/pull/83814 - module.exit_json(decision_environments=to_list_of_dict(ret)) + module.exit_json(decision_environments=ret) if __name__ == "__main__": diff --git a/plugins/modules/project.py b/plugins/modules/project.py index 9b1a7a77..0c984b51 100644 --- a/plugins/modules/project.py +++ b/plugins/modules/project.py @@ -95,6 +95,7 @@ from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client +from ..module_utils.common import lookup_resource_id from ..module_utils.controller import Controller from ..module_utils.errors import EDAError @@ -138,7 +139,7 @@ def main() -> None: ret = {} try: - project_type = controller.get_one_or_many(project_endpoint, name=project_name) + project_type = controller.get_exactly_one(project_endpoint, name=project_name) except EDAError as eda_err: module.fail_json(msg=str(eda_err)) @@ -158,28 +159,20 @@ def main() -> None: credential_id = None if credential: - try: - credential_id = controller.get_exactly_one( - "eda-credentials", name=credential - ) - except EDAError as eda_err: - module.fail_json(msg=str(eda_err)) - raise # https://github.com/ansible/ansible/pull/83814 + credential_id = lookup_resource_id( + module, controller, "eda-credentials", name=credential + ) if credential_id is not None: # this is resolved earlier, so save an API call and don't do it again # in the loop above - project_type_params["eda_credential_id"] = credential_id["id"] + project_type_params["eda_credential_id"] = credential_id organization_id = None if module.params.get("organization_name"): - try: - organization_id = controller.resolve_name_to_id( - "organizations", module.params["organization_name"] - ) - except EDAError as eda_err: - module.fail_json(msg=str(eda_err)) - raise + organization_id = lookup_resource_id( + module, controller, "organizations", module.params["organization_name"] + ) if organization_id: project_type_params["organization_id"] = organization_id diff --git a/plugins/modules/project_info.py b/plugins/modules/project_info.py index 983d9d05..5455351d 100644 --- a/plugins/modules/project_info.py +++ b/plugins/modules/project_info.py @@ -80,7 +80,6 @@ # pylint: disable=relative-beyond-top-level from ..module_utils.arguments import AUTH_ARGSPEC from ..module_utils.client import Client -from ..module_utils.common import to_list_of_dict from ..module_utils.controller import Controller from ..module_utils.errors import EDAError @@ -114,7 +113,7 @@ def main() -> None: except EDAError as eda_err: module.fail_json(msg=str(eda_err)) - module.exit_json(projects=to_list_of_dict(result)) + module.exit_json(projects=result) if __name__ == "__main__": diff --git a/plugins/modules/user.py b/plugins/modules/user.py index 9e077e16..0b63c65f 100644 --- a/plugins/modules/user.py +++ b/plugins/modules/user.py @@ -155,7 +155,7 @@ def main(): ret = {} try: - user_type = controller.get_one_or_many(user_endpoint, name=username) + user_type = controller.get_exactly_one(user_endpoint, name=username) except EDAError as eda_err: module.fail_json(msg=str(eda_err))