Ejemplo n.º 1
0
def get_paging_params(context, params, sort_keys):
    """
    Extract any paging parameters
    """
    marker = params.pop('marker', None)
    limit = params.pop('limit', cfg.CONF['service:api'].default_limit_v2)
    sort_key = params.pop('sort_key', None)
    sort_dir = params.pop('sort_dir', None)
    max_limit = cfg.CONF['service:api'].max_limit_v2

    if isinstance(limit, six.string_types) and limit.lower() == "max":
        # Support for retrieving the max results at once. If set to "max",
        # the configured max limit will be used.
        limit = max_limit

    elif limit:
        # Negative and zero limits are not caught in storage.
        # With a number bigger than MAXSIZE, rpc throws an 'OverflowError long
        # too big to convert'.
        # So the parameter 'limit' is checked here.
        invalid_limit_message = ('limit should be an integer between 1 and '
                                 '%(max)s' % {
                                     'max': max_limit
                                 })
        try:
            int_limit = int(limit)
            if int_limit <= 0 or int_limit > six.MAXSIZE:
                raise exceptions.InvalidLimit(invalid_limit_message)
        # This exception is raised for non ints when int(limit) is called
        except ValueError:
            raise exceptions.InvalidLimit(invalid_limit_message)

    # sort_dir is checked in paginate_query.
    # We duplicate the sort_dir check here to throw a more specific
    # exception than ValueError.
    if sort_dir and sort_dir not in ['asc', 'desc']:
        raise exceptions.InvalidSortDir(
            _("Unknown sort direction, "
              "must be 'desc' or 'asc'"))

    if sort_keys is None:
        sort_key = None
        sort_dir = None

    elif sort_key and sort_key not in sort_keys:
        msg = 'sort key must be one of %(keys)s' % {'keys': sort_keys}
        raise exceptions.InvalidSortKey(msg)
    elif sort_key == 'tenant_id' and not context.all_tenants:
        sort_key = None

    return marker, limit, sort_key, sort_dir
Ejemplo n.º 2
0
    def _get_paging_params(self, params):
        """
        Extract any paging parameters
        """
        marker = params.pop('marker', None)
        limit = params.pop('limit', None)
        sort_key = params.pop('sort_key', None)
        sort_dir = params.pop('sort_dir', None)

        # Negative and zero limits are not caught in storage.
        # With a number bigger than MAXSIZE, rpc throws an 'OverflowError long
        #  too big to convert'.
        # So the parameter 'limit' is checked here.
        if limit:
            try:
                invalid_limit_message = _(
                    str.format('limit should be an integer between 1 and {0}',
                               six.MAXSIZE))
                int_limit = int(limit)
                if int_limit <= 0 or int_limit > six.MAXSIZE:
                    raise exceptions.InvalidLimit(invalid_limit_message)
            # This exception is raised for non ints when int(limit) is called
            except ValueError:
                raise exceptions.InvalidLimit(invalid_limit_message)

        # sort_dir is checked in paginate_query.
        # We duplicate the sort_dir check here to throw a more specific
        # exception than ValueError.
        if sort_dir and sort_dir not in ['asc', 'desc']:
            raise exceptions.InvalidSortDir(
                _("Unknown sort direction, "
                  "must be 'desc' or 'asc'"))

        if sort_key and sort_key not in self.SORT_KEYS:
            raise exceptions.InvalidSortKey(
                _(
                    str.format('sort key must be one of {0}',
                               str(self.SORT_KEYS))))

        return marker, limit, sort_key, sort_dir