apply limit constraint on storage base interface

Currently we have implemented limit constraint on get_meters,
get_resources, get_samples and query_samples on most of major databases,
but haven't applied such change on storage interface of get_meters and
get_resources, this patch adds parameter limit to those two methods.

Change-Id: I10e43e082ec9fd87dcd491299a57c0116009220e
Closes-Bug: #1496677
This commit is contained in:
ZhiQiang Fan 2015-09-16 05:05:08 -07:00
parent 67b55c01c8
commit 5c17ff6470
7 changed files with 18 additions and 20 deletions

View File

@ -491,11 +491,8 @@ class MetersController(rest.RestController):
# Timestamp field is not supported for Meter queries # Timestamp field is not supported for Meter queries
limit = v2_utils.enforce_limit(limit) limit = v2_utils.enforce_limit(limit)
kwargs = v2_utils.query_to_kwargs( kwargs = v2_utils.query_to_kwargs(
q, pecan.request.storage_conn.get_meters, allow_timestamps=False) q, pecan.request.storage_conn.get_meters,
if 'limit' in kwargs: ['limit'], allow_timestamps=False)
raise base.ClientSideError(_(
"Limit is not a valid field for queries, "
"use 'limit' parameter."))
return [Meter.from_db_model(m) return [Meter.from_db_model(m)
for m in pecan.request.storage_conn.get_meters(limit=limit, for m in pecan.request.storage_conn.get_meters(limit=limit,
**kwargs)] **kwargs)]

View File

