class iaas::profile::haproxy (
  $stats_enabled = undef,
  $stats_ports = undef,
  $stats_refresh = undef,
  $stats_login = undef,
  $stats_password = undef,
  $stats_uri = undef,
  $servers = undef,
  $vip = undef,
  $controller_hosts = undef,
  $controller_ips = undef,
) {
  class { '::haproxy':
    defaults_options => {
      'mode' => 'http',
#     'log' => 'global',
      'option' => [
#	'tcplog',
	'dontlognull',
	'redispatch',
      ],
      'retries' => '3',
      'timeout' => [
        'http-request 10s',
        'queue 1m',
        'connect 10s',
        'client 1m',
        'server 1m',
	'http-keep-alive 10s',
        'check 10s',
      ],
      'maxconn' => '4096',
    },
  }

  if stats_enabled {
    haproxy::listen { 'stats':
      ipaddress => $vip,
      mode => 'http',
      ports => $stats_ports,
      options => {
        'stats' => [
          'enable',
          'hide-version',
          "refresh ${stats_refresh}",
          'show-node',
          "auth ${stats_login}:${stats_password}",
          "uri ${stats_uri}"
        ],
      }
    }
  }

  haproxy::listen { 'percona_cluster':
    ipaddress => $vip,
    mode => 'tcp',
    ports => '3306',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'leastconn',
      'timeout' => [
         'client 90m',
         'server 90m',
       ],
    }
  }

  if $servers != undef { 
     haproxy::balancermember { 'percona_cluster_prior':
       listening_service => 'percona_cluster',
       ports             => '3307',
       server_names      => $servers[priority_hostname],
       ipaddresses       => $servers[priority_ip],
       options           => 'check port 9200',
     }
     haproxy::balancermember { 'percona_cluster_secondaries':
       listening_service => 'percona_cluster',
       ports             => '3307',
       server_names      => $servers[hostnames], 
       ipaddresses       => $servers[ips], 
       options           => 'check port 9200 backup',
     }
  }

 haproxy::listen { 'keystone_admin_cluster':
    ipaddress => $vip,
    ports => '35357',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'keystone_admin_cluster':
     listening_service => 'keystone_admin_cluster',
     ports             => '35357',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }


 haproxy::listen { 'keystone_api_cluster':
    ipaddress => $vip,
    ports => '5000',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'keystone_api_cluster':
     listening_service => 'keystone_api_cluster',
     ports             => '5000',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'glance_api_cluster':
    ipaddress => $vip,
    ports => '9292',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'glance_api_cluster':
     listening_service => 'glance_api_cluster',
     ports             => '9292',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }


 haproxy::listen { 'glance_registry_cluster':
    ipaddress => $vip,
    ports => '9191',
    options => {
      'option' => ['tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'glance_registry_cluster':
     listening_service => 'glance_registry_cluster',
     ports             => '9191',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'cinder_api_cluster':
    ipaddress => $vip,
    ports => '8776',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'cinder_api_cluster':
     listening_service => 'cinder_api_cluster',
     ports             => '8776',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'nova_api_cluster':
    ipaddress => $vip,
    ports => '8774',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'nova_api_cluster':
     listening_service => 'nova_api_cluster',
     ports             => '8774',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'nova_metadata_api_cluster':
    ipaddress => $vip,
    ports => '8775',
    options => {
      'option' => ['tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'nova_metadata_api_cluster':
     listening_service => 'nova_metadata_api_cluster',
     ports             => '8775',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'nova_novncproxy_cluster':
    ipaddress => $vip,
    ports => '6080',
    options => {
      'option' => ['tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'nova_novncproxy_cluster':
     listening_service => 'nova_novncproxy_cluster',
     ports             => '6080',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'nova_ec2_api_cluster':
    ipaddress => $vip,
    ports => '8773',
    options => {
      'option' => ['tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'nova_ec2_api_cluster':
     listening_service => 'nova_ec2_api_cluster',
     ports             => '8773',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'neutron_api_cluster':
    ipaddress => $vip,
    ports => '9696',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'neutron_api_cluster':
     listening_service => 'neutron_api_cluster',
     ports             => '9696',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'heat_api_cluster':
    ipaddress => $vip,
    ports => '8004',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'heat_api_cluster':
     listening_service => 'heat_api_cluster',
     ports             => '8004',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'heat_api_cfn_cluster':
    ipaddress => $vip,
    ports => '8000',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'heat_api_cfn_cluster':
     listening_service => 'heat_api_cfn_cluster',
     ports             => '8000',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'heat_api_watch_cluster':
    ipaddress => $vip,
    ports => '8003',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'heat_api_watch_cluster':
     listening_service => 'heat_api_watch_cluster',
     ports             => '8003',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'ceilometer_api_cluster':
    ipaddress => $vip,
    ports => '8777',
    options => {
      'option' => ['tcpka'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'ceilometer_api_cluster':
     listening_service => 'ceilometer_api_cluster',
     ports             => '8777',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }

 haproxy::listen { 'horizon_cluster':
    ipaddress => $vip,
    mode => 'http',
    ports => '80',
    options => {
      'option' => ['httpchk','httpclose','forwardfor'],
      'balance' => 'source',
    }
  }

  haproxy::balancermember { 'horizon_cluster':
     listening_service => 'horizon_cluster',
     ports             => '80',
     server_names      => $controller_hosts,
     ipaddresses       => $controller_ips,
     options           => 'check inter 2000 rise 2 fall 5',
  }
#  
#  haproxy::listen { 'mysql_cluster_nova':
#    ipaddress => '0.0.0.0',
#    mode => 'tcp',
#    ports => '7306',
#    options => {
#      'option' => ['httpchk','tcpka'],
#      'balance' => 'leastconn',
#      'timeout' => [
#         'client 90m',
#         'server 90m',
#       ],
#    }
#  }
#
#  if $servers != undef { 
#     haproxy::balancermember { 'mysql_cluster_nova_prior':
#       listening_service => 'mysql_cluster_nova',
#       ports             => '3307',
#       server_names      => $servers[priority_hostname],
#       ipaddresses       => $servers[priority_ip],
#       options           => 'check port 9200',
#     }
#     haproxy::balancermember { 'mysql_cluster_nova_secondaries':
#       listening_service => 'mysql_cluster_nova',
#       ports             => '3307',
#       server_names      => $servers[hostnames], 
#       ipaddresses       => $servers[ips], 
#       options           => 'check port 9200 backup',
#     }
#  }
#  
#  haproxy::listen { 'mysql_cluster_neutron':
#    ipaddress => '0.0.0.0',
#    mode => 'tcp',
#    ports => '9306',
#    options => {
#      'option' => ['httpchk','tcpka'],
#      'balance' => 'leastconn',
#      'timeout' => [
#         'client 90m',
#         'server 90m',
#       ],
#    }
#  }
#
#  if $servers != undef { 
#     haproxy::balancermember { 'mysql_cluster_neutron_prior':
#       listening_service => 'mysql_cluster_neutron',
#       ports             => '3307',
#       server_names      => $servers[priority_hostname],
#       ipaddresses       => $servers[priority_ip],
#       options           => 'check port 9200',
#     }
#     haproxy::balancermember { 'mysql_cluster_neutron_secondaries':
#       listening_service => 'mysql_cluster_neutron',
#       ports             => '3307',
#       server_names      => $servers[hostnames], 
#       ipaddresses       => $servers[ips], 
#       options           => 'check port 9200 backup',
#     }
#  }

# haproxy::listen { 'rabbitmq':
 #   ipaddress => '0.0.0.0',
 #   mode => 'tcp',
 #   ports => '5672',
 #   options => {
 #     'option' => ['tcpka'],
 #     'timeout' => [
 #       'client 7d',
 #       'server 7d',
 #     ]
 #   }
 # }

#  haproxy::listen { 'keystone_admin_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '35357',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'keystone_public_internal_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '5000',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }

#  haproxy::listen { 'glance_api_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '9292',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'glance_registry_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '9191',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#      'http-check' => 'expect status 401',
#    }
#  }
#
#  haproxy::listen { 'cinder_api_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '8776',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'nova_api_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '8774',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'nova_metadata_api_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '8775',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'nova_novncproxy':
#    ipaddress => '0.0.0.0',
#    ports => '6080',
#    options => {
#      'option' => ['tcpka', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'neutron_api_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '9696',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'heat_api_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '8004',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'heat_api_cfn_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '8000',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'heat_api_cw_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '8003',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'horizon_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '80',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#    }
#  }
#
#  haproxy::listen { 'ceilometer_api_cluster':
#    ipaddress => '0.0.0.0',
#    ports => '8777',
#    options => {
#      'option' => ['tcpka', 'httpchk', 'tcplog'],
#      'http-check' => 'expect status 401',
#    }
#  }
}