---
- module_defaults:
    group/aws:
      aws_access_key: "{{ aws_access_key }}"
      aws_secret_key: "{{ aws_secret_key }}"
      security_token: "{{ security_token | default(omit) }}"
      region: "{{ aws_region }}"

  collections:
  - community.aws

  block:
  - name: Get available AZs
    aws_az_info:
      filters:
        region-name: "{{ aws_region }}"
    register: az_info

  - name: Pick an AZ
    set_fact:
      availability_zone: "{{ az_info['availability_zones'][0]['zone_name'] }}"

  # ============================================================
  - name: create a VPC
    ec2_vpc_net:
      name: "{{ resource_prefix }}-vpc"
      state: present
      cidr_block: "{{ vpc_cidr }}"
      tags:
        Name: "{{ resource_prefix }}-vpc"
        Description: "Created by ansible-test"
    register: vpc_result

  - name: create a subnet
    ec2_vpc_subnet:
      cidr: "{{ vpc_cidr }}"
      az: "{{ availability_zone }}"
      vpc_id: "{{ vpc_result.vpc.id }}"
      tags:
        Name: "{{ resource_prefix }}-vpc"
        Description: "Created by ansible-test"
      state: present
    register: vpc_subnet_result

  - name: create a security group
    ec2_group:
      name: "{{ resource_prefix }}-sg"
      description: "Created by {{ resource_prefix }}"
      rules: []
      state: present
      vpc_id: "{{ vpc_result.vpc.id }}"
    register: vpc_sg_result

  - name: Get a list of images
    ec2_ami_info:
      filters:
        owner-alias: amazon
        name: "amzn2-ami-minimal-hvm-*"
        description: "Amazon Linux 2 AMI *"
    register: images_info

  - name: Set facts to simplify use of extra resources
    set_fact:
      vpc_id: "{{ vpc_result.vpc.id }}"
      vpc_subnet_id: "{{ vpc_subnet_result.subnet.id }}"
      vpc_sg_id: "{{ vpc_sg_result.group_id }}"
      image_id: "{{ images_info.images | sort(attribute='creation_date') | reverse | first | json_query('image_id') }}"

  # ============================================================

  - name: Create 2 instances to test attaching and detaching network interfaces
    ec2_instance:
      name: "{{ resource_prefix }}-eni-instance-{{ item }}"
      image_id: "{{ image_id }}"
      vpc_subnet_id: "{{ vpc_subnet_id }}"
      instance_type: t2.micro
      wait: false
      security_group: "{{ vpc_sg_id }}"
      network:
        private_ip_address: '{{ ec2_ips[item] }}'
    register: ec2_instances
    loop:
      - 0
      - 1

  # We only need these instances to be running
  - name: set variables for the instance IDs
    set_fact:
      instance_id_1: "{{ ec2_instances.results[0].instance_ids[0] }}"
      instance_id_2: "{{ ec2_instances.results[1].instance_ids[0] }}"

  # ============================================================
  - name: test attaching and detaching network interfaces
    include_tasks: ./test_eni_basic_creation.yaml

  - name: test attaching and detaching network interfaces
    include_tasks: ./test_ipaddress_assign.yaml

  - name: test attaching and detaching network interfaces
    include_tasks: ./test_attachment.yaml

  - name: test modifying source_dest_check
    include_tasks: ./test_modifying_source_dest_check.yaml

  - name: test modifying tags
    include_tasks: ./test_modifying_tags.yaml

  # Note: will delete *both* EC2 instances
  - name: test modifying delete_on_termination
    include_tasks: ./test_modifying_delete_on_termination.yaml

  - name: test deleting ENIs
    include_tasks: ./test_deletion.yaml

  always:

    # ============================================================
      - name: remove the network interfaces
        ec2_eni:
          eni_id: "{{ item }}"
          force_detach: True
          state: absent
        ignore_errors: true
        retries: 5
        loop:
          - "{{ eni_id_1 | default(omit) }}"
          - "{{ eni_id_2 | default(omit) }}"

      - name: terminate the instances
        ec2_instance:
          state: absent
          instance_ids:
            - "{{ instance_id_1 }}"
            - "{{ instance_id_2 }}"
          wait: True
        ignore_errors: true
        retries: 5
        when: instance_id_1 is defined and instance_id_2 is defined

      - name: remove the security group
        ec2_group:
          name: "{{ resource_prefix }}-sg"
          description: "{{ resource_prefix }}"
          rules: []
          state: absent
          vpc_id: "{{ vpc_result.vpc.id }}"
        ignore_errors: true
        retries: 5

      - name: remove the subnet
        ec2_vpc_subnet:
          cidr: "{{ vpc_cidr }}"
          az: "{{ availability_zone }}"
          vpc_id: "{{ vpc_result.vpc.id }}"
          state: absent
        ignore_errors: true
        retries: 5
        when: vpc_subnet_result is defined

      - name: remove the VPC
        ec2_vpc_net:
          name: "{{ resource_prefix }}-vpc"
          cidr_block: "{{ vpc_cidr }}"
          state: absent
        ignore_errors: true
        retries: 5
