heat_template_version: 2014-10-16

parameters:

  fixed_subnet:
    type: string

  external_network:
    type: string

resources:
  # Admin Router is a customized Nginx that proxies all of the internal
  # services on port 80 and 443 (if https is configured)
  # See https://dcos.io/docs/1.8/administration/installing/custom/configuration-parameters/#-a-name-master-a-master_discovery
  # If parameter is specified to master_http_loadbalancer, the
  # load balancer must accept traffic on ports 8080, 5050, 80, and 443,
  # and forward it to the same ports on the master
  #
  # Opening ports 2181 and 8181 are not mentioned in DC/OS document.
  # When I create a cluster with load balancer, slave nodes will connect to
  # some services in master nodes with the IP of load balancer, if the port
  # is not open it will fail.
  loadbalancer:
    type: Magnum::Optional::Neutron::LBaaS::LoadBalancer
    properties:
      vip_subnet: {get_param: fixed_subnet}

  listener_80:
    type: Magnum::Optional::Neutron::LBaaS::Listener
    properties:
      loadbalancer: {get_resource: loadbalancer}
      protocol: HTTP
      protocol_port: 80

  pool_80:
    type: Magnum::Optional::Neutron::LBaaS::Pool
    properties:
      lb_algorithm: ROUND_ROBIN
      listener: {get_resource: listener_80}
      protocol: HTTP

  monitor_80:
    type: Magnum::Optional::Neutron::LBaaS::HealthMonitor
    properties:
      type: TCP
      delay: 5
      max_retries: 5
      timeout: 5
      pool: { get_resource: pool_80 }

  listener_443:
    depends_on: monitor_80
    type: Magnum::Optional::Neutron::LBaaS::Listener
    properties:
      loadbalancer: {get_resource: loadbalancer}
      protocol: HTTPS
      protocol_port: 443

  pool_443:
    type: Magnum::Optional::Neutron::LBaaS::Pool
    properties:
      lb_algorithm: ROUND_ROBIN
      listener: {get_resource: listener_443}
      protocol: HTTPS

  monitor_443:
    type: Magnum::Optional::Neutron::LBaaS::HealthMonitor
    properties:
      type: TCP
      delay: 5
      max_retries: 5
      timeout: 5
      pool: { get_resource: pool_443 }

  listener_8080:
    depends_on: monitor_443
    type: Magnum::Optional::Neutron::LBaaS::Listener
    properties:
      loadbalancer: {get_resource: loadbalancer}
      protocol: TCP
      protocol_port: 8080

  pool_8080:
    type: Magnum::Optional::Neutron::LBaaS::Pool
    properties:
      lb_algorithm: ROUND_ROBIN
      listener: {get_resource: listener_8080}
      protocol: TCP

  monitor_8080:
    type: Magnum::Optional::Neutron::LBaaS::HealthMonitor
    properties:
      type: TCP
      delay: 5
      max_retries: 5
      timeout: 5
      pool: { get_resource: pool_8080 }

  listener_5050:
    depends_on: monitor_8080
    type: Magnum::Optional::Neutron::LBaaS::Listener
    properties:
      loadbalancer: {get_resource: loadbalancer}
      protocol: TCP
      protocol_port: 5050

  pool_5050:
    type: Magnum::Optional::Neutron::LBaaS::Pool
    properties:
      lb_algorithm: ROUND_ROBIN
      listener: {get_resource: listener_5050}
      protocol: TCP

  monitor_5050:
    type: Magnum::Optional::Neutron::LBaaS::HealthMonitor
    properties:
      type: TCP
      delay: 5
      max_retries: 5
      timeout: 5
      pool: { get_resource: pool_5050 }

  listener_2181:
    depends_on: monitor_5050
    type: Magnum::Optional::Neutron::LBaaS::Listener
    properties:
      loadbalancer: {get_resource: loadbalancer}
      protocol: TCP
      protocol_port: 2181

  pool_2181:
    type: Magnum::Optional::Neutron::LBaaS::Pool
    properties:
      lb_algorithm: ROUND_ROBIN
      listener: {get_resource: listener_2181}
      protocol: TCP

  monitor_2181:
    type: Magnum::Optional::Neutron::LBaaS::HealthMonitor
    properties:
      type: TCP
      delay: 5
      max_retries: 5
      timeout: 5
      pool: { get_resource: pool_2181 }

  listener_8181:
    depends_on: monitor_2181
    type: Magnum::Optional::Neutron::LBaaS::Listener
    properties:
      loadbalancer: {get_resource: loadbalancer}
      protocol: TCP
      protocol_port: 8181

  pool_8181:
    type: Magnum::Optional::Neutron::LBaaS::Pool
    properties:
      lb_algorithm: ROUND_ROBIN
      listener: {get_resource: listener_8181}
      protocol: TCP

  monitor_8181:
    type: Magnum::Optional::Neutron::LBaaS::HealthMonitor
    properties:
      type: TCP
      delay: 5
      max_retries: 5
      timeout: 5
      pool: { get_resource: pool_8181 }

  floating:
    type: Magnum::Optional::Neutron::LBaaS::FloatingIP
    properties:
      floating_network: {get_param: external_network}
      port_id: {get_attr: [loadbalancer, vip_port_id]}

outputs:

  pool_80_id:
    value: {get_resource: pool_80}

  pool_443_id:
    value: {get_resource: pool_443}

  pool_8080_id:
    value: {get_resource: pool_8080}

  pool_5050_id:
    value: {get_resource: pool_5050}

  pool_2181_id:
    value: {get_resource: pool_2181}

  pool_8181_id:
    value: {get_resource: pool_8181}

  address:
    value: {get_attr: [loadbalancer, vip_address]}

  floating_address:
    value: {get_attr: [floating, floating_ip_address]}
