---
- name: Test servicedelegationrule
  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 servicedelegationrule test-delegation-rule is absent
    ipaservicedelegationrule:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-rule
      state: absent

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

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

  # CREATE TEST ITEMS

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

  - name: Ensure servicedelegationrule 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

  # TESTS

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

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

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

  - name: Do not fail to ensure absence of not existing servicedelegationrule test-delegation-rule member target
    ipaservicedelegationrule:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-rule
      target: not-existing-test-delegation-target
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

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

  - name: Ensure servicedelegationrule test-delegation-rule member target test-delegation-target is present, again
    ipaservicedelegationrule:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-rule
      target: test-delegation-target
      action: member
    register: result
    failed_when: result.changed or result.failed

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

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

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

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

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

  - name: Ensure servicedelegationrule test-delegation-rule member target test-delegation-target is absent, again
    ipaservicedelegationrule:
      ipaadmin_password: SomeADMINpassword
      ipaapi_context: "{{ ipa_context | default(omit) }}"
      name: test-delegation-rule
      target: test-delegation-target
      action: member
      state: absent
    register: result
    failed_when: result.changed or result.failed

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

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

  # CLEANUP TEST ITEMS

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

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