[placement] Separate API schemas (trait)

In compute APIs, they have their schemas in the
independent directory (nova/api/openstack/compute/schemas).
Placement APIs should be like that as well.

This patch separates API schemas to an independent directory
(nova/api/openstack/placement/schemas)
from nova/api/openstack/placement/handlers/trait.py.

Subsequent patches will move schemas of other handlers.

Change-Id: Ieba7ca1e58d931c7cc0b11cb9313d37c5fb7cd3a
This commit is contained in:
Takashi NATSUME 2017-11-16 23:37:57 +09:00
parent 0060c0bd8d
commit 6daf9aea3f
2 changed files with 57 additions and 45 deletions

View File

@ -11,8 +11,6 @@
# under the License. # under the License.
"""Traits handlers for Placement API.""" """Traits handlers for Placement API."""
import copy
import jsonschema import jsonschema
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslo_utils import encodeutils from oslo_utils import encodeutils
@ -20,52 +18,13 @@ from oslo_utils import timeutils
import webob import webob
from nova.api.openstack.placement import microversion from nova.api.openstack.placement import microversion
from nova.api.openstack.placement.schemas import trait as schema
from nova.api.openstack.placement import util from nova.api.openstack.placement import util
from nova.api.openstack.placement import wsgi_wrapper from nova.api.openstack.placement import wsgi_wrapper
from nova import exception from nova import exception
from nova.i18n import _ from nova.i18n import _
from nova.objects import resource_provider as rp_obj from nova.objects import resource_provider as rp_obj
TRAIT = {
"type": "string",
'minLength': 1, 'maxLength': 255,
}
CUSTOM_TRAIT = copy.deepcopy(TRAIT)
CUSTOM_TRAIT.update({"pattern": "^CUSTOM_[A-Z0-9_]+$"})
PUT_TRAITS_SCHEMA = {
"type": "object",
"properties": {
"traits": {
"type": "array",
"items": CUSTOM_TRAIT,
}
},
'required': ['traits'],
'additionalProperties': False
}
SET_TRAITS_FOR_RP_SCHEMA = copy.deepcopy(PUT_TRAITS_SCHEMA)
SET_TRAITS_FOR_RP_SCHEMA['properties']['traits']['items'] = TRAIT
SET_TRAITS_FOR_RP_SCHEMA['properties'][
'resource_provider_generation'] = {'type': 'integer'}
SET_TRAITS_FOR_RP_SCHEMA['required'].append('resource_provider_generation')
LIST_TRAIT_SCHEMA = {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"associated": {
"type": "string",
}
},
"additionalProperties": False
}
def _normalize_traits_qs_param(qs): def _normalize_traits_qs_param(qs):
try: try:
@ -109,7 +68,7 @@ def put_trait(req):
name = util.wsgi_path_item(req.environ, 'name') name = util.wsgi_path_item(req.environ, 'name')
try: try:
jsonschema.validate(name, CUSTOM_TRAIT) jsonschema.validate(name, schema.CUSTOM_TRAIT)
except jsonschema.ValidationError: except jsonschema.ValidationError:
raise webob.exc.HTTPBadRequest( raise webob.exc.HTTPBadRequest(
_('The trait is invalid. A valid trait must be no longer than ' _('The trait is invalid. A valid trait must be no longer than '
@ -190,7 +149,7 @@ def list_traits(req):
filters = {} filters = {}
try: try:
jsonschema.validate(dict(req.GET), LIST_TRAIT_SCHEMA, jsonschema.validate(dict(req.GET), schema.LIST_TRAIT_SCHEMA,
format_checker=jsonschema.FormatChecker()) format_checker=jsonschema.FormatChecker())
except jsonschema.ValidationError as exc: except jsonschema.ValidationError as exc:
raise webob.exc.HTTPBadRequest( raise webob.exc.HTTPBadRequest(
@ -259,7 +218,7 @@ def update_traits_for_resource_provider(req):
context = req.environ['placement.context'] context = req.environ['placement.context']
want_version = req.environ[microversion.MICROVERSION_ENVIRON] want_version = req.environ[microversion.MICROVERSION_ENVIRON]
uuid = util.wsgi_path_item(req.environ, 'uuid') uuid = util.wsgi_path_item(req.environ, 'uuid')
data = util.extract_json(req.body, SET_TRAITS_FOR_RP_SCHEMA) data = util.extract_json(req.body, schema.SET_TRAITS_FOR_RP_SCHEMA)
rp_gen = data['resource_provider_generation'] rp_gen = data['resource_provider_generation']
traits = data['traits'] traits = data['traits']
resource_provider = rp_obj.ResourceProvider.get_by_uuid( resource_provider = rp_obj.ResourceProvider.get_by_uuid(

View File

@ -0,0 +1,53 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Trait schemas for Placement API."""
import copy
TRAIT = {
"type": "string",
'minLength': 1, 'maxLength': 255,
}
CUSTOM_TRAIT = copy.deepcopy(TRAIT)
CUSTOM_TRAIT.update({"pattern": "^CUSTOM_[A-Z0-9_]+$"})
PUT_TRAITS_SCHEMA = {
"type": "object",
"properties": {
"traits": {
"type": "array",
"items": CUSTOM_TRAIT,
}
},
'required': ['traits'],
'additionalProperties': False
}
SET_TRAITS_FOR_RP_SCHEMA = copy.deepcopy(PUT_TRAITS_SCHEMA)
SET_TRAITS_FOR_RP_SCHEMA['properties']['traits']['items'] = TRAIT
SET_TRAITS_FOR_RP_SCHEMA['properties'][
'resource_provider_generation'] = {'type': 'integer'}
SET_TRAITS_FOR_RP_SCHEMA['required'].append('resource_provider_generation')
LIST_TRAIT_SCHEMA = {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"associated": {
"type": "string",
}
},
"additionalProperties": False
}