Merge "api: Simplify parameter types"
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
Common parameter types for validating request Body.
|
||||
|
||||
"""
|
||||
|
||||
import copy
|
||||
import functools
|
||||
import re
|
||||
@@ -27,7 +28,6 @@ _REGEX_RANGE_CACHE = {}
|
||||
|
||||
|
||||
def memorize(func):
|
||||
|
||||
@functools.wraps(func)
|
||||
def memorizer(*args, **kwargs):
|
||||
global _REGEX_RANGE_CACHE
|
||||
@@ -37,6 +37,7 @@ def memorize(func):
|
||||
value = func(*args, **kwargs)
|
||||
_REGEX_RANGE_CACHE[key] = value
|
||||
return value
|
||||
|
||||
return memorizer
|
||||
|
||||
|
||||
@@ -103,6 +104,7 @@ def _get_all_chars():
|
||||
# constraint fails and this causes issues for some unittests when
|
||||
# PYTHONHASHSEED is set randomly.
|
||||
|
||||
|
||||
@memorize
|
||||
def _build_regex_range(ws=True, invert=False, exclude=None):
|
||||
"""Build a range regex for a set of characters in utf8.
|
||||
@@ -139,8 +141,7 @@ def _build_regex_range(ws=True, invert=False, exclude=None):
|
||||
else:
|
||||
# Zs is the unicode class for space characters, of which
|
||||
# there are about 10 in this range.
|
||||
result = (_is_printable(char) and
|
||||
unicodedata.category(char) != "Zs")
|
||||
result = _is_printable(char) and unicodedata.category(char) != 'Zs'
|
||||
if invert is True:
|
||||
return not result
|
||||
return result
|
||||
@@ -165,7 +166,6 @@ def _build_regex_range(ws=True, invert=False, exclude=None):
|
||||
|
||||
valid_name_regex_base = '^(?![%s])[%s]*(?<![%s])$'
|
||||
|
||||
|
||||
valid_name_regex = ValidationRegex(
|
||||
valid_name_regex_base % (
|
||||
_build_regex_range(ws=False, invert=True),
|
||||
@@ -173,13 +173,11 @@ valid_name_regex = ValidationRegex(
|
||||
_build_regex_range(ws=False, invert=True)),
|
||||
_("printable characters. Can not start or end with whitespace."))
|
||||
|
||||
|
||||
# This regex allows leading/trailing whitespace
|
||||
valid_name_leading_trailing_spaces_regex_base = (
|
||||
"^[%(ws)s]*[%(no_ws)s]+[%(ws)s]*$|"
|
||||
"^[%(ws)s]*[%(no_ws)s][%(no_ws)s%(ws)s]+[%(no_ws)s][%(ws)s]*$")
|
||||
|
||||
|
||||
valid_az_name_regex = ValidationRegex(
|
||||
valid_name_regex_base % (
|
||||
_build_regex_range(ws=False, invert=True),
|
||||
@@ -188,7 +186,6 @@ valid_az_name_regex = ValidationRegex(
|
||||
_("printable characters except :."
|
||||
"Can not start or end with whitespace."))
|
||||
|
||||
|
||||
# az's name disallow ':'.
|
||||
valid_az_name_leading_trailing_spaces_regex = ValidationRegex(
|
||||
valid_name_leading_trailing_spaces_regex_base % {
|
||||
@@ -197,20 +194,15 @@ valid_az_name_leading_trailing_spaces_regex = ValidationRegex(
|
||||
_("printable characters except :, "
|
||||
"with at least one non space character"))
|
||||
|
||||
|
||||
valid_name_leading_trailing_spaces_regex = ValidationRegex(
|
||||
valid_name_leading_trailing_spaces_regex_base % {
|
||||
'ws': _build_regex_range(),
|
||||
'no_ws': _build_regex_range(ws=False)},
|
||||
_("printable characters with at least one non space character"))
|
||||
|
||||
|
||||
valid_description_regex_base = '^[%s]*$'
|
||||
|
||||
|
||||
valid_description_regex = valid_description_regex_base % (
|
||||
_build_regex_range())
|
||||
|
||||
valid_description_regex = valid_description_regex_base % (_build_regex_range())
|
||||
|
||||
boolean = {
|
||||
'type': ['boolean', 'string'],
|
||||
@@ -221,11 +213,6 @@ boolean = {
|
||||
}
|
||||
|
||||
|
||||
none = {
|
||||
'enum': ['None', None, {}]
|
||||
}
|
||||
|
||||
|
||||
name_or_none = {
|
||||
'oneOf': [
|
||||
{'type': 'string', 'minLength': 1, 'maxLength': 255},
|
||||
@@ -233,16 +220,18 @@ name_or_none = {
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
positive_integer = {
|
||||
'type': ['integer', 'string'],
|
||||
'pattern': '^[0-9]*$', 'minimum': 1, 'minLength': 1
|
||||
'pattern': '^[0-9]*$',
|
||||
'minimum': 1,
|
||||
'minLength': 1,
|
||||
}
|
||||
|
||||
|
||||
non_negative_integer = {
|
||||
'type': ['integer', 'string'],
|
||||
'pattern': '^[0-9]*$', 'minimum': 0, 'minLength': 1
|
||||
'pattern': '^[0-9]*$',
|
||||
'minimum': 0,
|
||||
'minLength': 1,
|
||||
}
|
||||
|
||||
# A host-specific or leaf label.
|
||||
@@ -272,79 +261,100 @@ non_negative_integer = {
|
||||
# handle host names of up to 255 characters.
|
||||
hostname = {
|
||||
'type': 'string',
|
||||
'pattern': '^[a-zA-Z0-9]+[a-zA-Z0-9-]*[a-zA-Z0-9]+$',
|
||||
'minLength': 2,
|
||||
'maxLength': 63,
|
||||
'pattern': '^[a-zA-Z0-9]+[a-zA-Z0-9-]*[a-zA-Z0-9]+$',
|
||||
}
|
||||
|
||||
fqdn = {
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'type': 'string',
|
||||
# NOTE: 'host' is defined in "services" table, and that
|
||||
# means a hostname. The hostname grammar in RFC952 does
|
||||
# not allow for underscores in hostnames. However, this
|
||||
# schema allows them, because it sometimes occurs in
|
||||
# real systems.
|
||||
'pattern': '^[a-zA-Z0-9-._]*$',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
|
||||
name = {
|
||||
# NOTE: Nova v2.1 API contains some 'name' parameters such
|
||||
# as server, flavor, aggregate and so on. They are
|
||||
# stored in the DB and Nova specific parameters.
|
||||
# This definition is used for all their parameters.
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'format': 'name'
|
||||
'type': 'string',
|
||||
'format': 'name',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
|
||||
az_name = {
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'format': 'az_name'
|
||||
'type': 'string',
|
||||
'format': 'az_name',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
keypair_name_special_chars = {
|
||||
'allOf': [
|
||||
name,
|
||||
{
|
||||
'type': 'string',
|
||||
'pattern': r'^[_\- a-zA-Z0-9]+$',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
keypair_name_special_chars = {'allOf': [name, {
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'format': 'keypair_name_20'
|
||||
}]}
|
||||
|
||||
|
||||
keypair_name_special_chars_292 = {'allOf': [name, {
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'format': 'keypair_name_292'
|
||||
}]}
|
||||
|
||||
keypair_name_special_chars_292 = {
|
||||
'allOf': [
|
||||
name,
|
||||
{
|
||||
'type': 'string',
|
||||
'pattern': r'^[_\-\@\. a-zA-Z0-9]+$',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
az_name_with_leading_trailing_spaces = {
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'format': 'az_name_with_leading_trailing_spaces'
|
||||
'type': 'string',
|
||||
'format': 'az_name_with_leading_trailing_spaces',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
|
||||
name_with_leading_trailing_spaces = {
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'format': 'name_with_leading_trailing_spaces'
|
||||
'type': 'string',
|
||||
'format': 'name_with_leading_trailing_spaces',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
|
||||
description = {
|
||||
'type': ['string', 'null'], 'minLength': 0, 'maxLength': 255,
|
||||
'type': ['string', 'null'],
|
||||
'pattern': valid_description_regex,
|
||||
'minLength': 0,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
|
||||
# TODO(stephenfin): This is no longer used and should be removed
|
||||
tcp_udp_port = {
|
||||
'type': ['integer', 'string'], 'pattern': '^[0-9]*$',
|
||||
'minimum': 0, 'maximum': 65535,
|
||||
'minLength': 1
|
||||
'type': ['integer', 'string'],
|
||||
'pattern': '^[0-9]*$',
|
||||
'minimum': 0,
|
||||
'maximum': 65535,
|
||||
'minLength': 1,
|
||||
}
|
||||
|
||||
|
||||
project_id = {
|
||||
'type': 'string', 'minLength': 1, 'maxLength': 255,
|
||||
'pattern': '^[a-zA-Z0-9-]*$'
|
||||
'type': 'string',
|
||||
'pattern': '^[a-zA-Z0-9-]*$',
|
||||
'minLength': 1,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
user_id = {
|
||||
@@ -355,12 +365,13 @@ user_id = {
|
||||
}
|
||||
|
||||
server_id = {
|
||||
'type': 'string', 'format': 'uuid'
|
||||
'type': 'string',
|
||||
'format': 'uuid',
|
||||
}
|
||||
|
||||
|
||||
image_id = {
|
||||
'type': 'string', 'format': 'uuid'
|
||||
'type': 'string',
|
||||
'format': 'uuid',
|
||||
}
|
||||
|
||||
share_id = {
|
||||
@@ -384,36 +395,36 @@ share_status = {
|
||||
image_id_or_empty_string = {
|
||||
'oneOf': [
|
||||
{'type': 'string', 'format': 'uuid'},
|
||||
{'type': 'string', 'maxLength': 0}
|
||||
{'type': 'string', 'maxLength': 0},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
volume_id = {
|
||||
'type': 'string', 'format': 'uuid'
|
||||
'type': 'string',
|
||||
'format': 'uuid',
|
||||
}
|
||||
|
||||
|
||||
attachment_id = {
|
||||
'type': 'string', 'format': 'uuid'
|
||||
'type': 'string',
|
||||
'format': 'uuid',
|
||||
}
|
||||
|
||||
|
||||
volume_type = {
|
||||
'type': ['string', 'null'], 'minLength': 0, 'maxLength': 255
|
||||
'type': ['string', 'null'],
|
||||
'minLength': 0,
|
||||
'maxLength': 255,
|
||||
}
|
||||
|
||||
|
||||
network_id = {
|
||||
'type': 'string', 'format': 'uuid'
|
||||
'type': 'string',
|
||||
'format': 'uuid',
|
||||
}
|
||||
|
||||
|
||||
network_port_id = {
|
||||
'type': 'string', 'format': 'uuid'
|
||||
'type': 'string',
|
||||
'format': 'uuid',
|
||||
}
|
||||
|
||||
|
||||
admin_password = {
|
||||
# NOTE: admin_password is the admin password of a server
|
||||
# instance, and it is not stored into nova's data base.
|
||||
@@ -423,12 +434,10 @@ admin_password = {
|
||||
'type': 'string',
|
||||
}
|
||||
|
||||
|
||||
flavor_ref = {
|
||||
'type': ['string', 'integer'], 'minLength': 1
|
||||
}
|
||||
|
||||
|
||||
metadata = {
|
||||
'type': 'object',
|
||||
'patternProperties': {
|
||||
@@ -439,18 +448,15 @@ metadata = {
|
||||
'additionalProperties': False
|
||||
}
|
||||
|
||||
|
||||
metadata_with_null = copy.deepcopy(metadata)
|
||||
metadata_with_null['patternProperties']['^[a-zA-Z0-9-_:. ]{1,255}$']['type'] =\
|
||||
['string', 'null']
|
||||
|
||||
|
||||
mac_address = {
|
||||
'type': 'string',
|
||||
'pattern': '^([0-9a-fA-F]{2})(:[0-9a-fA-F]{2}){5}$'
|
||||
}
|
||||
|
||||
|
||||
ip_address = {
|
||||
'type': 'string',
|
||||
'oneOf': [
|
||||
@@ -459,22 +465,18 @@ ip_address = {
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
ipv4 = {
|
||||
'type': 'string', 'format': 'ipv4'
|
||||
}
|
||||
|
||||
|
||||
ipv6 = {
|
||||
'type': 'string', 'format': 'ipv6'
|
||||
}
|
||||
|
||||
|
||||
cidr = {
|
||||
'type': 'string', 'format': 'cidr'
|
||||
}
|
||||
|
||||
|
||||
volume_size = {
|
||||
'type': ['integer', 'string'],
|
||||
'pattern': '^[0-9]+$',
|
||||
@@ -501,7 +503,6 @@ accessIPv6 = {
|
||||
|
||||
flavor_param_positive = copy.deepcopy(volume_size)
|
||||
|
||||
|
||||
flavor_param_non_negative = copy.deepcopy(volume_size)
|
||||
flavor_param_non_negative['minimum'] = 0
|
||||
|
||||
@@ -522,8 +523,9 @@ personality = {
|
||||
|
||||
tag = {
|
||||
"type": "string",
|
||||
"minLength": 1, "maxLength": tag.MAX_TAG_LENGTH,
|
||||
"pattern": "^[^,/]*$"
|
||||
"pattern": '^[^,/]*$',
|
||||
"minLength": 1,
|
||||
"maxLength": tag.MAX_TAG_LENGTH,
|
||||
}
|
||||
|
||||
pagination_parameters = {
|
||||
|
||||
@@ -17,7 +17,6 @@ Internal implementation of request Body validating middleware.
|
||||
"""
|
||||
|
||||
import re
|
||||
import string
|
||||
|
||||
import jsonschema
|
||||
from jsonschema import exceptions as jsonschema_exc
|
||||
@@ -194,28 +193,6 @@ def _validate_az_name(instance):
|
||||
raise exception.InvalidName(reason=regex.reason)
|
||||
|
||||
|
||||
@_FORMAT_CHECKER.checks('keypair_name_20',
|
||||
exception.InvalidName)
|
||||
def _validate_keypair_name_20(keypair_name):
|
||||
safe_chars = "_- " + string.digits + string.ascii_letters
|
||||
return _validate_keypair_name(keypair_name, safe_chars)
|
||||
|
||||
|
||||
@_FORMAT_CHECKER.checks('keypair_name_292',
|
||||
exception.InvalidName)
|
||||
def _validate_keypair_name_292(keypair_name):
|
||||
safe_chars = "@._- " + string.digits + string.ascii_letters
|
||||
return _validate_keypair_name(keypair_name, safe_chars)
|
||||
|
||||
|
||||
def _validate_keypair_name(keypair_name, safe_chars):
|
||||
clean_value = "".join(x for x in keypair_name if x in safe_chars)
|
||||
if clean_value != keypair_name:
|
||||
reason = _("Only expected characters: [%s]") % safe_chars
|
||||
raise exception.InvalidName(reason=reason)
|
||||
return True
|
||||
|
||||
|
||||
def _soft_validate_additional_properties(validator,
|
||||
additional_properties_value,
|
||||
instance,
|
||||
|
||||
@@ -76,7 +76,6 @@ def db_key_pair_create_duplicate(context):
|
||||
|
||||
class KeypairsTestV21(test.TestCase):
|
||||
base_url = '/v2/%s' % fakes.FAKE_PROJECT_ID
|
||||
validation_error = exception.ValidationError
|
||||
wsgi_api_version = os_wsgi.DEFAULT_API_VERSION
|
||||
|
||||
def _setup_app_and_controller(self):
|
||||
@@ -113,24 +112,12 @@ class KeypairsTestV21(test.TestCase):
|
||||
self.assertGreater(len(res_dict['keypair']['private_key']), 0)
|
||||
self._assert_keypair_type(res_dict)
|
||||
|
||||
def _test_keypair_create_bad_request_case(
|
||||
self, body, exception, error_msg=None
|
||||
):
|
||||
if error_msg:
|
||||
self.assertRaisesRegex(exception, error_msg,
|
||||
self.controller.create,
|
||||
self.req, body=body)
|
||||
else:
|
||||
self.assertRaises(exception,
|
||||
self.controller.create, self.req, body=body)
|
||||
|
||||
def test_keypair_create_with_empty_name(self):
|
||||
body = {'keypair': {'name': ''}}
|
||||
self._test_keypair_create_bad_request_case(
|
||||
body,
|
||||
self.validation_error,
|
||||
'(is too short)|' # jsonschema < 4.23.0
|
||||
'(should be non-empty)') # jsonschema >= 4.23.0
|
||||
self.assertRaisesRegex(
|
||||
exception.ValidationError,
|
||||
'(is too short)|(should be non-empty)', # jsonschema < / >= 4.23.0
|
||||
self.controller.create, self.req, body=body)
|
||||
|
||||
def test_keypair_create_with_name_too_long(self):
|
||||
body = {
|
||||
@@ -138,9 +125,9 @@ class KeypairsTestV21(test.TestCase):
|
||||
'name': 'a' * 256
|
||||
}
|
||||
}
|
||||
self._test_keypair_create_bad_request_case(body,
|
||||
self.validation_error,
|
||||
'is too long')
|
||||
self.assertRaisesRegex(
|
||||
exception.ValidationError, 'is too long',
|
||||
self.controller.create, self.req, body=body)
|
||||
|
||||
def test_keypair_create_with_name_leading_trailing_spaces(self):
|
||||
body = {
|
||||
@@ -149,9 +136,9 @@ class KeypairsTestV21(test.TestCase):
|
||||
}
|
||||
}
|
||||
expected_msg = 'Can not start or end with whitespace.'
|
||||
self._test_keypair_create_bad_request_case(body,
|
||||
self.validation_error,
|
||||
expected_msg)
|
||||
self.assertRaisesRegex(
|
||||
exception.ValidationError, expected_msg,
|
||||
self.controller.create, self.req, body=body)
|
||||
|
||||
def test_keypair_create_with_name_leading_trailing_spaces_compat_mode(
|
||||
self):
|
||||
@@ -166,10 +153,10 @@ class KeypairsTestV21(test.TestCase):
|
||||
'name': 'test/keypair'
|
||||
}
|
||||
}
|
||||
expected_msg = 'Only expected characters'
|
||||
self._test_keypair_create_bad_request_case(body,
|
||||
self.validation_error,
|
||||
expected_msg)
|
||||
expected_msg = 'Invalid input for field/attribute name.'
|
||||
self.assertRaisesRegex(
|
||||
exception.ValidationError, expected_msg,
|
||||
self.controller.create, self.req, body=body)
|
||||
|
||||
def test_keypair_create_with_special_characters(self):
|
||||
body = {
|
||||
@@ -177,10 +164,10 @@ class KeypairsTestV21(test.TestCase):
|
||||
'name': keypair_name_2_92_compatible
|
||||
}
|
||||
}
|
||||
expected_msg = 'Only expected characters'
|
||||
self._test_keypair_create_bad_request_case(body,
|
||||
self.validation_error,
|
||||
expected_msg)
|
||||
expected_msg = 'Invalid input for field/attribute name.'
|
||||
self.assertRaisesRegex(
|
||||
exception.ValidationError, expected_msg,
|
||||
self.controller.create, self.req, body=body)
|
||||
|
||||
def test_keypair_import_bad_key(self):
|
||||
body = {
|
||||
@@ -189,15 +176,16 @@ class KeypairsTestV21(test.TestCase):
|
||||
'public_key': 'ssh-what negative',
|
||||
},
|
||||
}
|
||||
self._test_keypair_create_bad_request_case(body,
|
||||
webob.exc.HTTPBadRequest)
|
||||
self.assertRaises(
|
||||
webob.exc.HTTPBadRequest, self.controller.create, self.req,
|
||||
body=body)
|
||||
|
||||
def test_keypair_create_with_invalid_keypair_body(self):
|
||||
body = {'alpha': {'name': 'create_test'}}
|
||||
expected_msg = "'keypair' is a required property"
|
||||
self._test_keypair_create_bad_request_case(body,
|
||||
self.validation_error,
|
||||
expected_msg)
|
||||
self.assertRaisesRegex(
|
||||
exception.ValidationError, expected_msg,
|
||||
self.controller.create, self.req, body=body)
|
||||
|
||||
def test_keypair_import(self):
|
||||
body = {
|
||||
|
||||
@@ -731,33 +731,6 @@ class NameWithLeadingTrailingSpacesTestCase(APIValidationTestCase):
|
||||
pass
|
||||
|
||||
|
||||
class NoneTypeTestCase(APIValidationTestCase):
|
||||
|
||||
post_schema = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'foo': parameter_types.none
|
||||
}
|
||||
}
|
||||
|
||||
def test_validate_none(self):
|
||||
self.post(body={'foo': 'None'}, req=FakeRequest())
|
||||
self.post(body={'foo': None}, req=FakeRequest())
|
||||
self.post(body={'foo': {}}, req=FakeRequest())
|
||||
|
||||
def test_validate_none_fails(self):
|
||||
detail = ("Invalid input for field/attribute foo. Value: ."
|
||||
" '' is not one of ['None', None, {}]")
|
||||
self.check_validation_error(self.post, body={'foo': ''},
|
||||
expected_detail=detail)
|
||||
|
||||
detail = ("Invalid input for field/attribute foo. Value: "
|
||||
"{'key': 'val'}. {'key': 'val'} is not one of "
|
||||
"['None', None, {}]")
|
||||
self.check_validation_error(self.post, body={'foo': {'key': 'val'}},
|
||||
expected_detail=detail)
|
||||
|
||||
|
||||
class NameOrNoneTestCase(APIValidationTestCase):
|
||||
|
||||
post_schema = {
|
||||
|
||||
Reference in New Issue
Block a user