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
22
from boto.ec2.elb.healthcheck import HealthCheck
23
from boto.ec2.elb.listener import Listener
24
from boto.ec2.elb.listelement import ListElement
25
from boto.ec2.instanceinfo import InstanceInfo
26
from boto.resultset import ResultSet
28
class LoadBalancer(object):
30
Represents an EC2 Load Balancer
33
def __init__(self, connection=None, name=None, endpoints=None):
34
self.connection = connection
37
self.health_check = None
39
self.created_time = None
41
self.availability_zones = ListElement()
44
return 'LoadBalancer:%s' % self.name
46
def startElement(self, name, attrs, connection):
47
if name == 'HealthCheck':
48
self.health_check = HealthCheck(self)
49
return self.health_check
50
elif name == 'Listeners':
51
self.listeners = ResultSet([('member', Listener)])
53
elif name == 'AvailabilityZones':
54
return self.availability_zones
55
elif name == 'Instances':
56
self.instances = ResultSet([('member', InstanceInfo)])
61
def endElement(self, name, value, connection):
62
if name == 'LoadBalancerName':
64
elif name == 'DNSName':
66
elif name == 'CreatedTime':
67
self.created_time = value
68
elif name == 'InstanceId':
69
self.instances.append(value)
71
setattr(self, name, value)
73
def enable_zones(self, zones):
75
Enable availability zones to this Access Point.
76
All zones must be in the same region as the Access Point.
78
:type zones: string or List of strings
79
:param zones: The name of the zone(s) to add.
82
if isinstance(zones, str) or isinstance(zones, unicode):
84
new_zones = self.connection.enable_availability_zones(self.name, zones)
85
self.availability_zones = new_zones
87
def disable_zones(self, zones):
89
Disable availability zones from this Access Point.
91
:type zones: string or List of strings
92
:param zones: The name of the zone(s) to add.
95
if isinstance(zones, str) or isinstance(zones, unicode):
97
new_zones = self.connection.disable_availability_zones(self.name, zones)
98
self.availability_zones = new_zones
100
def register_instances(self, instances):
102
Add instances to this Load Balancer
103
All instances must be in the same region as the Load Balancer.
104
Adding endpoints that are already registered with the Load Balancer
107
:type zones: string or List of instance id's
108
:param zones: The name of the endpoint(s) to add.
111
if isinstance(instances, str) or isinstance(instances, unicode):
112
instances = [instances]
113
new_instances = self.connection.register_instances(self.name, instances)
114
self.instances = new_instances
116
def deregister_instances(self, instances):
118
Remove instances from this Load Balancer.
119
Removing instances that are not registered with the Load Balancer
122
:type zones: string or List of instance id's
123
:param zones: The name of the endpoint(s) to add.
126
if isinstance(instances, str) or isinstance(instances, unicode):
127
instances = [instances]
128
new_instances = self.connection.deregister_instances(self.name, instances)
129
self.instances = new_instances
133
Delete this load balancer
135
return self.connection.delete_load_balancer(self.name)
137
def configure_health_check(self, health_check):
138
self.connection.configure_health_check(self.name, health_check)
140
def get_instance_health(self, instances=None):
141
self.connection.describe_instance_health(self.name, instances)