From c46f9dc501441ef449f41e726ec3cfbbe9f3de9d Mon Sep 17 00:00:00 2001 From: "jiahui.qiang" Date: Thu, 24 Nov 2016 17:05:25 +0800 Subject: [PATCH] Add options to "server list" command Add "--deleted" and "--changes-since" options to "server list" command. Change-Id: Id94f6e5831a60b172b6cfcfca29b1d89de8db621 Closes-Bug:#1647242 --- doc/source/command-objects/server.rst | 11 +++++ openstackclient/compute/v2/server.py | 25 ++++++++++ .../tests/unit/compute/v2/test_server.py | 46 +++++++++++++++++++ .../notes/bug-1647242-fdc39e564372857b.yaml | 6 +++ 4 files changed, 88 insertions(+) create mode 100644 releasenotes/notes/bug-1647242-fdc39e564372857b.yaml diff --git a/doc/source/command-objects/server.rst b/doc/source/command-objects/server.rst index 40ecc047a1..f18d091855 100644 --- a/doc/source/command-objects/server.rst +++ b/doc/source/command-objects/server.rst @@ -253,6 +253,8 @@ List servers [--long] [--marker ] [--limit ] + [--deleted] + [--changes-since ] .. option:: --reservation-id @@ -327,6 +329,15 @@ List servers be displayed. If limit is greater than 'osapi_max_limit' option of Nova API, 'osapi_max_limit' will be used instead. +.. option:: --deleted + + Only display deleted servers (Admin only). + +.. option:: --changes-since + + 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. + server lock ----------- diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index a1330e0195..dc32add5b2 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -26,6 +26,7 @@ from osc_lib.cli import parseractions from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils +from oslo_utils import timeutils import six try: @@ -793,6 +794,20 @@ class ListServer(command.Lister): " 'osapi_max_limit' option of Nova API," " 'osapi_max_limit' will be used instead."), ) + parser.add_argument( + '--deleted', + action="store_true", + default=False, + help=_('Only display deleted servers (Admin only).') + ) + 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 .") + ) return parser def take_action(self, parsed_args): @@ -844,9 +859,19 @@ class ListServer(command.Lister): 'tenant_id': project_id, 'all_tenants': parsed_args.all_projects, 'user_id': user_id, + 'deleted': parsed_args.deleted, + 'changes_since': parsed_args.changes_since, } LOG.debug('search options: %s', search_opts) + if search_opts['changes_since']: + try: + timeutils.parse_isotime(search_opts['changes_since']) + except ValueError: + raise exceptions.CommandError(_('Invalid changes-since value:' + ' %s') % search_opts['changes' + '_since']) + if parsed_args.long: columns = ( 'ID', diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 54f36209c0..deba4435a1 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -19,6 +19,7 @@ from mock import call from osc_lib import exceptions from osc_lib import utils as common_utils +from oslo_utils import timeutils from openstackclient.compute.v2 import server from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes @@ -826,6 +827,8 @@ class TestServerList(TestServer): 'tenant_id': None, 'all_tenants': False, 'user_id': None, + 'deleted': False, + 'changes_since': None, } # Default params of the core function of the command in the case of no @@ -902,6 +905,7 @@ class TestServerList(TestServer): verifylist = [ ('all_projects', False), ('long', False), + ('deleted', False), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -967,6 +971,48 @@ class TestServerList(TestServer): self.assertEqual(self.columns, columns) self.assertEqual(tuple(self.data), tuple(data)) + def test_server_list_with_changes_since(self): + + arglist = [ + '--changes-since', '2016-03-04T06:27:59Z', + '--deleted' + ] + verifylist = [ + ('changes_since', '2016-03-04T06: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_since'] = '2016-03-04T06: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_with_invalid_changes_since(self, mock_parse_isotime): + + arglist = [ + '--changes-since', 'Invalid time value', + ] + verifylist = [ + ('changes_since', '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-since value: Invalid time ' + 'value', str(e)) + mock_parse_isotime.assert_called_once_with( + 'Invalid time value' + ) + class TestServerLock(TestServer): diff --git a/releasenotes/notes/bug-1647242-fdc39e564372857b.yaml b/releasenotes/notes/bug-1647242-fdc39e564372857b.yaml new file mode 100644 index 0000000000..d4b3bdaa5d --- /dev/null +++ b/releasenotes/notes/bug-1647242-fdc39e564372857b.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add ``--deleted`` and ``--changes-since`` options to ``server list`` + command. + [Bug `1647242 `_]