Skip to content

Commit

Permalink
Fix update quota with invalid value
Browse files Browse the repository at this point in the history
If update quota with a value which is not integer, it will response
with http status code 500. Catch the exception and continue for
updating other quotas.

Fix LP# 1111327

Change-Id: I0ab3b10ff106a2e6c7677d1acdb55c2f35c492db
  • Loading branch information
Zhou ShaoYu committed Feb 2, 2013
1 parent 194ec62 commit 43bd52b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
11 changes: 10 additions & 1 deletion nova/api/openstack/compute/contrib/quotas.py
Expand Up @@ -23,10 +23,12 @@
from nova import db
from nova.db.sqlalchemy import api as sqlalchemy_api
from nova import exception
from nova.openstack.common import log as logging
from nova import quota


QUOTAS = quota.QUOTAS
LOG = logging.getLogger(__name__)


authorize_update = extensions.extension_authorizer('compute', 'quotas:update')
Expand Down Expand Up @@ -88,7 +90,14 @@ def update(self, req, id, body):
project_id = id
for key in body['quota_set'].keys():
if key in QUOTAS:
value = int(body['quota_set'][key])
try:
value = int(body['quota_set'][key])
except (ValueError, TypeError):
LOG.warn(_("Quota for %s should be integer.") % key)
# NOTE(hzzhoushaoyu): Do not prevent valid value to be
# updated. If raise BadRequest, some may be updated and
# others may be not.
continue
self._validate_quota_limit(value)
try:
db.quota_update(context, project_id, key, value)
Expand Down
39 changes: 39 additions & 0 deletions nova/tests/api/openstack/compute/contrib/test_quotas.py
Expand Up @@ -143,6 +143,45 @@ def test_quotas_update_invalid_limit(self):
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
req, 'update_me', body)

def test_quotas_update_invalid_value(self):
expected_resp = {'quota_set': {
'instances': 50, 'cores': 50,
'ram': 51200, 'floating_ips': 10,
'metadata_items': 128, 'injected_files': 5,
'injected_file_content_bytes': 10240,
'injected_file_path_bytes': 255,
'security_groups': 10,
'security_group_rules': 20,
'key_pairs': 100}}

# when PUT JSON format with empty string for quota
body = {'quota_set': {'instances': 50, 'cores': 50,
'ram': '', 'floating_ips': 10,
'metadata_items': 128, 'injected_files': 5,
'injected_file_content_bytes': 10240,
'injected_file_path_bytes': 255,
'security_groups': 10,
'security_group_rules': 20,
'key_pairs': 100}}
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/update_me',
use_admin_context=True)
res_dict = self.controller.update(req, 'update_me', body)
self.assertEqual(res_dict, expected_resp)

# when PUT XML format with empty string for quota
body = {'quota_set': {'instances': 50, 'cores': 50,
'ram': {}, 'floating_ips': 10,
'metadata_items': 128, 'injected_files': 5,
'injected_file_content_bytes': 10240,
'injected_file_path_bytes': 255,
'security_groups': 10,
'security_group_rules': 20,
'key_pairs': 100}}
req = fakes.HTTPRequest.blank('/v2/fake4/os-quota-sets/update_me',
use_admin_context=True)
res_dict = self.controller.update(req, 'update_me', body)
self.assertEqual(res_dict, expected_resp)


class QuotaXMLSerializerTest(test.TestCase):
def setUp(self):
Expand Down

0 comments on commit 43bd52b

Please sign in to comment.