Skip to content

Commit c46f9dc

Browse files
jiahui.qiangDean Troyer
authored andcommitted
Add options to "server list" command
Add "--deleted" and "--changes-since" options to "server list" command. Change-Id: Id94f6e5831a60b172b6cfcfca29b1d89de8db621 Closes-Bug:#1647242
1 parent b69b539 commit c46f9dc

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

doc/source/command-objects/server.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ List servers
253253
[--long]
254254
[--marker <server>]
255255
[--limit <limit>]
256+
[--deleted]
257+
[--changes-since <changes-since>]
256258
257259
.. option:: --reservation-id <reservation-id>
258260
@@ -327,6 +329,15 @@ List servers
327329
be displayed. If limit is greater than 'osapi_max_limit' option of Nova
328330
API, 'osapi_max_limit' will be used instead.
329331
332+
.. option:: --deleted
333+
334+
Only display deleted servers (Admin only).
335+
336+
.. option:: --changes-since <changes-since>
337+
338+
List only servers changed after a certain point of time. The provided time
339+
should be an ISO 8061 formatted time. ex 2016-03-04T06:27:59Z.
340+
330341
server lock
331342
-----------
332343

openstackclient/compute/v2/server.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from osc_lib.command import command
2727
from osc_lib import exceptions
2828
from osc_lib import utils
29+
from oslo_utils import timeutils
2930
import six
3031

3132
try:
@@ -793,6 +794,20 @@ def get_parser(self, prog_name):
793794
" 'osapi_max_limit' option of Nova API,"
794795
" 'osapi_max_limit' will be used instead."),
795796
)
797+
parser.add_argument(
798+
'--deleted',
799+
action="store_true",
800+
default=False,
801+
help=_('Only display deleted servers (Admin only).')
802+
)
803+
parser.add_argument(
804+
'--changes-since',
805+
metavar='<changes-since>',
806+
default=None,
807+
help=_("List only servers changed after a certain point of time."
808+
" The provided time should be an ISO 8061 formatted time."
809+
" ex 2016-03-04T06:27:59Z .")
810+
)
796811
return parser
797812

798813
def take_action(self, parsed_args):
@@ -844,9 +859,19 @@ def take_action(self, parsed_args):
844859
'tenant_id': project_id,
845860
'all_tenants': parsed_args.all_projects,
846861
'user_id': user_id,
862+
'deleted': parsed_args.deleted,
863+
'changes_since': parsed_args.changes_since,
847864
}
848865
LOG.debug('search options: %s', search_opts)
849866

867+
if search_opts['changes_since']:
868+
try:
869+
timeutils.parse_isotime(search_opts['changes_since'])
870+
except ValueError:
871+
raise exceptions.CommandError(_('Invalid changes-since value:'
872+
' %s') % search_opts['changes'
873+
'_since'])
874+
850875
if parsed_args.long:
851876
columns = (
852877
'ID',

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from osc_lib import exceptions
2121
from osc_lib import utils as common_utils
22+
from oslo_utils import timeutils
2223

2324
from openstackclient.compute.v2 import server
2425
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
@@ -826,6 +827,8 @@ def setUp(self):
826827
'tenant_id': None,
827828
'all_tenants': False,
828829
'user_id': None,
830+
'deleted': False,
831+
'changes_since': None,
829832
}
830833

831834
# Default params of the core function of the command in the case of no
@@ -902,6 +905,7 @@ def test_server_list_no_option(self):
902905
verifylist = [
903906
('all_projects', False),
904907
('long', False),
908+
('deleted', False),
905909
]
906910
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
907911

@@ -967,6 +971,48 @@ def test_server_list_with_flavor(self):
967971
self.assertEqual(self.columns, columns)
968972
self.assertEqual(tuple(self.data), tuple(data))
969973

974+
def test_server_list_with_changes_since(self):
975+
976+
arglist = [
977+
'--changes-since', '2016-03-04T06:27:59Z',
978+
'--deleted'
979+
]
980+
verifylist = [
981+
('changes_since', '2016-03-04T06:27:59Z'),
982+
('deleted', True),
983+
]
984+
985+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
986+
columns, data = self.cmd.take_action(parsed_args)
987+
988+
self.search_opts['changes_since'] = '2016-03-04T06:27:59Z'
989+
self.search_opts['deleted'] = True
990+
self.servers_mock.list.assert_called_with(**self.kwargs)
991+
992+
self.assertEqual(self.columns, columns)
993+
self.assertEqual(tuple(self.data), tuple(data))
994+
995+
@mock.patch.object(timeutils, 'parse_isotime', side_effect=ValueError)
996+
def test_server_list_with_invalid_changes_since(self, mock_parse_isotime):
997+
998+
arglist = [
999+
'--changes-since', 'Invalid time value',
1000+
]
1001+
verifylist = [
1002+
('changes_since', 'Invalid time value'),
1003+
]
1004+
1005+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1006+
try:
1007+
self.cmd.take_action(parsed_args)
1008+
self.fail('CommandError should be raised.')
1009+
except exceptions.CommandError as e:
1010+
self.assertEqual('Invalid changes-since value: Invalid time '
1011+
'value', str(e))
1012+
mock_parse_isotime.assert_called_once_with(
1013+
'Invalid time value'
1014+
)
1015+
9701016

9711017
class TestServerLock(TestServer):
9721018

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``--deleted`` and ``--changes-since`` options to ``server list``
5+
command.
6+
[Bug `1647242 <https://bugs.launchpad.net/bugs/1647272>`_]

0 commit comments

Comments
 (0)