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

- name: Generate names
  set_fact:
    project_name: "AWX-Collection-tests-tower_inventory_source_update-project-{{ test_id }}"
    inv_name: "AWX-Collection-tests-tower_inventory_source_update-inv-{{ test_id }}"
    inv_source1: "AWX-Collection-tests-tower_inventory_source_update-source1-{{ test_id }}"
    inv_source2: "AWX-Collection-tests-tower_inventory_source_update-source2-{{ test_id }}"
    inv_source3: "AWX-Collection-tests-tower_inventory_source_update-source3-{{ test_id }}"
    org_name: "AWX-Collection-tests-tower_inventory_source_update-org-{{ test_id }}"


- block:

    - name: "Create a new organization"
      tower_organization:
        name: "{{ org_name }}"
      register: created_org

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

    - name: Create an Inventory
      tower_inventory:
        name: "{{ inv_name }}"
        organization: "{{ org_name }}"
        state: present

    - name: Create another inventory w/ same name, different org
      tower_inventory:
        name: "{{ inv_name }}"
        organization: Default
        state: present
      register: created_inventory

    - name: Create an Inventory Source (specifically connected to the randomly generated org)
      tower_inventory_source:
        name: "{{ inv_source1 }}"
        source: scm
        source_project: "{{ project_name }}"
        source_path: inventories/inventory.ini
        description: Source for Test inventory
        organization: "{{ created_org.id }}"
        inventory: "{{ inv_name }}"

    - name: Create Another Inventory Source
      tower_inventory_source:
        name: "{{ inv_source2 }}"
        source: scm
        source_project: "{{ project_name }}"
        source_path: inventories/create_10_hosts.ini
        description: Source for Test inventory
        organization: Default
        inventory: "{{ inv_name }}"

    - name: Create Yet Another Inventory Source (to make lookup plugin find multiple inv sources)
      tower_inventory_source:
        name: "{{ inv_source3 }}"
        source: scm
        source_project: "{{ project_name }}"
        source_path: inventories/create_100_hosts.ini
        description: Source for Test inventory
        organization: Default
        inventory: "{{ inv_name }}"

    - name: Test Inventory Source Update
      tower_inventory_source_update:
        name: "{{ inv_source2 }}"
        inventory: "{{ inv_name }}"
        organization: Default
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Test Inventory Source Update for All Sources
      tower_inventory_source_update:
        name: "{{ item.name }}"
        inventory: "{{ inv_name }}"
        organization: Default
        wait: true
      loop: "{{ query('awx.awx.tower_api', 'inventory_sources', query_params={ 'inventory': created_inventory.id }, expect_objects=True, return_objects=True) }}"
      loop_control:
        label: "{{ item.name }}"
      register: result

    - assert:
        that:
          - "result is changed"

    - name: Test Inventory Source Update for All Sources (using inventory_source as alias for name)
      tower_inventory_source_update:
        inventory_source: "{{ item.name }}"
        inventory: "{{ inv_name }}"
        organization: Default
        wait: true
      loop: "{{ query('awx.awx.tower_api', 'inventory_sources', query_params={ 'inventory': created_inventory.id }, expect_objects=True, return_objects=True) }}"
      loop_control:
        label: "{{ item.name }}"
      register: result

    - assert:
        that:
          - "result is changed"

  always:
    - name: Delete Inventory
      tower_inventory:
        name: "{{ inv_name }}"
        organization: Default
        state: absent

    - name: Delete Project
      tower_project:
        name: "{{ project_name }}"
        organization: Default
        state: absent

    - name: "Remove the organization"
      tower_organization:
        name: "{{ org_name }}"
        state: absent
