diff --git a/designate/objects/quota.py b/designate/objects/quota.py index 0ad30094f..afe00784e 100644 --- a/designate/objects/quota.py +++ b/designate/objects/quota.py @@ -40,9 +40,7 @@ class QuotaList(base.ListObjectMixin, base.DesignateObject): @classmethod def from_dict(cls, _dict): - instance = cls() - for field, value in _dict.items(): item = cls.LIST_ITEM_TYPE() item.resource = field @@ -52,10 +50,7 @@ class QuotaList(base.ListObjectMixin, base.DesignateObject): return instance def to_dict(self): - _dict = {} - for quota in self.objects: _dict[quota.resource] = quota.hard_limit - return _dict diff --git a/designate/quota/impl_storage.py b/designate/quota/impl_storage.py index a98cb518d..1b7ad0b7d 100644 --- a/designate/quota/impl_storage.py +++ b/designate/quota/impl_storage.py @@ -59,32 +59,31 @@ class StorageQuota(base.Quota): context = context.deepcopy() context.all_tenants = True - def create_quota(): - quota = objects.Quota( - tenant_id=tenant_id, resource=resource, hard_limit=hard_limit) - - self.storage.create_quota(context, quota) - - def update_quota(quota): - quota.hard_limit = hard_limit - - self.storage.update_quota(context, quota) - if resource not in list(self.get_default_quotas(context).keys()): raise exceptions.QuotaResourceUnknown("%s is not a valid quota " "resource", resource) try: - create_quota() + self._create_quota(context, tenant_id, resource, hard_limit) except exceptions.Duplicate: quota = self.storage.find_quota(context, { 'tenant_id': tenant_id, 'resource': resource, }) - update_quota(quota) + self._update_quota(context, quota, hard_limit) return {resource: hard_limit} + def _create_quota(self, context, project_id, resource, hard_limit): + quota = objects.Quota( + tenant_id=project_id, resource=resource, hard_limit=hard_limit + ) + self.storage.create_quota(context, quota) + + def _update_quota(self, context, quota, hard_limit): + quota.hard_limit = hard_limit + self.storage.update_quota(context, quota) + @transaction def reset_quotas(self, context, tenant_id): context = context.deepcopy() diff --git a/designate/tests/unit/objects/test_quota.py b/designate/tests/unit/objects/test_quota.py new file mode 100644 index 000000000..d803897b3 --- /dev/null +++ b/designate/tests/unit/objects/test_quota.py @@ -0,0 +1,61 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +from oslo_log import log as logging +import oslotest.base + +from designate import objects + +LOG = logging.getLogger(__name__) + + +class QuotaTest(oslotest.base.BaseTestCase): + def test_quota(self): + quota = objects.Quota( + tenant_id='123', resource='dns', hard_limit=100 + ) + + self.assertEqual('123', quota.tenant_id) + self.assertEqual('dns', quota.resource) + self.assertEqual(100, quota.hard_limit) + + def test_quota_list(self): + quotas = objects.QuotaList() + quotas.append(objects.Quota(tenant_id='123', resource='dns1')) + quotas.append(objects.Quota(tenant_id='123', resource='dns2')) + quotas.append(objects.Quota(tenant_id='123', resource='dns3')) + + self.assertEqual('dns1', quotas[0].resource) + self.assertEqual('dns2', quotas[1].resource) + self.assertEqual('dns3', quotas[2].resource) + + def test_quota_list_from_dict(self): + quotas = objects.QuotaList().from_dict({ + 'zones': 100, + 'zone_recordsets': 101, + 'zone_records': 102, + 'recordset_records': 103, + 'api_export_size': 104, + }) + + self.assertEqual('zones', quotas[0].resource) + self.assertEqual(100, quotas[0].hard_limit) + self.assertEqual('api_export_size', quotas[4].resource) + self.assertEqual(104, quotas[4].hard_limit) + + def test_quota_list_to_dict(self): + quotas = objects.QuotaList().from_dict({ + 'zones': 100, + 'zone_recordsets': 101, + }) + + self.assertEqual(100, quotas.to_dict()['zones']) + self.assertEqual(101, quotas.to_dict()['zone_recordsets'])