{% set role = grains.id.split('_') | last %}
{% from 'common/maps/'~ role ~'.map.jinja' import docker with context %}

# Check if the service is enabled and append it's required containers
# to the list predefined by the role / minion id affix
{% macro append_containers(pillar_name, k, compare )%}
  {% if salt['pillar.get'](pillar_name~':'~k, {}) != compare %}
    {% if k == 'enabled' %}
      {% set k = pillar_name %}
    {% endif %}
    {% from 'common/maps/'~k~'.map.jinja' import docker as d with context %}
    {% for li in d['containers'] %}
      {{ docker['containers'].append(li) }}
    {% endfor %}
  {% endif %}
{% endmacro %}

{% set docker = salt['grains.filter_by']({
  '*_'~role: {
    'containers': docker['containers']
  } 
},grain='id', merge=salt['pillar.get']('docker')) %}

{% if role in ['eval', 'managersearch', 'manager', 'standalone'] %}
  {{ append_containers('manager', 'grafana', 0) }}
  {{ append_containers('global', 'fleet_manager', 0) }}
  {{ append_containers('global', 'wazuh', 0) }}
  {{ append_containers('manager', 'thehive', 0) }}
  {{ append_containers('manager', 'playbook', 0) }}
  {{ append_containers('manager', 'freq', 0) }}
  {{ append_containers('manager', 'domainstats', 0) }}
{% endif %}

{% if role in ['eval', 'heavynode', 'sensor', 'standalone'] %}
  {{ append_containers('strelka', 'enabled', 0) }}
{% endif %}

{% if role in ['heavynode', 'standalone'] %}
  {{ append_containers('global', 'mdengine', 'SURICATA') }}
{% endif %}

{% if role == 'searchnode' %}
  {{ append_containers('manager', 'wazuh', 0) }}
{% endif %}

{% if role == 'sensor' %}
  {{ append_containers('global', 'mdengine', 'SURICATA') }}
{% endif %}