From 23f7ee89ed5b5310b1604c3f745ae62b6969a0f0 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 7 Feb 2013 22:42:19 -0500 Subject: Ensure start time is earlier than end time in simple_tenant_usage Simple check and respond with a http 400 if the start time is later Fixes LP# 1118910 Change-Id: I95f12abca725f347488e620d8c699f424e53ed23 --- nova/api/openstack/compute/contrib/simple_tenant_usage.py | 7 +++++++ .../openstack/compute/contrib/test_simple_tenant_usage.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/nova/api/openstack/compute/contrib/simple_tenant_usage.py b/nova/api/openstack/compute/contrib/simple_tenant_usage.py index 2313c00ac..f219689f7 100644 --- a/nova/api/openstack/compute/contrib/simple_tenant_usage.py +++ b/nova/api/openstack/compute/contrib/simple_tenant_usage.py @@ -18,6 +18,8 @@ import datetime import urlparse +from webob import exc + from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova.api.openstack import xmlutil @@ -204,6 +206,11 @@ class SimpleTenantUsageController(object): period_start = self._parse_datetime(env.get('start', [None])[0]) period_stop = self._parse_datetime(env.get('end', [None])[0]) + if not period_start < period_stop: + msg = _("Invalid start time. The start time cannot occur after " + "the end time.") + raise exc.HTTPBadRequest(explanation=msg) + detailed = env.get('detailed', ['0'])[0] == '1' return (period_start, period_stop, detailed) diff --git a/nova/tests/api/openstack/compute/contrib/test_simple_tenant_usage.py b/nova/tests/api/openstack/compute/contrib/test_simple_tenant_usage.py index 440c97fbd..ef4aacae8 100644 --- a/nova/tests/api/openstack/compute/contrib/test_simple_tenant_usage.py +++ b/nova/tests/api/openstack/compute/contrib/test_simple_tenant_usage.py @@ -218,6 +218,21 @@ class SimpleTenantUsageTest(test.TestCase): finally: policy.reset() + def test_get_tenants_usage_with_bad_start_date(self): + future = NOW + datetime.timedelta(hours=HOURS) + tenant_id = 0 + req = webob.Request.blank( + '/v2/faketenant_0/os-simple-tenant-usage/' + 'faketenant_%s?start=%s&end=%s' % + (tenant_id, future.isoformat(), NOW.isoformat())) + req.method = "GET" + req.headers["content-type"] = "application/json" + + res = req.get_response(fakes.wsgi_app( + fake_auth_context=self.user_context, + init_only=('os-simple-tenant-usage',))) + self.assertEqual(res.status_int, 400) + class SimpleTenantUsageSerializerTest(test.TestCase): def _verify_server_usage(self, raw_usage, tree): -- cgit