Merge "Cleaned up quota implementation"
This commit is contained in:
commit
6b5a989fbf
|
@ -46,21 +46,15 @@ class Quota(DriverPlugin, metaclass=abc.ABCMeta):
|
|||
if resources_exceeded:
|
||||
resources_exceeded.sort(key=len)
|
||||
raise exceptions.OverQuota(
|
||||
'Quota exceeded for %s.' %
|
||||
', '.join(resources_exceeded)
|
||||
'Quota exceeded for %s.' % ', '.join(resources_exceeded)
|
||||
)
|
||||
|
||||
def get_quotas(self, context, tenant_id):
|
||||
quotas = self.get_default_quotas(context)
|
||||
|
||||
quotas.update(self._get_quotas(context, tenant_id))
|
||||
|
||||
return quotas
|
||||
|
||||
@abc.abstractmethod
|
||||
def _get_quotas(self, context, tenant_id):
|
||||
pass
|
||||
|
||||
def get_default_quotas(self, context):
|
||||
return {
|
||||
'zones': CONF.quota_zones,
|
||||
|
@ -70,17 +64,18 @@ class Quota(DriverPlugin, metaclass=abc.ABCMeta):
|
|||
'api_export_size': CONF.quota_api_export_size,
|
||||
}
|
||||
|
||||
@abc.abstractmethod
|
||||
def _get_quotas(self, context, tenant_id):
|
||||
"""Internal Get Quotas used by get_quotas"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_quota(self, context, tenant_id, resource):
|
||||
quotas = self._get_quotas(context, tenant_id)
|
||||
|
||||
if resource not in quotas:
|
||||
raise exceptions.QuotaResourceUnknown("%s is not a valid quota "
|
||||
"resource" % resource)
|
||||
|
||||
return quotas[resource]
|
||||
"""Get Quota"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def set_quota(self, context, tenant_id, resource, hard_limit):
|
||||
raise NotImplementedError()
|
||||
"""Set Quota"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def reset_quotas(self, context, tenant_id):
|
||||
raise NotImplementedError()
|
||||
"""Reset Quotas"""
|
||||
|
|
|
@ -24,4 +24,14 @@ class NoopQuota(base.Quota):
|
|||
__plugin_name__ = 'noop'
|
||||
|
||||
def _get_quotas(self, context, tenant_id):
|
||||
"""Internal Get Quotas used by get_quotas"""
|
||||
return {}
|
||||
|
||||
def get_quota(self, context, tenant_id, resource):
|
||||
"""Get Quota"""
|
||||
|
||||
def set_quota(self, context, tenant_id, resource, hard_limit):
|
||||
"""Set Quota"""
|
||||
|
||||
def reset_quotas(self, context, tenant_id):
|
||||
"""Reset Quotas"""
|
||||
|
|
|
@ -33,6 +33,7 @@ class StorageQuota(base.Quota):
|
|||
self.storage = storage.get_storage()
|
||||
|
||||
def _get_quotas(self, context, tenant_id):
|
||||
"""Internal Get Quotas used by get_quotas"""
|
||||
quotas = self.storage.find_quotas(context, {
|
||||
'tenant_id': tenant_id,
|
||||
})
|
||||
|
@ -40,6 +41,7 @@ class StorageQuota(base.Quota):
|
|||
return {q['resource']: q['hard_limit'] for q in quotas}
|
||||
|
||||
def get_quota(self, context, tenant_id, resource):
|
||||
"""Get Quota"""
|
||||
context = context.deepcopy()
|
||||
context.all_tenants = True
|
||||
|
||||
|
@ -48,10 +50,13 @@ class StorageQuota(base.Quota):
|
|||
'resource': resource,
|
||||
})
|
||||
|
||||
return {resource: quota['hard_limit']}
|
||||
return {
|
||||
resource: quota['hard_limit']
|
||||
}
|
||||
|
||||
@transaction
|
||||
def set_quota(self, context, tenant_id, resource, hard_limit):
|
||||
"""Set Quota"""
|
||||
context = context.deepcopy()
|
||||
context.all_tenants = True
|
||||
|
||||
|
@ -83,6 +88,7 @@ class StorageQuota(base.Quota):
|
|||
|
||||
@transaction
|
||||
def reset_quotas(self, context, tenant_id):
|
||||
"""Reset Quotas"""
|
||||
context = context.deepcopy()
|
||||
context.all_tenants = True
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
from oslo_log import log as logging
|
||||
|
||||
from designate.common import constants
|
||||
from designate import exceptions
|
||||
from designate import quota
|
||||
from designate import tests
|
||||
|
||||
|
@ -74,6 +75,27 @@ class StorageQuotaTest(tests.TestCase):
|
|||
self.assertEqual(current_quota, quota['resource'])
|
||||
self.assertEqual(constants.MAX_QUOTA, quota['hard_limit'])
|
||||
|
||||
def test_get_quota(self):
|
||||
context = self.get_admin_context()
|
||||
context.all_tenants = True
|
||||
|
||||
for current_quota in constants.VALID_QUOTAS:
|
||||
self.quota.set_quota(context, 'tenant_id', current_quota, 1500)
|
||||
self.assertEqual(
|
||||
{current_quota: 1500},
|
||||
self.quota.get_quota(context, 'tenant_id', current_quota)
|
||||
)
|
||||
|
||||
def test_set_unknown_quota(self):
|
||||
context = self.get_admin_context()
|
||||
context.all_tenants = True
|
||||
|
||||
self.assertRaisesRegex(
|
||||
exceptions.QuotaResourceUnknown,
|
||||
'unknown is not a valid quota resource',
|
||||
self.quota.set_quota, context, 'tenant_id', 'unknown', 1500
|
||||
)
|
||||
|
||||
def test_set_quota_update(self):
|
||||
context = self.get_admin_context()
|
||||
context.all_tenants = True
|
||||
|
|
Loading…
Reference in New Issue