diff --git a/doc/source/cli/command-objects/project.rst b/doc/source/cli/command-objects/project.rst index 6891a79a48..ac7e8cd1ce 100644 --- a/doc/source/cli/command-objects/project.rst +++ b/doc/source/cli/command-objects/project.rst @@ -102,6 +102,7 @@ List projects openstack project list [--domain ] + [--parent ] [--user ] [--my-projects] [--long] @@ -115,6 +116,12 @@ List projects .. versionadded:: 3 +.. option:: --parent + + Filter projects whose parent is :option:`\ <--parent>` (name or ID) + + .. versionadded:: 3 + .. option:: --user Filter projects by :option:`\ <--user>` (name or ID) diff --git a/openstackclient/identity/v3/project.py b/openstackclient/identity/v3/project.py index e819a0a89b..073fb6df11 100644 --- a/openstackclient/identity/v3/project.py +++ b/openstackclient/identity/v3/project.py @@ -185,6 +185,11 @@ class ListProject(command.Lister): metavar='', help=_('Filter projects by (name or ID)'), ) + parser.add_argument( + '--parent', + metavar='', + help=_('Filter projects whose parent is (name or ID)'), + ) parser.add_argument( '--user', metavar='', @@ -226,6 +231,11 @@ class ListProject(command.Lister): parsed_args.domain).id kwargs['domain'] = domain_id + if parsed_args.parent: + parent_id = common.find_project(identity_client, + parsed_args.parent).id + kwargs['parent'] = parent_id + if parsed_args.user: if parsed_args.domain: user_id = utils.find_resource(identity_client.users, diff --git a/openstackclient/tests/unit/identity/v3/test_project.py b/openstackclient/tests/unit/identity/v3/test_project.py index 266da22775..db27fedcdf 100644 --- a/openstackclient/tests/unit/identity/v3/test_project.py +++ b/openstackclient/tests/unit/identity/v3/test_project.py @@ -645,6 +645,28 @@ class TestProjectList(TestProject): self.assertEqual(self.columns, columns) self.assertEqual(self.datalist, tuple(data)) + def test_project_list_parent(self): + self.parent = identity_fakes.FakeProject.create_one_project() + self.project = identity_fakes.FakeProject.create_one_project( + attrs={'domain_id': self.domain.id, 'parent_id': self.parent.id}) + + arglist = [ + '--parent', self.parent.id, + ] + verifylist = [ + ('parent', self.parent.id), + ] + + self.projects_mock.get.return_value = self.parent + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + self.projects_mock.list.assert_called_with(parent=self.parent.id) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.datalist, tuple(data)) + def test_project_list_sort(self): self.projects_mock.list.return_value = self.projects diff --git a/releasenotes/notes/add-parent-list-option-to-projects-10382a7176993366.yaml b/releasenotes/notes/add-parent-list-option-to-projects-10382a7176993366.yaml new file mode 100644 index 0000000000..fe1801179b --- /dev/null +++ b/releasenotes/notes/add-parent-list-option-to-projects-10382a7176993366.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``--parent`` option to ``project list`` command to filter projects + by the specified parent project.