From fa66d9a730072869ac13797a37d3224855653f1e Mon Sep 17 00:00:00 2001 From: Eric Fried Date: Tue, 14 Aug 2018 17:30:04 -0500 Subject: [PATCH] [placement] Regex consts for placement schema Factor out some regexes used across multiple schemata into a new nova.api.openstack.placement.schemas.common module. The UUID part of this is in preparation for fixing the related bug. Change-Id: If62bfeeb32d0ad77dd1205116ee4e5e844bb07e4 Related-Bug: #1758057 --- .../openstack/placement/schemas/allocation.py | 12 +++++----- .../api/openstack/placement/schemas/common.py | 22 +++++++++++++++++++ .../openstack/placement/schemas/inventory.py | 6 ++--- .../placement/schemas/resource_class.py | 4 +++- nova/api/openstack/placement/schemas/trait.py | 5 ++++- 5 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 nova/api/openstack/placement/schemas/common.py diff --git a/nova/api/openstack/placement/schemas/allocation.py b/nova/api/openstack/placement/schemas/allocation.py index 7c0313b7fc92..169a953f58c3 100644 --- a/nova/api/openstack/placement/schemas/allocation.py +++ b/nova/api/openstack/placement/schemas/allocation.py @@ -13,6 +13,8 @@ import copy +from nova.api.openstack.placement.schemas import common + ALLOCATION_SCHEMA = { "type": "object", @@ -38,7 +40,7 @@ ALLOCATION_SCHEMA = { "type": "object", "minProperties": 1, "patternProperties": { - "^[0-9A-Z_]+$": { + common.RC_PATTERN: { "type": "integer", "minimum": 1, } @@ -78,7 +80,7 @@ ALLOCATION_SCHEMA_V1_12 = { "minProperties": 1, # resource provider uuid "patternProperties": { - "^[0-9a-fA-F-]{36}$": { + common.UUID_PATTERN: { "type": "object", "properties": { # generation is optional @@ -90,7 +92,7 @@ ALLOCATION_SCHEMA_V1_12 = { "minProperties": 1, # resource class "patternProperties": { - "^[0-9A-Z_]+$": { + common.RC_PATTERN: { "type": "integer", "minimum": 1, } @@ -137,7 +139,7 @@ POST_ALLOCATIONS_V1_13 = { "minProperties": 1, "additionalProperties": False, "patternProperties": { - "^[0-9a-fA-F-]{36}$": DELETABLE_ALLOCATIONS + common.UUID_PATTERN: DELETABLE_ALLOCATIONS } } @@ -163,5 +165,5 @@ alloc_props['consumer_generation'] = { REQUIRED_GENERATION_ALLOCS_POST['required'].append("consumer_generation") POST_ALLOCATIONS_V1_28 = copy.deepcopy(POST_ALLOCATIONS_V1_13) POST_ALLOCATIONS_V1_28["patternProperties"] = { - "^[0-9a-fA-F-]{36}$": REQUIRED_GENERATION_ALLOCS_POST + common.UUID_PATTERN: REQUIRED_GENERATION_ALLOCS_POST } diff --git a/nova/api/openstack/placement/schemas/common.py b/nova/api/openstack/placement/schemas/common.py new file mode 100644 index 000000000000..51d3ee925cd5 --- /dev/null +++ b/nova/api/openstack/placement/schemas/common.py @@ -0,0 +1,22 @@ +# 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. +_UUID_CHAR = "[0-9a-fA-F-]" +# TODO(efried): Use this stricter pattern, and replace string/uuid with it: +# UUID_PATTERN = "^%s{8}-%s{4}-%s{4}-%s{4}-%s{12}$" % ((_UUID_CHAR,) * 5) +UUID_PATTERN = "^%s{36}$" % _UUID_CHAR + +_RC_TRAIT_CHAR = "[A-Z0-9_]" +_RC_TRAIT_PATTERN = "^%s+$" % _RC_TRAIT_CHAR +RC_PATTERN = _RC_TRAIT_PATTERN +_CUSTOM_RC_TRAIT_PATTERN = "^CUSTOM_%s+$" % _RC_TRAIT_CHAR +CUSTOM_RC_PATTERN = _CUSTOM_RC_TRAIT_PATTERN +CUSTOM_TRAIT_PATTERN = _CUSTOM_RC_TRAIT_PATTERN diff --git a/nova/api/openstack/placement/schemas/inventory.py b/nova/api/openstack/placement/schemas/inventory.py index 78f1c45d340d..cddea13064da 100644 --- a/nova/api/openstack/placement/schemas/inventory.py +++ b/nova/api/openstack/placement/schemas/inventory.py @@ -13,10 +13,10 @@ import copy +from nova.api.openstack.placement.schemas import common from nova.db import constants as db_const -RESOURCE_CLASS_IDENTIFIER = "^[A-Z0-9_]+$" BASE_INVENTORY_SCHEMA = { "type": "object", "properties": { @@ -64,7 +64,7 @@ BASE_INVENTORY_SCHEMA = { POST_INVENTORY_SCHEMA = copy.deepcopy(BASE_INVENTORY_SCHEMA) POST_INVENTORY_SCHEMA['properties']['resource_class'] = { "type": "string", - "pattern": RESOURCE_CLASS_IDENTIFIER, + "pattern": common.RC_PATTERN, } POST_INVENTORY_SCHEMA['required'].append('resource_class') POST_INVENTORY_SCHEMA['required'].remove('resource_provider_generation') @@ -81,7 +81,7 @@ PUT_INVENTORY_SCHEMA = { "inventories": { "type": "object", "patternProperties": { - RESOURCE_CLASS_IDENTIFIER: PUT_INVENTORY_RECORD_SCHEMA, + common.RC_PATTERN: PUT_INVENTORY_RECORD_SCHEMA, } } }, diff --git a/nova/api/openstack/placement/schemas/resource_class.py b/nova/api/openstack/placement/schemas/resource_class.py index 8dac3c3456a0..32f75bc880d2 100644 --- a/nova/api/openstack/placement/schemas/resource_class.py +++ b/nova/api/openstack/placement/schemas/resource_class.py @@ -13,13 +13,15 @@ import copy +from nova.api.openstack.placement.schemas import common + POST_RC_SCHEMA_V1_2 = { "type": "object", "properties": { "name": { "type": "string", - "pattern": "^CUSTOM\_[A-Z0-9_]+$", + "pattern": common.CUSTOM_RC_PATTERN, "maxLength": 255, }, }, diff --git a/nova/api/openstack/placement/schemas/trait.py b/nova/api/openstack/placement/schemas/trait.py index a46ec5077ac7..b9c04e54de2e 100644 --- a/nova/api/openstack/placement/schemas/trait.py +++ b/nova/api/openstack/placement/schemas/trait.py @@ -13,13 +13,16 @@ import copy +from nova.api.openstack.placement.schemas import common + + TRAIT = { "type": "string", 'minLength': 1, 'maxLength': 255, } CUSTOM_TRAIT = copy.deepcopy(TRAIT) -CUSTOM_TRAIT.update({"pattern": "^CUSTOM_[A-Z0-9_]+$"}) +CUSTOM_TRAIT.update({"pattern": common.CUSTOM_TRAIT_PATTERN}) PUT_TRAITS_SCHEMA = { "type": "object",