---
- name: Generate names
  set_fact:
    openstack_cred: "AWX-Collection-tests-tower_inventory_source-cred-openstack-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
    openstack_inv: "AWX-Collection-tests-tower_inventory_source-inv-openstack-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
    openstack_inv_source: "AWX-Collection-tests-tower_inventory_source-inv-source-openstack-{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"

- name: Add a Tower credential
  tower_credential:
    description: Credentials for Openstack Test project
    name: "{{ openstack_cred }}"
    kind: openstack
    organization: Default
    project: Test
    username: admin
    host: https://example.org:5000
    password: passw0rd
    domain: test
  register: credential_result

- name: Add a Tower inventory
  tower_inventory:
    description: Test inventory
    organization: Default
    name: "{{ openstack_inv }}"

- name: Create a source inventory
  tower_inventory_source:
    name: "{{ openstack_inv_source }}"
    description: Source for Test inventory
    inventory: "{{ openstack_inv }}"
    credential: "{{ credential_result.id }}"
    overwrite: true
    update_on_launch: true
    source_vars:
      private: false
    source: openstack
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete the inventory source with an invalid cred, source_project, sourece_script specified
  tower_inventory_source:
    name: "{{ result.id }}"
    inventory: "{{ openstack_inv }}"
    credential: "Does Not Exit"
    source_project: "Does Not Exist"
    source_script: "Does Not Exist"
    state: absent

- assert:
    that:
      - "result is changed"

- name: Delete the credential
  tower_credential:
    description: Credentials for Openstack Test project
    name: "{{ openstack_cred }}"
    kind: openstack
    organization: Default
    project: Test
    username: admin
    host: https://example.org:5000
    password: passw0rd
    domain: test
    state: absent

- assert:
    that:
      - "result is changed"

- name: Delete the inventory
  tower_inventory:
    description: Test inventory
    organization: Default
    name: "{{ openstack_inv }}"
    state: absent

- assert:
    that:
      - "result is changed"
