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, #symbolize_keys, #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.



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

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



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

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



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

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



133
134
135
136
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 133

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

#disable_availability_zones(zones) ⇒ Object



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

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



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

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



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

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

#instances_in_serviceObject



62
63
64
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 62

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

#instances_out_of_serviceObject



66
67
68
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 66

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

#listenersObject



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

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

#policiesObject



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

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

#ready?Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 109

def ready?
  # ELB requests are synchronous
  true
end

#register_instances(instances) ⇒ Object



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

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



127
128
129
130
131
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 127

def reload
  super
  @instance_health = nil
  self
end

#saveObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 114

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



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

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

#set_listener_ssl_certificate(port, ssl_certificate_id) ⇒ Object



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

def set_listener_ssl_certificate(port, ssl_certificate_id)
  requires :id
  connection.set_load_balancer_listener_ssl_certificate(id, port, ssl_certificate_id)
  reload
end

#unset_listener_policy(port) ⇒ Object



105
106
107
# File 'lib/fog/aws/models/elb/load_balancer.rb', line 105

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