def as_resultset(things): if not isinstance(things, (list, tuple)): things = [things] result = ResultSet() for thing in things: result.append(thing) return result
def get_assignments(self, hit_id=None, page_size=None): rs = ResultSet() with open(os.path.join(self.output_dir, "hit" + str(hit_id) + ".json"), 'r') as f: hit = json.load(f) for a in hit['assignments']: if a['status'] == 'Submitted': answers = [] for k in a['answers']: ans = QuestionFormAnswer(None) ans.fields = [a['answers'][k]] ans.qid = k answers.append(ans) #answers = [type("QuestionFormAnswer",(object,),dict(fields=[a['answers'][k]],qid=k)) for k in a['answers']] ass = Assignment(AssignmentId=str(a['hit_id']) + '_' + str(a['worker']), WorkerId=a['worker'], HITId=a['hit_id'], AssignmentStatus=a['status'], Deadline=a['accept_time'] + hit['lifetime'], AcceptTime=a['accept_time'], SubmitTime=a['submit_time'], answers=[answers]) rs.append(ass) return rs
def get_all_groups(names=None, max_records=None, next_token=None): groups = ResultSet() for i in xrange(1, 3): tags = [ boto.ec2.autoscale.tag.Tag( None, key='aws:cloudformation:stack-name', value='test{0}'.format(i)) ] asg = AutoScalingGroup() asg.name = 'test{0}'.format(i) asg.tags = tags groups.append(asg) groups.next_token = None return groups
def create_hit(self, hit_type=None, question=None, lifetime=3600 * 24 * 7, max_assignments=1): with open( os.path.join(self.output_dir, "hit_type" + str(hit_type) + ".json"), 'r') as f: ht = json.load(f) rs = ResultSet() hit_id = len(ht['hits']) #if question: # matchObj = re.match( r'.*/hits/(.*).html', question.external_url, re.M|re.I) # if matchObj and matchObj.groups() and len(matchObj.groups()) > 0: # hit_id = matchObj.group(1) rs.append(HitResponse(HITId=hit_id)) hit = { 'mturk_id': rs[0].HITId, 'hit_type': hit_type, 'external_url': question.external_url, 'assignments': [], 'lifetime': lifetime, 'status': 'Assignable', 'max_assignments': max_assignments, 'create_time': time.time() } ht['hits'].append([rs[0].HITId, max_assignments, 0, [], 0]) with open( os.path.join(self.output_dir, "hit_type" + str(hit_type) + ".json"), 'w') as f: json.dump(ht, f) os.chmod( os.path.join(self.output_dir, "hit_type" + str(hit_type) + ".json"), 0666) with open( os.path.join(self.output_dir, "hit" + str(hit['mturk_id']) + ".json"), 'w') as f: json.dump(hit, f) os.chmod( os.path.join(self.output_dir, "hit" + str(hit['mturk_id']) + ".json"), 0666) return rs
def test_resultset_two_next(self): e1 = Mock() e2 = Mock() rs1 = ResultSet() rs1.append(e1) rs1.append(e2) rs1.next_token = 't1' e3 = Mock() e4 = Mock() rs2 = ResultSet() rs2.append(e3) rs2.append(e4) rs2.next_token = 't2' e5 = Mock() e6 = Mock() rs3 = ResultSet() rs3.append(e5) rs3.append(e6) func = Mock() results = [rs2, rs3] def se_invoke(f, *args, **argv): return results.pop(0) with patch('%s.invoke_with_throttling_retries' % pbm) as mock_invoke: mock_invoke.side_effect = se_invoke res = _paginate_resultset(rs1, func, 'foo', bar='barval') assert isinstance(res, ResultSet) assert len(res) == 6 assert res[0] == e1 assert res[1] == e2 assert res[2] == e3 assert res[3] == e4 assert res[4] == e5 assert res[5] == e6 assert mock_invoke.mock_calls == [ call(func, 'foo', bar='barval', next_token='t1'), call(func, 'foo', bar='barval', next_token='t2') ]
class LoadBalancer(object): """ Represents an EC2 Load Balancer """ def __init__(self, connection=None, name=None, endpoints=None): self.connection = connection self.name = name self.listeners = None self.health_check = None self.policies = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() def __repr__(self): return 'LoadBalancer:%s' % self.name def startElement(self, name, attrs, connection): if name == 'HealthCheck': self.health_check = HealthCheck(self) return self.health_check elif name == 'ListenerDescriptions': self.listeners = ResultSet([('member', Listener)]) return self.listeners elif name == 'AvailabilityZones': return self.availability_zones elif name == 'Instances': self.instances = ResultSet([('member', InstanceInfo)]) return self.instances elif name == 'Policies': self.policies = Policies(self) return self.policies else: return None def endElement(self, name, value, connection): if name == 'LoadBalancerName': self.name = value elif name == 'DNSName': self.dns_name = value elif name == 'CreatedTime': self.created_time = value elif name == 'InstanceId': self.instances.append(value) else: setattr(self, name, value) def enable_zones(self, zones): """ Enable availability zones to this Access Point. All zones must be in the same region as the Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, basestring): zones = [zones] new_zones = self.connection.enable_availability_zones(self.name, zones) self.availability_zones = new_zones def disable_zones(self, zones): """ Disable availability zones from this Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, basestring): zones = [zones] new_zones = self.connection.disable_availability_zones(self.name, zones) self.availability_zones = new_zones def register_instances(self, instances): """ Add instances to this Load Balancer All instances must be in the same region as the Load Balancer. Adding endpoints that are already registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, basestring): instances = [instances] new_instances = self.connection.register_instances(self.name, instances) self.instances = new_instances def deregister_instances(self, instances): """ Remove instances from this Load Balancer. Removing instances that are not registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, basestring): instances = [instances] new_instances = self.connection.deregister_instances(self.name, instances) self.instances = new_instances def delete(self): """ Delete this load balancer """ return self.connection.delete_load_balancer(self.name) def configure_health_check(self, health_check): return self.connection.configure_health_check(self.name, health_check) def get_instance_health(self, instances=None): return self.connection.describe_instance_health(self.name, instances) def create_listeners(self, listeners): return self.connection.create_load_balancer_listeners(self.name, listeners) def create_listener(self, inPort, outPort=None, proto="tcp"): if outPort == None: outPort = inPort return self.create_listeners([(inPort, outPort, proto)]) def delete_listeners(self, listeners): return self.connection.delete_load_balancer_listeners(self.name, listeners) def delete_listener(self, inPort, outPort=None, proto="tcp"): if outPort == None: outPort = inPort return self.delete_listeners([(inPort, outPort, proto)]) def delete_policy(self, policy_name): """ Deletes a policy from the LoadBalancer. The specified policy must not be enabled for any listeners. """ return self.connection.delete_lb_policy(self.name, policy_name) def set_policies_of_listener(self, lb_port, policies): return self.connection.set_lb_policies_of_listener(self.name, lb_port, policies) def create_cookie_stickiness_policy(self, cookie_expiration_period, policy_name): return self.connection.create_lb_cookie_stickiness_policy(cookie_expiration_period, self.name, policy_name) def create_app_cookie_stickiness_policy(self, name, policy_name): return self.connection.create_app_cookie_stickiness_policy(name, self.name, policy_name) def set_listener_SSL_certificate(self, lb_port, ssl_certificate_id): return self.connection.set_lb_listener_SSL_certificate(self.name, lb_port, ssl_certificate_id)
class LoadBalancer(object): """ Represents an EC2 Load Balancer. """ def __init__(self, connection=None, name=None, endpoints=None): """ :ivar boto.ec2.elb.ELBConnection connection: The connection this load balancer was instance was instantiated from. :ivar list listeners: A list of tuples in the form of ``(<Inbound port>, <Outbound port>, <Protocol>)`` :ivar boto.ec2.elb.healthcheck.HealthCheck health_check: The health check policy for this load balancer. :ivar boto.ec2.elb.policies.Policies policies: Cookie stickiness and other policies. :ivar str dns_name: The external DNS name for the balancer. :ivar str created_time: A date+time string showing when the load balancer was created. :ivar list instances: A list of :py:class:`boto.ec2.instanceinfo.InstanceInfo` instances, representing the EC2 instances this load balancer is distributing requests to. :ivar list availability_zones: The availability zones this balancer covers. :ivar str canonical_hosted_zone_name: Current CNAME for the balancer. :ivar str canonical_hosted_zone_name_id: The Route 53 hosted zone ID of this balancer. Needed when creating an Alias record in a Route 53 hosted zone. :ivar boto.ec2.elb.securitygroup.SecurityGroup source_security_group: The security group that you can use as part of your inbound rules for your load balancer back-end instances to disallow traffic from sources other than your load balancer. :ivar list subnets: A list of subnets this balancer is on. :ivar list security_groups: A list of additional security groups that have been applied. :ivar str vpc_id: The ID of the VPC that this ELB resides within. :ivar list backends: A list of :py:class:`boto.ec2.elb.loadbalancer.Backend back-end server descriptions. """ self.connection = connection self.name = name self.listeners = None self.health_check = None self.policies = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() self.canonical_hosted_zone_name = None self.canonical_hosted_zone_name_id = None self.source_security_group = None self.subnets = ListElement() self.security_groups = ListElement() self.vpc_id = None self.scheme = None self.backends = None self._attributes = None def __repr__(self): return "LoadBalancer:%s" % self.name def startElement(self, name, attrs, connection): if name == "HealthCheck": self.health_check = HealthCheck(self) return self.health_check elif name == "ListenerDescriptions": self.listeners = ResultSet([("member", Listener)]) return self.listeners elif name == "AvailabilityZones": return self.availability_zones elif name == "Instances": self.instances = ResultSet([("member", InstanceInfo)]) return self.instances elif name == "Policies": self.policies = Policies(self) return self.policies elif name == "SourceSecurityGroup": self.source_security_group = SecurityGroup() return self.source_security_group elif name == "Subnets": return self.subnets elif name == "SecurityGroups": return self.security_groups elif name == "VPCId": pass elif name == "BackendServerDescriptions": self.backends = ResultSet([("member", Backend)]) return self.backends else: return None def endElement(self, name, value, connection): if name == "LoadBalancerName": self.name = value elif name == "DNSName": self.dns_name = value elif name == "CreatedTime": self.created_time = value elif name == "InstanceId": self.instances.append(value) elif name == "CanonicalHostedZoneName": self.canonical_hosted_zone_name = value elif name == "CanonicalHostedZoneNameID": self.canonical_hosted_zone_name_id = value elif name == "VPCId": self.vpc_id = value elif name == "Scheme": self.scheme = value else: setattr(self, name, value) def enable_zones(self, zones): """ Enable availability zones to this Access Point. All zones must be in the same region as the Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, basestring): zones = [zones] new_zones = self.connection.enable_availability_zones(self.name, zones) self.availability_zones = new_zones def disable_zones(self, zones): """ Disable availability zones from this Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, basestring): zones = [zones] new_zones = self.connection.disable_availability_zones(self.name, zones) self.availability_zones = new_zones def get_attributes(self, force=False): """ Gets the LbAttributes. The Attributes will be cached. :type force: bool :param force: Ignore cache value and reload. :rtype: boto.ec2.elb.attributes.LbAttributes :return: The LbAttribues object """ if not self._attributes or force: self._attributes = self.connection.get_all_lb_attributes(self.name) return self._attributes def is_cross_zone_load_balancing(self, force=False): """ Identifies if the ELB is current configured to do CrossZone Balancing. :type force: bool :param force: Ignore cache value and reload. :rtype: bool :return: True if balancing is enabled, False if not. """ return self.get_attributes(force).cross_zone_load_balancing.enabled def enable_cross_zone_load_balancing(self): """ Turns on CrossZone Load Balancing for this ELB. :rtype: bool :return: True if successful, False if not. """ success = self.connection.modify_lb_attribute(self.name, "crossZoneLoadBalancing", True) if success and self._attributes: self._attributes.cross_zone_load_balancing.enabled = True return success def disable_cross_zone_load_balancing(self): """ Turns off CrossZone Load Balancing for this ELB. :rtype: bool :return: True if successful, False if not. """ success = self.connection.modify_lb_attribute(self.name, "crossZoneLoadBalancing", False) if success and self._attributes: self._attributes.cross_zone_load_balancing.enabled = False return success def register_instances(self, instances): """ Adds instances to this load balancer. All instances must be in the same region as the load balancer. Adding endpoints that are already registered with the load balancer has no effect. :param list instances: List of instance IDs (strings) that you'd like to add to this load balancer. """ if isinstance(instances, basestring): instances = [instances] new_instances = self.connection.register_instances(self.name, instances) self.instances = new_instances def deregister_instances(self, instances): """ Remove instances from this load balancer. Removing instances that are not registered with the load balancer has no effect. :param list instances: List of instance IDs (strings) that you'd like to remove from this load balancer. """ if isinstance(instances, basestring): instances = [instances] new_instances = self.connection.deregister_instances(self.name, instances) self.instances = new_instances def delete(self): """ Delete this load balancer. """ return self.connection.delete_load_balancer(self.name) def configure_health_check(self, health_check): """ Configures the health check behavior for the instances behind this load balancer. See :ref:`elb-configuring-a-health-check` for a walkthrough. :param boto.ec2.elb.healthcheck.HealthCheck health_check: A HealthCheck instance that tells the load balancer how to check its instances for health. """ return self.connection.configure_health_check(self.name, health_check) def get_instance_health(self, instances=None): """ Returns a list of :py:class:`boto.ec2.elb.instancestate.InstanceState` objects, which show the health of the instances attached to this load balancer. :rtype: list :returns: A list of :py:class:`InstanceState <boto.ec2.elb.instancestate.InstanceState>` instances, representing the instances attached to this load balancer. """ return self.connection.describe_instance_health(self.name, instances) def create_listeners(self, listeners): return self.connection.create_load_balancer_listeners(self.name, listeners) def create_listener(self, inPort, outPort=None, proto="tcp"): if outPort == None: outPort = inPort return self.create_listeners([(inPort, outPort, proto)]) def delete_listeners(self, listeners): return self.connection.delete_load_balancer_listeners(self.name, listeners) def delete_listener(self, inPort): return self.delete_listeners([inPort]) def delete_policy(self, policy_name): """ Deletes a policy from the LoadBalancer. The specified policy must not be enabled for any listeners. """ return self.connection.delete_lb_policy(self.name, policy_name) def set_policies_of_listener(self, lb_port, policies): return self.connection.set_lb_policies_of_listener(self.name, lb_port, policies) def set_policies_of_backend_server(self, instance_port, policies): return self.connection.set_lb_policies_of_backend_server(self.name, instance_port, policies) def create_cookie_stickiness_policy(self, cookie_expiration_period, policy_name): return self.connection.create_lb_cookie_stickiness_policy(cookie_expiration_period, self.name, policy_name) def create_app_cookie_stickiness_policy(self, name, policy_name): return self.connection.create_app_cookie_stickiness_policy(name, self.name, policy_name) def set_listener_SSL_certificate(self, lb_port, ssl_certificate_id): return self.connection.set_lb_listener_SSL_certificate(self.name, lb_port, ssl_certificate_id) def create_lb_policy(self, policy_name, policy_type, policy_attribute): return self.connection.create_lb_policy(self.name, policy_name, policy_type, policy_attribute) def attach_subnets(self, subnets): """ Attaches load balancer to one or more subnets. Attaching subnets that are already registered with the Load Balancer has no effect. :type subnets: string or List of strings :param subnets: The name of the subnet(s) to add. """ if isinstance(subnets, basestring): subnets = [subnets] new_subnets = self.connection.attach_lb_to_subnets(self.name, subnets) self.subnets = new_subnets def detach_subnets(self, subnets): """ Detaches load balancer from one or more subnets. :type subnets: string or List of strings :param subnets: The name of the subnet(s) to detach. """ if isinstance(subnets, basestring): subnets = [subnets] new_subnets = self.connection.detach_lb_from_subnets(self.name, subnets) self.subnets = new_subnets def apply_security_groups(self, security_groups): """ Applies security groups to the load balancer. Applying security groups that are already registered with the Load Balancer has no effect. :type security_groups: string or List of strings :param security_groups: The name of the security group(s) to add. """ if isinstance(security_groups, basestring): security_groups = [security_groups] new_sgs = self.connection.apply_security_groups_to_lb(self.name, security_groups) self.security_groups = new_sgs
class LoadBalancer(object): """ Represents an EC2 Load Balancer """ def __init__(self, connection=None, name=None, endpoints=None): self.connection = connection self.name = name self.listeners = None self.health_check = None self.policies = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() self.canonical_hosted_zone_name = None self.canonical_hosted_zone_name_id = None self.source_security_group = None def __repr__(self): return 'LoadBalancer:%s' % self.name def startElement(self, name, attrs, connection): if name == 'HealthCheck': self.health_check = HealthCheck(self) return self.health_check elif name == 'ListenerDescriptions': self.listeners = ResultSet([('member', Listener)]) return self.listeners elif name == 'AvailabilityZones': return self.availability_zones elif name == 'Instances': self.instances = ResultSet([('member', InstanceInfo)]) return self.instances elif name == 'Policies': self.policies = Policies(self) return self.policies elif name == 'SourceSecurityGroup': self.source_security_group = SecurityGroup() return self.source_security_group else: return None def endElement(self, name, value, connection): if name == 'LoadBalancerName': self.name = value elif name == 'DNSName': self.dns_name = value elif name == 'CreatedTime': self.created_time = value elif name == 'InstanceId': self.instances.append(value) elif name == 'CanonicalHostedZoneName': self.canonical_hosted_zone_name = value elif name == 'CanonicalHostedZoneNameID': self.canonical_hosted_zone_name_id = value else: setattr(self, name, value) def enable_zones(self, zones): """ Enable availability zones to this Access Point. All zones must be in the same region as the Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.enable_availability_zones(self.name, zones) self.availability_zones = new_zones def disable_zones(self, zones): """ Disable availability zones from this Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.disable_availability_zones(self.name, zones) self.availability_zones = new_zones def register_instances(self, instances): """ Add instances to this Load Balancer All instances must be in the same region as the Load Balancer. Adding endpoints that are already registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.register_instances(self.name, instances) self.instances = new_instances def deregister_instances(self, instances): """ Remove instances from this Load Balancer. Removing instances that are not registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.deregister_instances(self.name, instances) self.instances = new_instances def delete(self): """ Delete this load balancer """ return self.connection.delete_load_balancer(self.name) def configure_health_check(self, health_check): return self.connection.configure_health_check(self.name, health_check) def get_instance_health(self, instances=None): return self.connection.describe_instance_health(self.name, instances) def create_listeners(self, listeners): return self.connection.create_load_balancer_listeners(self.name, listeners) def create_listener(self, inPort, outPort=None, proto="tcp"): if outPort == None: outPort = inPort return self.create_listeners([(inPort, outPort, proto)]) def delete_listeners(self, listeners): return self.connection.delete_load_balancer_listeners(self.name, listeners) def delete_listener(self, inPort, outPort=None, proto="tcp"): if outPort == None: outPort = inPort return self.delete_listeners([(inPort, outPort, proto)]) def delete_policy(self, policy_name): """ Deletes a policy from the LoadBalancer. The specified policy must not be enabled for any listeners. """ return self.connection.delete_lb_policy(self.name, policy_name) def set_policies_of_listener(self, lb_port, policies): return self.connection.set_lb_policies_of_listener(self.name, lb_port, policies) def create_cookie_stickiness_policy(self, cookie_expiration_period, policy_name): return self.connection.create_lb_cookie_stickiness_policy(cookie_expiration_period, self.name, policy_name) def create_app_cookie_stickiness_policy(self, name, policy_name): return self.connection.create_app_cookie_stickiness_policy(name, self.name, policy_name) def set_listener_SSL_certificate(self, lb_port, ssl_certificate_id): return self.connection.set_lb_listener_SSL_certificate(self.name, lb_port, ssl_certificate_id)
class LoadBalancer(object): """ Represents an EC2 Load Balancer """ def __init__(self, connection=None, name=None, endpoints=None): self.connection = connection self.name = name self.listeners = None self.health_check = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() def __repr__(self): return 'LoadBalancer:%s' % self.name def startElement(self, name, attrs, connection): if name == 'HealthCheck': self.health_check = HealthCheck(self) return self.health_check elif name == 'Listeners': self.listeners = ResultSet([('member', Listener)]) return self.listeners elif name == 'AvailabilityZones': return self.availability_zones elif name == 'Instances': self.instances = ResultSet([('member', InstanceInfo)]) return self.instances else: return None def endElement(self, name, value, connection): if name == 'LoadBalancerName': self.name = value elif name == 'DNSName': self.dns_name = value elif name == 'CreatedTime': self.created_time = value elif name == 'InstanceId': self.instances.append(value) else: setattr(self, name, value) def enable_zones(self, zones): """ Enable availability zones to this Access Point. All zones must be in the same region as the Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.enable_availability_zones(self.name, zones) self.availability_zones = new_zones def disable_zones(self, zones): """ Disable availability zones from this Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.disable_availability_zones( self.name, zones) self.availability_zones = new_zones def register_instances(self, instances): """ Add instances to this Load Balancer All instances must be in the same region as the Load Balancer. Adding endpoints that are already registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.register_instances( self.name, instances) self.instances = new_instances def deregister_instances(self, instances): """ Remove instances from this Load Balancer. Removing instances that are not registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.deregister_instances( self.name, instances) self.instances = new_instances def delete(self): """ Delete this load balancer """ return self.connection.delete_load_balancer(self.name) def configure_health_check(self, health_check): self.connection.configure_health_check(self.name, health_check) def get_instance_health(self, instances=None): self.connection.describe_instance_health(self.name, instances)
def test_update_limits_from_api(self): mock_conn = Mock(spec_set=EC2Connection) rs = ResultSet() a1 = AccountAttribute(connection=mock_conn) a1.attribute_name = 'supported-platforms' a1.attribute_values = ['EC2', 'VPC'] rs.append(a1) a2 = AccountAttribute(connection=mock_conn) a2.attribute_name = 'vpc-max-security-groups-per-interface' a2.attribute_values = ['5'] rs.append(a2) a3 = AccountAttribute(connection=mock_conn) a3.attribute_name = 'max-elastic-ips' a3.attribute_values = ['40'] rs.append(a3) a4 = AccountAttribute(connection=mock_conn) a4.attribute_name = 'max-instances' a4.attribute_values = ['400'] rs.append(a4) a5 = AccountAttribute(connection=mock_conn) a5.attribute_name = 'vpc-max-elastic-ips' a5.attribute_values = ['200'] rs.append(a5) a6 = AccountAttribute(connection=mock_conn) a6.attribute_name = 'default-vpc' a6.attribute_values = ['none'] rs.append(a6) cls = _Ec2Service(21, 43) cls.conn = mock_conn with patch('awslimitchecker.services.ec2.logger') as mock_logger: with patch('%s.boto_query_wrapper' % self.pbm) as mock_wrapper: mock_wrapper.return_value = rs cls._update_limits_from_api() assert mock_wrapper.mock_calls == [ call(mock_conn.describe_account_attributes) ] assert mock_conn.mock_calls == [] assert mock_logger.mock_calls == [ call.info("Querying EC2 DescribeAccountAttributes for limits"), call.debug('Done setting limits from API') ] assert cls.limits['Elastic IP addresses (EIPs)'].api_limit == 40 assert cls.limits['Running On-Demand EC2 instances'].api_limit == 400 assert cls.limits['VPC Elastic IP addresses (EIPs)'].api_limit == 200 assert cls.limits['VPC security groups per elastic ' 'network interface'].api_limit == 5
class LoadBalancer(object): """ Represents an EC2 Load Balancer """ def __init__(self, connection=None, name=None, endpoints=None): self.connection = connection self.name = name self.listeners = None self.health_check = None self.policies = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() self.canonical_hosted_zone_name = None self.canonical_hosted_zone_name_id = None self.source_security_group = None self.subnets = ListElement() self.security_groups = ListElement() self.vpc_id = None def __repr__(self): return 'LoadBalancer:%s' % self.name def startElement(self, name, attrs, connection): if name == 'HealthCheck': self.health_check = HealthCheck(self) return self.health_check elif name == 'ListenerDescriptions': self.listeners = ResultSet([('member', Listener)]) return self.listeners elif name == 'AvailabilityZones': return self.availability_zones elif name == 'Instances': self.instances = ResultSet([('member', InstanceInfo)]) return self.instances elif name == 'Policies': self.policies = Policies(self) return self.policies elif name == 'SourceSecurityGroup': self.source_security_group = SecurityGroup() return self.source_security_group elif name == 'Subnets': return self.subnets elif name == 'SecurityGroups': return self.security_groups elif name == 'VPCId': pass else: return None def endElement(self, name, value, connection): if name == 'LoadBalancerName': self.name = value elif name == 'DNSName': self.dns_name = value elif name == 'CreatedTime': self.created_time = value elif name == 'InstanceId': self.instances.append(value) elif name == 'CanonicalHostedZoneName': self.canonical_hosted_zone_name = value elif name == 'CanonicalHostedZoneNameID': self.canonical_hosted_zone_name_id = value elif name == 'VPCId': self.vpc_id = value else: setattr(self, name, value) def enable_zones(self, zones): """ Enable availability zones to this Access Point. All zones must be in the same region as the Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.enable_availability_zones(self.name, zones) self.availability_zones = new_zones def disable_zones(self, zones): """ Disable availability zones from this Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.disable_availability_zones(self.name, zones) self.availability_zones = new_zones def register_instances(self, instances): """ Add instances to this Load Balancer All instances must be in the same region as the Load Balancer. Adding endpoints that are already registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.register_instances(self.name, instances) self.instances = new_instances def deregister_instances(self, instances): """ Remove instances from this Load Balancer. Removing instances that are not registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.deregister_instances(self.name, instances) self.instances = new_instances def delete(self): """ Delete this load balancer """ return self.connection.delete_load_balancer(self.name) def configure_health_check(self, health_check): return self.connection.configure_health_check(self.name, health_check) def get_instance_health(self, instances=None): return self.connection.describe_instance_health(self.name, instances) def create_listeners(self, listeners): return self.connection.create_load_balancer_listeners(self.name, listeners) def create_listener(self, inPort, outPort=None, proto="tcp"): if outPort == None: outPort = inPort return self.create_listeners([(inPort, outPort, proto)]) def delete_listeners(self, listeners): return self.connection.delete_load_balancer_listeners(self.name, listeners) def delete_listener(self, inPort): return self.delete_listeners([inPort]) def delete_policy(self, policy_name): """ Deletes a policy from the LoadBalancer. The specified policy must not be enabled for any listeners. """ return self.connection.delete_lb_policy(self.name, policy_name) def set_policies_of_listener(self, lb_port, policies): return self.connection.set_lb_policies_of_listener(self.name, lb_port, policies) def create_cookie_stickiness_policy(self, cookie_expiration_period, policy_name): return self.connection.create_lb_cookie_stickiness_policy(cookie_expiration_period, self.name, policy_name) def create_app_cookie_stickiness_policy(self, name, policy_name): return self.connection.create_app_cookie_stickiness_policy(name, self.name, policy_name) def set_listener_SSL_certificate(self, lb_port, ssl_certificate_id): return self.connection.set_lb_listener_SSL_certificate(self.name, lb_port, ssl_certificate_id) def attach_subnets(self, subnets): """ Attaches load balancer to one or more subnets. Attaching subnets that are already registered with the Load Balancer has no effect. :type subnets: string or List of strings :param subnets: The name of the subnet(s) to add. """ if isinstance(subnets, str) or isinstance(subnets, unicode): subnets = [subnets] new_subnets = self.connection.attach_lb_to_subnets(self.name, subnets) self.subnets = new_subnets def detach_subnets(self, subnets): """ Detaches load balancer from one or more subnets. :type subnets: string or List of strings :param subnets: The name of the subnet(s) to detach. """ if isinstance(subnets, str) or isinstance(subnets, unicode): subnets = [subnets] new_subnets = self.connection.detach_lb_to_subnets(self.name, subnets) self.subnets = new_subnets def apply_security_groups(self, security_groups): """ Applies security groups to the load balancer. Applying security groups that are already registered with the Load Balancer has no effect. :type security_groups: string or List of strings :param security_groups: The name of the security group(s) to add. """ if isinstance(security_groups, str) or \ isinstance(security_groups, unicode): security_groups = [security_groups] new_sgs = self.connection.apply_security_groups_to_lb( self.name, security_groups) self.security_groups = new_sgs
class LoadBalancer(object): """ Represents an EC2 Load Balancer, extended to support Euca-specific Loadbalancer definition """ def __init__(self, connection=None, name=None, endpoints=None): """ :ivar boto.ec2.elb.ELBConnection connection: The connection this load balancer was instance was instantiated from. :ivar list listeners: A list of tuples in the form of ``(<Inbound port>, <Outbound port>, <Protocol>)`` :ivar boto.ec2.elb.healthcheck.HealthCheck health_check: The health check policy for this load balancer. :ivar list servo.ws.policies.PolicyDescription: A list of load balancer policies :ivar str name: The name of the Load Balancer. :ivar str dns_name: The external DNS name for the balancer. :ivar str created_time: A date+time string showing when the load balancer was created. :ivar list instances: A list of :py:class:`servo.ws.backend_instance.BackendInstance` instances, representing the EC2 instances this load balancer is distributing requests to. :ivar list availability_zones: The availability zones this balancer covers. :ivar str canonical_hosted_zone_name: Current CNAME for the balancer. :ivar str canonical_hosted_zone_name_id: The Route 53 hosted zone ID of this balancer. Needed when creating an Alias record in a Route 53 hosted zone. :ivar boto.ec2.elb.securitygroup.SecurityGroup source_security_group: The security group that you can use as part of your inbound rules for your load balancer back-end instances to disallow traffic from sources other than your load balancer. :ivar list subnets: A list of subnets this balancer is on. :ivar list security_groups: A list of additional security groups that have been applied. :ivar str vpc_id: The ID of the VPC that this ELB resides within. :ivar list backends: A list of :py:class:`boto.ec2.elb.loadbalancer.Backend back-end server descriptions. """ self.connection = connection self.name = name self.listeners = None self.health_check = None self.policy_descriptions = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() self.canonical_hosted_zone_name = None self.canonical_hosted_zone_name_id = None self.source_security_group = None self.subnets = ListElement() self.security_groups = ListElement() self.vpc_id = None self.scheme = None self.backends = None self.attributes = None def __repr__(self): return 'LoadBalancer:%s' % self.name def startElement(self, name, attrs, connection): if name == 'HealthCheck': self.health_check = HealthCheck(self) return self.health_check elif name == 'ListenerDescriptions': self.listeners = ResultSet([('member', Listener)]) return self.listeners elif name == 'AvailabilityZones': return self.availability_zones elif name == 'BackendInstances': self.instances = ResultSet([('member', BackendInstance)]) return self.instances elif name == 'PolicyDescriptions': self.policy_descriptions = ResultSet([('member', PolicyDescription)]) return self.policy_descriptions elif name == 'SourceSecurityGroup': self.source_security_group = SecurityGroup() return self.source_security_group elif name == 'Subnets': return self.subnets elif name == 'SecurityGroups': return self.security_groups elif name == 'VPCId': pass elif name == "BackendServerDescriptions": self.backends = ResultSet([('member', Backend)]) return self.backends elif name == "LoadBalancerAttributes": self.attributes = LbAttributes(self) return self.attributes else: return None def endElement(self, name, value, connection): if name == 'LoadBalancerName': self.name = value elif name == 'DNSName': self.dns_name = value elif name == 'CreatedTime': self.created_time = value elif name == 'InstanceId': self.instances.append(value) elif name == 'CanonicalHostedZoneName': self.canonical_hosted_zone_name = value elif name == 'CanonicalHostedZoneNameID': self.canonical_hosted_zone_name_id = value elif name == 'VPCId': self.vpc_id = value elif name == 'Scheme': self.scheme = value else: setattr(self, name, value)
def register_hit_type(self, title='', description='', reward=Price(0.01), duration=3600, keywords='', approval_delay=3600 * 24, qual_req=None): hit_type_id = self.num_hit_types self.num_hit_types += 1 if not os.path.exists(self.output_dir): os.makedirs(self.output_dir) with open( os.path.join(self.output_dir, "hit_type" + str(hit_type_id) + ".json"), 'w') as f: json.dump( { 'hits': [], 'finished': False, 'duration': duration, 'approval_delay': approval_delay, 'keywords': keywords, 'reward': reward.amount, 'title': title, 'description': description, 'workers': {} }, f) os.chmod( os.path.join(self.output_dir, "hit_type" + str(hit_type_id) + ".json"), 0666) rs = ResultSet() rs.append(HitTypeResponse(HITTypeId=hit_type_id)) with open( os.path.join(self.output_dir, 'cgi-bin', 'new_assignment.py'), 'w') as f: f.write('''#!/usr/bin/env python import os import datetime import json import time import numpy as np def hit_add_assignment(h, hit_type, hit_type_id, curr, output_dir, worker_id, submit_url): found = False for w in h[3]: if w == worker_id: found = True if not found: h[2] += 1 h[3].append(worker_id) with open(os.path.join(output_dir, "hit_type"+str(hit_type_id)+".json"), 'w') as f: json.dump(hit_type, f) with open(os.path.join(output_dir, "hit"+str(h[0])+".json"), 'r') as f: hit = json.load(f) params = "?hitTypeId="+str(hit_type_id)+"&hitId="+str(hit['mturk_id'])+"&workerId="+str(worker_id) + "&turkSubmitTo=" + submit_url a = {'accept_time':curr, 'worker':worker_id, 'status':'In Progress', 'hit_id':hit['mturk_id'], 'external_url':hit['external_url']+params} hit['assignments'].append(a) with open(os.path.join(output_dir, "hit"+str(h[0])+".json"), 'w') as f: json.dump(hit, f) return a def new_assignment(output_dir, worker_id, hit_type_id): submit_url = "''' + self.url + '''/cgi-bin/submit"+str(hit_type_id)+".py" curr = time.time() with open(os.path.join(output_dir, "hit_type"+str(hit_type_id)+".json"), 'r') as f: hit_type = json.load(f) if hit_type['finished']: return None # Choose a random, unfinished hit that this worker hasn't done already for hi in np.random.permutation(len(hit_type['hits'])): h = hit_type['hits'][hi] if h[2] < h[1]: hit_add_assignment(h, hit_type, hit_type_id, curr, output_dir, worker_id, submit_url) # Handle expired hits and assignments new_hit_available = None any_changed = False in_progress_assignment, in_progress_hit, almost_finished_hit = None, None, None for h in hit_type['hits']: changed = False if h[2] >= h[1] and not h[4]: with open(os.path.join(output_dir, "hit"+str(h[0])+".json"), 'r') as f: hit = json.load(f) if curr >= hit['create_time'] + hit['lifetime']: hit['status'] = 'Expired' h[4] = 1 changed = True num_finished = 0 num_in_progress = 0 for a in hit['assignments']: if curr > a['accept_time']+hit_type['duration'] and a['status']=='In Progress': # Expired assignment a['status'] = 'Expired' h[2] -= 1 changed = True elif a['worker'] == worker_id and a['status']=='In Progress': in_progress_assignment = a in_progress_hit = hit elif a['status']=='In Progress': num_in_progress += 1 if a['status']=='Submitted': num_finished += 1 if num_finished >= h[1]: h[4] = 1 changed = True elif num_in_progress: almost_finished_hit = h if changed: any_changed = True with open(os.path.join(output_dir, "hit"+str(h[0])+".json"), 'w') as f: json.dump(hit, f) if h[2] < h[1]: new_hit_available = h if any_changed: with open(os.path.join(output_dir, "hit_type"+str(hit_type_id)+".json"), 'w') as f: json.dump(hit_type, f) if new_hit_available: return hit_add_assignment(new_hit_available, hit_type, hit_type_id, curr, output_dir, worker_id, submit_url) elif not in_progress_assignment is None: in_progress_assignment['accept_time'] = curr with open(os.path.join(output_dir, "hit"+str(in_progress_hit['mturk_id'])+".json"), 'w') as f: json.dump(in_progress_hit, f) return in_progress_assignment elif not almost_finished_hit is None: return hit_add_assignment(almost_finished_hit, hit_type, hit_type_id, curr, output_dir, worker_id, submit_url) else: return None ''') with open( os.path.join(self.output_dir, 'cgi-bin', 'annotate' + str(hit_type_id) + '.py'), 'w') as f: f.write('''#!/usr/bin/env python from new_assignment import * import os import Cookie import datetime import json import time import numpy as np import filelock hit_type_id = "''' + str(hit_type_id) + '''" output_dir = "''' + self.output_dir + '''" url = "''' + urlparse.urlsplit(self.url).netloc + '''" submit_url = "''' + self.url + '/cgi-bin/submit' + str(hit_type_id) + '.py' + '''" lock = filelock.FileLock(os.path.join(output_dir, "lock"+str(hit_type_id)+".json")) with lock.acquire(): try: cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"]) worker_id = cookie["worker_id"].value except (Cookie.CookieError, KeyError): # Add new worker with open(os.path.join(output_dir, "hit_type"+hit_type_id+".json"), 'r') as f: hit_type = json.load(f) worker_id = len(hit_type['workers']) hit_type['workers'][worker_id] = os.environ["REMOTE_ADDR"] with open(os.path.join(output_dir, "hit_type"+hit_type_id+".json"), 'w') as f: json.dump(hit_type, f) expiration = datetime.datetime.now() + datetime.timedelta(days=30) cookie = Cookie.SimpleCookie() cookie["worker_id"] = worker_id cookie["worker_id"]["domain"] = url cookie["worker_id"]["path"] = "/" cookie["worker_id"]["expires"] = expiration.strftime("%a, %d-%b-%Y %H:%M:%S PST") a = new_assignment(output_dir, worker_id, hit_type_id) print "Content-type: text/html" print cookie.output() print if a: args = "" if not '?' in a['external_url']: args = '?hitTypeId='+str(hit_type_id)+'&hitId='+str(a['hit_id'])+'&workerId='+str(worker_id)+'&turkSubmitTo='+submit_url print '<html><frameset cols="100%"><frame src="'+a['external_url']+args+'"></frame></frameset></html>' else: print '<html>No more HITs left</html>' ''') os.chmod( os.path.join(self.output_dir, 'cgi-bin', 'annotate' + str(hit_type_id) + '.py'), 0775) with open( os.path.join(self.output_dir, 'cgi-bin', 'submit' + str(hit_type_id) + '.py'), 'w') as f: f.write('''#!/usr/bin/env python from new_assignment import * import os import Cookie import datetime import json import cgi import time import filelock output_dir = "''' + self.output_dir + '''" params = cgi.FieldStorage() annotate_url = "''' + self.url + '/cgi-bin/annotate' + str(hit_type_id) + '.py' + '''" submit_url = "''' + self.url + '/cgi-bin/submit' + str(hit_type_id) + '.py' + '''" hit_type_id = "''' + str(hit_type_id) + '''" success = False lock = filelock.FileLock(os.path.join(output_dir, "lock"+str(hit_type_id)+".json")) with lock.acquire(): if 'hitId' in params: with open(os.path.join(output_dir, "hit"+str(params['hitId'].value)+".json"), 'r') as f: hit = json.load(f) for a in hit['assignments']: if a['status'] == 'In Progress' or a['status'] == 'Expired' and a['worker_id']==params['workerId'].value: answers = {} for k in params.keys(): answers[k] = params.getvalue(k) a['answers'] = answers a['status'] = 'Submitted' a['submit_time'] = time.time() with open(os.path.join(output_dir, "hit"+str(params['hitId'].value)+".json"), 'w') as f: json.dump(hit, f) success = True break if success: a = new_assignment(output_dir, params['workerId'].value, hit_type_id) print "Content-type: text/html\" print if a: args = "" if not '?' in a['external_url']: args = '?hitTypeId='+str(hit_type_id)+'&hitId='+str(a['hit_id'])+'&workerId='+str(worker_id)+'&turkSubmitTo='+submit_url print '<html>Loading next HIT...<script language="javascript">window.location.href = "'+a['external_url']+args+'"</script></html>' else: print '<html>No more HITs left</html>' print #print "HTTP/1.1 302 Found" #print "Location: "+annotate_url+"\\r\\n" #print "Connection: close\\r\\n\\n" else: print "Status: 400\\n\\n" ''') os.chmod( os.path.join(self.output_dir, 'cgi-bin', 'submit' + str(hit_type_id) + '.py'), 0775) return rs
def get_balance(self): rs = ResultSet() rs.append(0) return rs
class LoadBalancer(object): """ Represents an EC2 Load Balancer. """ def __init__(self, connection=None, name=None, endpoints=None): """ :ivar boto.ec2.elb.ELBConnection connection: The connection this load balancer was instance was instantiated from. :ivar list listeners: A list of tuples in the form of ``(<Inbound port>, <Outbound port>, <Protocol>)`` :ivar boto.ec2.elb.healthcheck.HealthCheck health_check: The health check policy for this load balancer. :ivar boto.ec2.elb.policies.Policies policies: Cookie stickiness and other policies. :ivar str dns_name: The external DNS name for the balancer. :ivar str created_time: A date+time string showing when the load balancer was created. :ivar list instances: A list of :py:class:`boto.ec2.instanceinfo.InstanceInfo` instances, representing the EC2 instances this load balancer is distributing requests to. :ivar list availability_zones: The availability zones this balancer covers. :ivar str canonical_hosted_zone_name: Current CNAME for the balancer. :ivar str canonical_hosted_zone_name_id: The Route 53 hosted zone ID of this balancer. Needed when creating an Alias record in a Route 53 hosted zone. :ivar boto.ec2.elb.securitygroup.SecurityGroup source_security_group: The security group that you can use as part of your inbound rules for your load balancer back-end instances to disallow traffic from sources other than your load balancer. :ivar list subnets: A list of subnets this balancer is on. :ivar list security_groups: A list of additional security groups that have been applied. :ivar str vpc_id: The ID of the VPC that this ELB resides within. """ self.connection = connection self.name = name self.listeners = None self.health_check = None self.policies = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() self.canonical_hosted_zone_name = None self.canonical_hosted_zone_name_id = None self.source_security_group = None self.subnets = ListElement() self.security_groups = ListElement() self.vpc_id = None self.scheme = None def __repr__(self): return 'LoadBalancer:%s' % self.name def startElement(self, name, attrs, connection): if name == 'HealthCheck': self.health_check = HealthCheck(self) return self.health_check elif name == 'ListenerDescriptions': self.listeners = ResultSet([('member', Listener)]) return self.listeners elif name == 'AvailabilityZones': return self.availability_zones elif name == 'Instances': self.instances = ResultSet([('member', InstanceInfo)]) return self.instances elif name == 'Policies': self.policies = Policies(self) return self.policies elif name == 'SourceSecurityGroup': self.source_security_group = SecurityGroup() return self.source_security_group elif name == 'Subnets': return self.subnets elif name == 'SecurityGroups': return self.security_groups elif name == 'VPCId': pass else: return None def endElement(self, name, value, connection): if name == 'LoadBalancerName': self.name = value elif name == 'DNSName': self.dns_name = value elif name == 'CreatedTime': self.created_time = value elif name == 'InstanceId': self.instances.append(value) elif name == 'CanonicalHostedZoneName': self.canonical_hosted_zone_name = value elif name == 'CanonicalHostedZoneNameID': self.canonical_hosted_zone_name_id = value elif name == 'VPCId': self.vpc_id = value elif name == 'Scheme': self.scheme = value else: setattr(self, name, value) def enable_zones(self, zones): """ Enable availability zones to this Access Point. All zones must be in the same region as the Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.enable_availability_zones(self.name, zones) self.availability_zones = new_zones def disable_zones(self, zones): """ Disable availability zones from this Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.disable_availability_zones( self.name, zones) self.availability_zones = new_zones def register_instances(self, instances): """ Adds instances to this load balancer. All instances must be in the same region as the load balancer. Adding endpoints that are already registered with the load balancer has no effect. :param list instances: List of instance IDs (strings) that you'd like to add to this load balancer. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.register_instances( self.name, instances) self.instances = new_instances def deregister_instances(self, instances): """ Remove instances from this load balancer. Removing instances that are not registered with the load balancer has no effect. :param list instances: List of instance IDs (strings) that you'd like to remove from this load balancer. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.deregister_instances( self.name, instances) self.instances = new_instances def delete(self): """ Delete this load balancer. """ return self.connection.delete_load_balancer(self.name) def configure_health_check(self, health_check): """ Configures the health check behavior for the instances behind this load balancer. See :ref:`elb-configuring-a-health-check` for a walkthrough. :param boto.ec2.elb.healthcheck.HealthCheck health_check: A HealthCheck instance that tells the load balancer how to check its instances for health. """ return self.connection.configure_health_check(self.name, health_check) def get_instance_health(self, instances=None): """ Returns a list of :py:class:`boto.ec2.elb.instancestate.InstanceState` objects, which show the health of the instances attached to this load balancer. :rtype: list :returns: A list of :py:class:`InstanceState <boto.ec2.elb.instancestate.InstanceState>` instances, representing the instances attached to this load balancer. """ return self.connection.describe_instance_health(self.name, instances) def create_listeners(self, listeners): return self.connection.create_load_balancer_listeners( self.name, listeners) def create_listener(self, inPort, outPort=None, proto="tcp"): if outPort == None: outPort = inPort return self.create_listeners([(inPort, outPort, proto)]) def delete_listeners(self, listeners): return self.connection.delete_load_balancer_listeners( self.name, listeners) def delete_listener(self, inPort): return self.delete_listeners([inPort]) def delete_policy(self, policy_name): """ Deletes a policy from the LoadBalancer. The specified policy must not be enabled for any listeners. """ return self.connection.delete_lb_policy(self.name, policy_name) def set_policies_of_listener(self, lb_port, policies): return self.connection.set_lb_policies_of_listener( self.name, lb_port, policies) def create_cookie_stickiness_policy(self, cookie_expiration_period, policy_name): return self.connection.create_lb_cookie_stickiness_policy( cookie_expiration_period, self.name, policy_name) def create_app_cookie_stickiness_policy(self, name, policy_name): return self.connection.create_app_cookie_stickiness_policy( name, self.name, policy_name) def set_listener_SSL_certificate(self, lb_port, ssl_certificate_id): return self.connection.set_lb_listener_SSL_certificate( self.name, lb_port, ssl_certificate_id) def attach_subnets(self, subnets): """ Attaches load balancer to one or more subnets. Attaching subnets that are already registered with the Load Balancer has no effect. :type subnets: string or List of strings :param subnets: The name of the subnet(s) to add. """ if isinstance(subnets, str) or isinstance(subnets, unicode): subnets = [subnets] new_subnets = self.connection.attach_lb_to_subnets(self.name, subnets) self.subnets = new_subnets def detach_subnets(self, subnets): """ Detaches load balancer from one or more subnets. :type subnets: string or List of strings :param subnets: The name of the subnet(s) to detach. """ if isinstance(subnets, str) or isinstance(subnets, unicode): subnets = [subnets] new_subnets = self.connection.detach_lb_to_subnets(self.name, subnets) self.subnets = new_subnets def apply_security_groups(self, security_groups): """ Applies security groups to the load balancer. Applying security groups that are already registered with the Load Balancer has no effect. :type security_groups: string or List of strings :param security_groups: The name of the security group(s) to add. """ if isinstance(security_groups, str) or \ isinstance(security_groups, unicode): security_groups = [security_groups] new_sgs = self.connection.apply_security_groups_to_lb( self.name, security_groups) self.security_groups = new_sgs
class LoadBalancer(object): """ Represents an EC2 Load Balancer """ def __init__(self, connection=None, name=None, endpoints=None): self.connection = connection self.name = name self.listeners = None self.health_check = None self.dns_name = None self.created_time = None self.instances = None self.availability_zones = ListElement() def __repr__(self): return 'LoadBalancer:%s' % self.name def startElement(self, name, attrs, connection): if name == 'HealthCheck': self.health_check = HealthCheck(self) return self.health_check elif name == 'Listeners': self.listeners = ResultSet([('member', Listener)]) return self.listeners elif name == 'AvailabilityZones': return self.availability_zones elif name == 'Instances': self.instances = ResultSet([('member', InstanceInfo)]) return self.instances else: return None def endElement(self, name, value, connection): if name == 'LoadBalancerName': self.name = value elif name == 'DNSName': self.dns_name = value elif name == 'CreatedTime': self.created_time = value elif name == 'InstanceId': self.instances.append(value) else: setattr(self, name, value) def enable_zones(self, zones): """ Enable availability zones to this Access Point. All zones must be in the same region as the Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.enable_availability_zones(self.name, zones) self.availability_zones = new_zones def disable_zones(self, zones): """ Disable availability zones from this Access Point. :type zones: string or List of strings :param zones: The name of the zone(s) to add. """ if isinstance(zones, str) or isinstance(zones, unicode): zones = [zones] new_zones = self.connection.disable_availability_zones(self.name, zones) self.availability_zones = new_zones def register_instances(self, instances): """ Add instances to this Load Balancer All instances must be in the same region as the Load Balancer. Adding endpoints that are already registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.register_instances(self.name, instances) self.instances = new_instances def deregister_instances(self, instances): """ Remove instances from this Load Balancer. Removing instances that are not registered with the Load Balancer has no effect. :type zones: string or List of instance id's :param zones: The name of the endpoint(s) to add. """ if isinstance(instances, str) or isinstance(instances, unicode): instances = [instances] new_instances = self.connection.deregister_instances(self.name, instances) self.instances = new_instances def delete(self): """ Delete this load balancer """ return self.connection.delete_load_balancer(self.name) def configure_health_check(self, health_check): return self.connection.configure_health_check(self.name, health_check) def get_instance_health(self, instances=None): return self.connection.describe_instance_health(self.name, instances)