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

- name: Generate names
  set_fact:
    group_name1: "AWX-Collection-tests-tower_instance_group-group1-{{ test_id }}"
    group_name2: "AWX-Collection-tests-tower_instance_group-group2-{{ test_id }}"
    cred_name1: "AWX-Collection-tests-tower_instance_group-cred1-{{ test_id }}"

- block:
    - name: Create an OpenShift Credential
      tower_credential:
        name: "{{ cred_name1 }}"
        organization: "Default"
        credential_type: "OpenShift or Kubernetes API Bearer Token"
        inputs:
          host: "https://openshift.org"
          bearer_token: "asdf1234"
          verify_ssl: false
      register: cred_result

    - assert:
        that:
          - "cred_result is changed"

    - name: Create an Instance Group
      tower_instance_group:
        name: "{{ group_name1 }}"
        policy_instance_percentage: 34
        policy_instance_minimum: 12
        state: present
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Update an Instance Group
      tower_instance_group:
        name: "{{ result.id }}"
        policy_instance_percentage: 34
        policy_instance_minimum: 24
        state: present
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Create a container group
      tower_instance_group:
        name: "{{ group_name2 }}"
        credential: "{{ cred_result.id }}"
      register: result

    - assert:
        that:
          - "result is changed"

  always:
    - name: Delete the instance groups
      tower_instance_group:
        name: "{{ item }}"
        state: absent
      loop:
        - "{{ group_name1 }}"
        - "{{ group_name2 }}"

    - name: Delete the credential
      tower_credential:
        name: "{{ cred_name1 }}"
        organization: "Default"
        credential_type: "OpenShift or Kubernetes API Bearer Token"
