# This file was generated by chef-alfresco

global
<% if node['haproxy']['general_config'] -%>
    <% node['haproxy']['general_config'].each do |line| -%>
    <%= line %>
    <% end -%>
<% end -%>

defaults
<% if node['haproxy']['default_config'] -%>
    <% node['haproxy']['default_config'].each do |line| -%>
    <%= line %>
    <% end -%> 
<% end -%>

<% if node['haproxy']['frontends'] -%>
# Frontends
  <% node['haproxy']['frontends'].each do |frontendName,frontend| -%>
frontend <%= frontendName %>
    <% if frontend['entries'] -%>
      <% frontend['entries'].each do |entry| -%>
    <%= entry %>
      <% end -%>
    <% end -%>
    <% if frontend['headers'] -%>
      <% frontend['headers'].each do |header| -%>
    <%= header %>
      <% end -%>
    <% end -%>
    <% if frontend['acls'] -%>

    # Backend ACLs
      <% frontend['acls'].each do |backendName,acls| -%>
        <% if acls -%>
          <% acls.each do |acl| -%>
    acl is_<%= backendName %> <%= acl %>
          <% end -%>
        <% end -%>
      <% end -%>
    <% end -%>
    <% if frontend['acl_lines'] -%>

    # Free formed ACLs
      <% frontend['acl_lines'].each do |acl| -%>
    acl <%= acl %>
      <% end -%>
    <% end -%>
    <% if frontend['other_config'] -%>

    # Other Configurations
      <% frontend['other_config'].each do |other_config| -%>
    <%= other_config %>
      <% end -%>
    <% end -%>
    <% if frontend['redirects'] -%>

    # Redirects
      <% frontend['redirects'].each do |redirect| -%>
    <%= redirect %>
      <% end -%>
    <% end -%>
    <% if frontend['acls'] -%>

    # Use backends
      <% frontend['acls'].each do |backendName,acls| -%>
    use_backend <%= backendName %> if is_<%= backendName %>
      <% end -%>
    <% end -%>

    # Default Backend
    default_backend <%= node['haproxy']['default_backend'] %>

  <% end -%>
<% end -%>

<% if @haproxy_backends -%>
# Backends
  <% @haproxy_backends.each do |roleName,role| -%>
backend <%= roleName %>
    <% if role['entries'] -%>
      <% role['entries'].each do |entry| -%>
    <%= entry %>
      <% end -%>
    <% end -%>
    <% if role['balanced'] -%>
    balance leastconn
    cookie JSESSIONID prefix
    <% end -%>
    <% if role['secure_entries'] -%>
      <% role['secure_entries'].each do |secure_entry| -%>
    <%= secure_entry %>
      <% end -%>
    <% end -%>
    <% if role['ordered_az'] -%>
      <% role['ordered_az'].each do |az| -%>
        <% az['id'].each do |instanceName,instance| -%>

    # Instance <%= instanceName %>, az <%= instance['az'] %>
    server <%= instanceName %> <%= instance['ip'] %>:<%= role['port'] %> <%= instance['options'] %>
        <% end -%>
      <% end -%>
    <% end -%>

  <% end -%>
<% end -%>

