Пример #1
0
    def _paginate(self, request, context, resources):
        resources = sorted(resources, key=lambda res: res.created_at)

        end = len(resources)
        if request.starting_token:
            try:
                marker = common.nano_to_date(request.starting_token)
            except ValueError:
                context.abort(grpc.StatusCode.ABORTED,
                              'Invalid starting_token')

            for i, res in enumerate(resources):
                if res.created_at > marker:
                    start = i
                    break
            else:
                start = end
        else:
            start = 0

        if not resources or start == end:
            return [], None

        if request.max_entries:
            end = min(start + request.max_entries, end)

        selected_resources = itertools.islice(resources, start, end)
        if end < len(resources):
            token = common.date_to_nano(resources[end - 1].created_at)
        else:
            token = None
        return selected_resources, token
Пример #2
0
 def _convert_snapshot_type(self, snap):
     created_at = int(common.date_to_nano(snap.created_at))
     snapshot = types.Snapshot(
         id=snap.id,
         source_volume_id=snap.volume_id,
         created_at=created_at,
         status=types.SnapStatus(type=types.SnapStatusType.READY))
     return snapshot