heat_template_version: 2015-04-30

description: |
  Template which tests Neutron load balancing requests to members of
  Heat AutoScalingGroup. This uses LBaas V2.
  Instances must be running some webserver on a given app_port
  producing HTTP response that is different between servers
  but stable over time for given server.

parameters:
  flavor:
    type: string
  image:
    type: string
  net:
    type: string
  subnet:
    type: string
  public_net:
    type: string
  app_port:
    type: number
    default: 8080
  lb_port:
    type: number
    default: 80
  timeout:
    type: number
    default: 600

resources:

  sec_group:
    type: OS::Neutron::SecurityGroup
    properties:
      rules:
      - remote_ip_prefix: 0.0.0.0/0
        protocol: tcp
        port_range_min: { get_param: app_port }
        port_range_max: { get_param: app_port }

  asg:
    type: OS::Heat::AutoScalingGroup
    properties:
      desired_capacity: 1
      max_size: 2
      min_size: 1
      resource:
        type: OS::Test::NeutronAppServer
        properties:
          image: { get_param: image }
          flavor: { get_param: flavor }
          net: { get_param: net}
          sec_group: { get_resource: sec_group }
          app_port: { get_param: app_port }
          pool: { get_resource: pool }
          subnet: { get_param: subnet }
          timeout: { get_param: timeout }

  scale_up:
    type: OS::Heat::ScalingPolicy
    properties:
      adjustment_type: change_in_capacity
      auto_scaling_group_id: { get_resource: asg }
      scaling_adjustment: 1

  scale_down:
    type: OS::Heat::ScalingPolicy
    properties:
      adjustment_type: change_in_capacity
      auto_scaling_group_id: { get_resource: asg }
      scaling_adjustment: -1

  health_monitor:
    type: OS::Neutron::LBaaS::HealthMonitor
    properties:
      delay: 3
      type: HTTP
      timeout: 3
      max_retries: 3
      pool: { get_resource: pool }

  pool:
    type: OS::Neutron::LBaaS::Pool
    properties:
      lb_algorithm: ROUND_ROBIN
      protocol: HTTP
      listener: { get_resource: listener }

  listener:
    type: OS::Neutron::LBaaS::Listener
    properties:
      loadbalancer: { get_resource: loadbalancer }
      protocol: HTTP
      protocol_port: { get_param: lb_port }

  loadbalancer:
    type: OS::Neutron::LBaaS::LoadBalancer
    properties:
      vip_subnet: { get_param: subnet }

  floating_ip:
    type: OS::Neutron::FloatingIP
    properties:
      floating_network: { get_param: public_net }
      port_id: { get_attr: [loadbalancer, vip_port_id] }

outputs:
  lburl:
    description: URL of the loadbalanced app
    value:
      str_replace:
        template: http://IP_ADDRESS:PORT
        params:
          IP_ADDRESS: { get_attr: [ floating_ip, floating_ip_address ] }
          PORT: { get_param: lb_port }
