---
- name: Generate a random string for test
  set_fact:
    test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
  when: test_id is not defined

- name: Generate names
  set_fact:
    src_cred_name: "AWX-Collection-tests-tower_credential_input_source-src_cred-{{ test_id }}"
    target_cred_name: "AWX-Collection-tests-tower_credential_input_source-target_cred-{{ test_id }}"

- block:
    - name: Add Tower credential Lookup
      tower_credential:
        description: Credential for Testing Source
        name: "{{ src_cred_name }}"
        credential_type: CyberArk AIM Central Credential Provider Lookup
        inputs:
          url: "https://cyberark.example.com"
          app_id: "My-App-ID"
        organization: Default
      register: src_cred_result

    - assert:
        that:
          - "src_cred_result is changed"

    - name: Add Tower credential Target
      tower_credential:
        description: Credential for Testing Target
        name: "{{ target_cred_name }}"
        credential_type: Machine
        inputs:
          username: user
        organization: Default
      register: target_cred_result

    - assert:
        that:
          - "target_cred_result is changed"

    - name: Add credential Input Source
      tower_credential_input_source:
        input_field_name: password
        target_credential: "{{ target_cred_result.id }}"
        source_credential: "{{ src_cred_result.id }}"
        metadata:
          object_query: "Safe=MY_SAFE;Object=AWX-user"
          object_query_format: "Exact"
        state: present
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Add Second Tower credential Lookup
      tower_credential:
        description: Credential for Testing Source Change
        name: "{{ src_cred_name }}-2"
        credential_type: CyberArk AIM Central Credential Provider Lookup
        inputs:
          url: "https://cyberark-prod.example.com"
          app_id: "My-App-ID"
        organization: Default
      register: result

    - name: Change credential Input Source
      tower_credential_input_source:
        input_field_name: password
        target_credential: "{{ target_cred_name }}"
        source_credential: "{{ src_cred_name }}-2"
        state: present

    - assert:
        that:
          - "result is changed"

  always:
    - name: Remove a Tower credential source
      tower_credential_input_source:
        input_field_name: password
        target_credential: "{{ target_cred_name }}"
        state: absent
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Remove Tower credential Lookup
      tower_credential:
        name: "{{ src_cred_name }}"
        organization: Default
        credential_type: CyberArk AIM Central Credential Provider Lookup
        state: absent
      register: result

    - name: Remove Alt Tower credential Lookup
      tower_credential:
        name: "{{ src_cred_name }}-2"
        organization: Default
        credential_type: CyberArk AIM Central Credential Provider Lookup
        state: absent
      register: result

    - name: Remove Tower credential
      tower_credential:
        name: "{{ target_cred_name }}"
        organization: Default
        credential_type: Machine
        state: absent
      register: result
