| OLD | NEW |
| 1 # Copyright (c) 2006-2011 Mitch Garnaat http://garnaat.org/ | 1 # Copyright (c) 2006-2011 Mitch Garnaat http://garnaat.org/ |
| 2 # | 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a | 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the | 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including | 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 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 | 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- | 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: | 9 # lowing conditions: |
| 10 # | 10 # |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 from boto.ec2.instanceinfo import InstanceInfo | 27 from boto.ec2.instanceinfo import InstanceInfo |
| 28 from boto.ec2.elb.loadbalancer import LoadBalancer | 28 from boto.ec2.elb.loadbalancer import LoadBalancer |
| 29 from boto.ec2.elb.instancestate import InstanceState | 29 from boto.ec2.elb.instancestate import InstanceState |
| 30 from boto.ec2.elb.healthcheck import HealthCheck | 30 from boto.ec2.elb.healthcheck import HealthCheck |
| 31 from boto.regioninfo import RegionInfo | 31 from boto.regioninfo import RegionInfo |
| 32 import boto | 32 import boto |
| 33 | 33 |
| 34 RegionData = { | 34 RegionData = { |
| 35 'us-east-1' : 'elasticloadbalancing.us-east-1.amazonaws.com', | 35 'us-east-1' : 'elasticloadbalancing.us-east-1.amazonaws.com', |
| 36 'us-west-1' : 'elasticloadbalancing.us-west-1.amazonaws.com', | 36 'us-west-1' : 'elasticloadbalancing.us-west-1.amazonaws.com', |
| 37 'us-west-2' : 'elasticloadbalancing.us-west-2.amazonaws.com', |
| 38 'sa-east-1' : 'elasticloadbalancing.sa-east-1.amazonaws.com', |
| 37 'eu-west-1' : 'elasticloadbalancing.eu-west-1.amazonaws.com', | 39 'eu-west-1' : 'elasticloadbalancing.eu-west-1.amazonaws.com', |
| 38 'ap-northeast-1' : 'elasticloadbalancing.ap-northeast-1.amazonaws.com', | 40 'ap-northeast-1' : 'elasticloadbalancing.ap-northeast-1.amazonaws.com', |
| 39 'ap-southeast-1' : 'elasticloadbalancing.ap-southeast-1.amazonaws.com'} | 41 'ap-southeast-1' : 'elasticloadbalancing.ap-southeast-1.amazonaws.com'} |
| 40 | 42 |
| 41 def regions(): | 43 def regions(): |
| 42 """ | 44 """ |
| 43 Get all available regions for the SDB service. | 45 Get all available regions for the SDB service. |
| 44 | 46 |
| 45 :rtype: list | 47 :rtype: list |
| 46 :return: A list of :class:`boto.RegionInfo` instances | 48 :return: A list of :class:`boto.RegionInfo` instances |
| (...skipping 17 matching lines...) Expand all Loading... |
| 64 :return: A connection to the given region, or None if an invalid region | 66 :return: A connection to the given region, or None if an invalid region |
| 65 name is given | 67 name is given |
| 66 """ | 68 """ |
| 67 for region in regions(): | 69 for region in regions(): |
| 68 if region.name == region_name: | 70 if region.name == region_name: |
| 69 return region.connect(**kw_params) | 71 return region.connect(**kw_params) |
| 70 return None | 72 return None |
| 71 | 73 |
| 72 class ELBConnection(AWSQueryConnection): | 74 class ELBConnection(AWSQueryConnection): |
| 73 | 75 |
| 74 APIVersion = boto.config.get('Boto', 'elb_version', '2011-04-05') | 76 APIVersion = boto.config.get('Boto', 'elb_version', '2011-11-15') |
| 75 DefaultRegionName = boto.config.get('Boto', 'elb_region_name', 'us-east-1') | 77 DefaultRegionName = boto.config.get('Boto', 'elb_region_name', 'us-east-1') |
| 76 DefaultRegionEndpoint = boto.config.get('Boto', 'elb_region_endpoint', | 78 DefaultRegionEndpoint = boto.config.get('Boto', 'elb_region_endpoint', |
| 77 'elasticloadbalancing.amazonaws.com'
) | 79 'elasticloadbalancing.amazonaws.com'
) |
| 78 | 80 |
| 79 def __init__(self, aws_access_key_id=None, aws_secret_access_key=None, | 81 def __init__(self, aws_access_key_id=None, aws_secret_access_key=None, |
| 80 is_secure=False, port=None, proxy=None, proxy_port=None, | 82 is_secure=False, port=None, proxy=None, proxy_port=None, |
| 81 proxy_user=None, proxy_pass=None, debug=0, | 83 proxy_user=None, proxy_pass=None, debug=0, |
| 82 https_connection_factory=None, region=None, path='/'): | 84 https_connection_factory=None, region=None, path='/'): |
| 83 """ | 85 """ |
| 84 Init method to create a new connection to EC2 Load Balancing Service. | 86 Init method to create a new connection to EC2 Load Balancing Service. |
| 85 | 87 |
| 86 B{Note:} The region argument is overridden by the region specified in | 88 .. note:: The region argument is overridden by the region specified in |
| 87 the boto configuration file. | 89 the boto configuration file. |
| 88 """ | 90 """ |
| 89 if not region: | 91 if not region: |
| 90 region = RegionInfo(self, self.DefaultRegionName, | 92 region = RegionInfo(self, self.DefaultRegionName, |
| 91 self.DefaultRegionEndpoint) | 93 self.DefaultRegionEndpoint) |
| 92 self.region = region | 94 self.region = region |
| 93 AWSQueryConnection.__init__(self, aws_access_key_id, | 95 AWSQueryConnection.__init__(self, aws_access_key_id, |
| 94 aws_secret_access_key, | 96 aws_secret_access_key, |
| 95 is_secure, port, proxy, proxy_port, | 97 is_secure, port, proxy, proxy_port, |
| 96 proxy_user, proxy_pass, | 98 proxy_user, proxy_pass, |
| 97 self.region.endpoint, debug, | 99 self.region.endpoint, debug, |
| 98 https_connection_factory, path) | 100 https_connection_factory, path) |
| 99 | 101 |
| 100 def _required_auth_capability(self): | 102 def _required_auth_capability(self): |
| 101 return ['ec2'] | 103 return ['ec2'] |
| 102 | 104 |
| 103 def build_list_params(self, params, items, label): | 105 def build_list_params(self, params, items, label): |
| 104 if isinstance(items, str): | 106 if isinstance(items, str): |
| 105 items = [items] | 107 items = [items] |
| 106 for i, item in enumerate(items, 1): | 108 for index, item in enumerate(items): |
| 107 params[label % i] = item | 109 params[label % (index + 1)] = item |
| 108 | 110 |
| 109 def get_all_load_balancers(self, load_balancer_names=None): | 111 def get_all_load_balancers(self, load_balancer_names=None): |
| 110 """ | 112 """ |
| 111 Retrieve all load balancers associated with your account. | 113 Retrieve all load balancers associated with your account. |
| 112 | 114 |
| 113 :type load_balancer_names: list | 115 :type load_balancer_names: list |
| 114 :param load_balancer_names: An optional list of load balancer names | 116 :keyword load_balancer_names: An optional list of load balancer names. |
| 115 | 117 |
| 116 :rtype: list | 118 :rtype: :py:class:`boto.resultset.ResultSet` |
| 117 :return: A list of :class:`boto.ec2.elb.loadbalancer.LoadBalancer` | 119 :return: A ResultSet containing instances of |
| 120 :class:`boto.ec2.elb.loadbalancer.LoadBalancer` |
| 118 """ | 121 """ |
| 119 params = {} | 122 params = {} |
| 120 if load_balancer_names: | 123 if load_balancer_names: |
| 121 self.build_list_params(params, load_balancer_names, | 124 self.build_list_params(params, load_balancer_names, |
| 122 'LoadBalancerNames.member.%d') | 125 'LoadBalancerNames.member.%d') |
| 123 return self.get_list('DescribeLoadBalancers', params, | 126 return self.get_list('DescribeLoadBalancers', params, |
| 124 [('member', LoadBalancer)]) | 127 [('member', LoadBalancer)]) |
| 125 | 128 |
| 126 def create_load_balancer(self, name, zones, listeners): | 129 def create_load_balancer(self, name, zones, listeners, subnets=None, |
| 130 security_groups=None): |
| 127 """ | 131 """ |
| 128 Create a new load balancer for your account. | 132 Create a new load balancer for your account. By default the load |
| 133 balancer will be created in EC2. To create a load balancer inside a |
| 134 VPC, parameter zones must be set to None and subnets must not be None. |
| 135 The load balancer will be automatically created under the VPC that |
| 136 contains the subnet(s) specified. |
| 129 | 137 |
| 130 :type name: string | 138 :type name: string |
| 131 :param name: The mnemonic name associated with the new load balancer | 139 :param name: The mnemonic name associated with the new load balancer |
| 132 | 140 |
| 133 :type zones: List of strings | 141 :type zones: List of strings |
| 134 :param zones: The names of the availability zone(s) to add. | 142 :param zones: The names of the availability zone(s) to add. |
| 135 | 143 |
| 136 :type listeners: List of tuples | 144 :type listeners: List of tuples |
| 137 :param listeners: Each tuple contains three or four values, | 145 :param listeners: Each tuple contains three or four values, |
| 138 (LoadBalancerPortNumber, InstancePortNumber, | 146 (LoadBalancerPortNumber, InstancePortNumber, |
| 139 Protocol, [SSLCertificateId]) | 147 Protocol, [SSLCertificateId]) |
| 140 where LoadBalancerPortNumber and InstancePortNumber | 148 where LoadBalancerPortNumber and InstancePortNumber |
| 141 are integer values between 1 and 65535, Protocol is a | 149 are integer values between 1 and 65535, Protocol is a |
| 142 string containing either 'TCP', 'HTTP' or 'HTTPS'; | 150 string containing either 'TCP', 'HTTP' or 'HTTPS'; |
| 143 SSLCertificateID is the ARN of a AWS AIM certificate, | 151 SSLCertificateID is the ARN of a AWS AIM certificate, |
| 144 and must be specified when doing HTTPS. | 152 and must be specified when doing HTTPS. |
| 145 | 153 |
| 146 :rtype: :class:`boto.ec2.elb.loadbalancer.LoadBalancer` | 154 :rtype: :class:`boto.ec2.elb.loadbalancer.LoadBalancer` |
| 147 :return: The newly created :class:`boto.ec2.elb.loadbalancer.LoadBalance
r` | 155 :return: The newly created :class:`boto.ec2.elb.loadbalancer.LoadBalance
r` |
| 148 """ | 156 """ |
| 149 params = {'LoadBalancerName' : name} | 157 params = {'LoadBalancerName' : name} |
| 150 for i, listener in enumerate(listeners, 1): | 158 for index, listener in enumerate(listeners): |
| 159 i = index + 1 |
| 151 params['Listeners.member.%d.LoadBalancerPort' % i] = listener[0] | 160 params['Listeners.member.%d.LoadBalancerPort' % i] = listener[0] |
| 152 params['Listeners.member.%d.InstancePort' % i] = listener[1] | 161 params['Listeners.member.%d.InstancePort' % i] = listener[1] |
| 153 params['Listeners.member.%d.Protocol' % i] = listener[2] | 162 params['Listeners.member.%d.Protocol' % i] = listener[2] |
| 154 if listener[2]=='HTTPS': | 163 if listener[2]=='HTTPS': |
| 155 params['Listeners.member.%d.SSLCertificateId' % i] = listener[3] | 164 params['Listeners.member.%d.SSLCertificateId' % i] = listener[3] |
| 156 self.build_list_params(params, zones, 'AvailabilityZones.member.%d') | 165 if zones: |
| 166 self.build_list_params(params, zones, 'AvailabilityZones.member.%d') |
| 167 |
| 168 if subnets: |
| 169 self.build_list_params(params, subnets, 'Subnets.member.%d') |
| 170 |
| 171 if security_groups: |
| 172 self.build_list_params(params, security_groups, |
| 173 'SecurityGroups.member.%d') |
| 174 |
| 157 load_balancer = self.get_object('CreateLoadBalancer', | 175 load_balancer = self.get_object('CreateLoadBalancer', |
| 158 params, LoadBalancer) | 176 params, LoadBalancer) |
| 159 load_balancer.name = name | 177 load_balancer.name = name |
| 160 load_balancer.listeners = listeners | 178 load_balancer.listeners = listeners |
| 161 load_balancer.availability_zones = zones | 179 load_balancer.availability_zones = zones |
| 180 load_balancer.subnets = subnets |
| 181 load_balancer.security_groups = security_groups |
| 162 return load_balancer | 182 return load_balancer |
| 163 | 183 |
| 164 def create_load_balancer_listeners(self, name, listeners): | 184 def create_load_balancer_listeners(self, name, listeners): |
| 165 """ | 185 """ |
| 166 Creates a Listener (or group of listeners) for an existing Load Balancer | 186 Creates a Listener (or group of listeners) for an existing Load Balancer |
| 167 | 187 |
| 168 :type name: string | 188 :type name: string |
| 169 :param name: The name of the load balancer to create the listeners for | 189 :param name: The name of the load balancer to create the listeners for |
| 170 | 190 |
| 171 :type listeners: List of tuples | 191 :type listeners: List of tuples |
| 172 :param listeners: Each tuple contains three values, | 192 :param listeners: Each tuple contains three values, |
| 173 (LoadBalancerPortNumber, InstancePortNumber, Protocol, | 193 (LoadBalancerPortNumber, InstancePortNumber, Protocol, |
| 174 [SSLCertificateId]) | 194 [SSLCertificateId]) |
| 175 where LoadBalancerPortNumber and InstancePortNumber ar
e | 195 where LoadBalancerPortNumber and InstancePortNumber ar
e |
| 176 integer values between 1 and 65535, Protocol is a | 196 integer values between 1 and 65535, Protocol is a |
| 177 string containing either 'TCP', 'HTTP' or 'HTTPS'; | 197 string containing either 'TCP', 'HTTP', 'HTTPS', or 'S
SL'; |
| 178 SSLCertificateID is the ARN of a AWS AIM certificate, | 198 SSLCertificateID is the ARN of a AWS AIM certificate, |
| 179 and must be specified when doing HTTPS. | 199 and must be specified when doing HTTPS or SSL. |
| 180 | 200 |
| 181 :return: The status of the request | 201 :return: The status of the request |
| 182 """ | 202 """ |
| 183 params = {'LoadBalancerName' : name} | 203 params = {'LoadBalancerName' : name} |
| 184 for i, listener in enumerate(listeners, 1): | 204 for index, listener in enumerate(listeners): |
| 205 i = index + 1 |
| 185 params['Listeners.member.%d.LoadBalancerPort' % i] = listener[0] | 206 params['Listeners.member.%d.LoadBalancerPort' % i] = listener[0] |
| 186 params['Listeners.member.%d.InstancePort' % i] = listener[1] | 207 params['Listeners.member.%d.InstancePort' % i] = listener[1] |
| 187 params['Listeners.member.%d.Protocol' % i] = listener[2] | 208 params['Listeners.member.%d.Protocol' % i] = listener[2] |
| 188 if listener[2]=='HTTPS': | 209 if listener[2] == 'HTTPS' or listener[2] == 'SSL': |
| 189 params['Listeners.member.%d.SSLCertificateId' % i] = listener[3] | 210 params['Listeners.member.%d.SSLCertificateId' % i] = listener[3] |
| 190 return self.get_status('CreateLoadBalancerListeners', params) | 211 return self.get_status('CreateLoadBalancerListeners', params) |
| 191 | 212 |
| 192 | 213 |
| 193 def delete_load_balancer(self, name): | 214 def delete_load_balancer(self, name): |
| 194 """ | 215 """ |
| 195 Delete a Load Balancer from your account. | 216 Delete a Load Balancer from your account. |
| 196 | 217 |
| 197 :type name: string | 218 :type name: string |
| 198 :param name: The name of the Load Balancer to delete | 219 :param name: The name of the Load Balancer to delete |
| 199 """ | 220 """ |
| 200 params = {'LoadBalancerName': name} | 221 params = {'LoadBalancerName': name} |
| 201 return self.get_status('DeleteLoadBalancer', params) | 222 return self.get_status('DeleteLoadBalancer', params) |
| 202 | 223 |
| 203 def delete_load_balancer_listeners(self, name, ports): | 224 def delete_load_balancer_listeners(self, name, ports): |
| 204 """ | 225 """ |
| 205 Deletes a load balancer listener (or group of listeners) | 226 Deletes a load balancer listener (or group of listeners) |
| 206 | 227 |
| 207 :type name: string | 228 :type name: string |
| 208 :param name: The name of the load balancer to create the listeners for | 229 :param name: The name of the load balancer to create the listeners for |
| 209 | 230 |
| 210 :type ports: List int | 231 :type ports: List int |
| 211 :param ports: Each int represents the port on the ELB to be removed | 232 :param ports: Each int represents the port on the ELB to be removed |
| 212 | 233 |
| 213 :return: The status of the request | 234 :return: The status of the request |
| 214 """ | 235 """ |
| 215 params = {'LoadBalancerName' : name} | 236 params = {'LoadBalancerName' : name} |
| 216 for i, port in enumerate(ports, 1): | 237 for index, port in enumerate(ports): |
| 217 params['LoadBalancerPorts.member.%d' % i] = port | 238 params['LoadBalancerPorts.member.%d' % (index + 1)] = port |
| 218 return self.get_status('DeleteLoadBalancerListeners', params) | 239 return self.get_status('DeleteLoadBalancerListeners', params) |
| 219 | 240 |
| 220 def enable_availability_zones(self, load_balancer_name, zones_to_add): | 241 def enable_availability_zones(self, load_balancer_name, zones_to_add): |
| 221 """ | 242 """ |
| 222 Add availability zones to an existing Load Balancer | 243 Add availability zones to an existing Load Balancer |
| 223 All zones must be in the same region as the Load Balancer | 244 All zones must be in the same region as the Load Balancer |
| 224 Adding zones that are already registered with the Load Balancer | 245 Adding zones that are already registered with the Load Balancer |
| 225 has no effect. | 246 has no effect. |
| 226 | 247 |
| 227 :type load_balancer_name: string | 248 :type load_balancer_name: string |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 balancer. Currently only zero (0) or one (1) policy can be associated | 451 balancer. Currently only zero (0) or one (1) policy can be associated |
| 431 with a listener. | 452 with a listener. |
| 432 """ | 453 """ |
| 433 params = { | 454 params = { |
| 434 'LoadBalancerName' : lb_name, | 455 'LoadBalancerName' : lb_name, |
| 435 'LoadBalancerPort' : lb_port, | 456 'LoadBalancerPort' : lb_port, |
| 436 } | 457 } |
| 437 self.build_list_params(params, policies, 'PolicyNames.member.%d') | 458 self.build_list_params(params, policies, 'PolicyNames.member.%d') |
| 438 return self.get_status('SetLoadBalancerPoliciesOfListener', params) | 459 return self.get_status('SetLoadBalancerPoliciesOfListener', params) |
| 439 | 460 |
| 461 def apply_security_groups_to_lb(self, name, security_groups): |
| 462 """ |
| 463 Applies security groups to the load balancer. |
| 464 Applying security groups that are already registered with the |
| 465 Load Balancer has no effect. |
| 440 | 466 |
| 467 :type name: string |
| 468 :param name: The name of the Load Balancer |
| 469 |
| 470 :type security_groups: List of strings |
| 471 :param security_groups: The name of the security group(s) to add. |
| 472 |
| 473 :rtype: List of strings |
| 474 :return: An updated list of security groups for this Load Balancer. |
| 475 |
| 476 """ |
| 477 params = {'LoadBalancerName' : name} |
| 478 self.build_list_params(params, security_groups, |
| 479 'SecurityGroups.member.%d') |
| 480 return self.get_list('ApplySecurityGroupsToLoadBalancer', |
| 481 params, |
| 482 None) |
| 483 |
| 484 def attach_lb_to_subnets(self, name, subnets): |
| 485 """ |
| 486 Attaches load balancer to one or more subnets. |
| 487 Attaching subnets that are already registered with the |
| 488 Load Balancer has no effect. |
| 489 |
| 490 :type name: string |
| 491 :param name: The name of the Load Balancer |
| 492 |
| 493 :type subnets: List of strings |
| 494 :param subnets: The name of the subnet(s) to add. |
| 495 |
| 496 :rtype: List of strings |
| 497 :return: An updated list of subnets for this Load Balancer. |
| 498 |
| 499 """ |
| 500 params = {'LoadBalancerName' : name} |
| 501 self.build_list_params(params, subnets, |
| 502 'Subnets.member.%d') |
| 503 return self.get_list('AttachLoadBalancerToSubnets', |
| 504 params, |
| 505 None) |
| 506 |
| 507 def detach_lb_from_subnets(self, name, subnets): |
| 508 """ |
| 509 Detaches load balancer from one or more subnets. |
| 510 |
| 511 :type name: string |
| 512 :param name: The name of the Load Balancer |
| 513 |
| 514 :type subnets: List of strings |
| 515 :param subnets: The name of the subnet(s) to detach. |
| 516 |
| 517 :rtype: List of strings |
| 518 :return: An updated list of subnets for this Load Balancer. |
| 519 |
| 520 """ |
| 521 params = {'LoadBalancerName' : name} |
| 522 self.build_list_params(params, subnets, |
| 523 'Subnets.member.%d') |
| 524 return self.get_list('DettachLoadBalancerFromSubnets', |
| 525 params, |
| 526 None) |
| OLD | NEW |