heat_template_version: 2014-10-16

description: |
  Template which tests neutron load balancing resources

parameters:
  key_name:
    type: string
  flavor:
    type: string
  image:
    type: string
  network:
    type: string
  private_subnet_id:
    type: string
  external_network_id:
    type: string
  port:
    type: string
    default: '80'
  timeout:
    type: number

resources:
  sec_group:
    type: OS::Neutron::SecurityGroup
    properties:
      description: Add security group rules for servers
      name: security-group
      rules:
        - remote_ip_prefix: 0.0.0.0/0
          protocol: tcp
          port_range_min: { get_param: port }
          port_range_max: { get_param: port }
        - remote_ip_prefix: 0.0.0.0/0
          protocol: icmp

  wait_condition:
    type: OS::Heat::WaitCondition
    properties:
      handle: { get_resource: wait_condition_handle }
      count: 2
      timeout: { get_param: timeout }

  wait_condition_handle:
    type: OS::Heat::WaitConditionHandle

  config:
    type: OS::Heat::SoftwareConfig
    properties:
      group: ungrouped
      config:
        str_replace:
          template: |
            #!/bin/bash -v
            echo  $(hostname) > index.html
            python -m SimpleHTTPServer port &
            wc_notify --data-binary '{"status": "SUCCESS"}'
          params:
            wc_notify: { get_attr: ['wait_condition_handle', 'curl_cli'] }
            port: { get_param: port }

  server1:
    type: OS::Nova::Server
    properties:
      name: Server1
      image: { get_param: image }
      flavor: { get_param: flavor }
      key_name: { get_param: key_name }
      networks: [{network: {get_param: network} }]
      security_groups: [{ get_resource: sec_group }]
      user_data_format: SOFTWARE_CONFIG
      user_data: { get_resource: config }

  server2:
    type: OS::Nova::Server
    properties:
      name: Server2
      image: { get_param: image }
      flavor: { get_param: flavor }
      key_name: { get_param: key_name }
      networks: [{network: {get_param: network} }]
      security_groups: [{ get_resource: sec_group }]
      user_data_format: SOFTWARE_CONFIG
      user_data: { get_resource: config }

  health_monitor:
    type: OS::Neutron::HealthMonitor
    properties:
      delay: 3
      type: HTTP
      timeout: 3
      max_retries: 3

  test_pool:
    type: OS::Neutron::Pool
    properties:
      lb_method: ROUND_ROBIN
      protocol: HTTP
      subnet: { get_param: private_subnet_id }
      monitors:
      - { get_resource: health_monitor }
      vip:
        protocol_port: { get_param: port }

  floating_ip:
     type: OS::Neutron::FloatingIP
     properties:
       floating_network: { get_param: external_network_id }
       port_id:
         { get_attr: [test_pool, vip, 'port_id'] }
       fixed_ip_address:
         { get_attr: [test_pool, vip, 'address'] }

  LBaaS:
    type: OS::Neutron::LoadBalancer
    depends_on: wait_condition
    properties:
      pool_id: { get_resource: test_pool }
      protocol_port: { get_param: port }
      members:
      - { get_resource: server1 }

outputs:
  serv1_ip:
    value: {get_attr: [server1, networks, { get_param: network }, 0]}
  serv2_ip:
    value: {get_attr: [server2, networks, { get_param: network }, 0]}
  vip:
    value: {get_attr: [test_pool, vip, address]}
  fip:
    value: {get_attr: [floating_ip, floating_ip_address]}
