Skip to content

Commit 6bd1135

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add options to "volume snapshot list" command"
2 parents b22af88 + 6ca4dc3 commit 6bd1135

6 files changed

Lines changed: 278 additions & 11 deletions

File tree

doc/source/command-objects/volume-snapshot.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ List volume snapshots
7171
[--long]
7272
[--limit <limit>]
7373
[--marker <marker>]
74+
[--name <name>]
75+
[--status <status>]
76+
[--volume <volume>]
7477
7578
.. option:: --all-projects
7679

@@ -80,6 +83,19 @@ List volume snapshots
8083

8184
List additional fields in output
8285

86+
.. option:: --status <status>
87+
88+
Filters results by a status.
89+
('available', 'error', 'creating', 'deleting' or 'error-deleting')
90+
91+
.. option:: --name <name>
92+
93+
Filters results by a name.
94+
95+
.. option:: --volume <volume>
96+
97+
Filters results by a volume (name or ID).
98+
8399
.. option:: --limit <limit>
84100

85101
Maximum number of snapshots to display

openstackclient/tests/unit/volume/v1/test_snapshot.py

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ def setUp(self):
268268
super(TestSnapshotList, self).setUp()
269269

270270
self.volumes_mock.list.return_value = [self.volume]
271+
self.volumes_mock.get.return_value = self.volume
271272
self.snapshots_mock.list.return_value = self.snapshots
272273
# Get the command to test
273274
self.cmd = volume_snapshot.ListVolumeSnapshot(self.app, None)
@@ -283,7 +284,13 @@ def test_snapshot_list_without_options(self):
283284
columns, data = self.cmd.take_action(parsed_args)
284285

285286
self.snapshots_mock.list.assert_called_once_with(
286-
search_opts={'all_tenants': False})
287+
search_opts={
288+
'all_tenants': False,
289+
'display_name': None,
290+
'status': None,
291+
'volume_id': None
292+
}
293+
)
287294
self.assertEqual(self.columns, columns)
288295
self.assertEqual(self.data, list(data))
289296

@@ -300,11 +307,88 @@ def test_snapshot_list_with_long(self):
300307
columns, data = self.cmd.take_action(parsed_args)
301308

302309
self.snapshots_mock.list.assert_called_once_with(
303-
search_opts={'all_tenants': False}
310+
search_opts={
311+
'all_tenants': False,
312+
'display_name': None,
313+
'status': None,
314+
'volume_id': None
315+
}
304316
)
305317
self.assertEqual(self.columns_long, columns)
306318
self.assertEqual(self.data_long, list(data))
307319

320+
def test_snapshot_list_name_option(self):
321+
arglist = [
322+
'--name', self.snapshots[0].display_name,
323+
]
324+
verifylist = [
325+
('all_projects', False),
326+
('long', False),
327+
('name', self.snapshots[0].display_name),
328+
]
329+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
330+
331+
columns, data = self.cmd.take_action(parsed_args)
332+
333+
self.snapshots_mock.list.assert_called_once_with(
334+
search_opts={
335+
'all_tenants': False,
336+
'display_name': self.snapshots[0].display_name,
337+
'status': None,
338+
'volume_id': None
339+
}
340+
)
341+
self.assertEqual(self.columns, columns)
342+
self.assertEqual(self.data, list(data))
343+
344+
def test_snapshot_list_status_option(self):
345+
arglist = [
346+
'--status', self.snapshots[0].status,
347+
]
348+
verifylist = [
349+
('all_projects', False),
350+
('long', False),
351+
('status', self.snapshots[0].status),
352+
]
353+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
354+
355+
columns, data = self.cmd.take_action(parsed_args)
356+
357+
self.snapshots_mock.list.assert_called_once_with(
358+
search_opts={
359+
'all_tenants': False,
360+
'display_name': None,
361+
'status': self.snapshots[0].status,
362+
'volume_id': None
363+
}
364+
)
365+
self.assertEqual(self.columns, columns)
366+
self.assertEqual(self.data, list(data))
367+
368+
def test_snapshot_list_volumeid_option(self):
369+
arglist = [
370+
'--volume', self.volume.id,
371+
]
372+
verifylist = [
373+
('all_projects', False),
374+
('long', False),
375+
('volume', self.volume.id),
376+
]
377+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
378+
379+
columns, data = self.cmd.take_action(parsed_args)
380+
381+
self.snapshots_mock.list.assert_called_once_with(
382+
search_opts={
383+
'all_tenants': False,
384+
'display_name': None,
385+
'status': None,
386+
'volume_id': self.volume.id
387+
}
388+
)
389+
self.assertEqual(self.columns, columns)
390+
self.assertEqual(self.data, list(data))
391+
308392
def test_snapshot_list_all_projects(self):
309393
arglist = [
310394
'--all-projects',
@@ -318,7 +402,13 @@ def test_snapshot_list_all_projects(self):
318402
columns, data = self.cmd.take_action(parsed_args)
319403

320404
self.snapshots_mock.list.assert_called_once_with(
321-
search_opts={'all_tenants': True})
405+
search_opts={
406+
'all_tenants': True,
407+
'display_name': None,
408+
'status': None,
409+
'volume_id': None
410+
}
411+
)
322412
self.assertEqual(self.columns, columns)
323413
self.assertEqual(self.data, list(data))
324414

