diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index 56020832..3ee98e53 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -22,7 +22,7 @@ def lookup_resource_id( endpoint: str, name: str, params: Optional[dict[str, Any]] = None, -) -> Optional[Any]: +) -> Optional[int]: result = None try: diff --git a/plugins/modules/rulebook_activation.py b/plugins/modules/rulebook_activation.py index 66946243..9212ac8e 100644 --- a/plugins/modules/rulebook_activation.py +++ b/plugins/modules/rulebook_activation.py @@ -31,12 +31,14 @@ project_name: description: - The name of the project associated with the rulebook activation. + - Required when state is present. type: str aliases: - project rulebook_name: description: - The name of the rulebook associated with the rulebook activation. + - Required when state is present. type: str aliases: - rulebook @@ -58,6 +60,7 @@ decision_environment_name: description: - The name of the decision environment associated with the rulebook activation. + - Required when state is present. type: str aliases: - decision_environment @@ -70,7 +73,7 @@ - token organization_name: description: - - The name of the organization. + - The name of the organization. Required when state is present. - This parameter is supported in AAP 2.5 and onwards. If specified for AAP 2.4, value will be ignored. type: str @@ -316,58 +319,54 @@ def create_params( ) -> Dict[str, Any]: activation_params: Dict[str, Any] = {} - # Get the project id - project_id = None - if module.params.get("project_name"): - 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 project id, only required to get the rulebook id + project_name = module.params["project_name"] + project_id = lookup_resource_id(module, controller, "projects", project_name) + + if project_id is None: + module.fail_json(msg=f"Project {project_name} not found.") # Get the rulebook id - rulebook_id = None - params = {} - if project_id is not None: - params = {"data": {"project_id": project_id}} - if module.params.get("rulebook_name"): - rulebook_id = lookup_resource_id( - module, - controller, - "rulebooks", - module.params["rulebook_name"], - params, + rulebook_name = module.params["rulebook_name"] + params = {"data": {"project_id": project_id}} + rulebook_id = lookup_resource_id( + module, + controller, + "rulebooks", + rulebook_name, + params, + ) + if rulebook_id is None: + module.fail_json( + msg=f"Rulebook {rulebook_name} not found for project {project_name}." ) - if rulebook_id is not None: - activation_params["rulebook_id"] = rulebook_id + + 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_resource_id( - module, - controller, - "decision-environments", - module.params["decision_environment_name"], + decision_environment_name = module.params["decision_environment_name"] + decision_environment_id = lookup_resource_id( + module, + controller, + "decision-environments", + decision_environment_name, + ) + if decision_environment_id is None: + module.fail_json( + msg=f"Decision Environment {decision_environment_name} not found." ) - if decision_environment_id is not None: - activation_params["decision_environment_id"] = decision_environment_id + activation_params["decision_environment_id"] = decision_environment_id # Get the organization id - organization_id = None - if not is_aap_24 and module.params.get("organization_name"): + organization_name = module.params["organization_name"] + if not is_aap_24: organization_id = lookup_resource_id( - module, controller, "organizations", module.params["organization_name"] + module, controller, "organizations", organization_name ) - if organization_id is not None: + if organization_id is None: + module.fail_json(msg=f"Organization {organization_name} not found.") activation_params["organization_id"] = organization_id - if module.params.get("description"): - activation_params["description"] = module.params["description"] - - if module.params.get("extra_vars"): - activation_params["extra_var"] = module.params["extra_vars"] - # Get the AWX token id awx_token_id = None if module.params.get("awx_token_name"): @@ -377,12 +376,6 @@ def create_params( if awx_token_id is not None: activation_params["awx_token_id"] = awx_token_id - if module.params.get("restart_policy"): - activation_params["restart_policy"] = module.params["restart_policy"] - - if module.params.get("enabled"): - activation_params["is_enabled"] = module.params["enabled"] - # Get the eda credential ids eda_credential_ids = None if not is_aap_24 and module.params.get("eda_credentials"): @@ -402,12 +395,26 @@ def create_params( # Process event streams and source mappings activation_params["source_mappings"] = yaml.dump( process_event_streams( - rulebook_id=rulebook_id, + # ignore type error, if rulebook_id is None, it will fail earlier + rulebook_id=rulebook_id, # type: ignore[arg-type] controller=controller, module=module, ) ) + # Set the remaining parameters + if module.params.get("description"): + activation_params["description"] = module.params["description"] + + if module.params.get("extra_vars"): + activation_params["extra_var"] = module.params["extra_vars"] + + if module.params.get("restart_policy"): + activation_params["restart_policy"] = module.params["restart_policy"] + + if module.params.get("enabled"): + activation_params["is_enabled"] = module.params["enabled"] + if not is_aap_24 and module.params.get("log_level"): activation_params["log_level"] = module.params["log_level"] @@ -492,9 +499,13 @@ def main() -> None: organization_name = module.params.get("organization_name") if state == "present" and not is_aap_24 and organization_name is None: module.fail_json( - msg="Parameter organization_name is required when state is present." + msg=( + "Parameter organization_name is required when state " + "is present for this version of EDA." + ), ) # Attempt to find rulebook activation based on the provided name + activation = {} try: activation = controller.get_exactly_one("activations", name=name) except EDAError as e: @@ -511,14 +522,13 @@ def main() -> None: module.exit_json( msg=f"A rulebook activation with name: {name} already exists. " "The module does not support modifying a rulebook activation.", - **{"changed": False, "id": activation["id"]}, + changed=False, + id=activation["id"], ) # Activation Data that will be sent for create/update activation_params = create_params(module, controller, is_aap_24=is_aap_24) - activation_params["name"] = ( - controller.get_item_name(activation) if activation else name - ) + activation_params["name"] = name # If the state was present and we can let the module build or update the # existing activation, this will return on its own diff --git a/tests/integration/targets/activation/tasks/main.yml b/tests/integration/targets/activation/tasks/main.yml index a65cf01d..6c94e461 100644 --- a/tests/integration/targets/activation/tasks/main.yml +++ b/tests/integration/targets/activation/tasks/main.yml @@ -172,6 +172,82 @@ that: - _result.changed + - name: Create a rulebook activation with missing project name + ansible.eda.rulebook_activation: + name: Invalid_activation + description: "Example Activation description" + rulebook_name: "{{ rulebook_name }}" + decision_environment_name: "{{ decision_env_name }}" + enabled: False + awx_token_name: "{{ awx_token_name }}" + organization_name: Default + project_name: Invalid_Project + register: _result + ignore_errors: true + + - name: Check rulebook activation creation + assert: + that: + - _result.failed + - "'Project Invalid_Project not found.' in _result.msg" + + - name: Create a rulebook activation with missing rulebook name + ansible.eda.rulebook_activation: + name: Invalid_activation + description: "Example Activation description" + project_name: "{{ project_name }}" + decision_environment_name: "{{ decision_env_name }}" + enabled: False + awx_token_name: "{{ awx_token_name }}" + organization_name: Default + rulebook_name: Invalid_Rulebook + register: _result + ignore_errors: true + + - name: Check rulebook activation creation + assert: + that: + - _result.failed + - "'Rulebook Invalid_Rulebook not found' in _result.msg" + + - name: Create a rulebook activation with missing decision environment name + ansible.eda.rulebook_activation: + name: Invalid_activation + description: "Example Activation description" + project_name: "{{ project_name }}" + rulebook_name: "{{ rulebook_name }}" + enabled: False + awx_token_name: "{{ awx_token_name }}" + organization_name: Default + decision_environment_name: Invalid_Decision_Env + register: _result + ignore_errors: true + + - name: Check rulebook activation creation + assert: + that: + - _result.failed + - "'Decision Environment Invalid_Decision_Env not found.' in _result.msg" + + - name: Create a rulebook activation with missing organization name + ansible.eda.rulebook_activation: + name: Invalid_activation + description: "Example Activation description" + project_name: "{{ project_name }}" + rulebook_name: "{{ rulebook_name }}" + decision_environment_name: "{{ decision_env_name }}" + enabled: False + awx_token_name: "{{ awx_token_name }}" + organization_name: Invalid_Organization + register: _result + ignore_errors: true + + - name: Check rulebook activation creation + assert: + that: + - _result.failed + - "'Organization Invalid_Organization not found.' in _result.msg" + - name: Create a new rulebook activation again ansible.eda.rulebook_activation: name: "{{ activation_name }}" @@ -379,6 +455,7 @@ - "{{ activation_name_source_name }}" - "{{ activation_name_source_index }}" - "{{ activation_name_wrong_source }}" + - Invalid_activation ignore_errors: true - name: Delete decision environment