# SPDX-License-Identifier: MIT
---
# Verify if constraint.resource_first.id exists
- name: Verify resource_first presence {{ constraint.resource_first.id }}
  ansible.builtin.command:
    cmd: >-
      crm -c {{ __ha_cluster_crm_shadow }}
      configure show {{ constraint.resource_first.id }}
  register: __ha_cluster_constraint_resource_first
  changed_when: false
  failed_when:
    - "'does not exist' in __ha_cluster_constraint_resource_first.stderr"

# Verify if constraint.resource_then.id exists
- name: Verify resource_then presence {{ constraint.resource_then.id }}
  ansible.builtin.command:
    cmd: >-
      crm -c {{ __ha_cluster_crm_shadow }}
      configure show {{ constraint.resource_then.id }}
  register: __ha_cluster_constraint_resource_then
  changed_when: false
  failed_when:
    - "'does not exist' in __ha_cluster_constraint_resource_then.stderr"

- name: Define order constraint.id {{ constraint.id | d(constraint_index) }}
  ansible.builtin.set_fact:
    __ha_cluster_constraint_id:
      "{{ constraint.id if constraint.id is defined else
      'ord_' + (constraint.resource_first.id | quote) }}"

# Verify if Shadow CIB already contains same constraint id.
- name: Verify order constraint presence {{ __ha_cluster_constraint_id }}
  ansible.builtin.command:
    cmd: >-
      crm -c {{ __ha_cluster_crm_shadow }}
      configure show {{ __ha_cluster_constraint_id }}
  register: __ha_cluster_constraint_status
  changed_when: false
  failed_when: false

# Delete constraint id in Shadow CIB to avoid errors during cibadmin patch.
- name: Delete present order constraint {{ __ha_cluster_constraint_id }}
  ansible.builtin.command:
    cmd: >-
      crm --force -c {{ __ha_cluster_crm_shadow }}
      configure delete {{ __ha_cluster_constraint_id }}
  when: __ha_cluster_constraint_status.rc == 0
  check_mode: false
  changed_when: not ansible_check_mode


# Yes command will skip all prompts, resulting in rc>0
# exit $? reveals return code of crm, which is masked by yes
- name: Configure order constraint {{ __ha_cluster_constraint_id }}
  ansible.builtin.shell:
    cmd: |
      yes 'n' | crm -c {{ __ha_cluster_crm_shadow }} \
      configure order {{ __ha_cluster_constraint_id | quote }} \
      {% for option in constraint.options | d([]) if option.name == 'kind' %}
        {{ option.value | quote }}: \
      {% endfor %}
      {{ constraint.resource_first.id
       | quote }}:{{ constraint.resource_first.action
        | default('start') | quote }} \
      {{ constraint.resource_then.id
       | quote }}:{{ constraint.resource_then.action
        | default('start') | quote }} \
      {% for option in constraint.options | d([])
       if option.name != 'score' and option.name != 'kind'%}
        {{ option.name | quote }}={{ option.value | quote }} \
      {% endfor %}
      ;exit $?
  check_mode: false
  changed_when: not ansible_check_mode
  ignore_errors: true
  register: __ha_cluster_crmsh_output


- name: Display crm command error details
  ansible.builtin.fail:
    msg:
      - "{{ __ha_cluster_crmsh_output.stderr_lines }}"
      - "{{ __ha_cluster_crmsh_output.stdout_lines }}"
  when:
    - __ha_cluster_crmsh_output is defined
    - __ha_cluster_crmsh_output.rc != 0
