From a3a0068929136d3bf6e25338889fd3090938eac6 Mon Sep 17 00:00:00 2001 From: zhangbailin Date: Wed, 3 Oct 2018 23:03:58 -0400 Subject: [PATCH] Add restrictions on updated_at when getting migrations If ``changes-before`` is less than ``changes-since`` in requested, there will get empty data when querying in db, so add the limit of these parameters. If ``changes-before`` < ``changes-since``, will be returned 400. Closes-Bug: #1796008 Change-Id: I2a39ac5a9fe969d383099b4e766c46ad05d9f67c --- api-ref/source/parameters.yaml | 6 +++ nova/api/openstack/compute/migrations.py | 7 ++++ .../api/openstack/compute/test_migrations.py | 39 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/api-ref/source/parameters.yaml b/api-ref/source/parameters.yaml index 226cc742a71a..4e084bafa821 100644 --- a/api-ref/source/parameters.yaml +++ b/api-ref/source/parameters.yaml @@ -485,6 +485,9 @@ changes_before_migration: The ``±hh:mm`` value, if included, returns the time zone as an offset from UTC. For example, ``2015-08-27T09:49:58-05:00``. If you omit the time zone, the UTC time zone is assumed. + When both ``changes-since`` and ``changes-before`` are specified, + the value of the ``changes-before`` must be later than or equal to + the value of the ``changes-since`` otherwise API will return 400. in: query required: false type: string @@ -538,6 +541,9 @@ changes_since_migration: The ``±hh:mm`` value, if included, returns the time zone as an offset from UTC. For example, ``2015-08-27T09:49:58-05:00``. If you omit the time zone, the UTC time zone is assumed. + When both ``changes-since`` and ``changes-before`` are specified, + the value of the ``changes-since`` must be earlier than or equal to + the value of the ``changes-before`` otherwise API will return 400. in: query required: false type: string diff --git a/nova/api/openstack/compute/migrations.py b/nova/api/openstack/compute/migrations.py index 98ee3adbdc79..f99cf72cbef7 100644 --- a/nova/api/openstack/compute/migrations.py +++ b/nova/api/openstack/compute/migrations.py @@ -20,6 +20,7 @@ from nova.api.openstack import wsgi from nova.api import validation from nova import compute from nova import exception +from nova.i18n import _ from nova.objects import base as obj_base from nova.policies import migrations as migrations_policies @@ -96,6 +97,12 @@ class MigrationsController(wsgi.Controller): if allow_changes_before: search_opts['changes-before'] = timeutils.parse_isotime( search_opts['changes-before']) + changes_since = search_opts.get('changes-since') + if (changes_since and search_opts['changes-before'] < + search_opts['changes-since']): + msg = _('The value of changes-since must be less than ' + 'or equal to changes-before.') + raise exc.HTTPBadRequest(explanation=msg) else: # Before microversion 2.59 the schema allowed # additionalProperties=True, so a user could pass diff --git a/nova/tests/unit/api/openstack/compute/test_migrations.py b/nova/tests/unit/api/openstack/compute/test_migrations.py index 188ebed347f0..367d6dfe6400 100644 --- a/nova/tests/unit/api/openstack/compute/test_migrations.py +++ b/nova/tests/unit/api/openstack/compute/test_migrations.py @@ -14,6 +14,7 @@ import datetime +import iso8601 import mock from oslo_utils.fixture import uuidsentinel as uuids import six @@ -363,6 +364,44 @@ class MigrationTestCaseV266(MigrationsTestCaseV259): self.assertRaises(exception.ValidationError, self.controller.index, req) + @mock.patch('nova.compute.api.API.get_migrations_sorted', + return_value=objects.MigrationList()) + def test_index_with_changes_since_and_changes_before( + self, get_migrations_sorted): + changes_since = '2013-10-22T13:42:02Z' + changes_before = '2013-10-22T13:42:03Z' + req = fakes.HTTPRequest.blank( + '/os-migrations?changes-since=%s&changes-before=%s&' + 'instance_uuid=%s' + % (changes_since, changes_before, uuids.instance_uuid), + version=self.wsgi_api_version, + use_admin_context=True) + + self.controller.index(req) + search_opts = {'instance_uuid': uuids.instance_uuid, + 'changes-before': + datetime.datetime(2013, 10, 22, 13, 42, 3, + tzinfo=iso8601.iso8601.UTC), + 'changes-since': + datetime.datetime(2013, 10, 22, 13, 42, 2, + tzinfo=iso8601.iso8601.UTC)} + get_migrations_sorted.assert_called_once_with( + req.environ['nova.context'], search_opts, sort_dirs=mock.ANY, + sort_keys=mock.ANY, limit=1000, marker=None) + + def test_get_migrations_filters_with_distinct_changes_time_bad_request( + self): + changes_since = '2018-09-04T05:45:27Z' + changes_before = '2018-09-03T05:45:27Z' + req = fakes.HTTPRequest.blank('/os-migrations?' + 'changes-since=%s&changes-before=%s' % + (changes_since, changes_before), + version=self.wsgi_api_version, + use_admin_context=True) + ex = self.assertRaises(exc.HTTPBadRequest, self.controller.index, req) + self.assertIn('The value of changes-since must be less than ' + 'or equal to changes-before', six.text_type(ex)) + def test_index_with_changes_before_old_microversion_failed(self): """Tests that the changes-before query parameter is an error before microversion 2.66.