---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

- set_fact:
    test_basedir: "{{ test_basedir | default(output_dir) }}"
    resource_id: "{{ resource_prefix }}-vnet"
    resource_group_name: "cloud.terraform_integration_resource_group"
    resource_group_location: "northeurope"

- name: Copy terraform files to workspace
  ansible.builtin.copy:
    src: "{{ item }}"
    dest: "{{ test_basedir }}/{{ item }}"
  loop:
    - cloud.tf

- name: Create a resource group
  azure.azcollection.azure_rm_resourcegroup:
    name: "{{ resource_group_name }}"
    location: "{{ resource_group_location }}"
    tags:
      cloud_terraform_integration: "true"

- name: Clean up all integration test resources
  block:
    - name: Get a list of vnets with a matching tag
      azure.azcollection.azure_rm_virtualnetwork_info:
        tags:
          - "cloud_terraform_integration:true"
      register: integration_vnets

    - name: Delete vnets
      azure.azcollection.azure_rm_virtualnetwork:
        name: "{{ item.name }}"
        resource_group: "{{ resource_group_name }}"
        state: absent
      loop: "{{ integration_vnets.virtualnetworks }}"

- &verification
  block:
    - name: Get a list of vnets with a matching tag
      azure.azcollection.azure_rm_virtualnetwork_info:
        tags:
          - "cloud_terraform_integration_id:{{ resource_id }}"
      register: vnet_info
    - name: Assert that there are {{ number_of_vnets }} vnets present
      assert:
        that:
          - (vnet_info.virtualnetworks | length) == number_of_vnets
  vars:
    number_of_vnets: 0

- name: Terraform in present check mode
  cloud.terraform.terraform:
    project_path: "{{ test_basedir }}"
    state: present
    force_init: true
    variables:
      cloud_terraform_integration_id: "{{ resource_id }}"
      cloud_terraform_integration_resource_group: "{{ resource_group_name }}"
      cloud_terraform_integration_location: "{{ resource_group_location }}"
  register: terraform_result
  check_mode: true
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vnets: 0

- name: Terraform in present non-check mode
  cloud.terraform.terraform:
    project_path: "{{ test_basedir }}"
    state: present
    force_init: true
    variables:
      cloud_terraform_integration_id: "{{ resource_id }}"
      cloud_terraform_integration_resource_group: "{{ resource_group_name }}"
      cloud_terraform_integration_location: "{{ resource_group_location }}"
  register: terraform_result
  check_mode: false
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vnets: 1

- name: Terraform in present non-check mode (idempotency)
  cloud.terraform.terraform:
    project_path: "{{ test_basedir }}"
    state: present
    force_init: true
    variables:
      cloud_terraform_integration_id: "{{ resource_id }}"
      cloud_terraform_integration_resource_group: "{{ resource_group_name }}"
      cloud_terraform_integration_location: "{{ resource_group_location }}"
  register: terraform_result
  check_mode: false
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is not changed
- <<: *verification
  vars:
    number_of_vnets: 1

- name: Terraform in absent check mode
  cloud.terraform.terraform:
    project_path: "{{ test_basedir }}"
    state: absent
    force_init: true
    variables:
      cloud_terraform_integration_id: "{{ resource_id }}"
      cloud_terraform_integration_resource_group: "{{ resource_group_name }}"
      cloud_terraform_integration_location: "{{ resource_group_location }}"
  register: terraform_result
  check_mode: true
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vnets: 1

- name: Terraform in absent non-check mode
  cloud.terraform.terraform:
    project_path: "{{ test_basedir }}"
    state: absent
    force_init: true
    variables:
      cloud_terraform_integration_id: "{{ resource_id }}"
      cloud_terraform_integration_resource_group: "{{ resource_group_name }}"
      cloud_terraform_integration_location: "{{ resource_group_location }}"
  register: terraform_result
  check_mode: false
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vnets: 0
