Class: Aws::Elb

Inherits:
AwsBase show all
Includes:
AwsBaseInterface
Defined in:
lib/elb/elb_interface.rb

Defined Under Namespace

Classes: QElbCreateParser, QElbDeleteParser, QElbDescribeInstancesHealthParser, QElbDescribeLoadBalancersParser, QElbRegisterInstancesParser

Constant Summary collapse

API_VERSION =

Amazon ELB API version being used

"2009-05-15"
DEFAULT_HOST =
"elasticloadbalancing.amazonaws.com"
DEFAULT_PATH =
'/'
DEFAULT_PROTOCOL =
'https'
DEFAULT_PORT =
443
@@bench =
AwsBenchmarkingBlock.new
@@api =

Current API version (sometimes we have to check it outside the GEM).

ENV['ELB_API_VERSION'] || API_VERSION

Constants included from AwsBaseInterface

AwsBaseInterface::DEFAULT_SIGNATURE_VERSION

Constants inherited from AwsBase

AwsBase::AMAZON_PROBLEMS

Instance Attribute Summary

Attributes included from AwsBaseInterface

#aws_access_key_id, #cache, #last_errors, #last_request, #last_request_id, #last_response, #logger, #params, #signature_version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsBaseInterface

#cache_hits?, caching, caching=, #caching?, #close_conn, #close_connection, #connection, #escape_params, #generate_request, #generate_request2, #get_conn, #hash_params, #init, #multi_thread, #on_exception, #request_cache_or_info, #request_info2, #request_info3, #request_info_impl, #request_info_xml_simple, #request_info_xml_simple3, #signed_service_params, #symbolize, #update_cache

Methods inherited from AwsBase

amazon_problems, amazon_problems=

Constructor Details

#initialize(aws_access_key_id = nil, aws_secret_access_key = nil, params = {}) ⇒ Elb

Returns a new instance of Elb.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/elb/elb_interface.rb', line 43

def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
  init({:name             => 'ELB',
        :default_host     => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).host : DEFAULT_HOST,
        :default_port     => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).port : DEFAULT_PORT,
        :default_service  => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).path : DEFAULT_PATH,
        :default_protocol => ENV['ELB_URL'] ? URI.parse(ENV['ELB_URL']).scheme : DEFAULT_PROTOCOL,
        :api_version      => API_VERSION},
       aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'],
       aws_secret_access_key|| ENV['AWS_SECRET_ACCESS_KEY'],
       params)
end

Class Method Details

.apiObject



38
39
40
# File 'lib/elb/elb_interface.rb', line 38

def self.api
  @@api
end

.benchObject



23
24
25
# File 'lib/elb/elb_interface.rb', line 23

def self.bench
  @@bench
end

.bench_ec2Object



31
32
33
# File 'lib/elb/elb_interface.rb', line 31

def self.bench_ec2
  @@bench.service
end

.bench_xmlObject



27
28
29
# File 'lib/elb/elb_interface.rb', line 27

def self.bench_xml
  @@bench.xml
end

.connection_nameObject



17
18
19
# File 'lib/elb/elb_interface.rb', line 17

def self.connection_name
  :elb_connection
end

Instance Method Details

#create_load_balancer(name, availability_zones, listeners) ⇒ Object

name: name of load balancer availability_zones: array of zones listeners: array of hashes containing :load_balancer_port, :instance_port, :protocol

eg: {:load_balancer_port=>80, :instance_port=>8080, :protocol=>"HTTP"}


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/elb/elb_interface.rb', line 91

def create_load_balancer(name, availability_zones, listeners)
  params = hash_params('AvailabilityZones.member', availability_zones)
  i      = 1
  listeners.each do |l|
    params["Listeners.member.#{i}.Protocol"]         = "#{l[:protocol]}"
    params["Listeners.member.#{i}.LoadBalancerPort"] = "#{l[:load_balancer_port]}"
    params["Listeners.member.#{i}.InstancePort"]     = "#{l[:instance_port]}"
    i                                                += 1
  end
  params['LoadBalancerName'] = name

  @logger.info("Creating LoadBalancer called #{params['LoadBalancerName']}")

  link = generate_request("CreateLoadBalancer", params)
  resp = request_info(link, QElbCreateParser.new(:logger => @logger))

rescue Exception
  on_exception
end

#delete_load_balancer(name) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/elb/elb_interface.rb', line 191

