From 5c17ff6470fd8b499d5187fdd5ee6f1c5ed4f3c3 Mon Sep 17 00:00:00 2001 From: ZhiQiang Fan Date: Wed, 16 Sep 2015 05:05:08 -0700 Subject: [PATCH] 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 --- ceilometer/api/controllers/v2/meters.py | 7 ++----- ceilometer/api/controllers/v2/resources.py | 6 +----- ceilometer/storage/base.py | 6 ++++-- ceilometer/storage/impl_log.py | 5 +++-- ceilometer/tests/functional/gabbi/gabbits/meters.yaml | 4 ++-- .../tests/functional/gabbi/gabbits/resources-fixtured.yaml | 4 ++-- ceilometer/tests/unit/api/v2/test_query.py | 6 ++++-- 7 files changed, 18 insertions(+), 20 deletions(-) diff --git a/ceilometer/api/controllers/v2/meters.py b/ceilometer/api/controllers/v2/meters.py index f359449f..85c2ac37 100644 --- a/ceilometer/api/controllers/v2/meters.py +++ b/ceilometer/api/controllers/v2/meters.py @@ -491,11 +491,8 @@ class MetersController(rest.RestController): # Timestamp field is not supported for Meter queries limit = v2_utils.enforce_limit(limit) kwargs = v2_utils.query_to_kwargs( - q, pecan.request.storage_conn.get_meters, allow_timestamps=False) - if 'limit' in kwargs: - raise base.ClientSideError(_( - "Limit is not a valid field for queries, " - "use 'limit' parameter.")) + q, pecan.request.storage_conn.get_meters, + ['limit'], allow_timestamps=False) return [Meter.from_db_model(m) for m in pecan.request.storage_conn.get_meters(limit=limit, **kwargs)] diff --git a/ceilometer/api/controllers/v2/resources.py b/ceilometer/api/controllers/v2/resources.py index 063fcaf2..5dfba331 100644 --- a/ceilometer/api/controllers/v2/resources.py +++ b/ceilometer/api/controllers/v2/resources.py @@ -142,11 +142,7 @@ class ResourcesController(rest.RestController): q = q or [] limit = utils.enforce_limit(limit) kwargs = utils.query_to_kwargs( - q, pecan.request.storage_conn.get_resources) - if 'limit' in kwargs: - raise base.ClientSideError(_( - "Limit is not a valid field for queries, " - "use 'limit' parameter.")) + q, pecan.request.storage_conn.get_resources, ['limit']) resources = [ Resource.from_db_and_links(r, self._resource_links(r.resource_id, diff --git a/ceilometer/storage/base.py b/ceilometer/storage/base.py index ea9adc4b..e056b6ba 100644 --- a/ceilometer/storage/base.py +++ b/ceilometer/storage/base.py @@ -176,7 +176,7 @@ class Connection(object): def get_resources(user=None, project=None, source=None, start_timestamp=None, start_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. Iterable items containing resource information. @@ -189,12 +189,13 @@ class Connection(object): :param end_timestamp_op: Optional timestamp end range operation. :param metaquery: Optional dict with metadata to match on. :param resource: Optional resource filter. + :param limit: Maximum number of results to return. """ raise ceilometer.NotImplementedError('Resources not implemented') @staticmethod def get_meters(user=None, project=None, resource=None, source=None, - metaquery=None): + metaquery=None, limit=None): """Return an iterable of model.Meter instances. Iterable items containing meter information. @@ -203,6 +204,7 @@ class Connection(object): :param resource: Optional resource filter. :param source: Optional source filter. :param metaquery: Optional dict with metadata to match on. + :param limit: Maximum number of results to return. """ raise ceilometer.NotImplementedError('Meters not implemented') diff --git a/ceilometer/storage/impl_log.py b/ceilometer/storage/impl_log.py index 02debb4c..0d81ee30 100644 --- a/ceilometer/storage/impl_log.py +++ b/ceilometer/storage/impl_log.py @@ -55,7 +55,7 @@ class Connection(base.Connection): def get_resources(self, user=None, project=None, source=None, start_timestamp=None, start_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. { '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 metaquery: Optional dict with metadata to match on. :param resource: Optional resource filter. + :param limit: Maximum number of results to return. """ return [] @@ -98,7 +99,7 @@ class Connection(base.Connection): """ return [] - def get_samples(self, sample_filter): + def get_samples(self, sample_filter, limit=None): """Return an iterable of samples. Items are created by diff --git a/ceilometer/tests/functional/gabbi/gabbits/meters.yaml b/ceilometer/tests/functional/gabbi/gabbits/meters.yaml index 2896f418..bf780d5d 100644 --- a/ceilometer/tests/functional/gabbi/gabbits/meters.yaml +++ b/ceilometer/tests/functional/gabbi/gabbits/meters.yaml @@ -374,10 +374,10 @@ tests: url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1 status: 400 response_strings: - - Limit is not a valid field for queries, + - 'Unknown argument: \"limit\": unrecognized field in query' - name: get meters filter limit and limit url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1&limit=1 status: 400 response_strings: - - Limit is not a valid field for queries, + - 'Unknown argument: \"limit\": unrecognized field in query' diff --git a/ceilometer/tests/functional/gabbi/gabbits/resources-fixtured.yaml b/ceilometer/tests/functional/gabbi/gabbits/resources-fixtured.yaml index 6f2ea9ca..cf138c27 100644 --- a/ceilometer/tests/functional/gabbi/gabbits/resources-fixtured.yaml +++ b/ceilometer/tests/functional/gabbi/gabbits/resources-fixtured.yaml @@ -77,10 +77,10 @@ tests: url: /v2/resources?q.field=limit&q.op=eq&q.type=&q.value=1 status: 400 response_strings: - - Limit is not a valid field for queries, + - 'Unknown argument: \"limit\": unrecognized field in query' - name: get resources filter limit and limit url: /v2/resources?q.field=limit&q.op=eq&q.type=&q.value=1&limit=1 status: 400 response_strings: - - Limit is not a valid field for queries, + - 'Unknown argument: \"limit\": unrecognized field in query' diff --git a/ceilometer/tests/unit/api/v2/test_query.py b/ceilometer/tests/unit/api/v2/test_query.py index 4967a08a..af919890 100644 --- a/ceilometer/tests/unit/api/v2/test_query.py +++ b/ceilometer/tests/unit/api/v2/test_query.py @@ -380,7 +380,8 @@ class TestQueryToKwArgs(tests_base.BaseTestCase): value='abc')] exc = self.assertRaises( 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'] msg = ("unrecognized field in query: %s, " "valid keys: %s") % (q, valid_keys) @@ -393,7 +394,8 @@ class TestQueryToKwArgs(tests_base.BaseTestCase): value='abc')] exc = self.assertRaises( 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', 'search_offset', 'source', 'timestamp', 'user'] msg = ("unrecognized field in query: %s, "