diff --git a/cinderclient/tests/v1/test_quotas.py b/cinderclient/tests/v1/test_quotas.py index faff9f622..4751954e9 100644 --- a/cinderclient/tests/v1/test_quotas.py +++ b/cinderclient/tests/v1/test_quotas.py @@ -25,7 +25,7 @@ class QuotaSetsTest(utils.TestCase): def test_tenant_quotas_get(self): tenant_id = 'test' cs.quotas.get(tenant_id) - cs.assert_called('GET', '/os-quota-sets/%s' % tenant_id) + cs.assert_called('GET', '/os-quota-sets/%s?usage=False' % tenant_id) def test_tenant_quotas_defaults(self): tenant_id = 'test' diff --git a/cinderclient/tests/v2/test_quotas.py b/cinderclient/tests/v2/test_quotas.py index eb4531a32..5a61dbb49 100644 --- a/cinderclient/tests/v2/test_quotas.py +++ b/cinderclient/tests/v2/test_quotas.py @@ -25,7 +25,7 @@ class QuotaSetsTest(utils.TestCase): def test_tenant_quotas_get(self): tenant_id = 'test' cs.quotas.get(tenant_id) - cs.assert_called('GET', '/os-quota-sets/%s' % tenant_id) + cs.assert_called('GET', '/os-quota-sets/%s?usage=False' % tenant_id) def test_tenant_quotas_defaults(self): tenant_id = 'test' diff --git a/cinderclient/utils.py b/cinderclient/utils.py index 0fdcce9f6..7a728d72c 100644 --- a/cinderclient/utils.py +++ b/cinderclient/utils.py @@ -168,7 +168,10 @@ def print_list(objs, fields, formatters={}, order_by=None): field_name = field.replace(' ', '_') else: field_name = field.lower().replace(' ', '_') - data = getattr(o, field_name, '') + if type(o) == dict and field in o: + data = o[field] + else: + data = getattr(o, field_name, '') row.append(data) pt.add_row(row) diff --git a/cinderclient/v1/quotas.py b/cinderclient/v1/quotas.py index 87d34056c..dd0018621 100644 --- a/cinderclient/v1/quotas.py +++ b/cinderclient/v1/quotas.py @@ -32,10 +32,11 @@ class QuotaSet(base.Resource): class QuotaSetManager(base.Manager): resource_class = QuotaSet - def get(self, tenant_id): + def get(self, tenant_id, usage=False): if hasattr(tenant_id, 'tenant_id'): tenant_id = tenant_id.tenant_id - return self._get("/os-quota-sets/%s" % (tenant_id), "quota_set") + return self._get("/os-quota-sets/%s?usage=%s" % (tenant_id, usage), + "quota_set") def update(self, tenant_id, **updates): body = {'quota_set': {'tenant_id': tenant_id}} diff --git a/cinderclient/v1/shell.py b/cinderclient/v1/shell.py index ff7842bff..50da46833 100644 --- a/cinderclient/v1/shell.py +++ b/cinderclient/v1/shell.py @@ -580,6 +580,7 @@ def do_credentials(cs, args): _quota_resources = ['volumes', 'snapshots', 'gigabytes'] +_quota_infos = ['Type', 'In_use', 'Reserved', 'Limit'] def _quota_show(quotas): @@ -595,6 +596,22 @@ def _quota_show(quotas): utils.print_dict(quota_dict) +def _quota_usage_show(quotas): + quota_list = [] + for resource in quotas._info.keys(): + good_name = False + for name in _quota_resources: + if resource.startswith(name): + good_name = True + if not good_name: + continue + quota_info = getattr(quotas, resource, None) + quota_info['Type'] = resource + quota_info = dict((k.capitalize(), v) for k, v in quota_info.items()) + quota_list.append(quota_info) + utils.print_list(quota_list, _quota_infos) + + def _quota_update(manager, identifier, args): updates = {} for resource in _quota_resources: @@ -617,6 +634,15 @@ def do_quota_show(cs, args): _quota_show(cs.quotas.get(args.tenant)) +@utils.arg('tenant', metavar='', + help='UUID of tenant to list the quota usage for.') +@utils.service_type('volume') +def do_quota_usage(cs, args): + """List the quota usage for a tenant.""" + + _quota_usage_show(cs.quotas.get(args.tenant, usage=True)) + + @utils.arg('tenant', metavar='', help='UUID of tenant to list the default quotas for.') @utils.service_type('volume') diff --git a/cinderclient/v2/quotas.py b/cinderclient/v2/quotas.py index 73844604b..3a094ff74 100644 --- a/cinderclient/v2/quotas.py +++ b/cinderclient/v2/quotas.py @@ -30,10 +30,11 @@ class QuotaSet(base.Resource): class QuotaSetManager(base.Manager): resource_class = QuotaSet - def get(self, tenant_id): + def get(self, tenant_id, usage=False): if hasattr(tenant_id, 'tenant_id'): tenant_id = tenant_id.tenant_id - return self._get("/os-quota-sets/%s" % (tenant_id), "quota_set") + return self._get("/os-quota-sets/%s?usage=%s" % (tenant_id, usage), + "quota_set") def update(self, tenant_id, **updates): body = {'quota_set': {'tenant_id': tenant_id}} diff --git a/cinderclient/v2/shell.py b/cinderclient/v2/shell.py index 014dbf74b..231ea1a17 100644 --- a/cinderclient/v2/shell.py +++ b/cinderclient/v2/shell.py @@ -636,6 +636,7 @@ def do_credentials(cs, args): _quota_resources = ['volumes', 'snapshots', 'gigabytes'] +_quota_infos = ['Type', 'In_use', 'Reserved', 'Limit'] def _quota_show(quotas): @@ -651,6 +652,22 @@ def _quota_show(quotas): utils.print_dict(quota_dict) +def _quota_usage_show(quotas): + quota_list = [] + for resource in quotas._info.keys(): + good_name = False + for name in _quota_resources: + if resource.startswith(name): + good_name = True + if not good_name: + continue + quota_info = getattr(quotas, resource, None) + quota_info['Type'] = resource + quota_info = dict((k.capitalize(), v) for k, v in quota_info.items()) + quota_list.append(quota_info) + utils.print_list(quota_list, _quota_infos) + + def _quota_update(manager, identifier, args): updates = {} for resource in _quota_resources: @@ -674,6 +691,15 @@ def do_quota_show(cs, args): _quota_show(cs.quotas.get(args.tenant)) +@utils.arg('tenant', metavar='', + help='UUID of tenant to list the quota usage for.') +@utils.service_type('volumev2') +def do_quota_usage(cs, args): + """List the quota usage for a tenant.""" + + _quota_usage_show(cs.quotas.get(args.tenant, usage=True)) + + @utils.arg('tenant', metavar='', help='UUID of tenant to list the default quotas for.')