From ef1fd388154eee11b9e83f80e5004670fdffb6cc Mon Sep 17 00:00:00 2001 From: zhangbailin Date: Mon, 6 May 2019 19:06:55 +0800 Subject: [PATCH] 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 --- openstackclient/compute/v2/server.py | 27 ++++++- .../functional/compute/v2/test_server.py | 78 +++++++++++++++++++ .../tests/unit/compute/v2/test_server.py | 66 ++++++++++++++++ .../notes/bug-1827844-8f1de120087c6a22.yaml | 6 ++ 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/bug-1827844-8f1de120087c6a22.yaml diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index cb9f8d43eb..bc6b137bf1 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -1128,13 +1128,22 @@ class ListServer(command.Lister): default=False, help=_('Only display deleted servers (Admin only).') ) + parser.add_argument( + '--changes-before', + metavar='', + 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='', 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']) diff --git a/openstackclient/tests/functional/compute/v2/test_server.py b/openstackclient/tests/functional/compute/v2/test_server.py index c8fb44d380..6330ac98d5 100644 --- a/openstackclient/tests/functional/compute/v2/test_server.py +++ b/openstackclient/tests/functional/compute/v2/test_server.py @@ -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() diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index c30af8fb8e..f0c8843b5d 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -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') diff --git a/releasenotes/notes/bug-1827844-8f1de120087c6a22.yaml b/releasenotes/notes/bug-1827844-8f1de120087c6a22.yaml new file mode 100644 index 0000000000..56b69c1cde --- /dev/null +++ b/releasenotes/notes/bug-1827844-8f1de120087c6a22.yaml @@ -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`]