---
- name: Test dnsconfig
  hosts: "{{ ipa_test_host | default('ipaserver') }}"
  become: true
  gather_facts: true

  tasks:
  # Setup.
  - name: Ensure forwarders are absent.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.8.8
        - ip_address: 8.8.4.4
      state: absent
      action: member

  # Tests.
  - name: Set forward with invalid IPv4.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 1.2.3.500
    register: result
    failed_when: not result.failed or "Invalid IP for DNS forwarder" not in result.msg

  - name: Set config to invalid IP.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 1.in.va.lid
    register: result
    failed_when: not result.failed or "Invalid IP for DNS forwarder" not in result.msg

  - name: Set config to invalid IPv6.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: fd00::invalid
    register: result
    failed_when: not result.failed or "Invalid IP for DNS forwarder" not in result.msg

  - name: Set dnsconfig.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.8.8
        - ip_address: 8.8.4.4
      forward_policy: only
      allow_sync_ptr: yes
    register: result
    failed_when: not result.changed or result.failed

  - name: Set dnsconfig, with the same values.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.8.8
        - ip_address: 8.8.4.4
      forward_policy: only
      allow_sync_ptr: yes
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure forwarder 8.8.8.8 is absent.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.8.8
      state: absent
      action: member
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure forwarder 8.8.8.8 is absent, again.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.8.8
      state: absent
      action: member
    register: result
    failed_when: result.changed or result.failed

  - name: Check if forwarder 8.8.4.4 is present.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.4.4
    check_mode: yes
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure forwarder 8.8.8.8 is present.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.8.8
    register: result
    failed_when: not result.changed or result.failed

  - name: Check forwarder 8.8.4.4 is still present.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.4.4
    check_mode: yes
    register: result
    failed_when: not result.changed or result.failed

  - name: Disable global forwarders.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forward_policy: none
    register: result
    failed_when: not result.changed or result.failed

  - name: Disable global forwarders, again.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forward_policy: none
    register: result
    failed_when: result.changed or result.failed

  - name: Re-enable global forwarders.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forward_policy: first
    register: result
    failed_when: not result.changed or result.failed

  - name: Re-enable global forwarders, again.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forward_policy: first
    register: result
    failed_when: result.changed or result.failed

  - name: Disable PTR record synchronization.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      allow_sync_ptr: no
    register: result
    failed_when: not result.changed or result.failed

  - name: Disable PTR record synchronization, again.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      allow_sync_ptr: no
    register: result
    failed_when: result.changed or result.failed

  - name: Re-enable PTR record synchronization.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      allow_sync_ptr: yes
    register: result
    failed_when: not result.changed or result.failed

  - name: Re-enable PTR record synchronization, again.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      allow_sync_ptr: yes
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure forwarders are absent.
    ipadnsconfig:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      forwarders:
        - ip_address: 8.8.8.8
        - ip_address: 8.8.4.4
      state: absent
      action: member
