# {{ ansible_managed }}
#---------------------------------------------------------------------
# service: {{ pg_cluster }}-{{ service.name }} @ {{ inventory_hostname }}:{{ service.port }}
#---------------------------------------------------------------------
{% set pg_cluster_members = hostvars|json_query(pg_cluster_members_query) %}
{% set pg_service_members = pg_cluster_members|json_query(service.selector) %}
{% set pg_service_member_names = pg_service_members|json_query('[].inventory_hostname') %}
{% set pg_service_backups = [] %}{% if service.backup is defined %}{% set pg_service_backups = pg_cluster_members|json_query(service.backup) %}{% endif %}
{% if not pg_service_backups %}{% set pg_service_backups = [] %}{% endif %}
{% set pg_service_backup_names = pg_service_backups|json_query('[].inventory_hostname') %}
{% set pg_rto_mode = pg_rto if pg_rto in pg_rto_plan else 'norm' %}
{% set rto = pg_rto_plan[pg_rto_mode] %}
{% set ha_inter = rto[5] %}{% set ha_fastinter = rto[6] %}{% set ha_downinter = rto[7] %}{% set ha_rise = rto[8] %}{% set ha_fall = rto[9] %}
{% set haproxy_rto_options = 'inter ' ~ ha_inter ~ ' fastinter ' ~ ha_fastinter ~ ' downinter ' ~ ha_downinter ~ ' rise ' ~ ha_rise ~ ' fall ' ~ ha_fall ~ ' on-marked-down shutdown-sessions slowstart 30s maxconn 3000 maxqueue 128 weight 100' %}
# service instances {{ pg_service_member_names|join(', ') }}
# service backups   {{ pg_service_backup_names|join(', ') }}
listen {{ pg_cluster }}-{{ service.name }}
    bind {{ service.ip|default('*') }}:{{ service.port }}
    mode tcp
    maxconn {{ service.maxconn|default(5000) }}
    balance {{ service.balance|default('roundrobin') }}
    option httpchk
    option http-keep-alive
    http-check send meth OPTIONS uri {{ service.check|default('/') }}
    http-check expect status 200
    default-server {{ service.options|default(haproxy_rto_options) }}

    # servers
{% for host in pg_service_members %}
{% if service.dest is not defined or service.dest == 'default' %}{% set svc_dest_port = pg_default_service_dest|default('postgres') %}{% else %}{% set svc_dest_port = service.dest %}{% endif %}
    server {{ host.pg_cluster }}-{{ host.pg_seq }} {{ host.inventory_hostname }}:{% if svc_dest_port is number %}{{ svc_dest_port }}{% elif svc_dest_port == 'postgres' %}{{ host.pg_port|default(5432) }}{% elif svc_dest_port == 'pgbouncer' %}{{ host.pgbouncer_port|default(6432) }}{% else %}5432{% endif %}
{% if host.patroni_ssl_enabled|default(false)|bool %} check-ssl ca-file /pg/cert/ca.crt{% endif %} check port {{ host.patroni_port|default(8008) }} weight {{ host.pg_weight|default(100) }} {% if host.inventory_hostname in pg_service_backup_names %}backup{% endif %}

{% endfor %}
{% for host in pg_service_backups %}
{% if host.inventory_hostname not in pg_service_member_names %}
{% if service.dest is not defined or service.dest == 'default' %}{% set svc_dest_port = pg_default_service_dest|default('postgres') %}{% else %}{% set svc_dest_port = service.dest %}{% endif %}
    server {{ host.pg_cluster }}-{{ host.pg_seq }} {{ host.inventory_hostname }}:{% if svc_dest_port is number %}{{ svc_dest_port }}{% elif svc_dest_port == 'postgres' %}{{ host.pg_port|default(5432) }}{% elif svc_dest_port == 'pgbouncer' %}{{ host.pgbouncer_port|default(6432) }}{% else %}5432{% endif %}
{% if host.patroni_ssl_enabled|default(false)|bool %} check-ssl ca-file /pg/cert/ca.crt{% endif %} check port {{ host.patroni_port|default(8008) }} weight {{ host.pg_weight|default(100) }} backup
{% endif %}

{% endfor %}
