Skip to content

Commit 432aeae

Browse files
[placement] Separate API schemas (inventory)
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/inventory.py. Subsequent patches will move schemas of other handlers. Change-Id: Iab542cfb4d09b26f1d9a3db8a8678a8dba173eb9
1 parent 0060c0b commit 432aeae

File tree

2 files changed

+97
-74
lines changed

2 files changed

+97
-74
lines changed

nova/api/openstack/placement/handlers/inventory.py

+4-74
Original file line numberDiff line numberDiff line change
@@ -19,84 +19,14 @@
1919
import webob
2020

2121
from nova.api.openstack.placement import microversion
22+
from nova.api.openstack.placement.schemas import inventory as schema
2223
from nova.api.openstack.placement import util
2324
from nova.api.openstack.placement import wsgi_wrapper
2425
from nova import db
2526
from nova import exception
2627
from nova.i18n import _
2728
from nova.objects import resource_provider as rp_obj
2829

29-
RESOURCE_CLASS_IDENTIFIER = "^[A-Z0-9_]+$"
30-
BASE_INVENTORY_SCHEMA = {
31-
"type": "object",
32-
"properties": {
33-
"resource_provider_generation": {
34-
"type": "integer"
35-
},
36-
"total": {
37-
"type": "integer",
38-
"maximum": db.MAX_INT,
39-
"minimum": 1,
40-
},
41-
"reserved": {
42-
"type": "integer",
43-
"maximum": db.MAX_INT,
44-
"minimum": 0,
45-
},
46-
"min_unit": {
47-
"type": "integer",
48-
"maximum": db.MAX_INT,
49-
"minimum": 1
50-
},
51-
"max_unit": {
52-
"type": "integer",
53-
"maximum": db.MAX_INT,
54-
"minimum": 1
55-
},
56-
"step_size": {
57-
"type": "integer",
58-
"maximum": db.MAX_INT,
59-
"minimum": 1
60-
},
61-
"allocation_ratio": {
62-
"type": "number",
63-
"maximum": db.SQL_SP_FLOAT_MAX
64-
},
65-
},
66-
"required": [
67-
"total",
68-
"resource_provider_generation"
69-
],
70-
"additionalProperties": False
71-
}
72-
POST_INVENTORY_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
73-
POST_INVENTORY_SCHEMA['properties']['resource_class'] = {
74-
"type": "string",
75-
"pattern": RESOURCE_CLASS_IDENTIFIER,
76-
}
77-
POST_INVENTORY_SCHEMA['required'].append('resource_class')
78-
POST_INVENTORY_SCHEMA['required'].remove('resource_provider_generation')
79-
PUT_INVENTORY_RECORD_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
80-
PUT_INVENTORY_RECORD_SCHEMA['required'].remove('resource_provider_generation')
81-
PUT_INVENTORY_SCHEMA = {
82-
"type": "object",
83-
"properties": {
84-
"resource_provider_generation": {
85-
"type": "integer"
86-
},
87-
"inventories": {
88-
"type": "object",
89-
"patternProperties": {
90-
RESOURCE_CLASS_IDENTIFIER: PUT_INVENTORY_RECORD_SCHEMA,
91-
}
92-
}
93-
},
94-
"required": [
95-
"resource_provider_generation",
96-
"inventories"
97-
],
98-
"additionalProperties": False
99-
}
10030

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

235165
inventory = _make_inventory_object(resource_provider,
@@ -361,7 +291,7 @@ def set_inventories(req):
361291
resource_provider = rp_obj.ResourceProvider.get_by_uuid(
362292
context, uuid)
363293

364-
data = _extract_inventories(req.body, PUT_INVENTORY_SCHEMA)
294+
data = _extract_inventories(req.body, schema.PUT_INVENTORY_SCHEMA)
365295
if data['resource_provider_generation'] != resource_provider.generation:
366296
raise webob.exc.HTTPConflict(
367297
_('resource provider generation conflict'))
@@ -456,7 +386,7 @@ def update_inventory(req):
456386
resource_provider = rp_obj.ResourceProvider.get_by_uuid(
457387
context, uuid)
458388

459-
data = _extract_inventory(req.body, BASE_INVENTORY_SCHEMA)
389+
data = _extract_inventory(req.body, schema.BASE_INVENTORY_SCHEMA)
460390
if data['resource_provider_generation'] != resource_provider.generation:
461391
raise webob.exc.HTTPConflict(
462392
_('resource provider generation conflict'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
"""Inventory schemas for Placement API."""
13+
14+
import copy
15+
16+
from nova import db
17+
18+
19+
RESOURCE_CLASS_IDENTIFIER = "^[A-Z0-9_]+$"
20+
BASE_INVENTORY_SCHEMA = {
21+
"type": "object",
22+
"properties": {
23+
"resource_provider_generation": {
24+
"type": "integer"
25+
},
26+
"total": {
27+
"type": "integer",
28+
"maximum": db.MAX_INT,
29+
"minimum": 1,
30+
},
31+
"reserved": {
32+
"type": "integer",
33+
"maximum": db.MAX_INT,
34+
"minimum": 0,
35+
},
36+
"min_unit": {
37+
"type": "integer",
38+
"maximum": db.MAX_INT,
39+
"minimum": 1
40+
},
41+
"max_unit": {
42+
"type": "integer",
43+
"maximum": db.MAX_INT,
44+
"minimum": 1
45+
},
46+
"step_size": {
47+
"type": "integer",
48+
"maximum": db.MAX_INT,
49+
"minimum": 1
50+
},
51+
"allocation_ratio": {
52+
"type": "number",
53+
"maximum": db.SQL_SP_FLOAT_MAX
54+
},
55+
},
56+
"required": [
57+
"total",
58+
"resource_provider_generation"
59+
],
60+
"additionalProperties": False
61+
}
62+
63+
64+
POST_INVENTORY_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
65+
POST_INVENTORY_SCHEMA['properties']['resource_class'] = {
66+
"type": "string",
67+
"pattern": RESOURCE_CLASS_IDENTIFIER,
68+
}
69+
POST_INVENTORY_SCHEMA['required'].append('resource_class')
70+
POST_INVENTORY_SCHEMA['required'].remove('resource_provider_generation')
71+
72+
73+
PUT_INVENTORY_RECORD_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA)
74+
PUT_INVENTORY_RECORD_SCHEMA['required'].remove('resource_provider_generation')
75+
PUT_INVENTORY_SCHEMA = {
76+
"type": "object",
77+
"properties": {
78+
"resource_provider_generation": {
79+
"type": "integer"
80+
},
81+
"inventories": {
82+
"type": "object",
83+
"patternProperties": {
84+
RESOURCE_CLASS_IDENTIFIER: PUT_INVENTORY_RECORD_SCHEMA,
85+
}
86+
}
87+
},
88+
"required": [
89+
"resource_provider_generation",
90+
"inventories"
91+
],
92+
"additionalProperties": False
93+
}

0 commit comments

Comments
 (0)