Validate maximum limit for quota

Currently, when updating quota using limit larger than 2**31 -1,
there will be a DBError. The maximum limit needs to be validated.
And the maximum value is defined by SQL integer type INT, which
is a general value:
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
http://www.postgresql.org/docs/9.1/static/datatype-numeric.html

Change-Id: Idf890c57342378d0a71f6b18eb42425d463b13ef
Closes-bug: #1433052
This commit is contained in:
liyingjun 2015-03-17 21:16:35 +08:00
parent 6969f270c5
commit 19c01509ca
3 changed files with 19 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import webob
from nova.api.openstack import extensions
from nova.api.openstack import wsgi
import nova.context
from nova import db
from nova import exception
from nova.i18n import _
from nova import objects
@ -65,11 +66,16 @@ class QuotaSetsController(wsgi.Controller):
return dict(quota_set=result)
def _validate_quota_limit(self, resource, limit, minimum, maximum):
# NOTE: -1 is a flag value for unlimited
if limit < -1:
# NOTE: -1 is a flag value for unlimited, maximum value is limited
# by SQL standard integer type `INT` which is `0x7FFFFFFF`, it's a
# general value for SQL, using a hardcoded value here is not a
# `nice` way, but it seems like the only way for now:
# http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
# http://www.postgresql.org/docs/9.1/static/datatype-numeric.html
if limit < -1 or limit > db.MAX_INT:
msg = (_("Quota limit %(limit)s for %(resource)s "
"must be -1 or greater.") %
{'limit': limit, 'resource': resource})
"must be in the range of -1 and %(max)s.") %
{'limit': limit, 'resource': resource, 'max': db.MAX_INT})
raise webob.exc.HTTPBadRequest(explanation=msg)
def conv_inf(value):

View File

@ -15,12 +15,14 @@
import copy
from nova.api.validation import parameter_types
from nova import db
common_quota = {
'type': ['integer', 'string'],
'pattern': '^-?[0-9]+$',
# -1 is a flag value for unlimited
'minimum': -1
'minimum': -1,
'maximum': db.MAX_INT
}
quota_resources = {

View File

@ -22,6 +22,7 @@ import webob
from nova.api.openstack.compute.contrib import quotas as quotas_v2
from nova.api.openstack.compute.plugins.v3 import quota_sets as quotas_v21
from nova.api.openstack import extensions
from nova import db
from nova import exception
from nova import quota
from nova import test
@ -167,6 +168,11 @@ class QuotaSetsTestV21(BaseQuotaSetsTest):
self.controller._validate_quota_limit,
resource, 50, -1, -1)
# Invalid - limit is larger than 0x7FFFFFFF
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._validate_quota_limit,
resource, db.MAX_INT + 1, -1, -1)
def test_quotas_defaults(self):
uri = '/v2/fake_tenant/os-quota-sets/fake_tenant/defaults'