Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: third_party/gsutil/boto/ec2/elb/__init__.py

Issue 12042069: Scripts to download files from google storage based on sha1 sums (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Removed gsutil/tests and gsutil/docs Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/
2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates.
3 # All Rights Reserved
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish, dis-
9 # tribute, sublicense, and/or sell copies of the Software, and to permit
10 # persons to whom the Software is furnished to do so, subject to the fol-
11 # lowing conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23 #
24 """
25 This module provides an interface to the Elastic Compute Cloud (EC2)
26 load balancing service from AWS.
27 """
28 from boto.connection import AWSQueryConnection
29 from boto.ec2.instanceinfo import InstanceInfo
30 from boto.ec2.elb.loadbalancer import LoadBalancer, LoadBalancerZones
31 from boto.ec2.elb.instancestate import InstanceState
32 from boto.ec2.elb.healthcheck import HealthCheck
33 from boto.ec2.elb.listelement import ListElement
34 from boto.regioninfo import RegionInfo
35 import boto
36
37 RegionData = {
38 'us-east-1': 'elasticloadbalancing.us-east-1.amazonaws.com',
39 'us-west-1': 'elasticloadbalancing.us-west-1.amazonaws.com',
40 'us-west-2': 'elasticloadbalancing.us-west-2.amazonaws.com',
41 'sa-east-1': 'elasticloadbalancing.sa-east-1.amazonaws.com',
42 'eu-west-1': 'elasticloadbalancing.eu-west-1.amazonaws.com',
43 'ap-northeast-1': 'elasticloadbalancing.ap-northeast-1.amazonaws.com',
44 'ap-southeast-1': 'elasticloadbalancing.ap-southeast-1.amazonaws.com',
45 'ap-southeast-2': 'elasticloadbalancing.ap-southeast-2.amazonaws.com',
46 }
47
48
49 def regions():
50 """
51 Get all available regions for the ELB service.
52
53 :rtype: list
54 :return: A list of :class:`boto.RegionInfo` instances
55 """
56 regions = []
57 for region_name in RegionData:
58 region = RegionInfo(name=region_name,
59 endpoint=RegionData[region_name],
60 connection_cls=ELBConnection)
61 regions.append(region)
62 return regions
63
64
65 def connect_to_region(region_name, **kw_params):
66 """
67 Given a valid region name, return a
68 :class:`boto.ec2.elb.ELBConnection`.
69
70 :param str region_name: The name of the region to connect to.
71
72 :rtype: :class:`boto.ec2.ELBConnection` or ``None``
73 :return: A connection to the given region, or None if an invalid region
74 name is given
75 """
76 for region in regions():
77 if region.name == region_name:
78 return region.connect(**kw_params)
79 return None
80
81
82 class ELBConnection(AWSQueryConnection):
83
84 APIVersion = boto.config.get('Boto', 'elb_version', '2012-06-01')
85 DefaultRegionName = boto.config.get('Boto', 'elb_region_name', 'us-east-1')
86 DefaultRegionEndpoint = boto.config.get('Boto', 'elb_region_endpoint',
87 'elasticloadbalancing.us-east-1.amaz onaws.com')
88
89 def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
90 is_secure=False, port=None, proxy=None, proxy_port=None,
91 proxy_user=None, proxy_pass=None, debug=0,
92 https_connection_factory=None, region=None, path='/',
93 security_token=None, validate_certs=True):
94 """
95 Init method to create a new connection to EC2 Load Balancing Service.
96
97 .. note:: The region argument is overridden by the region specified in
98 the boto configuration file.
99 """
100 if not region:
101 region = RegionInfo(self, self.DefaultRegionName,
102 self.DefaultRegionEndpoint)
103 self.region = region
104 AWSQueryConnection.__init__(self, aws_access_key_id,
105 aws_secret_access_key,
106 is_secure, port, proxy, proxy_port,
107 proxy_user, proxy_pass,
108 self.region.endpoint, debug,
109 https_connection_factory, path,
110 security_token,
111 validate_certs=validate_certs)
112
113 def _required_auth_capability(self):
114 return ['ec2']
115
116 def build_list_params(self, params, items, label):
117 if isinstance(items, str):
118 items = [items]
119 for index, item in enumerate(items):
120 params[label % (index + 1)] = item
121
122 def get_all_load_balancers(self, load_balancer_names=None):
123 """
124 Retrieve all load balancers associated with your account.
125
126 :type load_balancer_names: list
127 :keyword load_balancer_names: An optional list of load balancer names.
128
129 :rtype: :py:class:`boto.resultset.ResultSet`
130 :return: A ResultSet containing instances of
131 :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
132 """
133 params = {}
134 if load_balancer_names:
135 self.build_list_params(params, load_balancer_names,
136 'LoadBalancerNames.member.%d')
137 return self.get_list('DescribeLoadBalancers', params,
138 [('member', LoadBalancer)])
139
140 def create_load_balancer(self, name, zones, listeners, subnets=None,
141 security_groups=None, scheme='internet-facing'):
142 """
143 Create a new load balancer for your account. By default the load
144 balancer will be created in EC2. To create a load balancer inside a
145 VPC, parameter zones must be set to None and subnets must not be None.
146 The load balancer will be automatically created under the VPC that
147 contains the subnet(s) specified.
148
149 :type name: string
150 :param name: The mnemonic name associated with the new load balancer
151
152 :type zones: List of strings
153 :param zones: The names of the availability zone(s) to add.
154
155 :type listeners: List of tuples
156 :param listeners: Each tuple contains three or four values,
157 (LoadBalancerPortNumber, InstancePortNumber, Protocol,
158 [SSLCertificateId]) where LoadBalancerPortNumber and
159 InstancePortNumber are integer values between 1 and 65535,
160 Protocol is a string containing either 'TCP', 'HTTP' or
161 'HTTPS'; SSLCertificateID is the ARN of a AWS AIM
162 certificate, and must be specified when doing HTTPS.
163
164 :type subnets: list of strings
165 :param subnets: A list of subnet IDs in your VPC to attach to
166 your LoadBalancer.
167
168 :type security_groups: list of strings
169 :param security_groups: The security groups assigned to your
170 LoadBalancer within your VPC.
171
172 :type scheme: string
173 :param scheme: The type of a LoadBalancer. By default, Elastic
174 Load Balancing creates an internet-facing LoadBalancer with
175 a publicly resolvable DNS name, which resolves to public IP
176 addresses.
177
178 Specify the value internal for this option to create an
179 internal LoadBalancer with a DNS name that resolves to
180 private IP addresses.
181
182 This option is only available for LoadBalancers attached
183 to an Amazon VPC.
184
185 :rtype: :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
186 :return: The newly created
187 :class:`boto.ec2.elb.loadbalancer.LoadBalancer`
188 """
189 params = {'LoadBalancerName': name,
190 'Scheme': scheme}
191 for index, listener in enumerate(listeners):
192 i = index + 1
193 protocol = listener[2].upper()
194 params['Listeners.member.%d.LoadBalancerPort' % i] = listener[0]
195 params['Listeners.member.%d.InstancePort' % i] = listener[1]
196 params['Listeners.member.%d.Protocol' % i] = listener[2]
197 if protocol == 'HTTPS' or protocol == 'SSL':
198 params['Listeners.member.%d.SSLCertificateId' % i] = listener[3]
199 if zones:
200 self.build_list_params(params, zones, 'AvailabilityZones.member.%d')
201
202 if subnets:
203 self.build_list_params(params, subnets, 'Subnets.member.%d')
204
205 if security_groups:
206 self.build_list_params(params, security_groups,
207 'SecurityGroups.member.%d')
208
209 load_balancer = self.get_object('CreateLoadBalancer',
210 params, LoadBalancer)
211 load_balancer.name = name
212 load_balancer.listeners = listeners
213 load_balancer.availability_zones = zones
214 load_balancer.subnets = subnets
215 load_balancer.security_groups = security_groups
216 return load_balancer
217
218 def create_load_balancer_listeners(self, name, listeners):
219 """
220 Creates a Listener (or group of listeners) for an existing
221 Load Balancer
222
223 :type name: string
224 :param name: The name of the load balancer to create the listeners for
225
226 :type listeners: List of tuples
227 :param listeners: Each tuple contains three values,
228 (LoadBalancerPortNumber, InstancePortNumber, Protocol,
229 [SSLCertificateId]) where LoadBalancerPortNumber and
230 InstancePortNumber are integer values between 1 and 65535,
231 Protocol is a string containing either 'TCP', 'HTTP',
232 'HTTPS', or 'SSL'; SSLCertificateID is the ARN of a AWS
233 AIM certificate, and must be specified when doing HTTPS or
234 SSL.
235
236 :return: The status of the request
237 """
238 params = {'LoadBalancerName': name}
239 for index, listener in enumerate(listeners):
240 i = index + 1
241 protocol = listener[2].upper()
242 params['Listeners.member.%d.LoadBalancerPort' % i] = listener[0]
243 params['Listeners.member.%d.InstancePort' % i] = listener[1]
244 params['Listeners.member.%d.Protocol' % i] = listener[2]
245 if protocol == 'HTTPS' or protocol == 'SSL':
246 params['Listeners.member.%d.SSLCertificateId' % i] = listener[3]
247 return self.get_status('CreateLoadBalancerListeners', params)
248
249 def delete_load_balancer(self, name):
250 """
251 Delete a Load Balancer from your account.
252
253 :type name: string
254 :param name: The name of the Load Balancer to delete
255 """
256 params = {'LoadBalancerName': name}
257 return self.get_status('DeleteLoadBalancer', params)
258
259 def delete_load_balancer_listeners(self, name, ports):
260 """
261 Deletes a load balancer listener (or group of listeners)
262
263 :type name: string
264 :param name: The name of the load balancer to create the listeners for
265
266 :type ports: List int
267 :param ports: Each int represents the port on the ELB to be removed
268
269 :return: The status of the request
270 """
271 params = {'LoadBalancerName': name}
272 for index, port in enumerate(ports):
273 params['LoadBalancerPorts.member.%d' % (index + 1)] = port
274 return self.get_status('DeleteLoadBalancerListeners', params)
275
276 def enable_availability_zones(self, load_balancer_name, zones_to_add):
277 """
278 Add availability zones to an existing Load Balancer
279 All zones must be in the same region as the Load Balancer
280 Adding zones that are already registered with the Load Balancer
281 has no effect.
282
283 :type load_balancer_name: string
284 :param load_balancer_name: The name of the Load Balancer
285
286 :type zones: List of strings
287 :param zones: The name of the zone(s) to add.
288
289 :rtype: List of strings
290 :return: An updated list of zones for this Load Balancer.
291
292 """
293 params = {'LoadBalancerName': load_balancer_name}
294 self.build_list_params(params, zones_to_add,
295 'AvailabilityZones.member.%d')
296 obj = self.get_object('EnableAvailabilityZonesForLoadBalancer',
297 params, LoadBalancerZones)
298 return obj.zones
299
300 def disable_availability_zones(self, load_balancer_name, zones_to_remove):
301 """
302 Remove availability zones from an existing Load Balancer.
303 All zones must be in the same region as the Load Balancer.
304 Removing zones that are not registered with the Load Balancer
305 has no effect.
306 You cannot remove all zones from an Load Balancer.
307
308 :type load_balancer_name: string
309 :param load_balancer_name: The name of the Load Balancer
310
311 :type zones: List of strings
312 :param zones: The name of the zone(s) to remove.
313
314 :rtype: List of strings
315 :return: An updated list of zones for this Load Balancer.
316
317 """
318 params = {'LoadBalancerName': load_balancer_name}
319 self.build_list_params(params, zones_to_remove,
320 'AvailabilityZones.member.%d')
321 obj = self.get_object('DisableAvailabilityZonesForLoadBalancer',
322 params, LoadBalancerZones)
323 return obj.zones
324
325 def register_instances(self, load_balancer_name, instances):
326 """
327 Add new Instances to an existing Load Balancer.
328
329 :type load_balancer_name: string
330 :param load_balancer_name: The name of the Load Balancer
331
332 :type instances: List of strings
333 :param instances: The instance ID's of the EC2 instances to add.
334
335 :rtype: List of strings
336 :return: An updated list of instances for this Load Balancer.
337
338 """
339 params = {'LoadBalancerName': load_balancer_name}
340 self.build_list_params(params, instances,
341 'Instances.member.%d.InstanceId')
342 return self.get_list('RegisterInstancesWithLoadBalancer',
343 params, [('member', InstanceInfo)])
344
345 def deregister_instances(self, load_balancer_name, instances):
346 """
347 Remove Instances from an existing Load Balancer.
348
349 :type load_balancer_name: string
350 :param load_balancer_name: The name of the Load Balancer
351
352 :type instances: List of strings
353 :param instances: The instance ID's of the EC2 instances to remove.
354
355 :rtype: List of strings
356 :return: An updated list of instances for this Load Balancer.
357
358 """
359 params = {'LoadBalancerName': load_balancer_name}
360 self.build_list_params(params, instances,
361 'Instances.member.%d.InstanceId')
362 return self.get_list('DeregisterInstancesFromLoadBalancer',
363 params, [('member', InstanceInfo)])
364
365 def describe_instance_health(self, load_balancer_name, instances=None):
366 """
367 Get current state of all Instances registered to an Load Balancer.
368
369 :type load_balancer_name: string
370 :param load_balancer_name: The name of the Load Balancer
371
372 :type instances: List of strings
373 :param instances: The instance ID's of the EC2 instances
374 to return status for. If not provided,
375 the state of all instances will be returned.
376
377 :rtype: List of :class:`boto.ec2.elb.instancestate.InstanceState`
378 :return: list of state info for instances in this Load Balancer.
379
380 """
381 params = {'LoadBalancerName': load_balancer_name}
382 if instances:
383 self.build_list_params(params, instances,
384 'Instances.member.%d.InstanceId')
385 return self.get_list('DescribeInstanceHealth', params,
386 [('member', InstanceState)])
387
388 def configure_health_check(self, name, health_check):
389 """
390 Define a health check for the EndPoints.
391
392 :type name: string
393 :param name: The mnemonic name associated with the load balancer
394
395 :type health_check: :class:`boto.ec2.elb.healthcheck.HealthCheck`
396 :param health_check: A HealthCheck object populated with the desired
397 values.
398
399 :rtype: :class:`boto.ec2.elb.healthcheck.HealthCheck`
400 :return: The updated :class:`boto.ec2.elb.healthcheck.HealthCheck`
401 """
402 params = {'LoadBalancerName': name,
403 'HealthCheck.Timeout': health_check.timeout,
404 'HealthCheck.Target': health_check.target,
405 'HealthCheck.Interval': health_check.interval,
406 'HealthCheck.UnhealthyThreshold': health_check.unhealthy_thres hold,
407 'HealthCheck.HealthyThreshold': health_check.healthy_threshold }
408 return self.get_object('ConfigureHealthCheck', params, HealthCheck)
409
410 def set_lb_listener_SSL_certificate(self, lb_name, lb_port,
411 ssl_certificate_id):
412 """
413 Sets the certificate that terminates the specified listener's SSL
414 connections. The specified certificate replaces any prior certificate
415 that was used on the same LoadBalancer and port.
416 """
417 params = {'LoadBalancerName': lb_name,
418 'LoadBalancerPort': lb_port,
419 'SSLCertificateId': ssl_certificate_id}
420 return self.get_status('SetLoadBalancerListenerSSLCertificate', params)
421
422 def create_app_cookie_stickiness_policy(self, name, lb_name, policy_name):
423 """
424 Generates a stickiness policy with sticky session lifetimes that follow
425 that of an application-generated cookie. This policy can only be
426 associated with HTTP listeners.
427
428 This policy is similar to the policy created by
429 CreateLBCookieStickinessPolicy, except that the lifetime of the special
430 Elastic Load Balancing cookie follows the lifetime of the
431 application-generated cookie specified in the policy configuration. The
432 load balancer only inserts a new stickiness cookie when the application
433 response includes a new application cookie.
434
435 If the application cookie is explicitly removed or expires, the session
436 stops being sticky until a new application cookie is issued.
437 """
438 params = {'CookieName': name,
439 'LoadBalancerName': lb_name,
440 'PolicyName': policy_name}
441 return self.get_status('CreateAppCookieStickinessPolicy', params)
442
443 def create_lb_cookie_stickiness_policy(self, cookie_expiration_period,
444 lb_name, policy_name):
445 """
446 Generates a stickiness policy with sticky session lifetimes controlled
447 by the lifetime of the browser (user-agent) or a specified expiration
448 period. This policy can only be associated only with HTTP listeners.
449
450 When a load balancer implements this policy, the load balancer uses a
451 special cookie to track the backend server instance for each request.
452 When the load balancer receives a request, it first checks to see if
453 this cookie is present in the request. If so, the load balancer sends
454 the request to the application server specified in the cookie. If not,
455 the load balancer sends the request to a server that is chosen based on
456 the existing load balancing algorithm.
457
458 A cookie is inserted into the response for binding subsequent requests
459 from the same user to that server. The validity of the cookie is based
460 on the cookie expiration time, which is specified in the policy
461 configuration.
462
463 None may be passed for cookie_expiration_period.
464 """
465 params = {'LoadBalancerName': lb_name,
466 'PolicyName': policy_name}
467 if cookie_expiration_period is not None:
468 params['CookieExpirationPeriod'] = cookie_expiration_period
469 return self.get_status('CreateLBCookieStickinessPolicy', params)
470
471 def delete_lb_policy(self, lb_name, policy_name):
472 """
473 Deletes a policy from the LoadBalancer. The specified policy must not
474 be enabled for any listeners.
475 """
476 params = {'LoadBalancerName': lb_name,
477 'PolicyName': policy_name}
478 return self.get_status('DeleteLoadBalancerPolicy', params)
479
480 def set_lb_policies_of_listener(self, lb_name, lb_port, policies):
481 """
482 Associates, updates, or disables a policy with a listener on the load
483 balancer. Currently only zero (0) or one (1) policy can be associated
484 with a listener.
485 """
486 params = {'LoadBalancerName': lb_name,
487 'LoadBalancerPort': lb_port}
488 self.build_list_params(params, policies, 'PolicyNames.member.%d')
489 return self.get_status('SetLoadBalancerPoliciesOfListener', params)
490
491 def apply_security_groups_to_lb(self, name, security_groups):
492 """
493 Applies security groups to the load balancer.
494 Applying security groups that are already registered with the
495 Load Balancer has no effect.
496
497 :type name: string
498 :param name: The name of the Load Balancer
499
500 :type security_groups: List of strings
501 :param security_groups: The name of the security group(s) to add.
502
503 :rtype: List of strings
504 :return: An updated list of security groups for this Load Balancer.
505
506 """
507 params = {'LoadBalancerName': name}
508 self.build_list_params(params, security_groups,
509 'SecurityGroups.member.%d')
510 return self.get_list('ApplySecurityGroupsToLoadBalancer',
511 params, None)
512
513 def attach_lb_to_subnets(self, name, subnets):
514 """
515 Attaches load balancer to one or more subnets.
516 Attaching subnets that are already registered with the
517 Load Balancer has no effect.
518
519 :type name: string
520 :param name: The name of the Load Balancer
521
522 :type subnets: List of strings
523 :param subnets: The name of the subnet(s) to add.
524
525 :rtype: List of strings
526 :return: An updated list of subnets for this Load Balancer.
527
528 """
529 params = {'LoadBalancerName': name}
530 self.build_list_params(params, subnets,
531 'Subnets.member.%d')
532 return self.get_list('AttachLoadBalancerToSubnets',
533 params, None)
534
535 def detach_lb_from_subnets(self, name, subnets):
536 """
537 Detaches load balancer from one or more subnets.
538
539 :type name: string
540 :param name: The name of the Load Balancer
541
542 :type subnets: List of strings
543 :param subnets: The name of the subnet(s) to detach.
544
545 :rtype: List of strings
546 :return: An updated list of subnets for this Load Balancer.
547
548 """
549 params = {'LoadBalancerName': name}
550 self.build_list_params(params, subnets,
551 'Subnets.member.%d')
552 return self.get_list('DettachLoadBalancerFromSubnets',
553 params, None)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698