---
- set_fact:
    get_ip_script: |
      $adapter = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetConnectionID='{{ network_adapter_name }}'"
      $config = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index=$($adapter.DeviceID)"
      $ips = $config.DNSServerSearchOrder
      if ($ips) {
          $config.DNSServerSearchOrder[0]
          $config.DNSServerSearchOrder[1]
      }

- name: set a single IPv4 address (check mode)
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses: 192.168.34.5
  register: set_single_check
  check_mode: yes

- name: get result of set a single IPv4 address (check mode)
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: set_single_actual_check

- name: assert set a single IPv4 address (check mode)
  assert:
    that:
    - set_single_check is changed
    - set_single_actual_check.stdout_lines == []

- name: set a single IPv4 address
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses: 192.168.34.5
  register: set_single

- name: get result of set a single IPv4 address
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: set_single_actual

- name: assert set a single IPv4 address
  assert:
    that:
    - set_single is changed
    - set_single_actual.stdout_lines == ["192.168.34.5"]

- name: set a single IPv4 address (idempotent)
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses: 192.168.34.5
  register: set_single_again

- name: assert set a single IPv4 address (idempotent)
  assert:
    that:
    - not set_single_again is changed

- name: change IPv4 address to another value (check mode)
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses: 192.168.34.6
  register: change_single_check
  check_mode: yes

- name: get result of change IPv4 address to another value (check mode)
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: check_single_actual_check

- name: assert change IPv4 address to another value (check mode)
  assert:
    that:
    - change_single_check is changed
    - check_single_actual_check.stdout_lines == ["192.168.34.5"]

- name: change IPv4 address to another value
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses: 192.168.34.6
  register: change_single

- name: get result of change IPv4 address to another value
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: check_single_actual

- name: assert change IPv4 address to another value
  assert:
    that:
    - change_single is changed
    - check_single_actual.stdout_lines == ["192.168.34.6"]

- name: set multiple IPv4 addresses (check mode)
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses:
    - 192.168.34.7
    - 192.168.34.8
  register: set_multiple_check
  check_mode: yes

- name: get result of set multiple IPv4 addresses (check mode)
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: set_multiple_actual_check

- name: assert set multiple IPv4 addresses (check mode)
  assert:
    that:
    - set_multiple_check is changed
    - set_multiple_actual_check.stdout_lines == ["192.168.34.6"]

- name: set multiple IPv4 addresses
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses:
    - 192.168.34.7
    - 192.168.34.8
  register: set_multiple

- name: get result of set multiple IPv4 addresses
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: set_multiple_actual

- name: assert set multiple IPv4 addresses
  assert:
    that:
    - set_multiple is changed
    - set_multiple_actual.stdout_lines == ["192.168.34.7", "192.168.34.8"]

- name: set multiple IPv4 addresses (idempotent)
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses:
    - 192.168.34.7
    - 192.168.34.8
  register: set_multiple_again

- name: assert set multiple IPv4 addresses (idempotent)
  assert:
    that:
    - not set_multiple_again is changed

- name: reset IPv4 DNS back to DHCP (check mode)
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses: []
  register: set_dhcp_check
  check_mode: yes

- name: get result of reset IPv4 DNS back to DHCP (check mode)
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: set_dhcp_actual_check

- name: assert reset IPv4 DNS back to DHCP (check mode)
  assert:
    that:
    - set_dhcp_check is changed
    - set_dhcp_actual_check.stdout_lines == ["192.168.34.7", "192.168.34.8"]

- name: reset IPv4 DNS back to DHCP
  win_dns_client:
    adapter_names: '{{ network_adapter_name }}'
    ipv4_addresses: []
  register: set_dhcp

- name: get result of reset IPv4 DNS back to DHCP
  win_shell: '{{ get_ip_script }}'
  changed_when: no
  register: set_dhcp_actual

- name: assert reset IPv4 DNS back to DHCP
  assert:
    that:
    - set_dhcp is changed
    - set_dhcp_actual.stdout_lines == []
