Add schema to validate query parameter

Added schema to validate following query parameters:-

1. filter
2. all_fields
3. exclude_fields
4. fields
5. exclude_default

Business rules are defined in ETSI GS NFV-SOL 013 V2.6.1 (2019-03),
section 5.2 and 5.3.

Change-Id: I01cf08f46ffd69d10f5afde430c42c4bc211bd25
Implements: bp/enhance-vnf-package-support-part1
This commit is contained in:
tpatil 2020-03-30 04:37:51 +00:00
parent 7fb68faeda
commit 9c74c42a4b
4 changed files with 76 additions and 1 deletions

View File

@ -67,3 +67,15 @@ patch = {
{'required': ['userDefinedData']}],
'additionalProperties': False
}
query_params_v1 = {
'type': 'object',
"properties": {
'filter': {'type': 'string', 'minLength': 1},
'exclude_fields': {'type': 'string', 'minLength': 1},
'fields': {'type': 'string', 'minLength': 1},
'all_fields': {'format': 'all_fields'},
'exclude_default': {'format': 'exclude_default'},
},
'additionalProperties': False,
}

View File

@ -22,6 +22,7 @@ import functools
import webob
from tacker.api.validation import validators
from tacker.common import exceptions
def schema(request_body_schema):
@ -49,3 +50,42 @@ def schema(request_body_schema):
return wrapper
return add_validator
def query_schema(query_params_schema):
"""Register a schema to validate request query parameters.
Registered schema will be used for validating request query params just
before API method executing.
:param query_params_schema: A dict, the JSON-Schema for validating the
query parameters.
"""
def add_validator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# NOTE(tpatil): The second argument of the method
# calling this method should always be 'request'.
if 'request' in kwargs:
req = kwargs['request']
else:
req = args[1]
try:
req.GET.dict_of_lists()
except UnicodeDecodeError:
msg = _('Query string is not UTF-8 encoded')
raise exceptions.ValidationError(msg)
query_opts = {}
query_opts.update(req.GET)
schema_validator = validators._SchemaValidator(
query_params_schema)
schema_validator.validate(query_opts)
return func(*args, **kwargs)
return wrapper
return add_validator

View File

@ -55,6 +55,28 @@ def validate_mac_address_or_none(instance):
return True
def _validate_query_parameter_without_value(parameter_name, instance):
"""The query parameter is a flag without a value."""
if not (isinstance(instance, six.text_type) and len(instance)):
return True
msg = _("The parameter '%s' is a flag. It shouldn't contain any value.")
raise webob.exc.HTTPBadRequest(explanation=msg % parameter_name)
@jsonschema.FormatChecker.cls_checks('all_fields',
webob.exc.HTTPBadRequest)
def _validate_all_fields_query_parameter(instance):
return _validate_query_parameter_without_value('all_fields', instance)
@jsonschema.FormatChecker.cls_checks('exclude_default',
webob.exc.HTTPBadRequest)
def _validate_exclude_default_query_parameter(instance):
return _validate_query_parameter_without_value('exclude_default',
instance)
class FormatChecker(jsonschema.FormatChecker):
"""A FormatChecker can output the message from cause exception

View File

@ -108,7 +108,8 @@ class VnfPkgmController(wsgi.Controller):
return self._view_builder.show(request, vnf_package)
@wsgi.response(http_client.OK)
@wsgi.expected_errors((http_client.FORBIDDEN))
@wsgi.expected_errors((http_client.BAD_REQUEST, http_client.FORBIDDEN))
@validation.query_schema(vnf_packages.query_params_v1)
def index(self, request):
context = request.environ['tacker.context']
context.can(vnf_package_policies.VNFPKGM % 'index')