---
# 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
- environment:
    AWS_ACCESS_KEY_ID: "{{ aws_access_key | default(omit) }}"
    AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key | default(omit) }}"
    AWS_SESSION_TOKEN: "{{ security_token | default(omit) }}"
    AWS_REGION: "{{ aws_region | default(omit) }}"

  block:
    - set_fact:
        test_basedir: "{{ test_basedir | default(output_dir) }}"
        vpc_name: "{{ resource_prefix }}-awscc-vpc"
        vpc_cidr_block: '10.{{ 256 | random(seed=resource_prefix) }}.0.0/24'

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

    - name: Create VPC
      amazon.aws.ec2_vpc_net:
        name: "{{ vpc_name }}"
        cidr_block:
          - "{{ vpc_cidr_block }}"
        state: present
      register: vpc_info

    - set_fact:
        vpc_id: "{{ vpc_info.vpc.id }}"

    - &verification
      block:
        - name: Fetch Subnet info
          amazon.aws.ec2_vpc_subnet_info:
            filters:
              vpc-id: "{{ vpc_id }}"
              cidr-block: "{{ vpc_cidr_block }}"
          register: subnet_info

        - name: Assert that there are {{ number_of_subnets }} Subnets
          assert:
            that:
              - (subnet_info.subnets | length) == number_of_subnets
        - name: Assert that Subnet  is present and the info matches
          assert:
            that:
              - subnet_info.subnets[0].cidr_block == vpc_cidr_block
          when: number_of_subnets == 1
      vars:
        number_of_subnets: 0

    - name: Terraform in present check mode
      cloud.terraform.terraform:
        project_path: "{{ test_basedir }}"
        state: present
        force_init: true
        variables:
          vpc_id: "{{ vpc_id }}"
          cidr_block: "{{ vpc_cidr_block }}"
      register: terraform_result
      check_mode: true

    - assert:
        that:
          - terraform_result is not failed
          - terraform_result is changed

    - <<: *verification
      vars:
        number_of_subnets: 0

    - name: Terraform in present non-check mode
      cloud.terraform.terraform:
        project_path: "{{ test_basedir }}"
        state: present
        force_init: true
        variables:
          vpc_id: "{{ vpc_id }}"
          cidr_block: "{{ vpc_cidr_block }}"
      register: terraform_result
      check_mode: false

    - assert:
        that:
          - terraform_result is not failed
          - terraform_result is changed

    - <<: *verification
      vars:
        number_of_subnets: 1

    - name: Terraform in present non-check mode (idempotency)
      cloud.terraform.terraform:
        project_path: "{{ test_basedir }}"
        state: present
        force_init: true
        variables:
          vpc_id: "{{ vpc_id }}"
          cidr_block: "{{ vpc_cidr_block }}"
      register: terraform_result
      check_mode: false

    - assert:
        that:
          - terraform_result is not failed
          - terraform_result is not changed

    - <<: *verification
      vars:
        number_of_subnets: 1

    - name: Terraform in absent check mode
      cloud.terraform.terraform:
        project_path: "{{ test_basedir }}"
        state: absent
        force_init: true
        variables:
          vpc_id: "{{ vpc_id }}"
          cidr_block: "{{ vpc_cidr_block }}"
      register: terraform_result
      check_mode: true

    - assert:
        that:
          - terraform_result is not failed
          - terraform_result is changed

    - <<: *verification
      vars:
        number_of_subnets: 1

    - name: Terraform in absent non-check mode
      cloud.terraform.terraform:
        project_path: "{{ test_basedir }}"
        state: absent
        force_init: true
        variables:
          vpc_id: "{{ vpc_id }}"
          cidr_block: "{{ vpc_cidr_block }}"
      register: terraform_result
      check_mode: false

    - assert:
        that:
          - terraform_result is not failed
          - terraform_result is changed

    - <<: *verification
      vars:
        number_of_subnets: 0

  # Clean up all integration test resources
  always:
    - name: Fetch Subnet info
      amazon.aws.ec2_vpc_subnet_info:
        filters:
          vpc-id: "{{ vpc_id }}"
          cidr-block: "{{ vpc_cidr_block }}"
      register: subnet_info
      ignore_errors: true

    - name: Delete Subnets
      amazon.aws.ec2_vpc_subnet:
        cidr: "{{ item.cidr_block }}"
        vpc_id: "{{ vpc_id }}"
        wait: true
        state: absent
      with_items: "{{ subnet_info.subnets }}"
      ignore_errors: true

    - name: Delete VPC
      amazon.aws.ec2_vpc_net:
        name: "{{ vpc_name }}"
        cidr_block:
          - "{{ vpc_cidr_block }}"
        state: absent
      ignore_errors: true
