Skip to content

Commit 27e0be0

Browse files
author
jiahui.qiang
committed
Add --project and --project-domain option to "volume snapshot list"
Add "--project" and "--project-domain" option to volume v2's "volume snapshot list" command, it will filter list result by different project. Change-Id: I7dccd6d8d9f1889fa9cb0c2d04a42d77975c645b
1 parent 221cb53 commit 27e0be0

4 files changed

Lines changed: 56 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ List volume snapshots
8282
8383
openstack volume snapshot list
8484
[--all-projects]
85+
[--project <project> [--project-domain <project-domain>]]
8586
[--long]
8687
[--limit <limit>]
8788
[--marker <marker>]
@@ -93,6 +94,20 @@ List volume snapshots
9394
9495
Include all projects (admin only)
9596
97+
.. option:: --project <project>
98+
99+
Filter results by project (name or ID) (admin only)
100+
101+
*Volume version 2 only*
102+
103+
.. option:: --project-domain <project-domain>
104+
105+
Domain the project belongs to (name or ID).
106+
107+
This can be used in case collisions between project names exist.
108+
109+
*Volume version 2 only*
110+
96111
.. option:: --long
97112
98113
List additional fields in output

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from osc_lib import exceptions
2020
from osc_lib import utils
2121

22+
from openstackclient.tests.unit.identity.v3 import fakes as project_fakes
2223
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
2324
from openstackclient.volume.v2 import volume_snapshot
2425

@@ -32,6 +33,8 @@ def setUp(self):
3233
self.snapshots_mock.reset_mock()
3334
self.volumes_mock = self.app.client_manager.volume.volumes
3435
self.volumes_mock.reset_mock()
36+
self.project_mock = self.app.client_manager.identity.projects
37+
self.project_mock.reset_mock()
3538

3639

3740
class TestSnapshotCreate(TestSnapshot):
@@ -278,6 +281,7 @@ def test_delete_multiple_snapshots_with_exception(self):
278281
class TestSnapshotList(TestSnapshot):
279282

280283
volume = volume_fakes.FakeVolume.create_one_volume()
284+
project = project_fakes.FakeProject.create_one_project()
281285
snapshots = volume_fakes.FakeSnapshot.create_snapshots(
282286
attrs={'volume_id': volume.name}, count=3)
283287

@@ -321,6 +325,7 @@ def setUp(self):
321325

322326
self.volumes_mock.list.return_value = [self.volume]
323327
self.volumes_mock.get.return_value = self.volume
328+
self.project_mock.get.return_value = self.project
324329
self.snapshots_mock.list.return_value = self.snapshots
325330
# Get the command to test
326331
self.cmd = volume_snapshot.ListVolumeSnapshot(self.app, None)
@@ -341,6 +346,7 @@ def test_snapshot_list_without_options(self):
341346
'all_tenants': False,
342347
'name': None,
343348
'status': None,
349+
'project_id': None,
344350
'volume_id': None
345351
}
346352
)
@@ -351,11 +357,13 @@ def test_snapshot_list_with_options(self):
351357
arglist = [
352358
"--long",
353359
"--limit", "2",
360+
"--project", self.project.id,
354361
"--marker", self.snapshots[0].id,
355362
]
356363
verifylist = [
357364
("long", True),
358365
("limit", 2),
366+
("project", self.project.id),
359367
("marker", self.snapshots[0].id),
360368
('all_projects', False),
361369
]
@@ -367,7 +375,8 @@ def test_snapshot_list_with_options(self):
367375
limit=2,
368376
marker=self.snapshots[0].id,
369377
search_opts={
370-
'all_tenants': False,
378+
'all_tenants': True,
379+
'project_id': self.project.id,
371380
'name': None,
372381
'status': None,
373382
'volume_id': None
@@ -394,6 +403,7 @@ def test_snapshot_list_all_projects(self):
394403
'all_tenants': True,
395404
'name': None,
396405
'status': None,
406+
'project_id': None,
397407
'volume_id': None
398408
}
399409
)
@@ -419,6 +429,7 @@ def test_snapshot_list_name_option(self):
419429
'all_tenants': False,
420430
'name': self.snapshots[0].name,
421431
'status': None,
432+
'project_id': None,
422433
'volume_id': None
423434
}
424435
)
@@ -444,6 +455,7 @@ def test_snapshot_list_status_option(self):
444455
'all_tenants': False,
445456
'name': None,
446457
'status': self.snapshots[0].status,
458+
'project_id': None,
447459
'volume_id': None
448460
}
449461
)
@@ -469,6 +481,7 @@ def test_snapshot_list_volumeid_option(self):
469481
'all_tenants': False,
470482
'name': None,
471483
'status': None,
484+
'project_id': None,
472485
'volume_id': self.volume.id
473486
}
474487
)

openstackclient/volume/v2/volume_snapshot.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import six
2525

2626
from openstackclient.i18n import _
27+
from openstackclient.identity import common as identity_common
2728

2829

2930
LOG = logging.getLogger(__name__)
@@ -165,6 +166,12 @@ def get_parser(self, prog_name):
165166
default=False,
166167
help=_('Include all projects (admin only)'),
167168
)
169+
parser.add_argument(
170+
'--project',
171+
metavar='<project>',
172+
help=_('Filter results by project (name or ID) (admin only)')
173+
)
174+
identity_common.add_project_domain_option_to_parser(parser)
168175
parser.add_argument(
169176
'--long',
170177
action='store_true',
@@ -208,6 +215,7 @@ def get_parser(self, prog_name):
208215

209216
def take_action(self, parsed_args):
210217
volume_client = self.app.client_manager.volume
218+
identity_client = self.app.client_manager.identity
211219

212220
def _format_volume_id(volume_id):
213221
"""Return a volume name if available
@@ -245,8 +253,20 @@ def _format_volume_id(volume_id):
245253
volume_id = utils.find_resource(
246254
volume_client.volumes, parsed_args.volume).id
247255

256+
project_id = None
257+
if parsed_args.project:
258+
project_id = identity_common.find_project(
259+
identity_client,
260+
parsed_args.project,
261+
parsed_args.project_domain).id
262+
263+
# set value of 'all_tenants' when using project option
264+
all_projects = True if parsed_args.project else \
265+
parsed_args.all_projects
266+
248267
search_opts = {
249-
'all_tenants': parsed_args.all_projects,
268+
'all_tenants': all_projects,
269+
'project_id': project_id,
250270
'name': parsed_args.name,
251271
'status': parsed_args.status,
252272
'volume_id': volume_id,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``--project`` and ``--project-domain`` option to
5+
``volume snapshot list`` command, in order to filter list result
6+
by different project.

0 commit comments

Comments
 (0)