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

  tasks:

  # CLEANUP TEST ITEMS
  - name: Ensure ipa_server_name is set
    when: ipa_server_name is not defined
    block:
      - name: Get server name from hostname
        ansible.builtin.set_fact:
          ipa_server_name: "{{ ansible_facts['fqdn'].split('.')[0] }}"
    rescue:
      - name: Fallback to 'ipaserver'
        ansible.builtin.set_fact:
          ipa_server_name: ipaserver

  - name: Ensure ipaserver_domain is set
    when: ipaserver_domain is not defined
    block:
      - name: Get domain name from hostname.
        ansible.builtin.set_fact:
          ipaserver_domain: "{{ ansible_facts['fqdn'].split('.')[1:] | join('.') }}"
    rescue:
      - name: Fallback to 'ipa.test'
        ansible.builtin.set_fact:
          ipaserver_domain: "ipa.test"

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" without location
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      location: ""

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" without service weight
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      service_weight: -1

  - name: Ensure location "mylocation" is absent
    ipalocation:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: mylocation
      state: absent

  # CREATE TEST ITEMS

  - name: Ensure location "mylocation" is present
    ipalocation:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: mylocation
    register: result
    failed_when: not result.changed or result.failed

  # TESTS

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" is present
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" with location "mylocation"
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      location: "mylocation"
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" with location "mylocation" again
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      location: "mylocation"
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" without location
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      location: ""
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" without location again
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      location: ""
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" with service weight 1
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      service_weight: 1
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" with service weight 1 again
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      service_weight: 1
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" without service weight
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      service_weight: -1
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure server "{{ ipa_server_name + '.' + ipaserver_domain }}" without service weight again
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ ipa_server_name + '.' + ipaserver_domain }}"
      service_weight: -1
    register: result
    failed_when: result.changed or result.failed

  # hidden requires an additional server, not tested

  # absent requires an additional server, only sanity test with absent server

  - name: Ensure server "{{ 'absent.' + ipaserver_domain }}" is absent
    ipaserver:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ 'absent.' + ipaserver_domain }}"
      state: absent
    register: result
    failed_when: result.changed or result.failed

  # ignore_last_of_role requires an additional server, not tested

  # ignore_topology_disconnect requires an additional server, not tested

  # CLEANUP TEST ITEMS

  - name: Ensure location "mylocation" is absent
    ipalocation:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: mylocation
      state: absent
    register: result
    failed_when: not result.changed or result.failed
