From 1fefa254616df68fb13913c9a9272405d82b0efb Mon Sep 17 00:00:00 2001 From: gengjh Date: Thu, 28 Mar 2013 10:02:37 +0800 Subject: Show quota 'in_use' and 'reserved' info We need show all the quotas related info, including 'in_use' and 'reserved'--not just the limit--when executing 'nova-manage project quota'. Fix bug 1160752 Change-Id: I25e10e58c6af705e2189550ad762e6f39bca342e --- bin/nova-manage | 46 +++++++++++++++++++++++++++++------------- nova/tests/test_nova_manage.py | 3 ++- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/bin/nova-manage b/bin/nova-manage index f80660010..3bcccfcce 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -227,26 +227,44 @@ class ProjectCommands(object): @args('--key', metavar='', help='Key') @args('--value', metavar='', help='Value') def quota(self, project_id, key=None, value=None): - """Set or display quotas for project.""" + """ + Create, update or display quotas for project + + If no quota key is provided, the quota will be displayed. + If a valid quota key is provided and it does not exist, + it will be created. Otherwise, it will be updated. + """ + ctxt = context.get_admin_context() project_quota = QUOTAS.get_project_quotas(ctxt, project_id) - if key and key in project_quota: - if value.lower() == 'unlimited': - value = -1 - try: - db.quota_update(ctxt, project_id, key, value) - except exception.ProjectQuotaNotFound: - db.quota_create(ctxt, project_id, key, value) - else: - print _('%(key)s is not a valid quota key. Valid options are: ' - '%(options)s.') % {'key': key, - 'options': ', '.join(project_quota)} - sys.exit(2) + # if key is None, that means we need to show the quotas instead + # of updating them + if key: + if key in project_quota: + if value.lower() == 'unlimited': + value = -1 + try: + db.quota_update(ctxt, project_id, key, value) + except exception.ProjectQuotaNotFound: + db.quota_create(ctxt, project_id, key, value) + else: + print _('%(key)s is not a valid quota key. Valid options are: ' + '%(options)s.') % {'key': key, + 'options': ', '.join(project_quota)} + sys.exit(2) + print_format = "%-36s %-10s %-10s %-10s" + print print_format % ( + _('Quota'), + _('Limit'), + _('In Use'), + _('Reserved')) + # Retrieve the quota after update project_quota = QUOTAS.get_project_quotas(ctxt, project_id) for key, value in project_quota.iteritems(): if value['limit'] < 0 or value['limit'] is None: value['limit'] = 'unlimited' - print '%s: %s' % (key, value['limit']) + print print_format % (key, value['limit'], value['in_use'], + value['reserved']) @args('--project', dest='project_id', metavar='', help='Project name') diff --git a/nova/tests/test_nova_manage.py b/nova/tests/test_nova_manage.py index 49f9f3256..727746b18 100644 --- a/nova/tests/test_nova_manage.py +++ b/nova/tests/test_nova_manage.py @@ -366,7 +366,8 @@ class ProjectCommandsTestCase(test.TestCase): sys.stdout = sys.__stdout__ result = output.getvalue() - self.assertEquals(('instances: unlimited' in result), True) + print_format = "%-36s %-10s" % ('instances', 'unlimited') + self.assertEquals((print_format in result), True) def test_quota_update_invalid_key(self): self.assertRaises(SystemExit, -- cgit