@ -142,11 +142,7 @@ class ResourcesController(rest.RestController):
q = q or [] q = q or []
limit = utils.enforce_limit(limit) limit = utils.enforce_limit(limit)
kwargs = utils.query_to_kwargs( kwargs = utils.query_to_kwargs(
q, pecan.request.storage_conn.get_resources) q, pecan.request.storage_conn.get_resources, ['limit'])
if 'limit' in kwargs:
raise base.ClientSideError(_(
"Limit is not a valid field for queries, "
"use 'limit' parameter."))
resources = [ resources = [
Resource.from_db_and_links(r, Resource.from_db_and_links(r,
self._resource_links(r.resource_id, self._resource_links(r.resource_id,

View File

@ -176,7 +176,7 @@ class Connection(object):
def get_resources(user=None, project=None, source=None, def get_resources(user=None, project=None, source=None,
start_timestamp=None, start_timestamp_op=None, start_timestamp=None, start_timestamp_op=None,
end_timestamp=None, end_timestamp_op=None, end_timestamp=None, end_timestamp_op=None,
metaquery=None, resource=None): metaquery=None, resource=None, limit=None):
"""Return an iterable of models.Resource instances. """Return an iterable of models.Resource instances.
Iterable items containing resource information. Iterable items containing resource information.
@ -189,12 +189,13 @@ class Connection(object):
:param end_timestamp_op: Optional timestamp end range operation. :param end_timestamp_op: Optional timestamp end range operation.
:param metaquery: Optional dict with metadata to match on. :param metaquery: Optional dict with metadata to match on.
:param resource: Optional resource filter. :param resource: Optional resource filter.
:param limit: Maximum number of results to return.
""" """
raise ceilometer.NotImplementedError('Resources not implemented') raise ceilometer.NotImplementedError('Resources not implemented')
@staticmethod @staticmethod
def get_meters(user=None, project=None, resource=None, source=None, def get_meters(user=None, project=None, resource=None, source=None,
metaquery=None): metaquery=None, limit=None):
"""Return an iterable of model.Meter instances. """Return an iterable of model.Meter instances.
Iterable items containing meter information. Iterable items containing meter information.
@ -203,6 +204,7 @@ class Connection(object):
:param resource: Optional resource filter. :param resource: Optional resource filter.
:param source: Optional source filter. :param source: Optional source filter.
:param metaquery: Optional dict with metadata to match on. :param metaquery: Optional dict with metadata to match on.
:param limit: Maximum number of results to return.
""" """
raise ceilometer.NotImplementedError('Meters not implemented') raise ceilometer.NotImplementedError('Meters not implemented')

View File

@ -55,7 +55,7 @@ class Connection(base.Connection):
def get_resources(self, user=None, project=None, source=None, def get_resources(self, user=None, project=None, source=None,
start_timestamp=None, start_timestamp_op=None, start_timestamp=None, start_timestamp_op=None,
end_timestamp=None, end_timestamp_op=None, end_timestamp=None, end_timestamp_op=None,
metaquery=None, resource=None): metaquery=None, resource=None, limit=None):
"""Return an iterable of dictionaries containing resource information. """Return an iterable of dictionaries containing resource information.
{ 'resource_id': UUID of the resource, { 'resource_id': UUID of the resource,
@ -75,6 +75,7 @@ class Connection(base.Connection):
:param end_timestamp_op: Optional end time operator, like lt, le. :param end_timestamp_op: Optional end time operator, like lt, le.
:param metaquery: Optional dict with metadata to match on. :param metaquery: Optional dict with metadata to match on.
:param resource: Optional resource filter. :param resource: Optional resource filter.
:param limit: Maximum number of results to return.
""" """
return [] return []
@ -98,7 +99,7 @@ class Connection(base.Connection):
""" """
return [] return []
def get_samples(self, sample_filter): def get_samples(self, sample_filter, limit=None):
"""Return an iterable of samples. """Return an iterable of samples.
Items are created by Items are created by

View File

@ -374,10 +374,10 @@ tests:
url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1 url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1
status: 400 status: 400
response_strings: response_strings:
- Limit is not a valid field for queries, - 'Unknown argument: \"limit\": unrecognized field in query'
- name: get meters filter limit and limit - name: get meters filter limit and limit
url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1&limit=1 url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1&limit=1
status: 400 status: 400
response_strings: response_strings:
- Limit is not a valid field for queries, - 'Unknown argument: \"limit\": unrecognized field in query'

View File

@ -77,10 +77,10 @@ tests:
url: /v2/resources?q.field=limit&q.op=eq&q.type=&q.value=1 url: /v2/resources?q.field=limit&q.op=eq&q.type=&q.value=1
status: 400 status: 400
response_strings: response_strings:
- Limit is not a valid field for queries, - 'Unknown argument: \"limit\": unrecognized field in query'
- name: get resources filter limit and limit - name: get resources filter limit and limit
url: /v2/resources?q.field=limit&q.op=eq&q.type=&q.value=1&limit=1 url: /v2/resources?q.field=limit&q.op=eq&q.type=&q.value=1&limit=1
status: 400 status: 400
response_strings: response_strings:
- Limit is not a valid field for queries, - 'Unknown argument: \"limit\": unrecognized field in query'

View File

@ -380,7 +380,8 @@ class TestQueryToKwArgs(tests_base.BaseTestCase):
value='abc')] value='abc')]
exc = self.assertRaises( exc = self.assertRaises(
wsme.exc.UnknownArgument, wsme.exc.UnknownArgument,
utils.query_to_kwargs, q, storage_base.Connection.get_meters) utils.query_to_kwargs,
q, storage_base.Connection.get_meters, ['limit'])
valid_keys = ['project', 'resource', 'source', 'user'] valid_keys = ['project', 'resource', 'source', 'user']
msg = ("unrecognized field in query: %s, " msg = ("unrecognized field in query: %s, "
"valid keys: %s") % (q, valid_keys) "valid keys: %s") % (q, valid_keys)
@ -393,7 +394,8 @@ class TestQueryToKwArgs(tests_base.BaseTestCase):
value='abc')] value='abc')]
exc = self.assertRaises( exc = self.assertRaises(
wsme.exc.UnknownArgument, wsme.exc.UnknownArgument,
utils.query_to_kwargs, q, storage_base.Connection.get_resources) utils.query_to_kwargs,
q, storage_base.Connection.get_resources, ['limit'])
valid_keys = ['project', 'resource', valid_keys = ['project', 'resource',
'search_offset', 'source', 'timestamp', 'user'] 'search_offset', 'source', 'timestamp', 'user']
msg = ("unrecognized field in query: %s, " msg = ("unrecognized field in query: %s, "