{%- if slap_software_type == software_type %}

[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
output = ${buildout:directory}/${:filename}
extra-context =
context =
    import json_module json
    ${:extra-context}

[slave-test-configuration]
<=jinja2-template-base
url = {{ template_json_edgetest_test }}
filename = srv/monitor/private/test.json
extensions = jinja2.ext.do 
extra-context = 
  section slave_information slap-configuration

{%  set part_list = [] -%}
{%- set edgebot_software_type = 'edgebot' %}
{%- set slave_list_name = 'extra_slave_instance_list' %}
{%- set request_dict = {} %}
{%- set namebase = "edgebot" %}
{%- if 'region-dict' not in slapparameter_dict %}
{#-   Be nice and allow to work with default configuration #}
{%-   do slapparameter_dict.__setitem__('region-dict', {
          '1': {
            'sla-computer_guid': slap_configuration['computer'],
            'state': slap_configuration['instance-state'],
            'nameserver-list': slapparameter_dict.get('nameserver-list', []),
            'check-frontend-ip-list': slapparameter_dict.get('check-frontend-ip-list', []),
          }
        }) %}
{%- endif %}
{%- set active_region_list = [] %}
{%- for region_name in sorted(slapparameter_dict['region-dict']) %}
{%-   set region_parameter_dict = slapparameter_dict['region-dict'][region_name] %}
{%-   if region_parameter_dict.get('state', 'started') == 'started' %}
{%-     do active_region_list.append(region_name) %}
{%-   endif %}
{%- endfor %}
{%- set authorized_slave_dict = {} %}
{%- set publish_slave_dict_dict = {} %}
{%- for slave in slave_instance_list | sort(attribute='slave_title') %}
{%-   set slave_reference = slave.pop('slave_reference') %}
{%-   set publish_dict = {'assigned-region-dict': {}} %}
{%-   if '_' in slave %}
{%-     set base_slave_dict = json_module.loads(slave.pop('_')) %} {#- XXX: Unsafe! #}
{%-     do base_slave_dict.__setitem__('-slave-title', slave['slave_title']) %}
{%-     do base_slave_dict.__setitem__('-slave-reference', slave_reference) %}
{%-     set slave_region_dict = base_slave_dict.pop('region-dict', {}) %}
{%-     if slave_region_dict == {} %}
{%-       for region in active_region_list %}
{%-         do slave_region_dict.__setitem__(region, {}) %}
{%-       endfor %}
{%-     endif %}
{%-     for region in slave_region_dict %}
{%-       if region in active_region_list %}
{%-         set region_info = {
  'nameserver-list': slapparameter_dict['region-dict'][region].get('nameserver-list') or slapparameter_dict.get('slapparameter_dict') or [],
  'check-frontend-ip-list': slave_region_dict[region].get('check-frontend-ip-list') or base_slave_dict.get('check-frontend-ip-list') or slapparameter_dict['region-dict'][region].get('check-frontend-ip-list') or slapparameter_dict.get('check-frontend-ip-list') or [],
} %}
{%-         do publish_dict['assigned-region-dict'].__setitem__(region, region_info) %}
{%-         set slave_dict = base_slave_dict.copy() %}
{%-         do slave_dict.update(region_info) %}
{%-         if region not in authorized_slave_dict %}
{%-           do authorized_slave_dict.__setitem__(region, [slave_dict]) %}
{%-         else %}
{%-           do authorized_slave_dict[region].append(slave_dict) %}
{%-         endif %}
{%-       endif %}
{%-     endfor %}
{%-   endif %}
{%-   do publish_slave_dict_dict.__setitem__(slave_reference, publish_dict) %}
{%- endfor %}
{%- set monitor_base_port = int(slap_configuration['configuration.monitor-base-port']) %}
{%- set number = {'i': 1} %}
{%- for region_name in sorted(slapparameter_dict['region-dict']) %}
{%-   set region_parameter_dict = slapparameter_dict['region-dict'][region_name] %}
{%-   set edgebot_name = "%s-%s" % (namebase, region_name) %}
{%-   set request_section_title = 'request-%s' % (hashlib_module.md5(edgebot_name.encode('utf-8')).hexdigest(),) %}
{%-   do part_list.append(request_section_title) %}
{#-   Note: monitor-httpd-port will vary on regions being added and removed, 
            but this is accepted, as it's only internal trick #}
{%-   do request_dict.__setitem__(
        request_section_title,
        {
          'config': {
            'monitor-httpd-port': monitor_base_port + number['i'],
            'check-frontend-ip-list': region_parameter_dict.get('check-frontend-ip-list', []),
            'nameserver-list': region_parameter_dict.get('nameserver-list', []),
            'extra_slave_instance_list': authorized_slave_dict.get(region_name, [])
          },
          'name': edgebot_name,
          'sla': {'computer_guid': region_parameter_dict['sla-computer_guid']},
          'state': region_parameter_dict.get('state', 'started'),
        }) %}
{%-   do number.__setitem__('i', number['i'] + 1) %}
{%- endfor %}

[replicate]
<= slap-connection
recipe = slapos.cookbook:request.serialised
config-monitor-cors-domains = {{ slapparameter_dict.get('monitor-cors-domains', 'monitor.app.officejs.com') }}
config-monitor-username = ${monitor-instance-parameter:username}
config-monitor-password = ${monitor-htpasswd:passwd}
software-url = ${slap-connection:software-release-url}
software-type = {{edgebot_software_type}}

{%- set monitor_base_url_dict = {} -%}
{%  for section, edgebot_request in request_dict.items() %}
[{{section}}]
<= replicate
name = {{ edgebot_request['name'] }}
state = {{ edgebot_request['state'] }}
{%-   if edgebot_request['state'] != 'destroyed' %}
{%-     do monitor_base_url_dict.__setitem__(section, '${' ~ section ~ ':connection-monitor-base-url}') %}
return = monitor-base-url
{%-   endif %}
{%-   set edgebot_configuration_dict = edgebot_request['config'] %}
{%-   for config_key, config_value in edgebot_configuration_dict.items() %}
config-{{ config_key }} = {{ dumps(config_value) }}
{%    endfor -%}
{%-   for parameter, value in edgebot_request['sla'].items() %}
sla-{{ parameter }} = {{ value }}
{%-   endfor %}
{%- endfor %}
# Publish information for each slave
{%- for slave_reference, publish_dict in publish_slave_dict_dict.items() -%}
{%-   set publish_section_title = 'publish-%s' % (hashlib_module.md5(slave_reference.encode('utf-8')).hexdigest(),)  -%}
{%-   do part_list.append(publish_section_title) %}
[{{ publish_section_title }}]
recipe = slapos.cookbook:publish.serialised
available-region-list = {{ dumps(list(active_region_list)) }}
-slave-reference = {{ slave_reference }}
{%-   for key, value in publish_dict.items() %}
{{ key }} = {{ dumps(value) }}
{%-   endfor %}
{%  endfor %}

[monitor-conf-parameters]
monitor-title = Monitor
password = ${monitor-htpasswd:passwd}

[monitor-base-url-dict]
{%  for key, value in monitor_base_url_dict.items() -%}
{{ key }} = {{ value }}
{%  endfor %}

[buildout]
extends = {{ instance_base_monitor }}
parts +=
  slave-test-configuration
{%  for part in part_list %}
{{ '  %s' % part }}
{%- endfor %}

[publish-connection-information]
recipe = slapos.cookbook:publish.serialised
active-region-list = {{ dumps(list(active_region_list)) }}
sla-computer_guid = {{ dumps(slap_configuration['computer']) }}
sla-instance_guid = {{ dumps(slap_configuration['instance-guid']) }}
{%- endif %} {#- if slap_software_type == software_type #}
