From 6437166e1ea194f7466ad714245dd1e4a0403707 Mon Sep 17 00:00:00 2001 From: Seif Lotfy Date: Thu, 26 Sep 2013 21:48:17 +0000 Subject: [PATCH] Add quota-usage command Added a new command quota-usage to allow querying the usage of the quota for a given tenant Change-Id: I720e6df94f9fedbb8b6385bd1707600542aaea08 --- cinderclient/tests/v1/test_quotas.py | 2 +- cinderclient/tests/v2/test_quotas.py | 2 +- cinderclient/utils.py | 5 ++++- cinderclient/v1/quotas.py | 5 +++-- cinderclient/v1/shell.py | 26 ++++++++++++++++++++++++++ cinderclient/v2/quotas.py | 5 +++-- cinderclient/v2/shell.py | 26 ++++++++++++++++++++++++++ 7 files changed, 64 insertions(+), 7 deletions(-) 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 a0028a991..adcd33583 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 fbd76bdd9..97cd3cf4e 100644 --- a/cinderclient/v1/shell.py +++ b/cinderclient/v1/shell.py @@ -562,6 +562,7 @@ def do_credentials(cs, args): _quota_resources = ['volumes', 'snapshots', 'gigabytes'] +_quota_infos = ['Type', 'In_use', 'Reserved', 'Limit'] def _quota_show(quotas): @@ -577,6 +578,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: @@ -599,6 +616,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 09722104f..59beb513b 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 ce48744d3..247883fc5 100644 --- a/cinderclient/v2/shell.py +++ b/cinderclient/v2/shell.py @@ -622,6 +622,7 @@ def do_credentials(cs, args): _quota_resources = ['volumes', 'snapshots', 'gigabytes'] +_quota_infos = ['Type', 'In_use', 'Reserved', 'Limit'] def _quota_show(quotas): @@ -637,6 +638,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: @@ -660,6 +677,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.')