From 2a63d97bca0a9f90a9a6fda695e1ae4612420a17 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Fri, 16 Aug 2024 15:23:03 +0200 Subject: [PATCH] Fixes Signed-off-by: Alina Buzachis --- plugins/module_utils/controller.py | 2 +- plugins/modules/activation.py | 30 ++-- .../targets/activation/tasks/main.yml | 147 +++++++++++------- 3 files changed, 114 insertions(+), 65 deletions(-) diff --git a/plugins/module_utils/controller.py b/plugins/module_utils/controller.py index 43e3f853..74dd39a0 100644 --- a/plugins/module_utils/controller.py +++ b/plugins/module_utils/controller.py @@ -84,7 +84,7 @@ def fail_wanted_one(self, response, endpoint, query_params): def get_exactly_one(self, endpoint, name=None, **kwargs): return self.get_one_or_many(endpoint, name=name, allow_none=False, **kwargs) - def resolve_name_to_id(self, endpoint, name): + def resolve_name_to_id(self, endpoint, name=None): return self.get_exactly_one(endpoint, name)["id"] def get_one_or_many( diff --git a/plugins/modules/activation.py b/plugins/modules/activation.py index 69160af2..8db51ef6 100644 --- a/plugins/modules/activation.py +++ b/plugins/modules/activation.py @@ -165,19 +165,25 @@ def create_params(module, controller): # Get the project id project_id = None - if module.params.get("project"): - project_id = lookup(module, controller, "projects", module.params["project"]) + if module.params.get("project_name"): + project_id = lookup( + module, controller, "projects", module.params["project_name"] + ) if project_id is not None: activation_params["project_id"] = project_id # Get the rulebook id - rulebook_id = None + rulebook = None + params = {"data": {"project_id": project_id}} if module.params.get("rulebook_name"): - rulebook_id = lookup( - module, controller, "rulebooks", module.params["rulebook_name"] - ) - if rulebook_id is not None: - activation_params["rulebook_id"] = rulebook_id + try: + rulebook = controller.get_one_or_many( + "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"] # Get the decision environment id decision_environment_id = None @@ -276,9 +282,7 @@ def main(): ], ), enabled=dict(type="bool", default=True), - decision_environment_name=dict( - type="str", aliases=["decision_environment"] - ), + decision_environment_name=dict(type="str", aliases=["decision_environment"]), awx_token_name=dict(type="str", aliases=["awx_token", "token"]), organization_name=dict(type="str", aliases=["organization"]), event_streams=dict(type="list", elements="int"), @@ -328,7 +332,9 @@ def main(): # Activation Data that will be sent for create/update activation_params = create_params(module, controller) - activation_params["name"] = module.get_item_name(activation) if activation else name + activation_params["name"] = ( + controller.get_item_name(activation) if activation else 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 5b325ded..a600b2d0 100644 --- a/tests/integration/targets/activation/tasks/main.yml +++ b/tests/integration/targets/activation/tasks/main.yml @@ -26,11 +26,20 @@ decision_env_name: "Test_Decision_Env_{{ test_id }}" activation_name: "Test_Activation_{{ test_id }}" project_name: "Test_Project_{{ test_id }}" - awx_token_name: "your_private_access_token_name" + awx_token_name: "Test_AWXToken_{{ test_id }}" + token_value: "your_private_access_token_name" image_url: "quay.io/ansible/awx:latest" - scm_url: "https://gitlab.com/ansible/awx.git" + scm_url: https://github.com/ansible/event-driven-ansible.git + rulebook_name: "demo_controller_rulebook.yml" + + - name: Create an AWX controller token + ansible.eda.controller_token: + <<: *credential_defaults + name: "{{ awx_token_name }}" + description: "A test AWX controller token description" + token: "{{ token_value }}" + state: present - # CREATE - name: Create a new credential type ansible.eda.credential_type: <<: *credential_defaults @@ -39,23 +48,57 @@ description: "A test credential type" inputs: fields: - - id: "field1" + - id: "username" + label: "Username" + type: "string" + - id: "password" + label: "Password" + type: "string" + secret: true + - id: "ssh_key_data" + label: "SSH Private Key" type: "string" - label: "Field 5" + multiline: true + secret: true + - id: "ssh_key_unlock" + label: "SSH Key Passphrase" + type: "string" + secret: true + - id: "authorize" + label: "Authorize with SSL Certificate" + type: "boolean" + - id: "authorize_password" + label: "SSL Certificate Passphrase" + type: "string" + secret": true injectors: extra_vars: - field1: "field1" + username: joe + password: secret register: credential_type_creation - - name: Create a new credential + - name: Create a tempdir for an SSH key + local_action: shell mktemp -d + register: tempdir + + - name: Generate a local SSH key + local_action: "shell ssh-keygen -b 2048 -t rsa -f {{ tempdir.stdout }}/id_rsa -q -N 'passphrase'" + + - name: Read the generated key + set_fact: + ssh_key_data: "{{ lookup('file', tempdir.stdout + '/id_rsa') }}" + + - name: Create a new SCM credential ansible.eda.credential: <<: *credential_defaults - state: present name: "{{ credential_name }}" - description: "This is a test credential" - credential_type_name: "{{ credential_type_name }}" + state: present + credential_type_name: Source Control inputs: - field1: "testuser" + username: joe + password: secret + ssh_key_data: "{{ ssh_key_data }}" + ssh_key_unlock: "passphrase" register: credential_creation - name: Assert that the credential was created successfully @@ -69,7 +112,7 @@ <<: *credential_defaults name: "{{ project_name }}" description: "Test Project Description" - url: "https://example.com/project1" + url: "{{ scm_url }}" credential: "{{ credential_name }}" state: present register: project_creation @@ -94,11 +137,11 @@ <<: *credential_defaults name: "{{ activation_name }}" description: "Example Activation description" - project: "{{ project_name }}" - rulebook_name: "basic_short.yml" + project_name: "{{ project_name }}" + rulebook_name: "{{ rulebook_name }}" decision_environment_name: "{{ decision_env_name }}" enabled: False - awx_token_id: "{{ awx_token_name }}" + awx_token_name: "{{ awx_token_name }}" check_mode: true register: _result @@ -112,11 +155,11 @@ <<: *credential_defaults name: "{{ activation_name }}" description: "Example Activation description" - project: "{{ project_name }}" - rulebook_name: "basic_short.yml" + project_name: "{{ project_name }}" + rulebook_name: "{{ rulebook_name }}" decision_environment_name: "{{ decision_env_name }}" enabled: False - awx_token_id: "{{ awx_token_name }}" + awx_token_name: "{{ awx_token_name }}" register: _result - name: Check rulebook activation creation @@ -129,11 +172,11 @@ <<: *credential_defaults name: "{{ activation_name }}" description: "Example Activation description" - project: "{{ project_name }}" - rulebook_name: "basic_short.yml" + project_name: "{{ project_name }}" + rulebook_name: "{{ rulebook_name }}" decision_environment_name: "{{ decision_env_name }}" enabled: False - awx_token_id: "{{ awx_token_name }}" + awx_token_name: "{{ awx_token_name }}" register: _result - name: Check rulebook activation creation @@ -182,38 +225,38 @@ name: "{{ activation_name }}" state: absent - always: - - name: Delete project - ansible.eda.project: - <<: *credential_defaults - name: "{{ project_name }}" - state: absent - ignore_errors: true + # always: + # - name: Delete project + # ansible.eda.project: + # <<: *credential_defaults + # name: "{{ project_name }}" + # state: absent + # ignore_errors: true - - name: Delete decision environment - ansible.eda.decision_environment: - <<: *credential_defaults - name: "{{ decision_env_name }}" - state: absent - ignore_errors: true + # - name: Delete decision environment + # ansible.eda.decision_environment: + # <<: *credential_defaults + # name: "{{ decision_env_name }}" + # state: absent + # ignore_errors: true - - name: Delete credential - ansible.eda.credential: - <<: *credential_defaults - name: "{{ credential_name }}" - state: absent - ignore_errors: true + # - name: Delete credential + # ansible.eda.credential: + # <<: *credential_defaults + # name: "{{ credential_name }}" + # state: absent + # ignore_errors: true - - name: Delete credential type - ansible.eda.credential_type: - <<: *credential_defaults - name: "{{ credential_type_name }}" - state: absent - ignore_errors: true + # - name: Delete credential type + # ansible.eda.credential_type: + # <<: *credential_defaults + # name: "{{ credential_type_name }}" + # state: absent + # ignore_errors: true - - name: Delete rulebook activation - ansible.eda.activation: - <<: *credential_defaults - name: "{{ activation_name }}" - state: absent - ignore_errors: true + # - name: Delete rulebook activation + # ansible.eda.activation: + # <<: *credential_defaults + # name: "{{ activation_name }}" + # state: absent + # ignore_errors: true