Add changes-before attribute to server list

Closes-Bug: #1827844
Part of bp support-to-query-nova-resources-filter-by-changes-before

Change-Id: I4f28168188973730247bcbcb70ba0e70eb81e3be
This commit is contained in:
zhangbailin 2019-05-06 19:06:55 +08:00 committed by Brin Zhang
parent 91bc0f35de
commit ef1fd38815
4 changed files with 175 additions and 2 deletions

View File

@ -1128,13 +1128,22 @@ class ListServer(command.Lister):
default=False,
help=_('Only display deleted servers (Admin only).')
)
parser.add_argument(
'--changes-before',
metavar='<changes-before>',
default=None,
help=_("List only servers changed before a certain point of time. "
"The provided time should be an ISO 8061 formatted time "
"(e.g., 2016-03-05T06:27:59Z). "
"(Supported by API versions '2.66' - '2.latest')")
)
parser.add_argument(
'--changes-since',
metavar='<changes-since>',
default=None,
help=_("List only servers changed after a certain point of time."
" The provided time should be an ISO 8061 formatted time."
" ex 2016-03-04T06:27:59Z .")
" The provided time should be an ISO 8061 formatted time"
" (e.g., 2016-03-04T06:27:59Z).")
)
return parser
@ -1188,10 +1197,24 @@ class ListServer(command.Lister):
'all_tenants': parsed_args.all_projects,
'user_id': user_id,
'deleted': parsed_args.deleted,
'changes-before': parsed_args.changes_before,
'changes-since': parsed_args.changes_since,
}
LOG.debug('search options: %s', search_opts)
if search_opts['changes-before']:
if compute_client.api_version < api_versions.APIVersion('2.66'):
msg = _('--os-compute-api-version 2.66 or later is required')
raise exceptions.CommandError(msg)
try:
timeutils.parse_isotime(search_opts['changes-before'])
except ValueError:
raise exceptions.CommandError(
_('Invalid changes-before value: %s') %
search_opts['changes-before']
)
if search_opts['changes-since']:
try:
timeutils.parse_isotime(search_opts['changes-since'])

View File

@ -63,6 +63,84 @@ class ServerTests(common.ComputeTestCase):
self.assertNotIn(name1, col_name)
self.assertIn(name2, col_name)
def test_server_list_with_changes_before(self):
"""Test server list.
Getting the servers list with updated_at time equal or
before than changes-before.
"""
cmd_output = self.server_create()
server_name1 = cmd_output['name']
cmd_output = self.server_create()
server_name2 = cmd_output['name']
updated_at2 = cmd_output['updated']
cmd_output = self.server_create()
server_name3 = cmd_output['name']
cmd_output = json.loads(self.openstack(
'--os-compute-api-version 2.66 ' +
'server list -f json '
'--changes-before ' + updated_at2
))
col_updated = [server["Name"] for server in cmd_output]
self.assertIn(server_name1, col_updated)
self.assertIn(server_name2, col_updated)
self.assertNotIn(server_name3, col_updated)
def test_server_list_with_changes_since(self):
"""Test server list.
Getting the servers list with updated_at time equal or
later than changes-since.
"""
cmd_output = self.server_create()
server_name1 = cmd_output['name']
cmd_output = self.server_create()
server_name2 = cmd_output['name']
updated_at2 = cmd_output['updated']
cmd_output = self.server_create()
server_name3 = cmd_output['name']
cmd_output = json.loads(self.openstack(
'server list -f json '
'--changes-since ' + updated_at2
))
col_updated = [server["Name"] for server in cmd_output]
self.assertNotIn(server_name1, col_updated)
self.assertIn(server_name2, col_updated)
self.assertIn(server_name3, col_updated)
def test_server_list_with_changes_before_and_changes_since(self):
"""Test server list.
Getting the servers list with updated_at time equal or before than
changes-before and equal or later than changes-since.
"""
cmd_output = self.server_create()
server_name1 = cmd_output['name']
cmd_output = self.server_create()
server_name2 = cmd_output['name']
updated_at2 = cmd_output['updated']
cmd_output = self.server_create()
server_name3 = cmd_output['name']
updated_at3 = cmd_output['updated']
cmd_output = json.loads(self.openstack(
'--os-compute-api-version 2.66 ' +
'server list -f json ' +
'--changes-since ' + updated_at2 +
' --changes-before ' + updated_at3
))
col_updated = [server["Name"] for server in cmd_output]
self.assertNotIn(server_name1, col_updated)
self.assertIn(server_name2, col_updated)
self.assertIn(server_name3, col_updated)
def test_server_set(self):
"""Test server create, delete, set, show"""
cmd_output = self.server_create()

View File

@ -1991,6 +1991,7 @@ class TestServerList(TestServer):
'user_id': None,
'deleted': False,
'changes-since': None,
'changes-before': None,
}
# Default params of the core function of the command in the case of no
@ -2272,6 +2273,71 @@ class TestServerList(TestServer):
'Invalid time value'
)
def test_server_list_v266_with_changes_before(self):
self.app.client_manager.compute.api_version = (
api_versions.APIVersion('2.66'))
arglist = [
'--changes-before', '2016-03-05T06:27:59Z',
'--deleted'
]
verifylist = [
('changes_before', '2016-03-05T06:27:59Z'),
('deleted', True),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.search_opts['changes-before'] = '2016-03-05T06:27:59Z'
self.search_opts['deleted'] = True
self.servers_mock.list.assert_called_with(**self.kwargs)
self.assertEqual(self.columns, columns)
self.assertEqual(tuple(self.data), tuple(data))
@mock.patch.object(timeutils, 'parse_isotime', side_effect=ValueError)
def test_server_list_v266_with_invalid_changes_before(
self, mock_parse_isotime):
self.app.client_manager.compute.api_version = (
api_versions.APIVersion('2.66'))
arglist = [
'--changes-before', 'Invalid time value',
]
verifylist = [
('changes_before', 'Invalid time value'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
try:
self.cmd.take_action(parsed_args)
self.fail('CommandError should be raised.')
except exceptions.CommandError as e:
self.assertEqual('Invalid changes-before value: Invalid time '
'value', str(e))
mock_parse_isotime.assert_called_once_with(
'Invalid time value'
)
def test_server_with_changes_before_older_version(self):
self.app.client_manager.compute.api_version = (
api_versions.APIVersion('2.65'))
arglist = [
'--changes-before', '2016-03-05T06:27:59Z',
'--deleted'
]
verifylist = [
('changes_before', '2016-03-05T06:27:59Z'),
('deleted', True),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(exceptions.CommandError,
self.cmd.take_action,
parsed_args)
def test_server_list_v269_with_partial_constructs(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.69')

View File

@ -0,0 +1,6 @@
---
features:
- |
Add ``--changes-before`` option to the ``server list`` command.
This requires Compute API version '2.66' or later.
[:lpbug: `1827844`]