Skip to content

Commit

Permalink
Merge "[placement] Separate API schemas (inventory)"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed Dec 17, 2017
2 parents eabd1c1 + 432aeae commit 07c925a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 74 deletions.
78 changes: 4 additions & 74 deletions nova/api/openstack/placement/handlers/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,84 +19,14 @@
import webob

from nova.api.openstack.placement import microversion
from nova.api.openstack.placement.schemas import inventory as schema
from nova.api.openstack.placement import util
from nova.api.openstack.placement import wsgi_wrapper
from nova import db
from nova import exception
from nova.i18n import _
from nova.objects import resource_provider as rp_obj

RESOURCE_CLASS_IDENTIFIER = "^[A-Z0-9_]+$"
BASE_INVENTORY_SCHEMA = {
"type": "object",
"properties": {
"resource_provider_generation": {
"type": "integer"
},
"total": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1,
},
"reserved": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 0,
},
"min_unit": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1
},
"max_unit": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1
},
"step_size": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1
},
"allocation_ratio": {
"type": "number",
"maximum": db.SQL_SP_FLOAT_MAX
},
},
"required": [
"total",
"resource_provider_generation"
],
"additionalProperties": False
}
POST_INVENTORY_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
POST_INVENTORY_SCHEMA['properties']['resource_class'] = {
"type": "string",
"pattern": RESOURCE_CLASS_IDENTIFIER,
}
POST_INVENTORY_SCHEMA['required'].append('resource_class')
POST_INVENTORY_SCHEMA['required'].remove('resource_provider_generation')
PUT_INVENTORY_RECORD_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
PUT_INVENTORY_RECORD_SCHEMA['required'].remove('resource_provider_generation')
PUT_INVENTORY_SCHEMA = {
"type": "object",
"properties": {
"resource_provider_generation": {
"type": "integer"
},
"inventories": {
"type": "object",
"patternProperties": {
RESOURCE_CLASS_IDENTIFIER: PUT_INVENTORY_RECORD_SCHEMA,
}
}
},
"required": [
"resource_provider_generation",
"inventories"
],
"additionalProperties": False
}

# NOTE(cdent): We keep our own representation of inventory defaults
# and output fields, separate from the versioned object to avoid
Expand Down Expand Up @@ -229,7 +159,7 @@ def create_inventory(req):
uuid = util.wsgi_path_item(req.environ, 'uuid')
resource_provider = rp_obj.ResourceProvider.get_by_uuid(
context, uuid)
data = _extract_inventory(req.body, POST_INVENTORY_SCHEMA)
data = _extract_inventory(req.body, schema.POST_INVENTORY_SCHEMA)
resource_class = data.pop('resource_class')

inventory = _make_inventory_object(resource_provider,
Expand Down Expand Up @@ -361,7 +291,7 @@ def set_inventories(req):
resource_provider = rp_obj.ResourceProvider.get_by_uuid(
context, uuid)

data = _extract_inventories(req.body, PUT_INVENTORY_SCHEMA)
data = _extract_inventories(req.body, schema.PUT_INVENTORY_SCHEMA)
if data['resource_provider_generation'] != resource_provider.generation:
raise webob.exc.HTTPConflict(
_('resource provider generation conflict'))
Expand Down Expand Up @@ -456,7 +386,7 @@ def update_inventory(req):
resource_provider = rp_obj.ResourceProvider.get_by_uuid(
context, uuid)

data = _extract_inventory(req.body, BASE_INVENTORY_SCHEMA)
data = _extract_inventory(req.body, schema.BASE_INVENTORY_SCHEMA)
if data['resource_provider_generation'] != resource_provider.generation:
raise webob.exc.HTTPConflict(
_('resource provider generation conflict'))
Expand Down
93 changes: 93 additions & 0 deletions nova/api/openstack/placement/schemas/inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 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.
"""Inventory schemas for Placement API."""

import copy

from nova import db


RESOURCE_CLASS_IDENTIFIER = "^[A-Z0-9_]+$"
BASE_INVENTORY_SCHEMA = {
"type": "object",
"properties": {
"resource_provider_generation": {
"type": "integer"
},
"total": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1,
},
"reserved": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 0,
},
"min_unit": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1
},
"max_unit": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1
},
"step_size": {
"type": "integer",
"maximum": db.MAX_INT,
"minimum": 1
},
"allocation_ratio": {
"type": "number",
"maximum": db.SQL_SP_FLOAT_MAX
},
},
"required": [
"total",
"resource_provider_generation"
],
"additionalProperties": False
}


POST_INVENTORY_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
POST_INVENTORY_SCHEMA['properties']['resource_class'] = {
"type": "string",
"pattern": RESOURCE_CLASS_IDENTIFIER,
}
POST_INVENTORY_SCHEMA['required'].append('resource_class')
POST_INVENTORY_SCHEMA['required'].remove('resource_provider_generation')


PUT_INVENTORY_RECORD_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
PUT_INVENTORY_RECORD_SCHEMA['required'].remove('resource_provider_generation')
PUT_INVENTORY_SCHEMA = {
"type": "object",
"properties": {
"resource_provider_generation": {
"type": "integer"
},
"inventories": {
"type": "object",
"patternProperties": {
RESOURCE_CLASS_IDENTIFIER: PUT_INVENTORY_RECORD_SCHEMA,
}
}
},
"required": [
"resource_provider_generation",
"inventories"
],
"additionalProperties": False
}

0 comments on commit 07c925a

Please sign in to comment.