class iaas::profile::haproxy (
  $stats_enabled = undef,
  $stats_ports = undef,
  $stats_refresh = undef,
  $stats_login = undef,
  $stats_password = undef,
  $stats_uri = 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 => '0.0.0.0',
      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 => '0.0.0.0',
    mode => 'tcp',
    ports => '3306',
    options => {
      'option' => ['httpchk','tcpka'],
      'balance' => 'leastconn',
    }
  }
  
  haproxy::balancermember { 'percona_cluster_bm':
    listening_service => 'percona_cluster',
    ports             => '3307',
    server_names      => hiera('iaas::role::haproxy::servers', undef),
    ipaddresses       => hiera('iaas::role::haproxy::ipaddresses_server', undef),
    options           => 'check port 9200',
  }
 
# 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',
    }
  }
}