# SPDX-License-Identifier: MIT
---
# Verify if resources in sets exist
- name: Verify set resources presence
  ansible.builtin.command:
    cmd: >-
      crm -c {{ __ha_cluster_crm_shadow }}
      configure show {% for set in constraint.resource_sets %}
        {% for resource in set.resource_ids %}
          {{ resource | quote }}
        {% endfor %}
      {% endfor %}
  register: __ha_cluster_constraint_resource_set
  changed_when: false
  failed_when:
    - "'does not exist' in __ha_cluster_constraint_resource_set.stderr"

- name: Define {{ constraint_type }} set constraint.id '{{
    constraint_index | string }}'  # noqa name[template]
  ansible.builtin.set_fact:
    __ha_cluster_constraint_id:
      "{{ constraint.id if constraint.id is defined else
       'set_' + constraint_type + '_' + constraint_index | string }}"

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

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

# Expect module is used to combat crmsh freezing when asking for user input
- name: Configure {{ constraint_type }} constraint set '{{
    __ha_cluster_constraint_id }}'  # noqa name[template]
  ansible.builtin.expect:
    command: |
      crm -c {{ __ha_cluster_crm_shadow }} configure
      {{ constraint_type if constraint_type != 'ticket' else 'rsc_ticket' }}
      {{ __ha_cluster_constraint_id }}
      {% if constraint_type != 'ticket' %}
        {% if constraint_type == 'order' %}
          {% for option in constraint.options | d([])
            if option.name == 'kind' %}
            {{ option.value | quote }}:
          {% else %}
            Mandatory:
          {% endfor %}
        {% elif constraint_type == 'colocation' %}
          {% for option in constraint.options | d([])
            if option.name == 'score' %}
            {{ option.value | lower | replace('infinity', 'inf') | quote }}:
          {% else %}
            inf:
          {% endfor %}
        {% endif %}
        {% for set in constraint.resource_sets %}
            ({% for resource in set.resource_ids %}
              {{ resource | quote }}
            {% endfor %})
        {% endfor %}
      {% else %}
        {% for set in constraint.resource_sets %}
          {{ constraint.ticket | quote }}:
          {% for resource in set.resource_ids %}
            {{ resource | quote }}
          {% endfor %}
        {% endfor %}
      {% endif %}
      {% for option in constraint.options | d([]) %}
        {{ option.name | quote }}={{ option.value | quote }}
      {% endfor %}
    # ERROR and "Do you still want to commit (y/n)?" trigger response "n".
    responses:
      ".*ERROR.*": "n"
      ".*y/n*": "n"
  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.stdout_lines }}"
  when:
    - __ha_cluster_crmsh_output is defined
    - __ha_cluster_crmsh_output.rc != 0
