From 825499fffc7a320466e976d2842e175c2d158c0e Mon Sep 17 00:00:00 2001 From: "Luis A. Garcia" Date: Thu, 17 Oct 2013 23:38:19 +0000 Subject: [PATCH] Enable non-ascii characters in flavor names The current implementation provided under commit e6b42d made it so that flavor names did not have special characters (e.g. *&^%$ etc. except for -._ and space) however it also limited names to only ascii letters. This patch makes it so that special characters are still not allowed, except the ones currently allowed, but it allows the use of non-ascii characters so that flavor names can be created in locales other than English. Change-Id: I056451a5e8b7a49f86e0a11d0011393dc769db34 Closes-bug: #1241625 --- nova/compute/flavors.py | 16 +++++++++++----- nova/tests/test_flavors.py | 10 ++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/nova/compute/flavors.py b/nova/compute/flavors.py index df6f382abb60..15b20dc65c28 100644 --- a/nova/compute/flavors.py +++ b/nova/compute/flavors.py @@ -49,7 +49,11 @@ CONF.register_opts(flavor_opts) LOG = logging.getLogger(__name__) -VALID_NAME_OR_ID_REGEX = re.compile("^[\w\.\- ]*$") +# NOTE(luisg): Flavor names can include non-ascii characters so that users can +# create flavor names in locales that use them, however flavor IDs are limited +# to ascii characters. +VALID_ID_REGEX = re.compile("^[\w\.\- ]*$") +VALID_NAME_REGEX = re.compile("^[\w\.\- ]*$", re.UNICODE) def _int_or_none(val): @@ -90,9 +94,10 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=0, flavorid=None, utils.check_string_length(name, 'name', min_length=1, max_length=255) # ensure name does not contain any special characters - valid_name = VALID_NAME_OR_ID_REGEX.search(name) + valid_name = VALID_NAME_REGEX.search(name) if not valid_name: - msg = _("names can only contain [a-zA-Z0-9_.- ]") + msg = _("Flavor names can only contain alphanumeric characters, " + "periods, dashes, underscores and spaces.") raise exception.InvalidInput(reason=msg) # NOTE(vish): Internally, flavorid is stored as a string but it comes @@ -109,9 +114,10 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=0, flavorid=None, max_length=255) # ensure flavor id does not contain any special characters - valid_flavor_id = VALID_NAME_OR_ID_REGEX.search(flavorid) + valid_flavor_id = VALID_ID_REGEX.search(flavorid) if not valid_flavor_id: - msg = _("id can only contain [a-zA-Z0-9_.- ]") + msg = _("Flavor id can only contain letters from A-Z (both cases), " + "periods, dashes, underscores and spaces.") raise exception.InvalidInput(reason=msg) # Some attributes are positive ( > 0) integers diff --git a/nova/tests/test_flavors.py b/nova/tests/test_flavors.py index 6823562c809a..4675dac43efe 100644 --- a/nova/tests/test_flavors.py +++ b/nova/tests/test_flavors.py @@ -392,16 +392,22 @@ class CreateInstanceTypeTest(test.TestCase): *create_args, **create_kwargs) def test_create_with_valid_name(self): - # Names can contain [a-zA-Z0-9_.- ] + # Names can contain alphanumeric and [_.- ] flavors.create('azAZ09. -_', 64, 1, 120) + # And they are not limited to ascii characters + # E.g.: m1.huge in simplified Chinese + flavors.create(u'm1.\u5DE8\u5927', 6400, 100, 12000) def test_name_with_special_characters(self): - # Names can contain [a-zA-Z0-9_.- ] + # Names can contain alphanumeric and [_.- ] flavors.create('_foo.bar-123', 64, 1, 120) # Ensure instance types raises InvalidInput for invalid characters. self.assertInvalidInput('foobar#', 64, 1, 120) + def test_non_ascii_name_with_special_characters(self): + self.assertInvalidInput(u'm1.\u5DE8\u5927 #', 64, 1, 120) + def test_name_length_checks(self): MAX_LEN = 255