openstackclient/tests/unit/volume/v2/test_snapshot.py

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ def setUp(self):
275275
super(TestSnapshotList, self).setUp()
276276

277277
self.volumes_mock.list.return_value = [self.volume]
278+
self.volumes_mock.get.return_value = self.volume
278279
self.snapshots_mock.list.return_value = self.snapshots
279280
# Get the command to test
280281
self.cmd = volume_snapshot.ListVolumeSnapshot(self.app, None)
@@ -283,14 +284,21 @@ def test_snapshot_list_without_options(self):
283284
arglist = []
284285
verifylist = [
285286
('all_projects', False),
286-
("long", False)
287+
('long', False)
287288
]
288289
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
289290

290291
columns, data = self.cmd.take_action(parsed_args)
291292

292293
self.snapshots_mock.list.assert_called_once_with(
293-
limit=None, marker=None, search_opts={'all_tenants': False})
294+
limit=None, marker=None,
295+
search_opts={
296+
'all_tenants': False,
297+
'name': None,
298+
'status': None,
299+
'volume_id': None
300+
}
301+
)
294302
self.assertEqual(self.columns, columns)
295303
self.assertEqual(self.data, list(data))
296304

@@ -313,7 +321,12 @@ def test_snapshot_list_with_options(self):
313321
self.snapshots_mock.list.assert_called_once_with(
314322
limit=2,
315323
marker=self.snapshots[0].id,
316-
search_opts={'all_tenants': False}
324+
search_opts={
325+
'all_tenants': False,
326+
'name': None,
327+
'status': None,
328+
'volume_id': None
329+
}
317330
)
318331
self.assertEqual(self.columns_long, columns)
319332
self.assertEqual(self.data_long, list(data))
@@ -331,7 +344,89 @@ def test_snapshot_list_all_projects(self):
331344
columns, data = self.cmd.take_action(parsed_args)
332345

