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

- name: generate random string for project
  set_fact:
    cred1: "AWX-Collection-tests-tower_job_template-cred1-{{ test_id }}"
    cred2: "AWX-Collection-tests-tower_job_template-cred2-{{ test_id }}"
    cred3: "AWX-Collection-tests-tower_job_template-cred3-{{ test_id }}"
    proj1: "AWX-Collection-tests-tower_job_template-proj-{{ test_id }}"
    jt1: "AWX-Collection-tests-tower_job_template-jt1-{{ test_id }}"
    jt2: "AWX-Collection-tests-tower_job_template-jt2-{{ test_id }}"
    lab1: "AWX-Collection-tests-tower_job_template-lab1-{{ test_id }}"
    email_not: "AWX-Collection-tests-tower_job_template-email-not-{{ test_id }}"
    webhook_not: "AWX-Collection-tests-tower_notification_template-wehbook-not-{{ test_id }}"

- name: Create a Demo Project
  tower_project:
    name: "{{ proj1 }}"
    organization: Default
    state: present
    scm_type: git
    scm_url: https://github.com/ansible/ansible-tower-samples.git
  register: proj_result

- name: Create Credential1
  tower_credential:
    name: "{{ cred1 }}"
    organization: Default
    kind: tower
  register: cred1_result

- name: Create Credential2
  tower_credential:
    name: "{{ cred2 }}"
    organization: Default
    kind: ssh

- name: Create Credential3
  tower_credential:
    name: "{{ cred3 }}"
    organization: Default
    kind: ssh

- name: Create Label
  tower_label:
    name: "{{ lab1 }}"
    organization: Default

- name: Add email notification
  tower_notification_template:
    name: "{{ email_not }}"
    organization: Default
    notification_type: email
    username: user
    password: s3cr3t
    sender: tower@example.com
    recipients:
      - user1@example.com
    host: smtp.example.com
    port: 25
    use_tls: false
    use_ssl: false
    state: present

- name: Add webhook notification
  tower_notification_template:
    name: "{{ webhook_not }}"
    organization: Default
    notification_type: webhook
    url: http://www.example.com/hook
    headers:
      X-Custom-Header: value123
    state: present
  register: result

- name: Create Job Template 1
  tower_job_template:
    name: "{{ jt1 }}"
    project: "{{ proj1 }}"
    inventory: Demo Inventory
    playbook: hello_world.yml
    credentials: ["{{ cred1 }}", "{{ cred2 }}"]
    job_type: run
    state: present
  register: jt1_result

- assert:
    that:
      - "jt1_result is changed"

- name: Add a credential to this JT
  tower_job_template:
    name: "{{ jt1 }}"
    project: "{{ proj_result.id }}"
    playbook: hello_world.yml
    credentials:
      - "{{ cred1_result.id }}"
  register: result

- assert:
    that:
      - "result is changed"

- name: Try to add the same credential to this JT
  tower_job_template:
    name: "{{ jt1_result.id }}"
    project: "{{ proj1 }}"
    playbook: hello_world.yml
    credentials:
      - "{{ cred1 }}"
  register: result

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

- name: Add another credential to this JT
  tower_job_template:
    name: "{{ jt1 }}"
    project: "{{ proj1 }}"
    playbook: hello_world.yml
    credentials:
      - "{{ cred1 }}"
      - "{{ cred2 }}"
  register: result

- assert:
    that:
      - "result is changed"

- name: Remove a credential for this JT
  tower_job_template:
    name: "{{ jt1 }}"
    project: "{{ proj1 }}"
    playbook: hello_world.yml
    credentials:
      - "{{ cred1 }}"
  register: result

- assert:
    that:
      - "result is changed"

- name: Remove all credentials from this JT
  tower_job_template:
    name: "{{ jt1 }}"
    project: "{{ proj1 }}"
    playbook: hello_world.yml
    credentials: []
  register: result

- assert:
    that:
      - "result is changed"

# This doesnt work if you include the credentials parameter
- name: Delete Job Template 1
  tower_job_template:
    name: "{{ jt1 }}"
    playbook: hello_world.yml
    job_type: run
    project: "Does Not Exist"
    inventory: "Does Not Exist"
    webhook_credential: "Does Not Exist"
    state: absent
  register: result

- assert:
    that:
      - "result is changed"

- name: Create Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    project: "{{ proj1 }}"
    inventory: Demo Inventory
    playbook: hello_world.yml
    credential: "{{ cred3 }}"
    job_type: run
    labels:
      - "{{ lab1 }}"
    state: present
  register: result

- assert:
    that:
      - "result is changed"

- name: Add survey to Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    survey_enabled: true
    survey_spec:
      name: ""
      description: ""
      spec:
        - question_name: "Q1"
          question_description: "The first question"
          required: true
          type: "text"
          variable: "q1"
          min: 5
          max: 15
          default: "hello"
  register: result

- assert:
    that:
      - "result is changed"

- name: Re Add survey to Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    survey_enabled: true
    survey_spec:
      name: ""
      description: ""
      spec:
        - question_name: "Q1"
          question_description: "The first question"
          required: true
          type: "text"
          variable: "q1"
          min: 5
          max: 15
          default: "hello"
  register: result

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

- name: Add question to survey to Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    survey_enabled: true
    survey_spec:
      name: ""
      description: ""
      spec:
        - question_name: "Q1"
          question_description: "The first question"
          required: true
          type: "text"
          variable: "q1"
          min: 5
          max: 15
          default: "hello"
          choices: ""
        - question_name: "Q2"
          type: "text"
          variable: "q2"
          required: false
  register: result

- assert:
    that:
      - "result is changed"

- name: Remove survey from Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    survey_enabled: false
    survey_spec: {}
  register: result

- assert:
    that:
      - "result is changed"

- name: Add started notifications to Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    notification_templates_started:
      - "{{ email_not }}"
      - "{{ webhook_not }}"
  register: result

- assert:
    that:
      - "result is changed"

- name: Re Add started notifications to Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    notification_templates_started:
      - "{{ email_not }}"
      - "{{ webhook_not }}"
  register: result

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

- name: Add success notifications to Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    notification_templates_success:
      - "{{ email_not }}"
      - "{{ webhook_not }}"
  register: result

- assert:
    that:
      - "result is changed"

- name: Remove "on start" webhook notification from Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    notification_templates_started:
      - "{{ email_not }}"
  register: result

- assert:
    that:
      - "result is changed"


- name: Delete Job Template 2
  tower_job_template:
    name: "{{ jt2 }}"
    project: "{{ proj1 }}"
    inventory: Demo Inventory
    playbook: hello_world.yml
    credential: "{{ cred3 }}"
    job_type: run
    state: absent
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete the Demo Project
  tower_project:
    name: "{{ proj1 }}"
    organization: Default
    state: absent
    scm_type: git
    scm_url: https://github.com/ansible/ansible-tower-samples.git
  register: result

- name: Delete Credential1
  tower_credential:
    name: "{{ cred1 }}"
    organization: Default
    kind: tower
    state: absent

- name: Delete Credential2
  tower_credential:
    name: "{{ cred2 }}"
    organization: Default
    kind: ssh
    state: absent

- name: Delete Credential3
  tower_credential:
    name: "{{ cred3 }}"
    organization: Default
    kind: ssh
    state: absent

# You can't delete a label directly so no cleanup needed

- name: Delete email notification
  tower_notification_template:
    name: "{{ email_not }}"
    organization: Default
    state: absent

- name: Delete webhook notification
  tower_notification_template:
    name: "{{ webhook_not }}"
    organization: Default
    state: absent