def delete_load_balancer(name)
  @logger.info("Deleting Load Balancer - " + name.to_s)

  params                     = {}
  params['LoadBalancerName'] = name

  link                       = generate_request("DeleteLoadBalancer", params)

  resp                       = request_info(link, QElbDeleteParser.new(:logger => @logger))

rescue Exception
  on_exception
end

#deregister_instances_from_load_balancer(name, instance_ids) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/elb/elb_interface.rb', line 133

def deregister_instances_from_load_balancer(name, instance_ids)
  params                     = {}
  params['LoadBalancerName'] = name

  i                          = 1
  instance_ids.each do |l|
    params["Instances.member.#{i}.InstanceId"] = "#{l}"
    i                                          += 1
  end

  @logger.info("Deregistering Instances #{instance_ids.join(',')} from Load Balancer '#{name}'")

  link = generate_request("DeregisterInstancesFromLoadBalancer", params) # Same response as register I believe
  resp = request_info(link, QElbRegisterInstancesParser.new(:logger => @logger))

rescue Exception
  on_exception
end

#describe_instance_health(name, instance_ids = []) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/elb/elb_interface.rb', line 168

def describe_instance_health(name, instance_ids=[])
  instance_ids = [instance_ids] if instance_ids.is_a?(String)
#            @logger.info("Describing Instance Health")
  params                     = {}
  params['LoadBalancerName'] = name

  i                          = 1
  instance_ids.each do |l|
    params["Instances.member.#{i}.InstanceId"] = "#{l}"
    i                                          += 1
  end

  @logger.info("Describing Instances Health #{instance_ids.join(',')} with Load Balancer '#{name}'")

  link = generate_request("DescribeInstanceHealth", params)
  resp = request_info(link, QElbDescribeInstancesHealthParser.new(:logger => @logger))


rescue Exception
  on_exception
end

#describe_load_balancers(lparams = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/elb/elb_interface.rb', line 153

def describe_load_balancers(lparams={})
  @logger.info("Describing Load Balancers")

  params = {}
  params.update(hash_params('LoadBalancerNames.member', lparams[:names])) if lparams[:names]

  link = generate_request("DescribeLoadBalancers", params)

  resp = request_info(link, QElbDescribeLoadBalancersParser.new(:logger => @logger))

rescue Exception
  on_exception
end

#do_request(action, params, options = {}) ⇒ Object

todo: convert to xml-simple version and get rid of parser below



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/elb/elb_interface.rb', line 63

def do_request(action, params, options={})
  link = generate_request(action, params)
  resp = request_info_xml_simple(self.class.connection_name, @params, link, @logger,
                                 :group_tags     =>{"LoadBalancersDescriptions"=>"LoadBalancersDescription",
                                                    "DBParameterGroups"        =>"DBParameterGroup",
                                                    "DBSecurityGroups"         =>"DBSecurityGroup",
                                                    "EC2SecurityGroups"        =>"EC2SecurityGroup",
                                                    "IPRanges"                 =>"IPRange"},
                                 :force_array    =>["DBInstances",
                                                    "DBParameterGroups",
                                                    "DBSecurityGroups",
                                                    "EC2SecurityGroups",
                                                    "IPRanges"],
                                 :pull_out_array =>options[:pull_out_array],
                                 :pull_out_single=>options[:pull_out_single],
                                 :wrapper        =>options[:wrapper])
end

#register_instances_with_load_balancer(name, instance_ids) ⇒ Object

name: name of load balancer instance_ids: array of instance_id’s to add to load balancer



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/elb/elb_interface.rb', line 114

def register_instances_with_load_balancer(name, instance_ids)
  params                     = {}
  params['LoadBalancerName'] = name

  i                          = 1
  instance_ids.each do |l|
    params["Instances.member.#{i}.InstanceId"] = "#{l}"
    i                                          += 1
  end

  @logger.info("Registering Instances #{instance_ids.join(',')} with Load Balancer '#{name}'")

  link = generate_request("RegisterInstancesWithLoadBalancer", params)
  resp = request_info(link, QElbRegisterInstancesParser.new(:logger => @logger))

rescue Exception
  on_exception
end

#request_info(request, parser, options = {}) ⇒ Object

Sends request to Amazon and parses the response Raises AwsError if any banana happened



58
59
60
# File 'lib/elb/elb_interface.rb', line 58

def request_info(request, parser, options={})
  request_info2(request, parser, @params, self.class.connection_name, @logger, @@bench, options)
end