def _paginate_resultset(result, function_ref, *argv, **kwargs): """ Paginate through a query that returns a :py:class:`boto.resultset.ResultSet` object, and return the combined result. All function calls are passed through :py:func:`~.invoke_with_throttling_retries`. :param result: the first ResultSet from the query :type result: :py:class:`boto.resultset.ResultSet` :param function_ref: the function to call :type function_ref: function :param argv: the parameters to pass to the function :type argv: tuple :param kwargs: keyword arguments to pass to the function (:py:func:`~.invoke_with_throttling_retries`) :type kwargs: dict """ logger.debug("Iterating all ResultSets for query of %s", function_ref) # we don't want any markers in the final result final_result = ResultSet() final_result.extend(result) while hasattr(result, 'next_token') and result.next_token is not None: logger.debug("Getting next response set; next_token=%s", result.next_token) next_kwargs = deepcopy(kwargs) next_kwargs['next_token'] = result.next_token result = invoke_with_throttling_retries( function_ref, *argv, **next_kwargs) final_result.extend(result) return final_result
def __init__(self, connection=None, hosted_zone_id=None, comment=None): self.connection = connection self.hosted_zone_id = hosted_zone_id self.comment = comment self.changes = [] self.next_record_name = None self.next_record_type = None ResultSet.__init__(self, [('ResourceRecordSet', Record)])
def test_get_stacks_correctly_calls_aws_api(self, cloudformation_mock): stacks = [Mock(spec=Stack), Mock(spec=Stack)] result = ResultSet() result.extend(stacks) result.next_token = None cloudformation_mock.connect_to_region.return_value.describe_stacks.return_value = result cfn = CloudFormation() self.assertListEqual(stacks, cfn.get_stacks())
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 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 restore(self, data): obj = ResultSet.__new__(ResultSet) data.pop(jsonpickle.tags.OBJECT) items = data.pop(jsonpickle.tags.SEQ) items = self._base.restore(items) obj.extend(items) obj.__dict__.update(self._base.restore(data)) return obj
def endElement(self, name, value, connection): """Overwritten to also add the NextRecordName and NextRecordType to the base object""" if name == 'NextRecordName': self.next_record_name = value elif name == 'NextRecordType': self.next_record_type = value else: return ResultSet.endElement(self, name, value, connection)
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 set_reviewing(self, revert=False): with open(os.path.join(self.output_dir, "hit" + str(hit_id) + ".json"), 'r') as f: hit = json.load(f) hit['status'] = 'Reviewable' if revert else 'Reviewing' with open( os.path.join(self.output_dir, "hit" + str(hit['mturk_id']) + ".json"), 'w') as f: json.dump(hit, f) return ResultSet()
def expire_hit(self, hit_id=None): with open(os.path.join(self.output_dir, "hit" + str(hit_id) + ".json"), 'r') as f: hit = json.load(f) hit['lifetime'] = 0 with open( os.path.join(self.output_dir, "hit" + str(hit['mturk_id']) + ".json"), 'w') as f: json.dump(hit, f) return ResultSet()
def dispose_hit(self, hit_id=None): with open(os.path.join(self.output_dir, "hit" + str(hit_id) + ".json"), 'r') as f: hit = json.load(f) hit['status'] = 'Disposed' with open( os.path.join(self.output_dir, "hit" + str(hit['mturk_id']) + ".json"), 'w') as f: json.dump(hit, f) return ResultSet()
def endElement(self, name, value, connection): # the answer consists of embedded XML, so it needs to be parsed independantly if name == 'Answer': answer_rs = ResultSet([('Answer', QuestionFormAnswer),]) h = handler.XmlHandler(answer_rs, connection) # need to convert from unicode to string for sax value = str(value) xml.sax.parseString(value, h) self.answers.append(answer_rs) else: BaseAutoResultElement.endElement(self, name, value, connection)
def startElement(self, name, attrs, connection): if name == 'Status': self.status = ClusterStatus() return self.status elif name == 'EC2InstanceAttributes': self.ec2instanceattributes = Ec2InstanceAttributes() return self.ec2instanceattributes elif name == 'Applications': self.applications = ResultSet([('member', Application)]) else: return None
def endElement(self, name, value, connection): # the answer consists of embedded XML, so it needs to be parsed independantly if name == 'Answer': answer_rs = ResultSet([('Answer', QuestionFormAnswer)]) h = handler.XmlHandler(answer_rs, connection) value = connection.get_utf8_value(value) xml.sax.parseString(value, h) self.answers.append(answer_rs) else: super(QualificationRequest, self).endElement(name, value, connection)
def __init__(self, connection=None): """ Represents a reference quota :ivar reference: Reference for the quota. resource id if its a resource specific quota, otherwise 'global'. :ivar list quotas: list of quotas. """ super(ReferenceQuota, self).__init__(connection) self.reference = None self.quotas = ResultSet([('item', QuotaList)])
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 _get_all_objects(self, resource, tags): if not tags: tags=[('DistributionSummary', DistributionSummary)] response = self.make_request('GET', '/%s/%s' % (self.Version, resource)) body = response.read() if response.status >= 300: raise CloudFrontServerError(response.status, response.reason, body) rs = ResultSet(tags) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
def __iter__(self): """Override the next function to support paging""" results = ResultSet.__iter__(self) while results: for obj in results: yield obj if self.is_truncated: self.is_truncated = False results = self.connection.get_all_rrsets(self.hosted_zone_id, name=self.next_record_name, type=self.next_record_type) else: results = None
def test_set_rest_notification(self, with_mock): url = 'https://url-of-notification-route' hit_type_id = 'fake hittype id' with_mock.mturk.configure_mock( **{ 'set_rest_notification.return_value': ResultSet(), }) with_mock.set_rest_notification(url, hit_type_id) with_mock.mturk.set_rest_notification.assert_called_once()
def startElement(self, name, attrs, connection): if name == 'Instances': self.instances = ResultSet([('member', Instance)]) return self.instances elif name == 'LoadBalancerNames': return self.load_balancers elif name == 'AvailabilityZones': return self.availability_zones elif name == 'EnabledMetrics': self.enabled_metrics = ResultSet([('member', EnabledMetric)]) return self.enabled_metrics elif name == 'SuspendedProcesses': self.suspended_processes = ResultSet([('member', SuspendedProcess) ]) return self.suspended_processes elif name == 'Tags': self.tags = ResultSet([('member', Tag)]) return self.tags else: return
def startElement(self, name, attrs, connection): if name == 'groupSet': self.groups = ResultSet([('item', Group)]) return self.groups elif name == 'monitoring': self._in_monitoring_element = True elif name == 'blockDeviceMapping': self.block_device_mapping = BlockDeviceMapping() return self.block_device_mapping else: return None
def test_create_hit_calls_underlying_mturk_method(self, with_mock): with_mock.mturk.configure_mock( **{ 'register_hit_type.return_value': fake_hit_type_response(), 'set_rest_notification.return_value': ResultSet(), 'create_hit.return_value': fake_hit_response(), }) with_mock.create_hit(**standard_hit_config()) with_mock.mturk.create_hit.assert_called_once()
def set_queue_attribute(self, queue_url, attribute, value): params = {'Attribute': attribute, 'Value': value} response = self.make_request('SetQueueAttributes', params, queue_url) body = response.read() if response.status == 200: rs = ResultSet() h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs.status else: raise SQSError(response.status, response.reason, body)
def make_payment(self, amount, sender_token, charge_fee_to="Recipient", reference=None, senderReference=None, recipientReference=None, senderDescription=None, recipientDescription=None, callerDescription=None, metadata=None, transactionDate=None): """ Make a payment transaction You must specify the amount and the sender token. """ params = {} params['RecipientTokenId'] = boto.config.get("FPS", "recipient_token") params['CallerTokenId'] = boto.config.get("FPS", "caller_token") params['SenderTokenId'] = sender_token params['TransactionAmount.Amount'] = str(amount) params['TransactionAmount.CurrencyCode'] = "USD" params['ChargeFeeTo'] = charge_fee_to if (transactionDate != None): params['TransactionDate'] = transactionDate if (senderReference != None): params['SenderReference'] = senderReference if (recipientReference != None): params['RecipientReference'] = recipientReference if (senderDescription != None): params['SenderDescription'] = senderDescription if (recipientDescription != None): params['RecipientDescription'] = recipientDescription if (callerDescription != None): params['CallerDescription'] = callerDescription if (metadata != None): params['MetaData'] = metadata if (transactionDate != None): params['TransactionDate'] = transactionDate if (reference == None): reference = uuid.uuid4() params['CallerReference'] = reference response = self.make_request("Pay", params) body = response.read() if (response.status == 200): rs = ResultSet() h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs else: raise FPSResponseError(response.status, response.reason, body)
def startElement(self, name, attrs, connection): retval = TaggedEC2Object.startElement(self, name, attrs, connection) if retval is not None: return retval if name == 'attachmentSet': self.attach_data = AttachmentSet() return self.attach_data elif name == 'tagSet': self.tags = ResultSet([('item', Tag)]) return self.tags else: return None
def change_message_visibility(self, queue_url, message_id, vtimeout): params = {'MessageId': message_id, 'VisibilityTimeout': vtimeout} response = self.make_request('ChangeMessageVisibility', params, queue_url) body = response.read() if response.status == 200: rs = ResultSet() h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs.status else: raise SQSError(response.status, response.reason, body)
def startElement(self, name, value, connection): ename = name.replace('euca:', '') elem = super(EucaService, self).startElement(name, value, connection) if elem is not None: return elem if ename == 'uris': self.uris = EucaUris(connection=connection) return self.uris if ename == 'statusDetails': self.statusdetails = ResultSet([('item', EucaServiceStatus), ('euca:item', EucaServiceStatus)]) return self.statusdetails
def startElement(self, name, attrs, connection): retval = TaggedEC2Object.startElement(self, name, attrs, connection) if retval is not None: return retval if name == 'groupSet': self.groups = ResultSet([('item', Group)]) return self.groups elif name == 'attachment': self.attachment = Attachment() return self.attachment else: return None
def startElement(self, name, attrs, connection): result = super(InternetGateway, self).startElement(name, attrs, connection) if result is not None: # Parent found an interested element, just return it return result if name == 'attachmentSet': self.attachments = ResultSet([('item', InternetGatewayAttachment)]) return self.attachments else: return None
def startElement(self, name, attrs, connection): if name == 'SecurityGroups': return self.security_groups elif name == 'BlockDeviceMappings': if self.use_block_device_types: self.block_device_mappings = BDM() else: self.block_device_mappings = ResultSet([('member', BlockDeviceMapping)]) return self.block_device_mappings elif name == 'InstanceMonitoring': self.instance_monitoring = InstanceMonitoring(self) return self.instance_monitoring
def get_all_buckets(self, headers=None): response = self.make_request('GET') body = response.read() if response.status > 300: raise S3ResponseError(response.status, response.reason, body, headers=headers) rs = ResultSet([('Bucket', Bucket)]) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
def get_messages(self, num_messages=1, visibility_timeout=None): path = '%s/front?NumberOfMessages=%d' % (self.id, num_messages) if visibility_timeout: path = '%s&VisibilityTimeout=%d' % (path, visibility_timeout) response = self.connection.make_request('GET', path) body = response.read() if response.status >= 300: raise SQSError(response.status, response.reason, body) rs = ResultSet([('Message', self.message_class)]) h = XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
def get_all_buckets(self, headers=None): response = self.make_request('GET', headers=headers) body = response.read() if response.status > 300: raise self.provider.storage_response_error(response.status, response.reason, body) rs = ResultSet([('Bucket', self.bucket_class)]) h = handler.XmlHandler(rs, self) if not isinstance(body, bytes): body = body.encode('utf-8') xml.sax.parseString(body, h) return rs
def startElement(self, name, attrs, connection): if name == 'Endpoint': self._in_endpoint = True elif name == 'DBParameterGroups': self.parameter_groups = ResultSet([('DBParameterGroup', ParameterGroup)]) return self.parameter_groups elif name == 'DBSecurityGroups': self.security_groups = ResultSet([('DBSecurityGroup', DBSecurityGroup)]) return self.security_groups elif name == 'PendingModifiedValues': self.pending_modified_values = PendingModifiedValues() return self.pending_modified_values elif name == 'ReadReplicaDBInstanceIdentifiers': self.read_replica_dbinstance_identifiers = \ ReadReplicaDBInstanceIdentifiers() return self.read_replica_dbinstance_identifiers elif name == 'StatusInfos': self.status_infos = ResultSet([('StatusInfo', StatusInfo)]) return None
def unmarshall_policy(msg): markers = [('member', LoadBalancer)] rs = ResultSet(markers) h = boto.handler.XmlHandler(rs, None) if isinstance(msg, six.text_type): msg = msg.encode('utf-8') xml.sax.parseString(msg, h) lbs = rs if lbs and len(lbs) > 0: lb = lbs[0] if lb.policy_descriptions and len(lb.policy_descriptions) > 0: return lb.policy_descriptions[0] return None
def _process_response(self, response, marker_elems=None): """ Helper to process the xml response from AWS """ body = response.read() #print body if '<Errors>' not in body: rs = ResultSet(marker_elems) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs else: raise EC2ResponseError(response.status, response.reason, body)
def get_all_queues(self, prefix=''): if prefix: path = '/?QueueNamePrefix=%s' % prefix else: path = '/' response = self.make_request('GET', path) body = response.read() if response.status >= 300: raise SQSError(response.status, response.reason, body) rs = ResultSet([('QueueUrl', Queue)]) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
def verify_signature(self, end_point_url, http_parameters): params = dict( UrlEndPoint=end_point_url, HttpParameters=http_parameters, ) response = self.make_request("VerifySignature", params) body = response.read() if (response.status != 200): raise FPSResponseError(response.status, response.reason, body) rs = ResultSet([("VerifySignatureResponse", FPSResponse)]) h = handler.XmlHandler(rs, self) xml.sax.parseString(body, h) return rs
def test_get_stacks_correctly_aggregates_paged_results(self, cloudformation_mock): stacks_1 = [Mock(spec=Stack), Mock(spec=Stack)] stacks_2 = [Mock(spec=Stack), Mock(spec=Stack)] result_1 = ResultSet() result_1.extend(stacks_1) result_1.next_token = "my-next-token" result_2 = ResultSet() result_2.extend(stacks_2) result_2.next_token = None cloudformation_mock.connect_to_region.return_value.describe_stacks.side_effect = [result_1, result_2] cfn = CloudFormation() self.assertListEqual(stacks_1 + stacks_2, cfn.get_stacks())
def test_resultset_next_token(self): result = ResultSet() result.next_token = 'foo' func = Mock() final_result = Mock() with patch.multiple( pbm, invoke_with_throttling_retries=DEFAULT, _paginate_resultset=DEFAULT, _paginate_dict=DEFAULT, ) as mocks: mocks['invoke_with_throttling_retries'].return_value = result mocks['_paginate_resultset'].return_value = final_result res = paginate_query(func, 'foo', bar='barval') assert res == final_result assert mocks['invoke_with_throttling_retries'].mock_calls == [ call(func, 'foo', bar='barval') ] assert mocks['_paginate_resultset'].mock_calls == [ call(result, func, 'foo', bar='barval') ] assert mocks['_paginate_dict'].mock_calls == []
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 else: return None
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, 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)
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 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 """ 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)
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') ]