~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to vendor/boto/boto/ec2/elb/loadbalancer.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
 
2
#
 
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-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
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
 
20
# IN THE SOFTWARE.
 
21
 
 
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
 
27
 
 
28
class LoadBalancer(object):
 
29
    """
 
30
    Represents an EC2 Load Balancer
 
31
    """
 
32
 
 
33
    def __init__(self, connection=None, name=None, endpoints=None):
 
34
        self.connection = connection
 
35
        self.name = name
 
36
        self.listeners = None
 
37
        self.health_check = None
 
38
        self.dns_name = None
 
39
        self.created_time = None
 
40
        self.instances = None
 
41
        self.availability_zones = ListElement()
 
42
 
 
43
    def __repr__(self):
 
44
        return 'LoadBalancer:%s' % self.name
 
45
 
 
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)])
 
52
            return self.listeners
 
53
        elif name == 'AvailabilityZones':
 
54
            return self.availability_zones
 
55
        elif name == 'Instances':
 
56
            self.instances = ResultSet([('member', InstanceInfo)])
 
57
            return self.instances
 
58
        else:
 
59
            return None
 
60
 
 
61
    def endElement(self, name, value, connection):
 
62
        if name == 'LoadBalancerName':
 
63
            self.name = value
 
64
        elif name == 'DNSName':
 
65
            self.dns_name = value
 
66
        elif name == 'CreatedTime':
 
67
            self.created_time = value
 
68
        elif name == 'InstanceId':
 
69
            self.instances.append(value)
 
70
        else:
 
71
            setattr(self, name, value)
 
72
 
 
73
    def enable_zones(self, zones):
 
74
        """
 
75
        Enable availability zones to this Access Point.
 
76
        All zones must be in the same region as the Access Point.
 
77
 
 
78
        :type zones: string or List of strings
 
79
        :param zones: The name of the zone(s) to add.
 
80
 
 
81
        """
 
82
        if isinstance(zones, str) or isinstance(zones, unicode):
 
83
            zones = [zones]
 
84
        new_zones = self.connection.enable_availability_zones(self.name, zones)
 
85
        self.availability_zones = new_zones
 
86
 
 
87
    def disable_zones(self, zones):
 
88
        """
 
89
        Disable availability zones from this Access Point.
 
90
 
 
91
        :type zones: string or List of strings
 
92
        :param zones: The name of the zone(s) to add.
 
93
 
 
94
        """
 
95
        if isinstance(zones, str) or isinstance(zones, unicode):
 
96
            zones = [zones]
 
97
        new_zones = self.connection.disable_availability_zones(self.name, zones)
 
98
        self.availability_zones = new_zones
 
99
 
 
100
    def register_instances(self, instances):
 
101
        """
 
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
 
105
        has no effect.
 
106
 
 
107
        :type zones: string or List of instance id's
 
108
        :param zones: The name of the endpoint(s) to add.
 
109
 
 
110
        """
 
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
 
115
 
 
116
    def deregister_instances(self, instances):
 
117
        """
 
118
        Remove instances from this Load Balancer.
 
119
        Removing instances that are not registered with the Load Balancer
 
120
        has no effect.
 
121
 
 
122
        :type zones: string or List of instance id's
 
123
        :param zones: The name of the endpoint(s) to add.
 
124
 
 
125
        """
 
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
 
130
 
 
131
    def delete(self):
 
132
        """
 
133
        Delete this load balancer
 
134
        """
 
135
        return self.connection.delete_load_balancer(self.name)
 
136
 
 
137
    def configure_health_check(self, health_check):
 
138
        self.connection.configure_health_check(self.name, health_check)
 
139
 
 
140
    def get_instance_health(self, instances=None):
 
141
        self.connection.describe_instance_health(self.name, instances)
 
142