Merge "Check the length of flavor name in "flavor-create""
This commit is contained in:
commit
787e334a10
@ -563,18 +563,11 @@ class Controller(wsgi.Controller):
|
||||
return instance
|
||||
|
||||
def _check_string_length(self, value, name, max_length=None):
|
||||
if not isinstance(value, basestring):
|
||||
msg = _("%s is not a string or unicode") % name
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
if not value.strip():
|
||||
msg = _("%s is an empty string") % name
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
if max_length and len(value) > max_length:
|
||||
msg = _("%(name)s can be at most %(max_length)s "
|
||||
"characters.") % locals()
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
try:
|
||||
utils.check_string_length(value, name, min_length=1,
|
||||
max_length=max_length)
|
||||
except exception.InvalidInput as e:
|
||||
raise exc.HTTPBadRequest(explanation=str(e))
|
||||
|
||||
def _validate_server_name(self, value):
|
||||
self._check_string_length(value, 'Server name', max_length=255)
|
||||
|
@ -86,6 +86,9 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=None, flavorid=None,
|
||||
'rxtx_factor': rxtx_factor,
|
||||
}
|
||||
|
||||
# ensure name do not exceed 255 characters
|
||||
utils.check_string_length(name, 'name', min_length=1, max_length=255)
|
||||
|
||||
# ensure name does not contain any special characters
|
||||
invalid_name = INVALID_NAME_REGEX.search(name)
|
||||
if invalid_name:
|
||||
|
@ -142,6 +142,17 @@ class InstanceTypeTestCase(test.TestCase):
|
||||
self.assertRaises(exception.InvalidInput, instance_types.create,
|
||||
name, 256, 1, 120, 100, flavorid)
|
||||
|
||||
def test_instance_type_create_with_long_flavor_name(self):
|
||||
# Flavor name with 255 characters or less is valid.
|
||||
name = 'a' * 255
|
||||
inst_type = instance_types.create(name, 64, 1, 120, flavorid=11)
|
||||
self.assertEqual(inst_type['name'], name)
|
||||
|
||||
# Flavor name which is more than 255 characters will cause error.
|
||||
name = 'a' * 256
|
||||
self.assertRaises(exception.InvalidInput, instance_types.create,
|
||||
name, 64, 1, 120, flavorid=11)
|
||||
|
||||
def test_add_instance_type_access(self):
|
||||
user_id = 'fake'
|
||||
project_id = 'fake'
|
||||
|
@ -964,3 +964,18 @@ class GetCallArgsTestCase(test.TestCase):
|
||||
self.assertEqual(3, callargs['red'])
|
||||
self.assertTrue('blue' in callargs)
|
||||
self.assertEqual(None, callargs['blue'])
|
||||
|
||||
|
||||
class StringLengthTestCase(test.TestCase):
|
||||
def test_check_string_length(self):
|
||||
self.assertIsNone(utils.check_string_length(
|
||||
'test', 'name', max_length=255))
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
utils.check_string_length,
|
||||
11, 'name', max_length=255)
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
utils.check_string_length,
|
||||
'', 'name', min_length=1)
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
utils.check_string_length,
|
||||
'a' * 256, 'name', max_length=255)
|
||||
|
@ -1341,3 +1341,25 @@ class ExceptionHelper(object):
|
||||
except rpc_common.ClientException, e:
|
||||
raise (e._exc_info[1], None, e._exc_info[2])
|
||||
return wrapper
|
||||
|
||||
|
||||
def check_string_length(value, name, min_length=0, max_length=None):
|
||||
"""Check the length of specified string
|
||||
:param value: the value of the string
|
||||
:param name: the name of the string
|
||||
:param min_length: the min_length of the string
|
||||
:param max_length: the max_length of the string
|
||||
"""
|
||||
if not isinstance(value, basestring):
|
||||
msg = _("%s is not a string or unicode") % name
|
||||
raise exception.InvalidInput(message=msg)
|
||||
|
||||
if len(value) < min_length:
|
||||
msg = _("%(name)s has less than %(min_length)s "
|
||||
"characters.") % locals()
|
||||
raise exception.InvalidInput(message=msg)
|
||||
|
||||
if max_length and len(value) > max_length:
|
||||
msg = _("%(name)s has more than %(max_length)s "
|
||||
"characters.") % locals()
|
||||
raise exception.InvalidInput(message=msg)
|
||||
|
Loading…
x
Reference in New Issue
Block a user