~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/boto/boto/ec2/elb/__init__.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
"""
 
23
This module provides an interface to the Elastic Compute Cloud (EC2)
 
24
load balancing service from AWS.
 
25
"""
 
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
 
31
import boto
 
32
 
 
33
class ELBConnection(AWSQueryConnection):
 
34
 
 
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
 
39
 
 
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='/'):
 
44
        """
 
45
        Init method to create a new connection to EC2 Load Balancing Service.
 
46
 
 
47
        B{Note:} The host argument is overridden by the host specified in the boto configuration file.
 
48
        """
 
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)
 
52
 
 
53
    def build_list_params(self, params, items, label):
 
54
        if isinstance(items, str):
 
55
            items = [items]
 
56
        for i in range(1, len(items)+1):
 
57
            params[label % i] = items[i-1]
 
58
 
 
59
    def get_all_load_balancers(self, load_balancer_name=None):
 
60
        """
 
61
        Retrieve all load balancers associated with your account.
 
62
 
 
63
        :type load_balancer_names: str
 
64
        :param load_balancer_names: An optional filter string to get only one ELB
 
65
 
 
66
        :rtype: list
 
67
        :return: A list of :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
 
68
        """
 
69
        params = {}
 
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)])
 
74
 
 
75
 
 
76
    def create_load_balancer(self, name, zones, listeners):
 
77
        """
 
78
        Create a new load balancer for your account.
 
79
 
 
80
        :type name: string
 
81
        :param name: The mnemonic name associated with the new load balancer
 
82
 
 
83
        :type zones: List of strings
 
84
        :param zones: The names of the availability zone(s) to add.
 
85
 
 
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'.
 
92
 
 
93
        :rtype: :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
 
94
        :return: The newly created :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
 
95
        """
 
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
 
106
        return load_balancer
 
107
 
 
108
    def delete_load_balancer(self, name):
 
109
        """
 
110
        Delete a Load Balancer from your account.
 
111
 
 
112
        :type name: string
 
113
        :param name: The name of the Load Balancer to delete
 
114
        """
 
115
        params = {'LoadBalancerName': name}
 
116
        return self.get_status('DeleteLoadBalancer', params)
 
117
 
 
118
    def enable_availability_zones(self, load_balancer_name, zones_to_add):
 
119
        """
 
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
 
123
        has no effect.
 
124
 
 
125
        :type load_balancer_name: string
 
126
        :param load_balancer_name: The name of the Load Balancer
 
127
 
 
128
        :type zones: List of strings
 
129
        :param zones: The name of the zone(s) to add.
 
130
 
 
131
        :rtype: List of strings
 
132
        :return: An updated list of zones for this Load Balancer.
 
133
 
 
134
        """
 
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)
 
138
 
 
139
    def disable_availability_zones(self, load_balancer_name, zones_to_remove):
 
140
        """
 
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
 
144
        has no effect.
 
145
        You cannot remove all zones from an Load Balancer.
 
146
 
 
147
        :type load_balancer_name: string
 
148
        :param load_balancer_name: The name of the Load Balancer
 
149
 
 
150
        :type zones: List of strings
 
151
        :param zones: The name of the zone(s) to remove.
 
152
 
 
153
        :rtype: List of strings
 
154
        :return: An updated list of zones for this Load Balancer.
 
155
 
 
156
        """
 
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)
 
160
 
 
161
    def register_instances(self, load_balancer_name, instances):
 
162
        """
 
163
        Add new Instances to an existing Load Balancer.
 
164
 
 
165
        :type load_balancer_name: string
 
166
        :param load_balancer_name: The name of the Load Balancer
 
167
 
 
168
        :type instances: List of strings
 
169
        :param instances: The instance ID's of the EC2 instances to add.
 
170
 
 
171
        :rtype: List of strings
 
172
        :return: An updated list of instances for this Load Balancer.
 
173
 
 
174
        """
 
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)])
 
178
 
 
179
    def deregister_instances(self, load_balancer_name, instances):
 
180
        """
 
181
        Remove Instances from an existing Load Balancer.
 
182
 
 
183
        :type load_balancer_name: string
 
184
        :param load_balancer_name: The name of the Load Balancer
 
185
 
 
186
        :type instances: List of strings
 
187
        :param instances: The instance ID's of the EC2 instances to remove.
 
188
 
 
189
        :rtype: List of strings
 
190
        :return: An updated list of instances for this Load Balancer.
 
191
 
 
192
        """
 
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)])
 
196
 
 
197
    def describe_instance_health(self, load_balancer_name, instances=None):
 
198
        """
 
199
        Get current state of all Instances registered to an Load Balancer.
 
200
 
 
201
        :type load_balancer_name: string
 
202
        :param load_balancer_name: The name of the Load Balancer
 
203
 
 
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.
 
208
 
 
209
        :rtype: List of :class:`boto.ec2.elb.instancestate.InstanceState`
 
210
        :return: list of state info for instances in this Load Balancer.
 
211
 
 
212
        """
 
213
        params = {'LoadBalancerName' : load_balancer_name}
 
214
        if instances:
 
215
            self.build_list_params(params, instances, 'instances.member.%d')
 
216
        return self.get_list('DescribeInstanceHealth', params, [('member', InstanceState)])
 
217
 
 
218
    def configure_health_check(self, name, health_check):
 
219
        """
 
220
        Define a health check for the EndPoints.
 
221
 
 
222
        :type name: string
 
223
        :param name: The mnemonic name associated with the new access point
 
224
 
 
225
        :type health_check: :class:`boto.ec2.elb.healthcheck.HealthCheck`
 
226
        :param health_check: A HealthCheck object populated with the desired
 
227
                             values.
 
228
 
 
229
        :rtype: :class:`boto.ec2.elb.healthcheck.HealthCheck`
 
230
        :return: The updated :class:`boto.ec2.elb.healthcheck.HealthCheck`
 
231
        """
 
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)