From 27024d70af4756cb6e4b210b025ed7427541f773 Mon Sep 17 00:00:00 2001 From: Richard Theis Date: Thu, 21 Apr 2016 11:33:24 -0500 Subject: [PATCH] Support quota show for current project The "os quota show" command "" argument is now optional. If not specified, the user's current project is used. This allows non-admin users to show quotas for their current project. Change-Id: I602d4cc09c9d29ce84271eff78137f8810cb1a47 Closes-Bug: #1572733 --- doc/source/command-objects/quota.rst | 12 ++++---- functional/tests/common/test_quota.py | 5 ++++ openstackclient/common/quota.py | 29 ++++++++++++------- openstackclient/tests/common/test_quota.py | 11 +++++++ .../notes/bug-1572733-874b37a7fa8292d0.yaml | 6 ++++ 5 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 releasenotes/notes/bug-1572733-874b37a7fa8292d0.yaml diff --git a/doc/source/command-objects/quota.rst b/doc/source/command-objects/quota.rst index 98e6df3317..9e09bd484f 100644 --- a/doc/source/command-objects/quota.rst +++ b/doc/source/command-objects/quota.rst @@ -4,7 +4,7 @@ quota Resource quotas appear in multiple APIs, OpenStackClient presents them as a single object with multiple properties. -Compute v2, Block Storage v1 +Block Storage v1, Compute v2, Network v2 quota set --------- @@ -129,14 +129,14 @@ Set quotas for class quota show ---------- -Show quotas for project +Show quotas for project or class .. program:: quota show .. code:: bash os quota show [--default] - + [] .. option:: --default @@ -146,13 +146,13 @@ Show quotas for project .. _quota_show-project: .. describe:: - Show quotas for class + Show quotas for this project (name or ID) .. code:: bash os quota show --class - + [] .. option:: --class @@ -161,4 +161,4 @@ Show quotas for project .. _quota_show-class: .. describe:: - Class to show + Show quotas for this class (name or ID) diff --git a/functional/tests/common/test_quota.py b/functional/tests/common/test_quota.py index 62b43a34ab..22549efe8c 100644 --- a/functional/tests/common/test_quota.py +++ b/functional/tests/common/test_quota.py @@ -36,3 +36,8 @@ class QuotaTests(test.TestCase): raw_output = self.openstack('quota show ' + self.PROJECT_NAME) for expected_field in self.EXPECTED_FIELDS: self.assertIn(expected_field, raw_output) + + def test_quota_show_default_project(self): + raw_output = self.openstack('quota show') + for expected_field in self.EXPECTED_FIELDS: + self.assertIn(expected_field, raw_output) diff --git a/openstackclient/common/quota.py b/openstackclient/common/quota.py index b3d4c3b618..b497a44d62 100644 --- a/openstackclient/common/quota.py +++ b/openstackclient/common/quota.py @@ -145,7 +145,8 @@ class ShowQuota(command.ShowOne): parser.add_argument( 'project', metavar='', - help='Show this project or class (name/ID)', + nargs='?', + help='Show quotas for this project or class (name or ID)', ) type_group = parser.add_mutually_exclusive_group() type_group.add_argument( @@ -164,12 +165,22 @@ class ShowQuota(command.ShowOne): ) return parser + def _get_project(self, parsed_args): + if parsed_args.project is not None: + identity_client = self.app.client_manager.identity + project = utils.find_resource( + identity_client.projects, + parsed_args.project, + ).id + elif self.app.client_manager.auth_ref: + # Get the project from the current auth + project = self.app.client_manager.auth_ref.project_id + else: + project = None + return project + def get_compute_volume_quota(self, client, parsed_args): - identity_client = self.app.client_manager.identity - project = utils.find_resource( - identity_client.projects, - parsed_args.project, - ).id + project = self._get_project(parsed_args) try: if parsed_args.quota_class: @@ -189,11 +200,7 @@ class ShowQuota(command.ShowOne): if parsed_args.quota_class or parsed_args.default: return {} if self.app.client_manager.is_network_endpoint_enabled(): - identity_client = self.app.client_manager.identity - project = utils.find_resource( - identity_client.projects, - parsed_args.project, - ).id + project = self._get_project(parsed_args) return self.app.client_manager.network.get_quota(project) else: return {} diff --git a/openstackclient/tests/common/test_quota.py b/openstackclient/tests/common/test_quota.py index edf29c9b1f..ba7ee469c9 100644 --- a/openstackclient/tests/common/test_quota.py +++ b/openstackclient/tests/common/test_quota.py @@ -59,6 +59,7 @@ class TestQuota(compute_fakes.TestComputev2): self.service_catalog_mock = \ self.app.client_manager.auth_ref.service_catalog self.service_catalog_mock.reset_mock() + self.app.client_manager.auth_ref.project_id = identity_fakes.project_id class TestQuotaSet(TestQuota): @@ -304,3 +305,13 @@ class TestQuotaShow(TestQuota): identity_fakes.project_id) self.volume_quotas_class_mock.get.assert_called_with( identity_fakes.project_id) + + def test_quota_show_no_project(self): + parsed_args = self.check_parser(self.cmd, [], []) + + self.cmd.take_action(parsed_args) + + self.quotas_mock.get.assert_called_with(identity_fakes.project_id) + self.volume_quotas_mock.get.assert_called_with( + identity_fakes.project_id) + self.network.get_quota.assert_called_with(identity_fakes.project_id) diff --git a/releasenotes/notes/bug-1572733-874b37a7fa8292d0.yaml b/releasenotes/notes/bug-1572733-874b37a7fa8292d0.yaml new file mode 100644 index 0000000000..3ddf493705 --- /dev/null +++ b/releasenotes/notes/bug-1572733-874b37a7fa8292d0.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - The ``quota show`` command ```` argument is now + optional. If not specified, the user's current project is used. + This allows non-admin users to show quotas for their current project. + [Bug `1572733 `_]