def to_server_paged_list(provider, objects, limit=None): """ A convenience function for wrapping a list of OpenStack native objects in a ServerPagedResultList. OpenStack initial list of objects. Thereafter, the return list is wrapped in a BaseResultList, enabling extra properties like `is_truncated` and `marker` to be accessed. """ limit = limit or provider.config.default_result_limit is_truncated = len(objects) > limit next_token = objects[limit - 1].id if is_truncated else None results = ServerPagedResultList(is_truncated, next_token, False) for obj in itertools.islice(objects, limit): results.append(obj) return results
def list(self, limit=None, marker=None, collection=None, **kwargs): """ List a set of resources. :type collection: ``ResourceCollection`` :param collection: Boto resource collection object corresponding to the current resource. See http://boto3.readthedocs.io/ en/latest/guide/collections.html """ limit = limit or self.provider.config.default_result_limit collection = collection or self.boto_collection.filter(**kwargs) pag_type, resume_token, boto_objs = self._make_query( collection, limit, marker) # Wrap in CB objects. results = [self.cb_resource(self.provider, obj) for obj in boto_objs] if pag_type == 'server': log.debug("Using server pagination.") return ServerPagedResultList( is_truncated=True if resume_token else False, marker=resume_token if resume_token else None, supports_total=False, data=results) else: log.debug("Did not received a resume token, will page in client" " if necessary.") return ClientPagedResultList(self.provider, results, limit=limit, marker=marker)
def list(self, limit=None, marker=None): """ List all volumes. limit: The maximum number of volumes to return. The returned ResultList's is_truncated property can be used to determine whether more records are available. """ # For GCE API, Acceptable values are 0 to 500, inclusive. # (Default: 500). max_result = limit if limit is not None and limit < 500 else 500 response = (self.provider.gce_compute.disks().list( project=self.provider.project_name, zone=self.provider.default_zone, maxResults=max_result, pageToken=marker).execute()) gce_vols = [ GCEVolume(self.provider, vol) for vol in response.get('items', []) ] if len(gce_vols) > max_result: cb.log.warning('Expected at most %d results; got %d', max_result, len(gce_vols)) return ServerPagedResultList('nextPageToken' in response, response.get('nextPageToken'), False, data=gce_vols)
def to_server_paged_list(provider, objects, limit): """ A convenience function for wrapping a list of OpenStack native objects in a ServerPagedResultList. OpenStack initial list of objects. Thereafter, the return list is wrapped in a BaseResultList, enabling extra properties like `is_truncated` and `marker` to be accessed. """ limit = limit or provider.config.default_result_limit is_truncated = len(objects) > limit next_token = objects[limit].id if is_truncated else None results = ServerPagedResultList(is_truncated, next_token, False) for obj in itertools.islice(objects, limit): results.append(obj) return results
def list(self, limit=None, marker=None): key_pairs, resume_marker = self.provider.azure_client.list_public_keys( AzureKeyPairService.PARTITION_KEY, marker=marker, limit=limit or self.provider.config.default_result_limit) results = [ AzureKeyPair(self.provider, key_pair) for key_pair in key_pairs ] return ServerPagedResultList(is_truncated=resume_marker, marker=resume_marker, supports_total=False, data=results)
def list(self, limit=None, marker=None): """ List all instances. """ reservations = self.provider.ec2_conn.get_all_reservations( max_results=limit, next_token=marker) instances = [ AWSInstance(self.provider, inst) for res in reservations for inst in res.instances ] return ServerPagedResultList(reservations.is_truncated, reservations.next_token, False, data=instances)
def test_server_paged_result_list(self): objects = list(itertools.islice(self.objects, 2)) results = ServerPagedResultList(is_truncated=True, marker=objects[-1].id, supports_total=True, total=2, data=objects) self.assertTrue(results.is_truncated) self.assertListEqual(results, objects) self.assertEqual(results.marker, objects[-1].id) self.assertTrue(results.supports_total) self.assertEqual(results.total_results, 2) self.assertTrue(results.supports_server_paging, "Server paged result" " lists should return True for server paging.") with self.assertRaises(NotImplementedError): results.data
def list(self, limit=None, marker=None): max_result = limit if limit is not None and limit < 500 else 500 regions_response = (self.provider.gce_compute.regions().list( project=self.provider.project_name, maxResults=max_result, pageToken=marker).execute()) regions = [ GCERegion(self.provider, region) for region in regions_response['items'] ] if len(regions) > max_result: cb.log.warning('Expected at most %d results; got %d', max_result, len(regions)) return ServerPagedResultList('nextPageToken' in regions_response, regions_response.get('nextPageToken'), False, data=regions)
def find(self, name, limit=None, marker=None): """ Searches for an instance by a given list of attributes. :rtype: ``object`` of :class:`.Instance` :return: an Instance object """ filtr = {'tag:Name': name} reservations = self.provider.ec2_conn.get_all_reservations( filters=filtr, max_results=limit, next_token=marker) instances = [ AWSInstance(self.provider, inst) for res in reservations for inst in res.instances ] return ServerPagedResultList(reservations.is_truncated, reservations.next_token, False, data=instances)
def list(self, limit=None, marker=None): region = self.provider.region_name max_result = limit if limit is not None and limit < 500 else 500 response = (self.provider.gce_compute.routers().list( project=self.provider.project_name, region=region, maxResults=max_result, pageToken=marker).execute()) routers = [] for router in response.get('items', []): routers.append(GCERouter(self.provider, router)) if len(routers) > max_result: cb.log.warning('Expected at most %d results; go %d', max_result, len(routers)) return ServerPagedResultList('nextPageToken' in response, response.get('nextPageToken'), False, data=routers)
def list(self, limit=None, marker=None): """ List all containers. """ max_result = limit if limit is not None and limit < 500 else 500 response = (self.provider.gcs_storage.buckets().list( project=self.provider.project_name, maxResults=max_result, pageToken=marker).execute()) buckets = [] for bucket in response.get('items', []): buckets.append(GCSBucket(self.provider, bucket)) if len(buckets) > max_result: cb.log.warning('Expected at most %d results; got %d', max_result, len(buckets)) return ServerPagedResultList('nextPageToken' in response, response.get('nextPageToken'), False, data=buckets)
def list(self, limit=None, marker=None): """ List all snapshots. """ max_result = limit if limit is not None and limit < 500 else 500 response = (self.provider.gce_compute.snapshots().list( project=self.provider.project_name, maxResults=max_result, pageToken=marker).execute()) snapshots = [ GCESnapshot(self.provider, snapshot) for snapshot in response.get('items', []) ] if len(snapshots) > max_result: cb.log.warning('Expected at most %d results; got %d', max_result, len(snapshots)) return ServerPagedResultList('nextPageToken' in response, response.get('nextPageToken'), False, data=snapshots)
def list(self, limit=None, marker=None, collection=None, **kwargs): collection = collection or self.boto_collection.filter(**kwargs) resume_token, boto_objs = self._make_query(collection, limit, marker) # Wrap in CB objects. results = [self.cb_resource(self.provider, obj) for obj in boto_objs] if resume_token: log.debug("Received a resume token, using server pagination.") return ServerPagedResultList(is_truncated=True, marker=resume_token, supports_total=False, data=results) else: log.debug("Did not received a resume token, will page in client" " if necessary.") return ClientPagedResultList(self.provider, results, limit=limit, marker=marker)
def find(self, label, limit=None, marker=None): """ Searches for a snapshot by a given list of attributes. """ filtr = 'labels.cblabel eq ' + label max_result = limit if limit is not None and limit < 500 else 500 response = (self.provider.gce_compute.snapshots().list( project=self.provider.project_name, filter=filtr, maxResults=max_result, pageToken=marker).execute()) snapshots = [ GCESnapshot(self.provider, snapshot) for snapshot in response.get('items', []) ] if len(snapshots) > max_result: cb.log.warning('Expected at most %d results; got %d', max_result, len(snapshots)) return ServerPagedResultList('nextPageToken' in response, response.get('nextPageToken'), False, data=snapshots)
def list(self, limit=None, marker=None): """ List all instances. """ # For GCE API, Acceptable values are 0 to 500, inclusive. # (Default: 500). max_result = limit if limit is not None and limit < 500 else 500 response = (self.provider.gce_compute.instances().list( project=self.provider.project_name, zone=self.provider.default_zone, maxResults=max_result, pageToken=marker).execute()) instances = [ GCEInstance(self.provider, inst) for inst in response.get('items', []) ] if len(instances) > max_result: cb.log.warning('Expected at most %d results; got %d', max_result, len(instances)) return ServerPagedResultList('nextPageToken' in response, response.get('nextPageToken'), False, data=instances)