From bb89dd91fad4b55ca51e4afe4a91634371370bd1 Mon Sep 17 00:00:00 2001 From: "Castulo J. Martinez" Date: Sat, 21 May 2016 21:21:11 -0700 Subject: [PATCH] Fixes the use of dates when listing images When listing images there are several optional parameters that can be used to filter the list of images retrieved by the API. The following two parameters are not working properly: the created_at and the updated_at. Before the Mitaka release it was possible to use these filters just using a datetime in the format ISO 8601, starting on Mitaka, you need to add an operator along with the datetime stamp or the API call fails. This commit adds backwards compatibility so it is possible to filter the images list using only a datetime stamp without also specifing an operator. If no operator is used an eq operator is assumed. Change-Id: Id5d5455e77637e0dc7baec25c8163b21634d72c4 Partial-Bug: 1584415 --- glance/common/utils.py | 14 ++++++++++++-- glance/tests/unit/common/test_utils.py | 10 ++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/glance/common/utils.py b/glance/common/utils.py index cad42607..e24d4604 100644 --- a/glance/common/utils.py +++ b/glance/common/utils.py @@ -44,6 +44,7 @@ import six from webob import exc from glance.common import exception +from glance.common import timeutils from glance.i18n import _, _LE, _LW CONF = cfg.CONF @@ -603,8 +604,17 @@ def split_filter_op(expression): """ left, sep, right = expression.partition(':') if sep: - op = left - threshold = right + # If the expression is a date of the format ISO 8601 like + # CCYY-MM-DDThh:mm:ss+hh:mm and has no operator, it should + # not be partitioned, and a default operator of eq should be + # assumed. + try: + timeutils.parse_isotime(expression) + op = 'eq' + threshold = expression + except ValueError: + op = left + threshold = right else: op = 'eq' # default operator threshold = left diff --git a/glance/tests/unit/common/test_utils.py b/glance/tests/unit/common/test_utils.py index 423b95df..27879fdd 100644 --- a/glance/tests/unit/common/test_utils.py +++ b/glance/tests/unit/common/test_utils.py @@ -450,6 +450,16 @@ class SplitFilterOpTestCase(test_utils.BaseTestCase): returned = utils.split_filter_op(expr) self.assertEqual(('eq', expr), returned) + def test_default_operator_with_datetime(self): + expr = '2015-08-27T09:49:58Z' + returned = utils.split_filter_op(expr) + self.assertEqual(('eq', expr), returned) + + def test_operator_with_datetime(self): + expr = 'lt:2015-08-27T09:49:58Z' + returned = utils.split_filter_op(expr) + self.assertEqual(('lt', '2015-08-27T09:49:58Z'), returned) + class EvaluateFilterOpTestCase(test_utils.BaseTestCase):