From e0482b4d7d9dd654a93efc404d491629c1917117 Mon Sep 17 00:00:00 2001 From: Morgan Jones Date: Fri, 17 Jun 2016 15:47:17 -0400 Subject: [PATCH] Quota Management Modifies the management extension for Quotas to include usage information and update return values to current paradigms. Change-Id: I39122673082544e33bec0d3d5070df2c17aacb55 Depends-On: I6ca1a87fbc46781f83bc9b7bd33745783b8aaab1 Implements blueprint: quota-management --- .../notes/quota-management-3792cbc25ebe16bb.yaml | 4 ++++ trove/extensions/mgmt/quota/service.py | 7 +++++-- trove/extensions/mgmt/quota/views.py | 13 +++++++++++++ trove/quota/quota.py | 9 +++++++++ trove/tests/api/instances.py | 3 ++- 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/quota-management-3792cbc25ebe16bb.yaml diff --git a/releasenotes/notes/quota-management-3792cbc25ebe16bb.yaml b/releasenotes/notes/quota-management-3792cbc25ebe16bb.yaml new file mode 100644 index 0000000000..3cee10e055 --- /dev/null +++ b/releasenotes/notes/quota-management-3792cbc25ebe16bb.yaml @@ -0,0 +1,4 @@ +features: + - New quota management APIs for reviewing and changing + the quota for a particular tenant. Requires admin + privileges. diff --git a/trove/extensions/mgmt/quota/service.py b/trove/extensions/mgmt/quota/service.py index 0859a726da..8c0c8ff202 100644 --- a/trove/extensions/mgmt/quota/service.py +++ b/trove/extensions/mgmt/quota/service.py @@ -36,8 +36,11 @@ class QuotaController(wsgi.Controller): "req : '%(req)s'\n\n") % { "id": id, "req": req}) - quotas = quota_engine.get_all_quotas_by_tenant(id) - return wsgi.Result(views.QuotaView(quotas).data(), 200) + usages = quota_engine.get_all_quota_usages_by_tenant(id) + limits = quota_engine.get_all_quotas_by_tenant(id) + map(lambda r: setattr(usages[r], "limit", limits[r].hard_limit), + usages.keys()) + return wsgi.Result(views.QuotaUsageView(usages).data(), 200) @admin_context def update(self, req, body, tenant_id, id): diff --git a/trove/extensions/mgmt/quota/views.py b/trove/extensions/mgmt/quota/views.py index e723106383..1d8d341c8e 100644 --- a/trove/extensions/mgmt/quota/views.py +++ b/trove/extensions/mgmt/quota/views.py @@ -24,3 +24,16 @@ class QuotaView(object): for resource_name, quota in self.quotas.items(): rtn[resource_name] = quota.hard_limit return {'quotas': rtn} + + +class QuotaUsageView(object): + + def __init__(self, usages): + self.usages = usages + + def data(self): + return {'quotas': [{'resource': resource, + 'in_use': usage['in_use'], + 'reserved': usage['reserved'], + 'limit': usage['limit'] + } for resource, usage in self.usages.items()]} diff --git a/trove/quota/quota.py b/trove/quota/quota.py index eeb97eaffe..1cb48cd56e 100644 --- a/trove/quota/quota.py +++ b/trove/quota/quota.py @@ -274,6 +274,15 @@ class QuotaEngine(object): return self._driver.get_all_quotas_by_tenant(tenant_id, self._resources) + def get_all_quota_usages_by_tenant(self, tenant_id): + """Retrieve the quota usages for the given tenant. + + :param tenant_id: The ID of the tenant to return quota usages for. + """ + + return self._driver.get_all_quota_usages_by_tenant(tenant_id, + self._resources) + def check_quotas(self, tenant_id, **deltas): self._driver.check_quotas(tenant_id, self._resources, deltas) diff --git a/trove/tests/api/instances.py b/trove/tests/api/instances.py index 25515edf98..2d9d9416ea 100644 --- a/trove/tests/api/instances.py +++ b/trove/tests/api/instances.py @@ -277,7 +277,8 @@ class CreateInstanceQuotaTest(unittest.TestCase): new_quotas = dbaas_admin.quota.update(self.test_info.user.tenant_id, quota_dict) - verify_quota = dbaas_admin.quota.show(self.test_info.user.tenant_id) + set_quota = dbaas_admin.quota.show(self.test_info.user.tenant_id) + verify_quota = {q.resource: q.limit for q in set_quota} assert_equal(new_quotas['instances'], quota_dict['instances']) assert_equal(0, verify_quota['instances'])