{#   Copyright (c) 2018 OpenStack Foundation
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
#}

{%- macro lb_algo_macro(pool) -%}
lb_algo {{ pool.lb_algorithm }}
{%- endmacro -%}

{% macro misc_path_macro(member, health_monitor) -%}
misc_path "{{ health_monitor.check_script_path }} {{ member.address }} {{ member.protocol_port }}"
{%- endmacro %}

{%- macro misc_check_macro(pool, member, health_monitor) -%}
MISC_CHECK {
            {{ misc_path_macro(member, health_monitor) }}
            misc_timeout {{ pool.health_monitor.delay }}
        }
{%- endmacro -%}

{% macro health_monitor_rs_macro(constants, pool, member) %}
        {% if pool.health_monitor and pool.health_monitor.enabled %}
        {% if pool.health_monitor.timeout %}
        delay_before_retry {{ pool.health_monitor.timeout }}
        {% endif %}
        {% if pool.health_monitor.fall_threshold %}
        retry {{ pool.health_monitor.fall_threshold }}
        {% endif %}
        {% if pool.health_monitor.type == constants.HEALTH_MONITOR_UDP_CONNECT %}
        {{ misc_check_macro(pool, member, pool.health_monitor) -}}
        {% endif %}
        {% endif %}
{% endmacro %}

{% macro realserver_macro(constants, pool, member, listener) %}
    # Configuration for Member {{ member.id }}
    real_server {{ member.address }} {{ member.protocol_port }} {
        weight {{ member.weight }}
        {% if listener.connection_limit %}
        uthreshold {{ listener.connection_limit }}
        {% endif %}
{{- health_monitor_rs_macro(constants, pool, member) }}
    }
{% endmacro %}

{% macro health_monitor_vs_macro(default_pool) %}
{% if default_pool and default_pool.health_monitor and default_pool.health_monitor.enabled %}
    {% if default_pool.health_monitor.delay %}
    delay_loop {{ default_pool.health_monitor.delay }}
    {% endif %}
    {% if default_pool.health_monitor.timeout %}
    delay_before_retry {{ default_pool.health_monitor.timeout }}
    {% endif %}
    {% if default_pool.health_monitor.fall_threshold %}
    retry {{ default_pool.health_monitor.fall_threshold }}
    {% endif %}
{% endif %}
{% endmacro %}

{% macro virtualserver_macro(constants, listener, lb_vip_address, default_pool) %}
{% set need_render = [] %}
{% if default_pool and default_pool.enabled and default_pool.members %}
    {% for member in default_pool.members if member.enabled %}
        {% do need_render.append(member.enabled) %}
    {% endfor %}
{% endif %}
{% if need_render|length > 0 %}
virtual_server {{ lb_vip_address }} {{ listener.protocol_port }} {
    {{ lb_algo_macro(default_pool) }}
    lb_kind NAT
    protocol {{ listener.protocol_mode.upper() }}
    {% if default_pool.session_persistence and default_pool.session_persistence.type == constants.SESSION_PERSISTENCE_SOURCE_IP %}
      {# set our defined defaults as I saw this not be consistent #}
      {# in testing #}
      {% if default_pool.session_persistence.persistence_timeout %}
    persistence_timeout {{ default_pool.session_persistence.persistence_timeout }}
      {% else %}
    persistence_timeout 360
      {% endif %}
      {% if default_pool.session_persistence.persistence_granularity %}
    persistence_granularity {{ default_pool.session_persistence.persistence_granularity }}
      {% else %}
    persistence_granularity 255.255.255.255
      {% endif %}
   {% endif %}
{{ health_monitor_vs_macro(default_pool) }}

{% if default_pool.protocol.lower() == "udp" %}
    # Configuration for Pool {{ default_pool.id }}
    {% if default_pool.health_monitor and default_pool.health_monitor.enabled %}
    # Configuration for HealthMonitor {{ default_pool.health_monitor.id }}
    {% endif %}
    {% for member in default_pool.members if member.enabled %}
        {{- realserver_macro(constants, default_pool, member, listener) }}
    {% endfor %}
{% endif %}
}
{% endif %}
{% endmacro %}
