{% if slap_software_type in software_type -%}

[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
rendered = ${buildout:directory}/${:filename}
extra-context =
context =
    import json_module json
    key eggs_directory buildout:eggs-directory
    key develop_eggs_directory buildout:develop-eggs-directory
    key slap_software_type slap-parameter:slap_software_type
    key slave_instance_list slap-parameter:slave_instance_list
    ${:extra-context}

{% set part_list = [] -%}
{% set monitor_url_list = [] -%}
{% set single_type_key = 'single-' %}
{% if slap_software_type in ("replicate", "RootSoftwareInstance") %}
{%   set dns_type = slapparameter_dict.pop('-dns-type', 'single-default') -%}
{% else -%}
{%   set dns_type = "%s%s" % (single_type_key, slap_software_type) -%}
{% endif -%}
{% set dns_quantity = slapparameter_dict.pop('-dns-quantity', '1') | int -%}
{% set slave_list_name = 'extra_slave_instance_list' -%}
{% set dns_list = [] %}
{% set dns_domain_list = [] %}
{% set dns_section_list = [] %}
{% set namebase = 'powerdns' -%}
# XXX Dirty hack, not possible to define default value before
{% set sla_computer_powerdns_1_key = '-sla-1-computer_guid' -%}
{% if not sla_computer_powerdns_1_key in slapparameter_dict -%}
{%   do slapparameter_dict.__setitem__(sla_computer_powerdns_1_key, '${slap-connection:computer-id}') -%}
{% endif -%}

## DNS set up
{%- set supported_zone_list = slapparameter_dict.pop('supported-zone-list', 'domain.com').split() %}
{% set server_admin = slapparameter_dict.pop('server-admin', 'admin@domain.com') %}
{% set dns_name_template_string = slapparameter_dict.pop('dns-name-template-string', 'ns%s.domain.com') %}

# Here we request individualy each dns.
# The presence of sla parameters is checked and added if found
{% for i in range(1, dns_quantity + 1) -%}
{%   set dns_name = 'ns%s' % i -%}
{%   set dns_domain = dns_name_template_string % i %}
{%   set request_section_title = 'request-%s' % dns_name -%}
{%   set promise_section_title = 'promise-listen-port-%s' % dns_name -%}
{%   set sla_key = "-sla-%s-" % i -%}
{%   set sla_key_length = sla_key | length %}
{%   set sla_parameters = [] %}
{%   for key in slapparameter_dict.keys() %}
{%     if key.startswith(sla_key) %}
{%       do sla_parameters.append(key[sla_key_length:]) %}
{%     endif -%}
{%   endfor -%}
{%   do dns_list.append(dns_name) -%}
{%   do dns_domain_list.append(dns_domain) -%}
{%   do dns_section_list.append(request_section_title) -%}
{%   do part_list.append(request_section_title) -%}
{%   do part_list.append(promise_section_title) -%}

[{{request_section_title}}]
<= replicate
name = {{dns_name}}
{%   set state_key = "-dns-%s-state" % i %}
{%   if state_key in slapparameter_dict %}
state = {{ slapparameter_dict.pop(state_key) }}
{%   endif%}
config-supported-zone-list = {{ ' '.join(supported_zone_list) }}
config-soa = {{ "%s,%s" % (dns_domain, server_admin) }}
{%   for parameter in sla_parameters -%}
sla-{{ parameter }} = {{ slapparameter_dict.pop( sla_key + parameter ) }}
{%   endfor -%}

[{{promise_section_title}}]
<= monitor-promise-base
module = check_socket_listening
name = {{promise_section_title}}.py
config-host = {{ '${' ~ request_section_title ~ ':connection-powerdns-ipv6}' }}
config-port = {{ '${' ~ request_section_title ~ ':connection-powerdns-port}' }}

{%  do monitor_url_list.append('${' ~ request_section_title ~ ':connection-monitor-base-url}') -%}
{% endfor -%}

[monitor-htpasswd]
recipe = slapos.cookbook:generate.password
storage-path = ${directory:etc}/.monitor_user
bytes = 8
username = admin

[monitor-instance-parameter]
monitor-httpd-port = 8099
cors-domains = {{ slapparameter_dict.get('monitor-cors-domains', 'monitor.app.officejs.com') }}
username = admin
password = ${monitor-htpasswd:passwd}

[monitor-conf-parameters]
monitor-url-list +=
{% for url in monitor_url_list -%}
{{ '  ' ~ url }}
{% endfor %}

{% set ns_record = slapparameter_dict.pop('ns-record', ','.join(dns_domain_list)) %}
[replicate]
<= slap-connection
recipe = slapos.cookbook:requestoptional
{% set dns_software_url_key = "-dns-software-release-url" %}
{% if dns_software_url_key in slapparameter_dict %}
software-url = {{ slapparameter_dict.pop(dns_software_url_key) }}
{% else %}
software-url = ${slap-connection:software-release-url}
{% endif %}
software-type = {{dns_type}}
return = slave-instance-information-list monitor-base-url powerdns-ipv6 powerdns-port powerdns-ipv4
config-server-admin = {{ server_admin }}
config-ns-record = {{ ns_record }}
{% for parameter, value in slapparameter_dict.items() -%}
config-{{parameter}} = {{ value }}
{% endfor -%}
config-{{ slave_list_name }} = {{ json_module.dumps(slave_instance_list) }}
config-monitor-cors-domains = {{ slapparameter_dict.get('monitor-cors-domains', 'monitor.node.vifib.com') }}
config-monitor-username = admin
config-monitor-password = ${monitor-htpasswd:passwd}

[publish-information]
<= monitor-publish
recipe = slapos.cookbook:publish
slave-amount = {{ slave_instance_list | length }}
ns-record = {{ ns_record }}
{% for dns_name, dns_section in zip(dns_list, dns_section_list) -%}
{%   set dns_ipv6 = '${' ~ dns_section ~ ':connection-powerdns-ipv6}' -%}
{%   set dns_port = '${' ~ dns_section ~ ':connection-powerdns-port}' -%}
{{ dns_name }}-ipv6 = {{ dns_ipv6 }}
{{ dns_name }}-port = {{ dns_port }}
{% endfor -%}

[buildout]

extends = 
  {{ template_monitor }}
parts =
  monitor-base
  publish-information
{% for part in part_list -%}
{{ '  %s' % part }}
{% endfor -%}

eggs-directory = {{ eggs_directory }}
develop-eggs-directory = {{ develop_eggs_directory }}
offline = true


[slap_connection]
# Kept for backward compatiblity
computer_id = ${slap-connection:computer-id}
partition_id = ${slap-connection:partition-id}
server_url = ${slap-connection:server-url}
software_release_url = ${slap-connection:software-release-url}
key_file = ${slap-connection:key-file}
cert_file = ${slap-connection:cert-file}

[slap-parameter]
{% for k, v in slapparameter_dict.items() -%}
{{ k }} = {{ v }}
{% endfor -%}
{%- endif %}
