Class: Fog::AWS::ELB::LoadBalancer

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/aws/models/elb/load_balancer.rb

Instance Attribute Summary

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#inspect, #to_json, #wait_for

Methods included from Fog::Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ LoadBalancer

Returns a new instance of LoadBalancer.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 16

def initialize(attributes={})
  attributes[:availability_zones] ||= attributes['AvailabilityZones'] || %w(us-east-1a us-east-1b us-east-1c us-east-1d)
  unless attributes['ListenerDescriptions']
    new_listener = Fog::AWS::ELB::Listener.new
    attributes['ListenerDescriptions'] = [{
      'Listener' => new_listener.to_params,
      'PolicyNames' => new_listener.policy_names
    }]
  end
  attributes['Policies'] ||= {'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => []}
  super
end

Instance Method Details

#configure_health_check(health_check) ⇒ Object



68
69
70
71
72
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 68

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

#deregister_instances(instances) ⇒ Object



36
37
38
39
40
41
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 36

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

#destroyObject



125
126
127
128
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 125

def destroy
  requires :id
  connection.delete_load_balancer(id)
end

#disable_availability_zones(zones) ⇒ Object



49
50
51
52
53
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 49

def disable_availability_zones(zones)
  requires :id
  data = connection.disable_availability_zones_for_load_balancer(zones, id).body['DisableAvailabilityZonesForLoadBalancerResult']
  merge_attributes(data)
end

#enable_availability_zones(zones) ⇒ Object



43
44
45
46
47
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 43

def enable_availability_zones(zones)
  requires :id
  data = connection.enable_availability_zones_for_load_balancer(zones, id).body['EnableAvailabilityZonesForLoadBalancerResult']
  merge_attributes(data)
end

#instance_healthObject



55
56
57
58
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 55

def instance_health
  requires :id
  @instance_health ||= connection.describe_instance_health(id).body['DescribeInstanceHealthResult']['InstanceStates']
end

#instances_in_serviceObject



60
61
62
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 60

def instances_in_service
  instance_health.select{|hash| hash['State'] == 'InService'}.map{|hash| hash['InstanceId']}
end

#instances_out_of_serviceObject



64
65
66
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 64

def instances_out_of_service
  instance_health.select{|hash| hash['State'] == 'OutOfService'}.map{|hash| hash['InstanceId']}
end

#listenersObject



74
75
76
77
78
79
80
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 74

def listeners
  Fog::AWS::ELB::Listeners.new({
    :data => attributes['ListenerDescriptions'],
    :connection => connection,
    :load_balancer => self
  })
end

#policiesObject



82
83
84
85
86
87
88
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 82

def policies
  Fog::AWS::ELB::Policies.new({
    :data => attributes['Policies'],
    :connection => connection,
    :load_balancer => self
  })
end

#ready?Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 101

def ready?
  # ELB requests are synchronous
  true
end

#register_instances(instances) ⇒ Object



29
30
31
32
33
34
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 29

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

#reloadObject



119
120
121
122
123
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 119

def reload
  super
  @instance_health = nil
  self
end

#saveObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 106

def save
  requires :id
  requires :listeners
  requires :availability_zones

  connection.create_load_balancer(availability_zones, id, listeners.map{|l| l.to_params})

  # 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

#set_listener_policy(port, policy_name) ⇒ Object



90
91
92
93
94
95
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 90

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

#unset_listener_policy(port) ⇒ Object



97
98
99
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 97

def unset_listener_policy(port)
  set_listener_policy(port, [])
end