From 8b5dcee15eb82cd81d604be94e8f395627af4120 Mon Sep 17 00:00:00 2001 From: liyingjun Date: Tue, 30 Jul 2013 10:18:59 +0800 Subject: [PATCH] Add user quota client API support Implements blueprint user-quota-related-client-api This patch adds user arguments to the following subcommands: * quota-show * quota-update * quota-delete Change-Id: I6556de366a758f7550e9b26357f231666caae419 --- novaclient/tests/v1_1/test_quotas.py | 22 +++++++++++++++++++++ novaclient/tests/v1_1/test_shell.py | 24 +++++++++++++++++++++++ novaclient/v1_1/quotas.py | 25 ++++++++++++++++++------ novaclient/v1_1/shell.py | 29 +++++++++++++++++++++------- 4 files changed, 87 insertions(+), 13 deletions(-) diff --git a/novaclient/tests/v1_1/test_quotas.py b/novaclient/tests/v1_1/test_quotas.py index ce1c0388..b545e6b1 100644 --- a/novaclient/tests/v1_1/test_quotas.py +++ b/novaclient/tests/v1_1/test_quotas.py @@ -26,6 +26,13 @@ class QuotaSetsTest(utils.TestCase): cs.quotas.get(tenant_id) cs.assert_called('GET', '/os-quota-sets/%s' % tenant_id) + def test_user_quotas_get(self): + tenant_id = 'test' + user_id = 'fake_user' + cs.quotas.get(tenant_id, user_id=user_id) + url = '/os-quota-sets/%s?user_id=%s' % (tenant_id, user_id) + cs.assert_called('GET', url) + def test_tenant_quotas_defaults(self): tenant_id = '97f4c221bff44578b0300df4ef119353' cs.quotas.defaults(tenant_id) @@ -37,6 +44,14 @@ class QuotaSetsTest(utils.TestCase): cs.assert_called('PUT', '/os-quota-sets/97f4c221bff44578b0300df4ef119353') + def test_update_user_quota(self): + tenant_id = '97f4c221bff44578b0300df4ef119353' + user_id = 'fake_user' + q = cs.quotas.get(tenant_id) + q.update(volumes=2, user_id=user_id) + url = '/os-quota-sets/%s?user_id=%s' % (tenant_id, user_id) + cs.assert_called('PUT', url) + def test_force_update_quota(self): q = cs.quotas.get('97f4c221bff44578b0300df4ef119353') q.update(cores=2, force=True) @@ -59,3 +74,10 @@ class QuotaSetsTest(utils.TestCase): tenant_id = 'test' cs.quotas.delete(tenant_id) cs.assert_called('DELETE', '/os-quota-sets/%s' % tenant_id) + + def test_user_quotas_delete(self): + tenant_id = 'test' + user_id = 'fake_user' + cs.quotas.delete(tenant_id, user_id=user_id) + url = '/os-quota-sets/%s?user_id=%s' % (tenant_id, user_id) + cs.assert_called('DELETE', url) diff --git a/novaclient/tests/v1_1/test_shell.py b/novaclient/tests/v1_1/test_shell.py index 1ca22c62..727a1f16 100644 --- a/novaclient/tests/v1_1/test_shell.py +++ b/novaclient/tests/v1_1/test_shell.py @@ -1213,6 +1213,12 @@ class ShellTest(utils.TestCase): self.assert_called('GET', '/os-quota-sets/97f4c221bff44578b0300df4ef119353') + def test_user_quota_show(self): + self.run_command('quota-show --tenant ' + '97f4c221bff44578b0300df4ef119353 --user u1') + self.assert_called('GET', + '/os-quota-sets/97f4c221bff44578b0300df4ef119353?user_id=u1') + def test_quota_show_no_tenant(self): self.run_command('quota-show') self.assert_called('GET', '/os-quota-sets/tenant_id') @@ -1237,6 +1243,17 @@ class ShellTest(utils.TestCase): {'quota_set': {'instances': 5, 'tenant_id': '97f4c221bff44578b0300df4ef119353'}}) + def test_user_quota_update(self): + self.run_command( + 'quota-update 97f4c221bff44578b0300df4ef119353' + ' --user=u1' + ' --instances=5') + self.assert_called( + 'PUT', + '/os-quota-sets/97f4c221bff44578b0300df4ef119353?user_id=u1', + {'quota_set': {'instances': 5, + 'tenant_id': '97f4c221bff44578b0300df4ef119353'}}) + def test_quota_force_update(self): self.run_command( 'quota-update 97f4c221bff44578b0300df4ef119353' @@ -1262,6 +1279,13 @@ class ShellTest(utils.TestCase): self.assert_called('DELETE', '/os-quota-sets/97f4c221bff44578b0300df4ef119353') + def test_user_quota_delete(self): + self.run_command('quota-delete --tenant ' + '97f4c221bff44578b0300df4ef119353 ' + '--user u1') + self.assert_called('DELETE', + '/os-quota-sets/97f4c221bff44578b0300df4ef119353?user_id=u1') + def test_quota_class_show(self): self.run_command('quota-class-show test') self.assert_called('GET', '/os-quota-class-sets/test') diff --git a/novaclient/v1_1/quotas.py b/novaclient/v1_1/quotas.py index e174c6f1..7c7ce54e 100644 --- a/novaclient/v1_1/quotas.py +++ b/novaclient/v1_1/quotas.py @@ -31,17 +31,22 @@ class QuotaSet(base.Resource): class QuotaSetManager(base.Manager): resource_class = QuotaSet - def get(self, tenant_id): + def get(self, tenant_id, user_id=None): if hasattr(tenant_id, 'tenant_id'): tenant_id = tenant_id.tenant_id - return self._get("/os-quota-sets/%s" % (tenant_id), "quota_set") + if user_id: + url = '/os-quota-sets/%s?user_id=%s' % (tenant_id, user_id) + else: + url = '/os-quota-sets/%s' % tenant_id + return self._get(url, "quota_set") def update(self, tenant_id, metadata_items=None, injected_file_content_bytes=None, injected_file_path_bytes=None, volumes=None, gigabytes=None, ram=None, floating_ips=None, fixed_ips=None, instances=None, injected_files=None, cores=None, key_pairs=None, - security_groups=None, security_group_rules=None, force=None): + security_groups=None, security_group_rules=None, force=None, + user_id=None): body = {'quota_set': { 'tenant_id': tenant_id, @@ -65,11 +70,19 @@ class QuotaSetManager(base.Manager): if body['quota_set'][key] is None: body['quota_set'].pop(key) - return self._update('/os-quota-sets/%s' % tenant_id, body, 'quota_set') + if user_id: + url = '/os-quota-sets/%s?user_id=%s' % (tenant_id, user_id) + else: + url = '/os-quota-sets/%s' % tenant_id + return self._update(url, body, 'quota_set') def defaults(self, tenant_id): return self._get('/os-quota-sets/%s/defaults' % tenant_id, 'quota_set') - def delete(self, tenant_id): - self._delete("/os-quota-sets/%s" % tenant_id) + def delete(self, tenant_id, user_id=None): + if user_id: + url = '/os-quota-sets/%s?user_id=%s' % (tenant_id, user_id) + else: + url = '/os-quota-sets/%s' % tenant_id + self._delete(url) diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 7358a87a..01ddadd2 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -2895,8 +2895,10 @@ def _quota_update(manager, identifier, args): # default value of force is None to make sure this client # will be compatibile with old nova server force_update = getattr(args, 'force', None) + user_id = getattr(args, 'user', None) if isinstance(manager, quotas.QuotaSetManager): - manager.update(identifier, force=force_update, **updates) + manager.update(identifier, force=force_update, user_id=user_id, + **updates) else: manager.update(identifier, **updates) @@ -2905,13 +2907,17 @@ def _quota_update(manager, identifier, args): metavar='', default=None, help='ID of tenant to list the quotas for.') +@utils.arg('--user', + metavar='', + default=None, + help='ID of user to list the quotas for.') def do_quota_show(cs, args): - """List the quotas for a tenant.""" + """List the quotas for a tenant/user.""" if not args.tenant: - _quota_show(cs.quotas.get(cs.client.tenant_id)) + _quota_show(cs.quotas.get(cs.client.tenant_id, user_id=args.user)) else: - _quota_show(cs.quotas.get(args.tenant)) + _quota_show(cs.quotas.get(args.tenant, user_id=args.user)) @utils.arg('--tenant', @@ -2930,6 +2936,10 @@ def do_quota_defaults(cs, args): @utils.arg('tenant', metavar='', help='ID of tenant to set the quotas for.') +@utils.arg('--user', + metavar='', + default=None, + help='ID of user to set the quotas for.') @utils.arg('--instances', metavar='', type=int, default=None, @@ -3014,7 +3024,7 @@ def do_quota_defaults(cs, args): help='Whether force update the quota even if the already used' ' and reserved exceeds the new quota') def do_quota_update(cs, args): - """Update the quotas for a tenant.""" + """Update the quotas for a tenant/user.""" _quota_update(cs.quotas, args.tenant, args) @@ -3022,10 +3032,15 @@ def do_quota_update(cs, args): @utils.arg('--tenant', metavar='', help='ID of tenant to delete quota for.') +@utils.arg('--user', + metavar='', + help='ID of user to delete quota for.') def do_quota_delete(cs, args): - """Delete quota for a tenant so their quota will revert back to default.""" + """Delete quota for a tenant/user so their quota will Revert + back to default. + """ - cs.quotas.delete(args.tenant) + cs.quotas.delete(args.tenant, user_id=args.user) @utils.arg('class_name',