From 3c80b1b3b29307381b869f6a767e14e9d47e212f Mon Sep 17 00:00:00 2001 From: okozachenko Date: Thu, 10 Dec 2020 20:22:55 +0200 Subject: [PATCH] Add project field in image list subcommand The motivation is to filter the image by owner Change-Id: I1f08da175a06e62a844f76b0ec18cb3332efef86 --- openstackclient/image/v2/image.py | 15 +++++++++++++++ openstackclient/tests/unit/image/v2/test_image.py | 15 +++++++++++++++ ...e-project-filter-support-ed6204391e503adf.yaml | 5 +++++ 3 files changed, 35 insertions(+) create mode 100644 releasenotes/notes/add-image-project-filter-support-ed6204391e503adf.yaml diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index 58d92f5165..fa7f4be562 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -591,6 +591,12 @@ class ListImage(command.Lister): "The supported options are: %s. ") % ', '.join(MEMBER_STATUS_CHOICES)) ) + parser.add_argument( + '--project', + metavar='', + help=_("Search by project (admin only) (name or ID)") + ) + common.add_project_domain_option_to_parser(parser) parser.add_argument( '--tag', metavar='', @@ -636,6 +642,7 @@ class ListImage(command.Lister): return parser def take_action(self, parsed_args): + identity_client = self.app.client_manager.identity image_client = self.app.client_manager.image kwargs = {} @@ -659,6 +666,14 @@ class ListImage(command.Lister): kwargs['member_status'] = parsed_args.member_status if parsed_args.tag: kwargs['tag'] = parsed_args.tag + project_id = None + if parsed_args.project: + project_id = common.find_project( + identity_client, + parsed_args.project, + parsed_args.project_domain, + ).id + kwargs['owner'] = project_id if parsed_args.long: columns = ( 'ID', diff --git a/openstackclient/tests/unit/image/v2/test_image.py b/openstackclient/tests/unit/image/v2/test_image.py index b72e983599..ebeb8353a5 100644 --- a/openstackclient/tests/unit/image/v2/test_image.py +++ b/openstackclient/tests/unit/image/v2/test_image.py @@ -769,6 +769,21 @@ class TestImageList(TestImage): self.assertEqual(self.columns, columns) self.assertEqual(ret_limit, len(tuple(data))) + def test_image_list_project_option(self): + self.client.find_image = mock.Mock(return_value=self._image) + arglist = [ + '--project', 'nova', + ] + verifylist = [ + ('project', 'nova'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(self.columns, columns) + self.assertItemsEqual(self.datalist, tuple(data)) + @mock.patch('osc_lib.utils.find_resource') def test_image_list_marker_option(self, fr_mock): # tangchen: Since image_fakes.IMAGE is a dict, it cannot offer a .id diff --git a/releasenotes/notes/add-image-project-filter-support-ed6204391e503adf.yaml b/releasenotes/notes/add-image-project-filter-support-ed6204391e503adf.yaml new file mode 100644 index 0000000000..e909803c93 --- /dev/null +++ b/releasenotes/notes/add-image-project-filter-support-ed6204391e503adf.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``--project`` and ``--project-domain``options to ``image list`` + command to filter by owner.