2011-03-08 17:41:49 -05:00
|
|
|
require 'fog/core/model'
|
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class ELB
|
|
|
|
|
|
|
|
class LoadBalancer < Fog::Model
|
|
|
|
|
|
|
|
identity :id, :aliases => 'LoadBalancerName'
|
2011-05-30 18:08:58 -04:00
|
|
|
attribute :availability_zones, :aliases => 'AvailabilityZones'
|
2011-03-08 17:41:49 -05:00
|
|
|
attribute :created_at, :aliases => 'CreatedTime'
|
|
|
|
attribute :dns_name, :aliases => 'DNSName'
|
2011-05-30 18:08:58 -04:00
|
|
|
attribute :health_check, :aliases => 'HealthCheck'
|
2011-03-08 17:41:49 -05:00
|
|
|
attribute :instances, :aliases => 'Instances'
|
2011-05-30 18:57:07 -04:00
|
|
|
attribute :source_group, :aliases => 'SourceSecurityGroup'
|
2011-03-08 17:41:49 -05:00
|
|
|
|
|
|
|
def initialize(attributes={})
|
|
|
|
attributes[:availability_zones] ||= %w(us-east-1a us-east-1b us-east-1c us-east-1d)
|
2011-05-04 18:29:07 -04:00
|
|
|
attributes['ListenerDescriptions'] ||= [{
|
|
|
|
'Listener' => {'LoadBalancerPort' => 80, 'InstancePort' => 80, 'Protocol' => 'http'},
|
|
|
|
'PolicyNames' => []
|
|
|
|
}]
|
|
|
|
attributes['Policies'] ||= {'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => []}
|
2011-03-08 17:41:49 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_instances(instances)
|
|
|
|
requires :id
|
|
|
|
data = connection.register_instances_with_load_balancer(instances, id).body['RegisterInstancesWithLoadBalancerResult']
|
|
|
|
data['Instances'].map!{|h| h['InstanceId']}
|
|
|
|
merge_attributes(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deregister_instances(instances)
|
|
|
|
requires :id
|
|
|
|
data = connection.deregister_instances_from_load_balancer(instances, id).body['DeregisterInstancesFromLoadBalancerResult']
|
|
|
|
data['Instances'].map!{|h| h['InstanceId']}
|
|
|
|
merge_attributes(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_availability_zones(zones)
|
|
|
|
requires :id
|
|
|
|
data = connection.enable_availability_zones_for_load_balancer(zones, id).body['EnableAvailabilityZonesForLoadBalancerResult']
|
|
|
|
merge_attributes(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_availability_zones(zones)
|
|
|
|
requires :id
|
|
|
|
data = connection.disable_availability_zones_for_load_balancer(zones, id).body['DisableAvailabilityZonesForLoadBalancerResult']
|
|
|
|
merge_attributes(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def instance_health
|
|
|
|
requires :id
|
|
|
|
@instance_health ||= connection.describe_instance_health(id).body['DescribeInstanceHealthResult']['InstanceStates']
|
|
|
|
end
|
|
|
|
|
|
|
|
def instances_in_service
|
|
|
|
instance_health.select{|hash| hash['State'] == 'InService'}.map{|hash| hash['InstanceId']}
|
|
|
|
end
|
|
|
|
|
|
|
|
def instances_out_of_service
|
|
|
|
instance_health.select{|hash| hash['State'] == 'OutOfService'}.map{|hash| hash['InstanceId']}
|
|
|
|
end
|
|
|
|
|
|
|
|
def configure_health_check(health_check)
|
|
|
|
requires :id
|
|
|
|
data = connection.configure_health_check(id, health_check).body['ConfigureHealthCheckResult']['HealthCheck']
|
|
|
|
merge_attributes(:health_check => data)
|
|
|
|
end
|
|
|
|
|
2011-05-04 18:29:07 -04:00
|
|
|
def listeners
|
|
|
|
Fog::AWS::ELB::Listeners.new({
|
|
|
|
:data => attributes['ListenerDescriptions'],
|
|
|
|
:connection => connection,
|
|
|
|
:load_balancer => self
|
|
|
|
})
|
2011-03-08 17:41:49 -05:00
|
|
|
end
|
|
|
|
|
2011-05-02 18:17:19 -04:00
|
|
|
def policies
|
2011-05-04 18:29:07 -04:00
|
|
|
Fog::AWS::ELB::Policies.new({
|
|
|
|
:data => attributes['Policies'],
|
|
|
|
:connection => connection,
|
|
|
|
:load_balancer => self
|
|
|
|
})
|
2011-03-08 17:41:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_listener_policy(port, policy_name)
|
|
|
|
requires :id
|
|
|
|
policy_name = [policy_name].flatten
|
|
|
|
connection.set_load_balancer_policies_of_listener(id, port, policy_name)
|
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def unset_listener_policy(port)
|
|
|
|
set_listener_policy(port, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def ready?
|
|
|
|
# ELB requests are synchronous
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
requires :id
|
|
|
|
requires :listeners
|
|
|
|
requires :availability_zones
|
|
|
|
|
2011-05-04 18:29:07 -04:00
|
|
|
connection.create_load_balancer(availability_zones, id, listeners.map{|l| l.to_params})
|
2011-03-08 17:41:49 -05:00
|
|
|
|
|
|
|
# reload instead of merge attributes b/c some attrs (like HealthCheck)
|
|
|
|
# may be set, but only the DNS name is returned in the create_load_balance
|
|
|
|
# API call
|
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
super
|
2011-05-04 18:29:07 -04:00
|
|
|
@instance_health = nil
|
2011-05-02 18:17:19 -04:00
|
|
|
self
|
2011-03-08 17:41:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :id
|
|
|
|
connection.delete_load_balancer(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|