OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 from boto.ec2.elb.healthcheck import HealthCheck |
| 24 from boto.ec2.elb.listener import Listener |
| 25 from boto.ec2.elb.listelement import ListElement |
| 26 from boto.ec2.elb.policies import Policies |
| 27 from boto.ec2.elb.securitygroup import SecurityGroup |
| 28 from boto.ec2.instanceinfo import InstanceInfo |
| 29 from boto.resultset import ResultSet |
| 30 |
| 31 |
| 32 class LoadBalancerZones(object): |
| 33 """ |
| 34 Used to collect the zones for a Load Balancer when enable_zones |
| 35 or disable_zones are called. |
| 36 """ |
| 37 def __init__(self, connection=None): |
| 38 self.connection = connection |
| 39 self.zones = ListElement() |
| 40 |
| 41 def startElement(self, name, attrs, connection): |
| 42 if name == 'AvailabilityZones': |
| 43 return self.zones |
| 44 |
| 45 |
| 46 class LoadBalancer(object): |
| 47 """ |
| 48 Represents an EC2 Load Balancer. |
| 49 """ |
| 50 |
| 51 def __init__(self, connection=None, name=None, endpoints=None): |
| 52 """ |
| 53 :ivar boto.ec2.elb.ELBConnection connection: The connection this load |
| 54 balancer was instance was instantiated from. |
| 55 :ivar list listeners: A list of tuples in the form of |
| 56 ``(<Inbound port>, <Outbound port>, <Protocol>)`` |
| 57 :ivar boto.ec2.elb.healthcheck.HealthCheck health_check: The health |
| 58 check policy for this load balancer. |
| 59 :ivar boto.ec2.elb.policies.Policies policies: Cookie stickiness and |
| 60 other policies. |
| 61 :ivar str dns_name: The external DNS name for the balancer. |
| 62 :ivar str created_time: A date+time string showing when the |
| 63 load balancer was created. |
| 64 :ivar list instances: A list of :py:class:`boto.ec2.instanceinfo.Instanc
eInfo` |
| 65 instances, representing the EC2 instances this load balancer is |
| 66 distributing requests to. |
| 67 :ivar list availability_zones: The availability zones this balancer |
| 68 covers. |
| 69 :ivar str canonical_hosted_zone_name: Current CNAME for the balancer. |
| 70 :ivar str canonical_hosted_zone_name_id: The Route 53 hosted zone |
| 71 ID of this balancer. Needed when creating an Alias record in a |
| 72 Route 53 hosted zone. |
| 73 :ivar boto.ec2.elb.securitygroup.SecurityGroup source_security_group: |
| 74 The security group that you can use as part of your inbound rules |
| 75 for your load balancer back-end instances to disallow traffic |
| 76 from sources other than your load balancer. |
| 77 :ivar list subnets: A list of subnets this balancer is on. |
| 78 :ivar list security_groups: A list of additional security groups that |
| 79 have been applied. |
| 80 :ivar str vpc_id: The ID of the VPC that this ELB resides within. |
| 81 """ |
| 82 self.connection = connection |
| 83 self.name = name |
| 84 self.listeners = None |
| 85 self.health_check = None |
| 86 self.policies = None |
| 87 self.dns_name = None |
| 88 self.created_time = None |
| 89 self.instances = None |
| 90 self.availability_zones = ListElement() |
| 91 self.canonical_hosted_zone_name = None |
| 92 self.canonical_hosted_zone_name_id = None |
| 93 self.source_security_group = None |
| 94 self.subnets = ListElement() |
| 95 self.security_groups = ListElement() |
| 96 self.vpc_id = None |
| 97 self.scheme = None |
| 98 |
| 99 def __repr__(self): |
| 100 return 'LoadBalancer:%s' % self.name |
| 101 |
| 102 def startElement(self, name, attrs, connection): |
| 103 if name == 'HealthCheck': |
| 104 self.health_check = HealthCheck(self) |
| 105 return self.health_check |
| 106 elif name == 'ListenerDescriptions': |
| 107 self.listeners = ResultSet([('member', Listener)]) |
| 108 return self.listeners |
| 109 elif name == 'AvailabilityZones': |
| 110 return self.availability_zones |
| 111 elif name == 'Instances': |
| 112 self.instances = ResultSet([('member', InstanceInfo)]) |
| 113 return self.instances |
| 114 elif name == 'Policies': |
| 115 self.policies = Policies(self) |
| 116 return self.policies |
| 117 elif name == 'SourceSecurityGroup': |
| 118 self.source_security_group = SecurityGroup() |
| 119 return self.source_security_group |
| 120 elif name == 'Subnets': |
| 121 return self.subnets |
| 122 elif name == 'SecurityGroups': |
| 123 return self.security_groups |
| 124 elif name == 'VPCId': |
| 125 pass |
| 126 else: |
| 127 return None |
| 128 |
| 129 def endElement(self, name, value, connection): |
| 130 if name == 'LoadBalancerName': |
| 131 self.name = value |
| 132 elif name == 'DNSName': |
| 133 self.dns_name = value |
| 134 elif name == 'CreatedTime': |
| 135 self.created_time = value |
| 136 elif name == 'InstanceId': |
| 137 self.instances.append(value) |
| 138 elif name == 'CanonicalHostedZoneName': |
| 139 self.canonical_hosted_zone_name = value |
| 140 elif name == 'CanonicalHostedZoneNameID': |
| 141 self.canonical_hosted_zone_name_id = value |
| 142 elif name == 'VPCId': |
| 143 self.vpc_id = value |
| 144 elif name == 'Scheme': |
| 145 self.scheme = value |
| 146 else: |
| 147 setattr(self, name, value) |
| 148 |
| 149 def enable_zones(self, zones): |
| 150 """ |
| 151 Enable availability zones to this Access Point. |
| 152 All zones must be in the same region as the Access Point. |
| 153 |
| 154 :type zones: string or List of strings |
| 155 :param zones: The name of the zone(s) to add. |
| 156 |
| 157 """ |
| 158 if isinstance(zones, str) or isinstance(zones, unicode): |
| 159 zones = [zones] |
| 160 new_zones = self.connection.enable_availability_zones(self.name, zones) |
| 161 self.availability_zones = new_zones |
| 162 |
| 163 def disable_zones(self, zones): |
| 164 """ |
| 165 Disable availability zones from this Access Point. |
| 166 |
| 167 :type zones: string or List of strings |
| 168 :param zones: The name of the zone(s) to add. |
| 169 |
| 170 """ |
| 171 if isinstance(zones, str) or isinstance(zones, unicode): |
| 172 zones = [zones] |
| 173 new_zones = self.connection.disable_availability_zones(self.name, zones) |
| 174 self.availability_zones = new_zones |
| 175 |
| 176 def register_instances(self, instances): |
| 177 """ |
| 178 Adds instances to this load balancer. All instances must be in the same |
| 179 region as the load balancer. Adding endpoints that are already |
| 180 registered with the load balancer has no effect. |
| 181 |
| 182 :param list instances: List of instance IDs (strings) that you'd like |
| 183 to add to this load balancer. |
| 184 |
| 185 """ |
| 186 if isinstance(instances, str) or isinstance(instances, unicode): |
| 187 instances = [instances] |
| 188 new_instances = self.connection.register_instances(self.name, |
| 189 instances) |
| 190 self.instances = new_instances |
| 191 |
| 192 def deregister_instances(self, instances): |
| 193 """ |
| 194 Remove instances from this load balancer. Removing instances that are |
| 195 not registered with the load balancer has no effect. |
| 196 |
| 197 :param list instances: List of instance IDs (strings) that you'd like |
| 198 to remove from this load balancer. |
| 199 |
| 200 """ |
| 201 if isinstance(instances, str) or isinstance(instances, unicode): |
| 202 instances = [instances] |
| 203 new_instances = self.connection.deregister_instances(self.name, |
| 204 instances) |
| 205 self.instances = new_instances |
| 206 |
| 207 def delete(self): |
| 208 """ |
| 209 Delete this load balancer. |
| 210 """ |
| 211 return self.connection.delete_load_balancer(self.name) |
| 212 |
| 213 def configure_health_check(self, health_check): |
| 214 """ |
| 215 Configures the health check behavior for the instances behind this |
| 216 load balancer. See :ref:`elb-configuring-a-health-check` for a |
| 217 walkthrough. |
| 218 |
| 219 :param boto.ec2.elb.healthcheck.HealthCheck health_check: A |
| 220 HealthCheck instance that tells the load balancer how to check |
| 221 its instances for health. |
| 222 """ |
| 223 return self.connection.configure_health_check(self.name, health_check) |
| 224 |
| 225 def get_instance_health(self, instances=None): |
| 226 """ |
| 227 Returns a list of :py:class:`boto.ec2.elb.instancestate.InstanceState` |
| 228 objects, which show the health of the instances attached to this |
| 229 load balancer. |
| 230 |
| 231 :rtype: list |
| 232 :returns: A list of |
| 233 :py:class:`InstanceState <boto.ec2.elb.instancestate.InstanceState>` |
| 234 instances, representing the instances |
| 235 attached to this load balancer. |
| 236 """ |
| 237 return self.connection.describe_instance_health(self.name, instances) |
| 238 |
| 239 def create_listeners(self, listeners): |
| 240 return self.connection.create_load_balancer_listeners(self.name, |
| 241 listeners) |
| 242 |
| 243 def create_listener(self, inPort, outPort=None, proto="tcp"): |
| 244 if outPort == None: |
| 245 outPort = inPort |
| 246 return self.create_listeners([(inPort, outPort, proto)]) |
| 247 |
| 248 def delete_listeners(self, listeners): |
| 249 return self.connection.delete_load_balancer_listeners(self.name, |
| 250 listeners) |
| 251 |
| 252 def delete_listener(self, inPort): |
| 253 return self.delete_listeners([inPort]) |
| 254 |
| 255 def delete_policy(self, policy_name): |
| 256 """ |
| 257 Deletes a policy from the LoadBalancer. The specified policy must not |
| 258 be enabled for any listeners. |
| 259 """ |
| 260 return self.connection.delete_lb_policy(self.name, policy_name) |
| 261 |
| 262 def set_policies_of_listener(self, lb_port, policies): |
| 263 return self.connection.set_lb_policies_of_listener(self.name, |
| 264 lb_port, |
| 265 policies) |
| 266 |
| 267 def create_cookie_stickiness_policy(self, cookie_expiration_period, |
| 268 policy_name): |
| 269 return self.connection.create_lb_cookie_stickiness_policy(cookie_expirat
ion_period, self.name, policy_name) |
| 270 |
| 271 def create_app_cookie_stickiness_policy(self, name, policy_name): |
| 272 return self.connection.create_app_cookie_stickiness_policy(name, |
| 273 self.name, |
| 274 policy_name) |
| 275 |
| 276 def set_listener_SSL_certificate(self, lb_port, ssl_certificate_id): |
| 277 return self.connection.set_lb_listener_SSL_certificate(self.name, |
| 278 lb_port, |
| 279 ssl_certificate_i
d) |
| 280 |
| 281 def attach_subnets(self, subnets): |
| 282 """ |
| 283 Attaches load balancer to one or more subnets. |
| 284 Attaching subnets that are already registered with the |
| 285 Load Balancer has no effect. |
| 286 |
| 287 :type subnets: string or List of strings |
| 288 :param subnets: The name of the subnet(s) to add. |
| 289 |
| 290 """ |
| 291 if isinstance(subnets, str) or isinstance(subnets, unicode): |
| 292 subnets = [subnets] |
| 293 new_subnets = self.connection.attach_lb_to_subnets(self.name, subnets) |
| 294 self.subnets = new_subnets |
| 295 |
| 296 def detach_subnets(self, subnets): |
| 297 """ |
| 298 Detaches load balancer from one or more subnets. |
| 299 |
| 300 :type subnets: string or List of strings |
| 301 :param subnets: The name of the subnet(s) to detach. |
| 302 |
| 303 """ |
| 304 if isinstance(subnets, str) or isinstance(subnets, unicode): |
| 305 subnets = [subnets] |
| 306 new_subnets = self.connection.detach_lb_to_subnets(self.name, subnets) |
| 307 self.subnets = new_subnets |
| 308 |
| 309 def apply_security_groups(self, security_groups): |
| 310 """ |
| 311 Applies security groups to the load balancer. |
| 312 Applying security groups that are already registered with the |
| 313 Load Balancer has no effect. |
| 314 |
| 315 :type security_groups: string or List of strings |
| 316 :param security_groups: The name of the security group(s) to add. |
| 317 |
| 318 """ |
| 319 if isinstance(security_groups, str) or \ |
| 320 isinstance(security_groups, unicode): |
| 321 security_groups = [security_groups] |
| 322 new_sgs = self.connection.apply_security_groups_to_lb( |
| 323 self.name, security_groups) |
| 324 self.security_groups = new_sgs |
OLD | NEW |