---
{% raw -%}
- name: Create
  hosts: localhost
  connection: local
  gather_facts: false
  no_log: "{{ molecule_no_log }}"
  vars:
    resource_group_name: molecule
    location: "{{ lookup('env', 'AZURE_REGION') or 'westus' }}"
    ssh_user: molecule
    ssh_port: 22
    virtual_network_name: molecule_vnet
    subnet_name: molecule_subnet
    keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
  tasks:
    - name: Create resource group
      azure.azcollection.azure_rm_resourcegroup:
        name: "{{ resource_group_name }}"
        location: "{{ location }}"

    - name: Create virtual network
      azure.azcollection.azure_rm_virtualnetwork:
        resource_group: "{{ resource_group_name }}"
        name: "{{ virtual_network_name }}"
        address_prefixes: "10.10.0.0/16"

    - name: Create subnet
      azure.azcollection.azure_rm_subnet:
        resource_group: "{{ resource_group_name }}"
        name: "{{ subnet_name }}"
        address_prefix_cidr: 10.10.1.0/24
        virtual_network_name: "{{ virtual_network_name }}"

    - name: Create key pair
      ansible.builtin.user:
        name: "{{ lookup('env', 'USER') }}"
        generate_ssh_key: true
        ssh_key_file: "{{ keypair_path }}"
      register: key_pair

    - name: Create molecule instance(s)
      azure.azcollection.azure_rm_virtualmachine:
        resource_group: "{{ resource_group_name }}"
        name: "{{ item.name }}"
        vm_size: Standard_B1ms
        admin_username: "{{ ssh_user }}"
        public_ip_allocation_method: Dynamic
        ssh_password_enabled: false
        ssh_public_keys:
          - path: "/home/{{ ssh_user }}/.ssh/authorized_keys"
            key_data: "{{ key_pair.ssh_public_key }}"
        image:
          offer: CentOS
          publisher: OpenLogic
          sku: '8_5-gen2'
          version: latest
      register: server
      with_items: "{{ molecule_yml.platforms }}"
      async: 7200
      poll: 0

    - name: Wait for instance(s) creation to complete
      ansible.builtin.async_status:
        jid: "{{ item.ansible_job_id }}"
      register: azure_jobs
      until: azure_jobs.finished
      retries: 300
      with_items: "{{ server.results }}"
      notify:
        - Populate instance config dict
        - Convert instance config dict to a list
        - Dump instance config
        - Wait for SSH

  handlers:
    - name: Populate instance config dict
      ansible.builtin.set_fact:
        instance_conf_dict:  # noqa: jinja[spacing]
          {
            "instance": "{{ item.ansible_facts.azure_vm.name }}",
            "address": "{{ item.ansible_facts.azure_vm
              .properties.networkProfile.networkInterfaces[0]
              .properties.ipConfigurations[0]
              .properties.publicIPAddress
              .properties.ipAddress }}",
            "user": "{{ ssh_user }}",
            "port": "{{ ssh_port }}",
            "identity_file": "{{ keypair_path }}",
          }
      with_items: "{{ azure_jobs.results }}"
      register: instance_config_dict

    - name: Convert instance config dict to a list
      ansible.builtin.set_fact:
        instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"

    - name: Dump instance config
      ansible.builtin.copy:
        content: "{{ instance_conf | to_json | from_json | to_yaml }}"
        dest: "{{ molecule_instance_config }}"
        mode: "0664"

    - name: Wait for SSH
      ansible.builtin.wait_for:
        port: "{{ ssh_port }}"
        host: "{{ item.address }}"
        search_regex: SSH
        delay: 10
      with_items: "{{ instance_conf }}"
{%- endraw %}
