---
# 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

# the gcloud modules and terraform provider can't use the project implicitly specified in the ADC
# we get the configured project directly from the credentials file
# we abuse the variable loader for this
- include_vars:
    name: gcloud_auth
    file: "~/.config/gcloud/application_default_credentials.json"
  no_log: true

- set_fact:
    test_basedir: "{{ test_basedir | default(output_dir) }}"
    # project names must match this regex, but can start with a number by default
    # "^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$"
    resource_id: "{{ resource_prefix }}-vpc"
    resource_project: "{{ gcloud_auth.quota_project_id }}"

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

- name: Clean up all integration test resources
  block:
    - name: Get a list of VPCs with a matching tag
      google.cloud.gcp_compute_network_info:
        auth_kind: application
        project: "{{ resource_project }}"
        filters:
          - "name = {{ resource_id }}"
      register: integration_vpcs

    - name: Delete VPCs
      google.cloud.gcp_compute_network:
        auth_kind: application
        project: "{{ resource_project }}"
        name: "{{ item.name }}"
        state: absent
      loop: "{{ integration_vpcs.resources }}"

- &verification
  block:
    - name: Get a list of VPCs with a matching tag
      google.cloud.gcp_compute_network_info:
        auth_kind: application
        project: "{{ resource_project }}"
        filters:
          - "name = {{ resource_id }}"
      register: vpc_info
    - name: Assert that there are {{ number_of_vpcs }} VPCs present
      assert:
        that:
          - (vpc_info.resources | length) == number_of_vpcs
  vars:
    number_of_vpcs: 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_project: "{{ resource_project }}"
  register: terraform_result
  check_mode: true
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vpcs: 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_project: "{{ resource_project }}"
  register: terraform_result
  check_mode: false
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vpcs: 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_project: "{{ resource_project }}"
  register: terraform_result
  check_mode: false
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is not changed
- <<: *verification
  vars:
    number_of_vpcs: 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_project: "{{ resource_project }}"
  register: terraform_result
  check_mode: true
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vpcs: 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_project: "{{ resource_project }}"
  register: terraform_result
  check_mode: false
- assert:
    that:
      - terraform_result is not failed
      - terraform_result is changed
- <<: *verification
  vars:
    number_of_vpcs: 0
