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
This commit is contained in:
zhangbailin 2018-10-03 23:24:08 -04:00 committed by Matt Riedemann
parent a3a0068929
commit 3d4a7021db
3 changed files with 49 additions and 0 deletions

View File

@ -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

View File

@ -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:

View File

@ -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.