---
- name: Create
  hosts: localhost
  connection: local
  gather_facts: false
  no_log: "{{ molecule_no_log }}"
  vars:
    ssh_identity_file: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
    gcp_project_id: "{{ molecule_yml.driver.project_id | default(lookup('env', 'GCE_PROJECT_ID')) }}"

  tasks:
    - name: Make sure if linux or windows either specified
      ansible.builtin.assert:
        that:
          - molecule_yml.driver.instance_os_type | lower == "linux" or molecule_yml.driver.instance_os_type | lower == "windows"
        fail_msg: instance_os_type is possible only to specify linux or windows either

    - name: Get network info
      google.cloud.gcp_compute_network_info:
        filters:
          - name = "{{ molecule_yml.driver.network_name | default('default') }}"
        project: "{{ molecule_yml.driver.vpc_host_project | default(gcp_project_id) }}"
        service_account_email: "{{ molecule_yml.driver.service_account_email | default(omit, true) }}"
        service_account_file: "{{ molecule_yml.driver.service_account_file | default(omit, true) }}"
        auth_kind: "{{ molecule_yml.driver.auth_kind | default(omit, true) }}"
      register: my_network

    - name: Get subnetwork info
      google.cloud.gcp_compute_subnetwork_info:
        filters:
          - name = "{{ molecule_yml.driver.subnetwork_name | default('default') }}"
        project: "{{ molecule_yml.driver.vpc_host_project | default(gcp_project_id) }}"
        region: "{{ molecule_yml.driver.region }}"
        service_account_email: "{{ molecule_yml.driver.service_account_email | default(omit, true) }}"
        service_account_file: "{{ molecule_yml.driver.service_account_file | default(omit, true) }}"
        auth_kind: "{{ molecule_yml.driver.auth_kind | default(omit, true) }}"
      register: my_subnetwork

    - name: Set external access config
      ansible.builtin.set_fact:
        external_access_config:
          - access_configs:
              - name: External NAT
                type: ONE_TO_NAT
      when: molecule_yml.driver.external_access

    - name: Include create_linux_instance tasks
      ansible.builtin.include_tasks: tasks/create_linux_instance.yml
      when:
        - molecule_yml.driver.instance_os_type  | lower == "linux"

    - name: Include create_windows_instance tasks
      ansible.builtin.include_tasks: tasks/create_windows_instance.yml
      when:
        - molecule_yml.driver.instance_os_type  | lower == "windows"

  handlers:
    - name: Import main handler tasks
      ansible.builtin.import_tasks: handlers/main.yml
