---
- 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:
    org_name1: "AWX-Collection-tests-tower_import-organization-{{ test_id }}"
    org_name2: "AWX-Collection-tests-tower_import-organization2-{{ test_id }}"

- block:
    - name: "Import something"
      tower_import:
        assets:
          organizations:
            - name: "{{ org_name1 }}"
              description: ""
              max_hosts: 0
              custom_virtualenv: null
              related:
                notification_templates: []
                notification_templates_started: []
                notification_templates_success: []
                notification_templates_error: []
                notification_templates_approvals: []
              natural_key:
                name: "{{ org_name1 }}"
                type: "organization"
      register: import_output

    - assert:
        that:
          - import_output is changed

    - name: "Import the same thing again"
      tower_import:
        assets:
          organizations:
            - name: "{{ org_name1 }}"
              description: ""
              max_hosts: 0
              custom_virtualenv: null
              related:
                notification_templates: []
                notification_templates_started: []
                notification_templates_success: []
                notification_templates_error: []
                notification_templates_approvals: []
              natural_key:
                name: "{{ org_name1 }}"
                type: "organization"
      register: import_output
      ignore_errors: true

    - assert:
        that:
          - import_output is not failed
          # - import_output is not changed  # FIXME: module not idempotent

    - name: "Write out a json file"
      copy:
        content: |
            {
                 "organizations": [
                      {
                           "name": "{{ org_name2 }}",
                           "description": "",
                           "max_hosts": 0,
                           "custom_virtualenv": null,
                           "related": {
                                "notification_templates": [],
                                "notification_templates_started": [],
                                "notification_templates_success": [],
                                "notification_templates_error": [],
                                "notification_templates_approvals": []
                           },
                           "natural_key": {
                                "name": "{{ org_name2 }}",
                                "type": "organization"
                           }
                      }
                 ]
            }
        dest: ./org.json

    - name: "Load assets from a file"
      tower_import:
        assets: "{{ lookup('file', 'org.json') | from_json() }}"
      register: import_output

    - assert:
        that:
          - import_output is changed

  always:
    - name: Remove organizations
      tower_organization:
        name: "{{ item }}"
        state: absent
      loop:
        - "{{ org_name1 }}"
        - "{{ org_name2 }}"

    - name: Delete org.json
      file:
        path: ./org.json
        state: absent
