---
- name: IP facts | Get the preferred network interface index
  register: cmd_output
  win_shell: |
    $ErrorActionPreference = "Stop"
    $hnsNet = Get-HnsNetwork | Where-Object {
        ($_.Name -eq "{{ sdn_info.sdn_network_name }}") -and ($_.Type -eq "Transparent")
    }
    if($hnsNet) {
        if($hnsNet.Count -gt 1) {
            Throw "There is more than one HNS transparent networks with the name {{ sdn_info.sdn_network_name }}"
        }
        (Get-NetAdapter -Name $hnsNet.NetworkAdapterName).InterfaceIndex
        exit 0
    }
    $preferredIfName = "{{ sdn_preferred_nic_name | default('') }}"
    if($preferredIfName) {
        (Get-NetAdapter -Name $preferredIfName).InterfaceIndex
        exit 0
    }
    $defaultRoute = Get-NetRoute -DestinationPrefix "0.0.0.0/0"
    if(!$defaultRoute) {
        Throw "No default route. Cannot get the default SDN preferred NIC"
    }
    if($defaultRoute.Count -gt 1) {
        Throw "There is more than one default gateway. Cannot get the default SDN preferred NIC"
    }
    $defaultRoute.ifIndex

- name: IP facts | Set the interface_index fact
  set_fact:
    interface_index: "{{ cmd_output.stdout | trim }}"

- name: IP facts | Get the default gateway
  register: cmd_output
  win_shell: (Get-NetRoute -DestinationPrefix "0.0.0.0/0").NextHop

- name: IP facts | Set the interface_default_gateway fact
  set_fact:
    interface_default_gateway: "{{ cmd_output.stdout | trim }}"

- name: IP facts | Get the preferred network interface name
  register: cmd_output
  win_shell: (Get-NetAdapter -InterfaceIndex "{{ interface_index }}").Name

- name: IP facts | Set the interface_name fact
  set_fact:
    interface_name: "{{ cmd_output.stdout | trim }}"

- name: IP facts | Get the configured network interface index
  register: cmd_output
  win_shell: |
    $ErrorActionPreference = "Stop"
    $vEthernet = Get-NetAdapter -Name "vEthernet ({{ interface_name }})" -ErrorAction SilentlyContinue
    if($vEthernet) {
        $vEthernet.InterfaceIndex
    } else {
        "{{ interface_index }}"
    }

- name: IP facts | Set the configured_interface_index fact
  set_fact:
    configured_interface_index: "{{ cmd_output.stdout | trim }}"

- name: IP facts | Validate IP address on the preferred network interface
  win_shell: |
    $ErrorActionPreference = "Stop"
    $netAddress = Get-NetIPAddress -InterfaceIndex "{{ configured_interface_index }}"
    if(!$netAddress) {
        Throw "The network interface {{ configured_interface_index }} doesn't have any addresses configured"
    }
    if($netAddress.Count -gt 1) {
        Throw "There is more than one IP address configured on the interface {{ configured_interface_index }}"
    }

- name: IP facts | Get the internal IP address
  register: cmd_output
  win_shell: (Get-NetIPAddress -InterfaceIndex "{{ configured_interface_index }}").IPAddress

- name: IP facts | Set the host_internal_ip and host_public_ip facts
  set_fact:
    host_internal_ip: "{{ cmd_output.stdout | trim }}"
    # TODO: Set this to the proper public IP address in order to have support
    #       for GCE or cross-cloud deployments of k8s. For now, we simply set
    #       it to the host_internal_ip.
    host_public_ip: "{{ cmd_output.stdout | trim }}"

- debug: var=interface_index
- debug: var=configured_interface_index
- debug: var=interface_name
- debug: var=interface_default_gateway
- debug: var=host_internal_ip
- debug: var=host_public_ip
