1
# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
This module provides an interface to the Elastic Compute Cloud (EC2)
24
load balancing service from AWS.
26
from boto.connection import AWSQueryConnection
27
from boto.ec2.instanceinfo import InstanceInfo
28
from boto.ec2.elb.loadbalancer import LoadBalancer
29
from boto.ec2.elb.instancestate import InstanceState
30
from boto.ec2.elb.healthcheck import HealthCheck
33
class ELBConnection(AWSQueryConnection):
35
APIVersion = boto.config.get('Boto', 'elb_version', '2009-05-15')
36
Endpoint = boto.config.get('Boto', 'elb_endpoint', 'elasticloadbalancing.amazonaws.com')
37
SignatureVersion = '1'
38
#ResponseError = EC2ResponseError
40
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
41
is_secure=False, port=None, proxy=None, proxy_port=None,
42
proxy_user=None, proxy_pass=None, host=Endpoint, debug=0,
43
https_connection_factory=None, path='/'):
45
Init method to create a new connection to EC2 Load Balancing Service.
47
B{Note:} The host argument is overridden by the host specified in the boto configuration file.
49
AWSQueryConnection.__init__(self, aws_access_key_id, aws_secret_access_key,
50
is_secure, port, proxy, proxy_port, proxy_user, proxy_pass,
51
host, debug, https_connection_factory, path)
53
def build_list_params(self, params, items, label):
54
if isinstance(items, str):
56
for i in range(1, len(items)+1):
57
params[label % i] = items[i-1]
59
def get_all_load_balancers(self, load_balancer_name=None):
61
Retrieve all load balancers associated with your account.
63
:type load_balancer_names: str
64
:param load_balancer_names: An optional filter string to get only one ELB
67
:return: A list of :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
70
if load_balancer_name:
71
#self.build_list_params(params, load_balancer_names, 'LoadBalancerName.%d')
72
params['LoadBalancerName'] = load_balancer_name
73
return self.get_list('DescribeLoadBalancers', params, [('member', LoadBalancer)])
76
def create_load_balancer(self, name, zones, listeners):
78
Create a new load balancer for your account.
81
:param name: The mnemonic name associated with the new load balancer
83
:type zones: List of strings
84
:param zones: The names of the availability zone(s) to add.
86
:type listeners: List of tuples
87
:param listeners: Each tuple contains three values.
88
(LoadBalancerPortNumber, InstancePortNumber, Protocol)
89
where LoadBalancerPortNumber and InstancePortNumber are
90
integer values between 1 and 65535 and Protocol is a
91
string containing either 'TCP' or 'HTTP'.
93
:rtype: :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
94
:return: The newly created :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
96
params = {'LoadBalancerName' : name}
97
for i in range(0, len(listeners)):
98
params['Listeners.member.%d.LoadBalancerPort' % (i+1)] = listeners[i][0]
99
params['Listeners.member.%d.InstancePort' % (i+1)] = listeners[i][1]
100
params['Listeners.member.%d.Protocol' % (i+1)] = listeners[i][2]
101
self.build_list_params(params, zones, 'AvailabilityZones.member.%d')
102
load_balancer = self.get_object('CreateLoadBalancer', params, LoadBalancer)
103
load_balancer.name = name
104
load_balancer.listeners = listeners
105
load_balancer.availability_zones = zones
108
def delete_load_balancer(self, name):
110
Delete a Load Balancer from your account.
113
:param name: The name of the Load Balancer to delete
115
params = {'LoadBalancerName': name}
116
return self.get_status('DeleteLoadBalancer', params)
118
def enable_availability_zones(self, load_balancer_name, zones_to_add):
120
Add availability zones to an existing Load Balancer
121
All zones must be in the same region as the Load Balancer
122
Adding zones that are already registered with the Load Balancer
125
:type load_balancer_name: string
126
:param load_balancer_name: The name of the Load Balancer
128
:type zones: List of strings
129
:param zones: The name of the zone(s) to add.
131
:rtype: List of strings
132
:return: An updated list of zones for this Load Balancer.
135
params = {'LoadBalancerName' : load_balancer_name}
136
self.build_list_params(params, zones_to_add, 'AvailabilityZones.member.%d')
137
return self.get_list('EnableAvailabilityZonesForLoadBalancer', params, None)
139
def disable_availability_zones(self, load_balancer_name, zones_to_remove):
141
Remove availability zones from an existing Load Balancer.
142
All zones must be in the same region as the Load Balancer.
143
Removing zones that are not registered with the Load Balancer
145
You cannot remove all zones from an Load Balancer.
147
:type load_balancer_name: string
148
:param load_balancer_name: The name of the Load Balancer
150
:type zones: List of strings
151
:param zones: The name of the zone(s) to remove.
153
:rtype: List of strings
154
:return: An updated list of zones for this Load Balancer.
157
params = {'LoadBalancerName' : load_balancer_name}
158
self.build_list_params(params, zones_to_remove, 'AvailabilityZones.member.%d')
159
return self.get_list('DisableAvailabilityZonesForLoadBalancer', params, None)
161
def register_instances(self, load_balancer_name, instances):
163
Add new Instances to an existing Load Balancer.
165
:type load_balancer_name: string
166
:param load_balancer_name: The name of the Load Balancer
168
:type instances: List of strings
169
:param instances: The instance ID's of the EC2 instances to add.
171
:rtype: List of strings
172
:return: An updated list of instances for this Load Balancer.
175
params = {'LoadBalancerName' : load_balancer_name}
176
self.build_list_params(params, instances, 'Instances.member.%d.InstanceId')
177
return self.get_list('RegisterInstancesWithLoadBalancer', params, [('member', InstanceInfo)])
179
def deregister_instances(self, load_balancer_name, instances):
181
Remove Instances from an existing Load Balancer.
183
:type load_balancer_name: string
184
:param load_balancer_name: The name of the Load Balancer
186
:type instances: List of strings
187
:param instances: The instance ID's of the EC2 instances to remove.
189
:rtype: List of strings
190
:return: An updated list of instances for this Load Balancer.
193
params = {'LoadBalancerName' : load_balancer_name}
194
self.build_list_params(params, instances, 'Instances.member.%d.InstanceId')
195
return self.get_list('DeregisterInstancesFromLoadBalancer', params, [('member', InstanceInfo)])
197
def describe_instance_health(self, load_balancer_name, instances=None):
199
Get current state of all Instances registered to an Load Balancer.
201
:type load_balancer_name: string
202
:param load_balancer_name: The name of the Load Balancer
204
:type instances: List of strings
205
:param instances: The instance ID's of the EC2 instances
206
to return status for. If not provided,
207
the state of all instances will be returned.
209
:rtype: List of :class:`boto.ec2.elb.instancestate.InstanceState`
210
:return: list of state info for instances in this Load Balancer.
213
params = {'LoadBalancerName' : load_balancer_name}
215
self.build_list_params(params, instances, 'instances.member.%d')
216
return self.get_list('DescribeInstanceHealth', params, [('member', InstanceState)])
218
def configure_health_check(self, name, health_check):
220
Define a health check for the EndPoints.
223
:param name: The mnemonic name associated with the new access point
225
:type health_check: :class:`boto.ec2.elb.healthcheck.HealthCheck`
226
:param health_check: A HealthCheck object populated with the desired
229
:rtype: :class:`boto.ec2.elb.healthcheck.HealthCheck`
230
:return: The updated :class:`boto.ec2.elb.healthcheck.HealthCheck`
232
params = {'LoadBalancerName' : name,
233
'HealthCheck.Timeout' : health_check.timeout,
234
'HealthCheck.Target' : health_check.target,
235
'HealthCheck.Interval' : health_check.interval,
236
'HealthCheck.UnhealthyThreshold' : health_check.unhealthy_threshold,
237
'HealthCheck.HealthyThreshold' : health_check.healthy_threshold}
238
return self.get_object('ConfigureHealthCheck', params, HealthCheck)