---
# 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) }}"

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

- name: Init terraform files
  ansible.builtin.shell: |
    cd {{ test_basedir }}
    terraform init
    terraform apply -auto-approve
    terraform apply -auto-approve -state mycustomstate.tfstate

- name: Terraform checkout - default state file + implicit json
  cloud.terraform.terraform_output:
    project_path: "{{ test_basedir }}"
  register: terraform_output
- assert: &json_assert
    that:
      - terraform_output is not changed
      - terraform_output.outputs.my_output.sensitive == false
      - terraform_output.outputs.my_output.type == "string"
      - terraform_output.outputs.my_output.value == "file generated"
      - terraform_output.value is not defined

- name: Terraform checkout - default state file + explicit json
  cloud.terraform.terraform_output:
    project_path: "{{ test_basedir }}"
    format: json
  register: terraform_output
- assert: *json_assert

- name: Terraform checkout - list only selected output in json format
  cloud.terraform.terraform_output:
    project_path: "{{ test_basedir }}"
    name: my_output
  register: terraform_output
- assert:
    that:
      - terraform_output is not changed
      - terraform_output.outputs is not defined
      - terraform_output.value == "file generated"

- name: Terraform checkout - list only selected output in raw format
  cloud.terraform.terraform_output:
    project_path: "{{ test_basedir }}"
    format: raw
    name: my_output
  register: terraform_output
- assert:
    that:
      - terraform_output is not changed
      - terraform_output.outputs is not defined
      - terraform_output.value == "file generated"

- name: Terraform checkout - specified state file
  cloud.terraform.terraform_output:
    state_file: "{{ test_basedir }}/mycustomstate.tfstate"
  register: terraform_output
- assert:
    that:
      - terraform_output is not changed
      - terraform_output.outputs.my_output.sensitive == false
      - terraform_output.outputs.my_output.type == "string"
      - terraform_output.outputs.my_output.value == "file generated"
      - terraform_output.value is not defined

- name: Terraform checkout - specified state file and specified output
  cloud.terraform.terraform_output:
    state_file: "{{ test_basedir }}/mycustomstate.tfstate"
    name: my_output
  register: terraform_output
- assert:
    that:
      - terraform_output is not changed
      - terraform_output.outputs is not defined
      - terraform_output.value == "file generated"

# terraform lookup using workspace
- name: Test cloud.terraform.terraform_output using workspace
  block:
    - name: Create temporary directory to work in
      ansible.builtin.tempfile:
        state: directory
        suffix: .tf
      register: tf_dir

    - name: Copy terraform files
      ansible.builtin.copy:
        src: outputs_workspace.tf
        dest: "{{ tf_dir.path }}"

    - name: Apply terraform projects
      cloud.terraform.terraform:
        force_init: true
        workspace: '{{ item }}'
        project_path: '{{ tf_dir.path }}'
        variables:
          workspace: "{{ item }}"
      with_items: "{{ test_workspaces }}"

    - name: Terraform checkout - specified workspace and project_path
      cloud.terraform.terraform_output:
        project_path: '{{ tf_dir.path }}'
        workspace: '{{ item }}'
      register: terraform_output
      with_items: "{{ test_workspaces }}"

    - name: Ensure module returned values as expected
      ansible.builtin.assert:
        that:
          - terraform_output.results | map(attribute='outputs.my_workspace.value') | list == test_workspaces

  always:
    - name: Delete temporary directory
      ansible.builtin.file:
        state: absent
        path: "{{ tf_dir.path }}"