diff options
-rw-r--r-- | nova/api/openstack/compute/servers.py | 17 | ||||
-rw-r--r-- | nova/compute/instance_types.py | 3 | ||||
-rw-r--r-- | nova/tests/test_instance_types.py | 11 | ||||
-rw-r--r-- | nova/tests/test_utils.py | 15 | ||||
-rw-r--r-- | nova/utils.py | 22 |
5 files changed, 56 insertions, 12 deletions
diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 5858ad640..c10c6e1b3 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -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) diff --git a/nova/compute/instance_types.py b/nova/compute/instance_types.py index 045a24d4d..4cc5d5d4e 100644 --- a/nova/compute/instance_types.py +++ b/nova/compute/instance_types.py @@ -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: diff --git a/nova/tests/test_instance_types.py b/nova/tests/test_instance_types.py index 0829df2c6..6ae28a1c9 100644 --- a/nova/tests/test_instance_types.py +++ b/nova/tests/test_instance_types.py @@ -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' diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index aaa826a70..8fb173385 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -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) diff --git a/nova/utils.py b/nova/utils.py index 7ad810504..57f9433df 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -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) |