---
- name: Test dnszone
  hosts: ipaserver
  become: yes
  gather_facts: yes

  tasks:

  # Setup
  - name: Setup testing environment
    ansible.builtin.include_tasks: env_setup.yml

  # Tests
  - name: Ensure zone exists for reverse IP.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: 192.0.2.3/24
    register: ipv4_zone
    failed_when: not ipv4_zone.changed or ipv4_zone.failed

  - name: Ensure zone exists for reverse IP, again.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: 192.0.2.3/24
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure zone exists for reverse IP, given the zone name.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name: "{{ ipv4_zone.dnszone.name }}"
    register: result
    failed_when: result.changed or result.failed

  - name: Modify existing zone, using `name_from_ip`.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: 192.0.2.3/24
      default_ttl: 1234
    register: result
    failed_when: not result.changed or result.failed

  - name: Modify existing zone, using `name_from_ip`, again.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: 192.0.2.3/24
      default_ttl: 1234
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure ipv6 zone exists for reverse IPv6.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: fd00::0001
    register: ipv6_zone
    failed_when: not ipv6_zone.changed or ipv6_zone.failed

  # - ansible.builtin.debug:
  #     msg: "{{ipv6_zone}}"

  - name: Ensure ipv6 zone was created.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name: "{{ ipv6_zone.dnszone.name }}"
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure ipv6 zone exists for reverse IPv6, again.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: fd00::0001
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure second ipv6 zone exists for reverse IPv6.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: 2001:db8:cafe:1::1
    register: ipv6_sec_zone
    failed_when: not ipv6_sec_zone.changed or ipv6_zone.failed or ipv6_sec_zone.failed

  - name: Ensure second ipv6 zone was created.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name: "{{ ipv6_sec_zone.dnszone.name }}"
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure second ipv6 zone exists for reverse IPv6, again.
    ipadnszone:
      ipaadmin_password: SomeADMINpassword
      name_from_ip: 2001:db8:cafe:1::1
    register: result
    failed_when: result.changed or result.failed

  # Teardown
  - name: Teardown testing environment
    ansible.builtin.include_tasks: env_teardown.yml
