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

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

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

# Verify if Shadow CIB already contains same constraint id.
- name: Verify colocation 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 colocation 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 colocation constraint {{ __ha_cluster_constraint_id }}
  ansible.builtin.shell:
    cmd: |
      yes 'n' | crm -c {{ __ha_cluster_crm_shadow }} \
      configure colocation {{ __ha_cluster_constraint_id }} \
      {% for option in constraint.options | d([]) if option.name == 'score' %}
        {{ option.value | lower | replace('infinity', 'inf') | quote }}: \
      {% else %}
        inf: \
      {% endfor %}
      {% if constraint.resource_follower.role | d() and
      constraint.resource_follower.role | lower in __ha_cluster_crmsh_roles %}
        {{ constraint.resource_follower.id | quote }}:{{
          constraint.resource_follower.role | lower | capitalize | quote }} \
      {% else %}
        {{ constraint.resource_follower.id | quote }} \
      {% endif %}
      {% if constraint.resource_leader.role | d() and
        constraint.resource_leader.role | lower in __ha_cluster_crmsh_roles %}
        {{ constraint.resource_leader.id | quote }}:{{
          constraint.resource_leader.role | lower | capitalize | quote }} \
      {% else %}
        {{ constraint.resource_leader.id | quote }} \
      {% endif %}
      {% for option in constraint.options | d([]) if option.name != 'score' %}
        {{ 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
