From 3d4a7021db4f5bc0dc570d6784e9ff9e457b4c91 Mon Sep 17 00:00:00 2001 From: zhangbailin Date: Wed, 3 Oct 2018 23:24:08 -0400 Subject: [PATCH] Add restrictions on updated_at when getting instance action records 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: #1796009 Change-Id: I44546bc9798708a48a250cc3a21bdbcabe2649e1 --- api-ref/source/parameters.yaml | 6 +++ .../api/openstack/compute/instance_actions.py | 6 +++ .../compute/test_instance_actions.py | 37 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/api-ref/source/parameters.yaml b/api-ref/source/parameters.yaml index 4e084bafa8..547e95b7e8 100644 --- a/api-ref/source/parameters.yaml +++ b/api-ref/source/parameters.yaml @@ -467,6 +467,9 @@ changes_before_instance_action: 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 @@ -524,6 +527,9 @@ changes_since_instance_action: 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/instance_actions.py b/nova/api/openstack/compute/instance_actions.py index 656fca2ece..89ab245f98 100644 --- a/nova/api/openstack/compute/instance_actions.py +++ b/nova/api/openstack/compute/instance_actions.py @@ -110,6 +110,12 @@ class InstanceActionsController(wsgi.Controller): if 'changes-before' in search_opts: 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) limit, marker = common.get_limit_and_marker(req) try: diff --git a/nova/tests/unit/api/openstack/compute/test_instance_actions.py b/nova/tests/unit/api/openstack/compute/test_instance_actions.py index 9865b9d033..4b4ce129a4 100644 --- a/nova/tests/unit/api/openstack/compute/test_instance_actions.py +++ b/nova/tests/unit/api/openstack/compute/test_instance_actions.py @@ -14,7 +14,9 @@ # under the License. import copy +import datetime +import iso8601 import mock from oslo_policy import policy as oslo_policy from oslo_utils.fixture import uuidsentinel as uuids @@ -30,6 +32,7 @@ from nova import objects from nova import policy from nova import test from nova.tests.unit.api.openstack import fakes +from nova.tests.unit import fake_instance from nova.tests.unit import fake_server_actions @@ -340,6 +343,40 @@ class InstanceActionsTestV266(InstanceActionsTestV258): self.assertIn('Invalid input for query parameters changes-before', six.text_type(ex)) + @mock.patch('nova.compute.api.InstanceActionAPI.actions_get') + @mock.patch('nova.api.openstack.common.get_instance') + def test_get_action_with_changes_since_and_changes_before( + self, mock_get_instance, mock_action_get): + param = 'changes-since=2012-12-05T00:00:00Z&' \ + 'changes-before=2012-12-05T01:00:00Z' + req = self._get_http_req_with_version('os-instance-actions?%s' % + param, use_admin_context=True, + version=self.wsgi_api_version) + instance = fake_instance.fake_instance_obj(req.environ['nova.context']) + mock_get_instance.return_value = instance + + self.controller.index(req, FAKE_UUID) + filters = {'changes-since': datetime.datetime( + 2012, 12, 5, 0, 0, tzinfo=iso8601.iso8601.UTC), + 'changes-before': datetime.datetime( + 2012, 12, 5, 1, 0, tzinfo=iso8601.iso8601.UTC)} + mock_action_get.assert_called_once_with(req.environ['nova.context'], + instance, limit=1000, + marker=None, + filters=filters) + + def test_instance_actions_filters_with_distinct_changes_time_bad_request( + self): + changes_since = '2018-09-04T05:45:27Z' + changes_before = '2018-09-03T05:45:27Z' + req = self._get_http_req('os-instance-actions?' + 'changes-since=%s&changes-before=%s' % + (changes_since, changes_before)) + ex = self.assertRaises(exc.HTTPBadRequest, self.controller.index, + req, FAKE_UUID) + self.assertIn('The value of changes-since must be less than ' + 'or equal to changes-before', six.text_type(ex)) + def test_get_action_with_changes_before_old_microversion(self): """Tests that the changes-before query parameter is an error before microversion 2.66.