---
- name: Test servicedelegationtarget
  hosts: "{{ ipa_test_host | default('ipaserver') }}"
  # Change "become" or "gather_facts" to "yes",
  # if you test playbook requires any.
  become: no
  gather_facts: yes

  tasks:

  # CLEANUP TEST ITEMS

  - name: Ensure servicedelegationtarget test-delegation-target is absent
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      state: absent

  - name: Ensure test services are absent
    ipaservice:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name:
      - "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      - "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
      - "{{ 'test-service3/' + ansible_facts['fqdn'] }}"
      - "{{ 'not-existing-test-service/' + ansible_facts['fqdn'] }}"
      state: absent
      continue: yes

  # CREATE TEST ITEMS

  - name: Ensure service test-sevice1 is present
    ipaservice:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure service test-sevice2 is present
    ipaservice:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure service test-sevice3 is present
    ipaservice:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: "{{ 'test-service3/' + ansible_facts['fqdn'] }}"
    register: result
    failed_when: not result.changed or result.failed

  # TESTS

  - name: Ensure servicedelegationtarget test-delegation-target is present
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target is present again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
    register: result
    failed_when: result.changed or result.failed

  - name: Do not fail to ensure absence of not existing servicedelegationtarget test-delegation-target member principal
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'not-existing-test-service/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service1/' + ansible_facts['fqdn'] }}" is present
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      action: member
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service1/' + ansible_facts['fqdn'] }}" is present again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      action: member
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service2/' + ansible_facts['fqdn'] }}" is present
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
      action: member
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service2/' + ansible_facts['fqdn'] }}" is present again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
      action: member
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target principal is empty
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: []
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target principal is empty, again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: []
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service1/' + ansible_facts['fqdn'] }}" is absent
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service1/' + ansible_facts['fqdn'] }}" is absent
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service3/' + ansible_facts['fqdn'] }}" is present
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service3/' + ansible_facts['fqdn'] }}"
      action: member
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service3/' + ansible_facts['fqdn'] }}" is present again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service3/' + ansible_facts['fqdn'] }}"
      action: member
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target principal
      "{{ 'test-service1/' + ansible_facts['fqdn'] }}" and
      "{{ 'test-service2/' + ansible_facts['fqdn'] }}" are set
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal:
      - "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      - "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target principal
      "{{ 'test-service1/' + ansible_facts['fqdn'] }}" and
      "{{ 'test-service2/' + ansible_facts['fqdn'] }}" are set, again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal:
      - "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      - "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
    register: result
    failed_when: result.changed or result.failed

  - name: Fail if servicedelegationtarget test-delegation-target member principal "{{ 'test-service3/' + ansible_facts['fqdn'] }}" can be removed
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service3/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service1/' + ansible_facts['fqdn'] }}" is absent
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service1/' + ansible_facts['fqdn'] }}" is absent, again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service2/' + ansible_facts['fqdn'] }}" is absent
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target member principal "{{ 'test-service2/' + ansible_facts['fqdn'] }}" is absent, again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      principal: "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target is absent
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      state: absent
    register: result
    failed_when: not result.changed or result.failed

  - name: Ensure servicedelegationtarget test-delegation-target is absent again
    ipaservicedelegationtarget:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-target
      state: absent
    register: result
    failed_when: result.changed or result.failed

  # CLEANUP TEST ITEMS

  - name: Ensure test services are absent
    ipaservice:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name:
      - "{{ 'test-service1/' + ansible_facts['fqdn'] }}"
      - "{{ 'test-service2/' + ansible_facts['fqdn'] }}"
      - "{{ 'test-service3/' + ansible_facts['fqdn'] }}"
      state: absent
      continue: yes
