Skip to content

Commit

Permalink
Fixes per tenant quota doesn't work
Browse files Browse the repository at this point in the history
Fixes bug 1101331

Change-Id: I9c4e88b437506ed5818ee56ff6fd70588cb0f965
  • Loading branch information
soulxu committed Jan 24, 2013
1 parent 200f9d5 commit e1006d9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
14 changes: 6 additions & 8 deletions quantum/api/v2/base.py
Expand Up @@ -40,8 +40,6 @@
ValueError: webob.exc.HTTPBadRequest,
}

QUOTAS = quota.QUOTAS


def _fields(request):
"""
Expand Down Expand Up @@ -284,9 +282,9 @@ def create(self, request, body=None, **kwargs):
plugin=self._plugin)
try:
tenant_id = item[self._resource]['tenant_id']
count = QUOTAS.count(request.context, self._resource,
self._plugin, self._collection,
tenant_id)
count = quota.QUOTAS.count(request.context, self._resource,
self._plugin, self._collection,
tenant_id)
if bulk:
delta = deltas.get(tenant_id, 0) + 1
deltas[tenant_id] = delta
Expand All @@ -297,9 +295,9 @@ def create(self, request, body=None, **kwargs):
# We don't want to quota this resource
LOG.debug(e)
else:
QUOTAS.limit_check(request.context,
item[self._resource]['tenant_id'],
**kwargs)
quota.QUOTAS.limit_check(request.context,
item[self._resource]['tenant_id'],
**kwargs)

def notify(create_result):
notifier_api.notify(request.context,
Expand Down
2 changes: 1 addition & 1 deletion quantum/db/quota_db.py
Expand Up @@ -145,7 +145,7 @@ def _get_quotas(self, context, tenant_id, resources, keys):
quotas = DbQuotaDriver.get_tenant_quotas(
context, sub_resources, context.tenant_id)

return dict((k, v['limit']) for k, v in quotas.items())
return dict((k, v) for k, v in quotas.items())

def limit_check(self, context, tenant_id, resources, values):
"""Check simple quota limits.
Expand Down
13 changes: 8 additions & 5 deletions quantum/quota.py
Expand Up @@ -282,9 +282,12 @@ def _count_resource(context, plugin, resources, tenant_id):
return len(obj_list) if obj_list else 0


resources = []
for resource_item in cfg.CONF.QUOTAS.quota_items:
resources.append(CountableResource(resource_item, _count_resource,
'quota_' + resource_item))
def register_resources_from_config():
resources = []
for resource_item in cfg.CONF.QUOTAS.quota_items:
resources.append(CountableResource(resource_item, _count_resource,
'quota_' + resource_item))
QUOTAS.register_resources(resources)

QUOTAS.register_resources(resources)

register_resources_from_config()
38 changes: 36 additions & 2 deletions quantum/tests/unit/test_quota_per_tenant_ext.py
@@ -1,11 +1,12 @@
import unittest
import unittest2 as unittest
import webtest

import mock

from quantum.api import extensions
from quantum.api.v2 import attributes
from quantum.common import config
from quantum.common import exceptions
from quantum import context
from quantum.db import api as db
from quantum import manager
Expand Down Expand Up @@ -53,7 +54,8 @@ def setUp(self):
'quota_items',
['network', 'subnet', 'port', 'extra1'],
group='QUOTAS')

quota.QUOTAS = quota.QuotaEngine()
quota.register_resources_from_config()
self._plugin_patcher = mock.patch(TARGET_PLUGIN, autospec=True)
self.plugin = self._plugin_patcher.start()
self.plugin.return_value.supported_extension_aliases = ['quotas']
Expand Down Expand Up @@ -153,3 +155,35 @@ def test_quotas_loaded_bad(self):
self.assertEqual(404, res.status_int)
except Exception:
pass

def test_quotas_limit_check(self):
tenant_id = 'tenant_id1'
env = {'quantum.context': context.Context('', tenant_id,
is_admin=True)}
quotas = {'quota': {'network': 5}}
res = self.api.put_json(_get_path('quotas', id=tenant_id, fmt='json'),
quotas, extra_environ=env)
self.assertEqual(200, res.status_int)
quota.QUOTAS.limit_check(context.Context('', tenant_id),
tenant_id,
network=4)

def test_quotas_limit_check_with_over_quota(self):
tenant_id = 'tenant_id1'
env = {'quantum.context': context.Context('', tenant_id,
is_admin=True)}
quotas = {'quota': {'network': 5}}
res = self.api.put_json(_get_path('quotas', id=tenant_id, fmt='json'),
quotas, extra_environ=env)
self.assertEqual(200, res.status_int)
with self.assertRaises(exceptions.OverQuota):
quota.QUOTAS.limit_check(context.Context('', tenant_id),
tenant_id,
network=6)

def test_quotas_limit_check_with_invalid_quota_value(self):
tenant_id = 'tenant_id1'
with self.assertRaises(exceptions.InvalidQuotaValue):
quota.QUOTAS.limit_check(context.Context('', tenant_id),
tenant_id,
network=-1)

0 comments on commit e1006d9

Please sign in to comment.