Support quota show for current project

The "os quota show" command "<project/class>" 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
This commit is contained in:
Richard Theis 2016-04-21 11:33:24 -05:00
parent 0d3a06db1c
commit 27024d70af
5 changed files with 46 additions and 17 deletions

View File

@ -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]
<project>
[<project>]
.. option:: --default
@ -146,13 +146,13 @@ Show quotas for project
.. _quota_show-project:
.. describe:: <project>
Show quotas for class
Show quotas for this project (name or ID)
.. code:: bash
os quota show
--class
<class>
[<class>]
.. option:: --class
@ -161,4 +161,4 @@ Show quotas for project
.. _quota_show-class:
.. describe:: <class>
Class to show
Show quotas for this class (name or ID)

View File

@ -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)

View File

@ -145,7 +145,8 @@ class ShowQuota(command.ShowOne):
parser.add_argument(
'project',
metavar='<project/class>',
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 {}

View File

@ -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)

View File

@ -0,0 +1,6 @@
---
fixes:
- The ``quota show`` command ``<project/class>`` 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 <https://bugs.launchpad.net/bugs/1572733>`_]