333346
self.snapshots_mock.list.assert_called_once_with(
334-
limit=None, marker=None, search_opts={'all_tenants': True})
347+
limit=None, marker=None,
348+
search_opts={
349+
'all_tenants': True,
350+
'name': None,
351+
'status': None,
352+
'volume_id': None
353+
}
354+
)
355+
self.assertEqual(self.columns, columns)
356+
self.assertEqual(self.data, list(data))
357+
358+
def test_snapshot_list_name_option(self):
359+
arglist = [
360+
'--name', self.snapshots[0].name,
361+
]
362+
verifylist = [
363+
('all_projects', False),
364+
('long', False),
365+
('name', self.snapshots[0].name),
366+
]
367+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
368+
369+
columns, data = self.cmd.take_action(parsed_args)
370+
371+
self.snapshots_mock.list.assert_called_once_with(
372+
limit=None, marker=None,
373+
search_opts={
374+
'all_tenants': False,
375+
'name': self.snapshots[0].name,
376+
'status': None,
377+
'volume_id': None
378+
}
379+
)
380+
self.assertEqual(self.columns, columns)
381+
self.assertEqual(self.data, list(data))
382+
383+
def test_snapshot_list_status_option(self):
384+
arglist = [
385+
'--status', self.snapshots[0].status,
386+
]
387+
verifylist = [
388+
('all_projects', False),
389+
('long', False),
390+
('status', self.snapshots[0].status),
391+
]
392+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
393+
394+
columns, data = self.cmd.take_action(parsed_args)
395+
396+
self.snapshots_mock.list.assert_called_once_with(
397+
limit=None, marker=None,
398+
search_opts={
399+
'all_tenants': False,
400+
'name': None,
401+
'status': self.snapshots[0].status,
402+
'volume_id': None
403+
}
404+
)
405+
self.assertEqual(self.columns, columns)
406+
self.assertEqual(self.data, list(data))
407+
408+
def test_snapshot_list_volumeid_option(self):
409+
arglist = [
410+
'--volume', self.volume.id,
411+
]
412+
verifylist = [
413+
('all_projects', False),
414+
('long', False),
415+
('volume', self.volume.id),
416+
]
417+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
418+
419+
columns, data = self.cmd.take_action(parsed_args)
420+
421+
self.snapshots_mock.list.assert_called_once_with(
422+
limit=None, marker=None,
423+
search_opts={
424+
'all_tenants': False,
425+
'name': None,
426+
'status': None,
427+
'volume_id': self.volume.id
428+
}
429+
)
335430
self.assertEqual(self.columns, columns)
336431
self.assertEqual(self.data, list(data))
337432

openstackclient/volume/v1/volume_snapshot.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,31 @@ def get_parser(self, prog_name):
135135
default=False,
136136
help=_('List additional fields in output'),
137137
)
138+
parser.add_argument(
139+
'--name',
140+
metavar='<name>',
141+
default=None,
142+
help=_('Filters results by a name.')
143+
)
144+
parser.add_argument(
145+
'--status',
146+
metavar='<status>',
147+
choices=['available', 'error', 'creating', 'deleting',
148+
'error-deleting'],
149+
help=_("Filters results by a status. "
150+
"('available', 'error', 'creating', 'deleting'"
151+
" or 'error-deleting')")
152+
)
153+
parser.add_argument(
154+
'--volume',
155+
metavar='<volume>',
156+
default=None,
157+
help=_('Filters results by a volume (name or ID).')
158+
)
138159
return parser
139160

140161
def take_action(self, parsed_args):
162+
volume_client = self.app.client_manager.volume
141163

142164
def _format_volume_id(volume_id):
143165
"""Return a volume name if available
@@ -169,17 +191,25 @@ def _format_volume_id(volume_id):
169191
# Cache the volume list
170192
volume_cache = {}
171193
try:
172-
for s in self.app.client_manager.volume.volumes.list():
194+
for s in volume_client.volumes.list():
173195
volume_cache[s.id] = s
174196
except Exception:
175197
# Just forget it if there's any trouble
176198
pass
177199

200+
volume_id = None
201+
if parsed_args.volume:
202+
volume_id = utils.find_resource(
203+
volume_client.volumes, parsed_args.volume).id
204+
178205
search_opts = {
179206
'all_tenants': parsed_args.all_projects,
207+
'display_name': parsed_args.name,
208+
'status': parsed_args.status,
209+
'volume_id': volume_id,
180210
}
181211

182-
data = self.app.client_manager.volume.volume_snapshots.list(
212+
data = volume_client.volume_snapshots.list(
183213
search_opts=search_opts)
184214
return (column_headers,
185215
(utils.get_item_properties(

0 commit comments

Comments
 (0)