Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Aug 16, 2024
1 parent 468fb54 commit 2a63d97
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 65 deletions.
2 changes: 1 addition & 1 deletion plugins/module_utils/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 18 additions & 12 deletions plugins/modules/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down
147 changes: 95 additions & 52 deletions tests/integration/targets/activation/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 2a63d97

Please sign in to comment.