---
- name: Generate a test id
  set_fact:
    test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"

- name: Generate names
  set_fact:
    username: "AWX-Collection-tests-tower_role-user-{{ test_id }}"
    project_name: "AWX-Collection-tests-tower_role-project-1-{{ test_id }}"
    jt1: "AWX-Collection-tests-tower_role-jt1-{{ test_id }}"
    jt2: "AWX-Collection-tests-tower_role-jt2-{{ test_id }}"
    wfjt_name: "AWX-Collection-tests-tower_role-project-wfjt-{{ test_id }}"

- block:
    - name: Create a User
      tower_user:
        first_name: Joe
        last_name: User
        username: "{{ username }}"
        password: "{{ 65535 | random | to_uuid }}"
        email: joe@example.org
        state: present
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Create a project
      tower_project:
        name: "{{ project_name }}"
        organization: Default
        scm_type: git
        scm_url: https://github.com/ansible/test-playbooks
        wait: true
      register: project_info

    - assert:
        that:
          - project_info is changed

    - name: Create job templates
      tower_job_template:
        name: "{{ item }}"
        project: "{{ project_name }}"
        inventory: "Demo Inventory"
        playbook: become.yml
      with_items:
        - jt1
        - jt2
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Add Joe to the update role of the default Project with lookup Organization
      tower_role:
        user: "{{ username }}"
        role: update
        lookup_organization: Default
        project: "Demo Project"
        state: "{{ item }}"
      register: result
      with_items:
        - "present"
        - "absent"

    - assert:
        that:
          - "result is changed"

    - name: Add Joe to the new project by ID
      tower_role:
        user: "{{ username }}"
        role: update
        project: "{{ project_info['id'] }}"
        state: "{{ item }}"
      register: result
      with_items:
        - "present"
        - "absent"

    - assert:
        that:
          - "result is changed"

    - name: Create a workflow
      tower_workflow_job_template:
        name: test-role-workflow
        organization: Default
        state: present

    - name: Add Joe to workflow execute role
      tower_role:
        user: "{{ username }}"
        role: execute
        workflow: test-role-workflow
        job_templates:
          - jt1
          - jt2
        state: present
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Add Joe to workflow execute role, no-op
      tower_role:
        user: "{{ username }}"
        role: execute
        workflow: test-role-workflow
        state: present
      register: result

    - assert:
        that:
          - "result is not changed"

    - name: Add Joe to workflow approve role
      tower_role:
        user: "{{ username }}"
        role: approval
        workflow: test-role-workflow
        state: present
      register: result

    - assert:
        that:
          - "result is changed"

  always:
    - name: Delete a User
      tower_user:
        username: "{{ username }}"
        email: joe@example.org
        state: absent
      register: result

    - name: Delete job templates
      tower_job_template:
        name: "{{ item }}"
        project: "{{ project_name }}"
        inventory: "Demo Inventory"
        playbook: debug.yml
        state: absent
      with_items:
        - jt1
        - jt2
      register: result

    - name: Delete the project
      tower_project:
        name: "{{ project_name }}"
        organization: Default
        state: absent
      register: result
