From 3f7f526deca38c22f6c216cae7b6620841b3afd7 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Mon, 5 Nov 2012 22:40:50 +0800 Subject: [PATCH] Implements _validate_uuid Validates uuids with uuidutils.is_uuid_like Reordered validation funtions alphabetically Change-Id: I4c138d471c420ed520e6c881e07d36478119d12b --- quantum/api/v2/attributes.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/quantum/api/v2/attributes.py b/quantum/api/v2/attributes.py index 306df78ca..c6784084c 100644 --- a/quantum/api/v2/attributes.py +++ b/quantum/api/v2/attributes.py @@ -22,6 +22,8 @@ import re from quantum.common import exceptions as q_exc from quantum.openstack.common import log as logging +from quantum.openstack.common import uuidutils + LOG = logging.getLogger(__name__) @@ -111,6 +113,15 @@ def _validate_regex(data, valid_values=None): return msg +def _validate_uuid(data, valid_values=None): + if uuidutils.is_uuid_like(data): + return + else: + msg = _("%s is not a valid UUID") % data + LOG.debug("validate_uuid: %s", msg) + return msg + + def convert_to_boolean(data): try: i = int(data) @@ -170,13 +181,14 @@ MAC_PATTERN = "^%s[aceACE02468](:%s{2}){5}$" % (HEX_ELEM, HEX_ELEM) # Dictionary that maintains a list of validation functions validators = {'type:boolean': _validate_boolean, - 'type:values': _validate_values, - 'type:range': _validate_range, - 'type:mac_address': _validate_mac_address, 'type:ip_address': _validate_ip_address, 'type:ip_address_or_none': _validate_ip_address_or_none, + 'type:mac_address': _validate_mac_address, + 'type:range': _validate_range, + 'type:regex': _validate_regex, 'type:subnet': _validate_subnet, - 'type:regex': _validate_regex} + 'type:uuid': _validate_uuid, + 'type:values': _validate_values} # Note: a default of ATTR_NOT_SPECIFIED indicates that an # attribute is not required, but will be generated by the plugin @@ -204,7 +216,7 @@ validators = {'type:boolean': _validate_boolean, RESOURCE_ATTRIBUTE_MAP = { 'networks': { 'id': {'allow_post': False, 'allow_put': False, - 'validate': {'type:regex': UUID_PATTERN}, + 'validate': {'type:uuid': None}, 'is_visible': True}, 'name': {'allow_post': True, 'allow_put': True, 'default': '', 'is_visible': True}, @@ -232,13 +244,13 @@ RESOURCE_ATTRIBUTE_MAP = { }, 'ports': { 'id': {'allow_post': False, 'allow_put': False, - 'validate': {'type:regex': UUID_PATTERN}, + 'validate': {'type:uuid': None}, 'is_visible': True}, 'name': {'allow_post': True, 'allow_put': True, 'default': '', 'is_visible': True}, 'network_id': {'allow_post': True, 'allow_put': False, 'required_by_policy': True, - 'validate': {'type:regex': UUID_PATTERN}, + 'validate': {'type:uuid': None}, 'is_visible': True}, 'admin_state_up': {'allow_post': True, 'allow_put': True, 'default': True, @@ -269,7 +281,7 @@ RESOURCE_ATTRIBUTE_MAP = { }, 'subnets': { 'id': {'allow_post': False, 'allow_put': False, - 'validate': {'type:regex': UUID_PATTERN}, + 'validate': {'type:uuid': None}, 'is_visible': True}, 'name': {'allow_post': True, 'allow_put': True, 'default': '', 'is_visible': True}, @@ -279,7 +291,7 @@ RESOURCE_ATTRIBUTE_MAP = { 'is_visible': True}, 'network_id': {'allow_post': True, 'allow_put': False, 'required_by_policy': True, - 'validate': {'type:regex': UUID_PATTERN}, + 'validate': {'type:uuid': None}, 'is_visible': True}, 'cidr': {'allow_post': True, 'allow_put': False, 'validate': {'type:subnet': None}, -- 2.45.2