Merge "Add JSON schema and validation for region"

This commit is contained in:
Zuul authored and Gerrit Code Review committed 2025-06-06 18:12:52 +00:00
commit 50d266e590
4 files changed
+162 -53

No files matched your search

+43 -17
View File
@@ -14,10 +14,11 @@
import http.client
from keystone.api import validation
from keystone.catalog import schema
from keystone.common import json_home
from keystone.common import provider_api
from keystone.common import rbac_enforcer
from keystone.common import validation
from keystone import exception
from keystone.i18n import _
from keystone.server import flask as ks_flask
@@ -26,30 +27,24 @@ ENFORCER = rbac_enforcer.RBACEnforcer
PROVIDERS = provider_api.ProviderAPIs
class RegionResource(ks_flask.ResourceBase):
class RegionsResource(ks_flask.ResourceBase):
collection_key = 'regions'
member_key = 'region'
def _get_region(self, region_id):
ENFORCER.enforce_call(action='identity:get_region')
return self.wrap_member(PROVIDERS.catalog_api.get_region(region_id))
def _list_regions(self):
@validation.request_query_schema(schema.regions_index_request_query)
@validation.response_body_schema(schema.regions_index_response_body)
def get(self):
filters = ['parent_region_id']
ENFORCER.enforce_call(action='identity:list_regions', filters=filters)
hints = self.build_driver_hints(filters)
refs = PROVIDERS.catalog_api.list_regions(hints)
return self.wrap_collection(refs, hints=hints)
def get(self, region_id=None):
if region_id is not None:
return self._get_region(region_id)
return self._list_regions()
@validation.request_body_schema(schema.region_create_request_body)
@validation.response_body_schema(schema.region_show_response_body)
def post(self):
ENFORCER.enforce_call(action='identity:create_region')
region = self.request_body_json.get('region')
validation.lazy_validate(schema.region_create, region)
region = self._normalize_dict(region)
if not region.get('id'):
# NOTE(morgan): even though we officially only support 'id' setting
@@ -61,10 +56,22 @@ class RegionResource(ks_flask.ResourceBase):
)
return self.wrap_member(ref), http.client.CREATED
class RegionResource(ks_flask.ResourceBase):
collection_key = 'regions'
member_key = 'region'
@validation.request_body_schema(None)
@validation.response_body_schema(schema.region_show_response_body)
def get(self, region_id=None):
ENFORCER.enforce_call(action='identity:get_region')
return self.wrap_member(PROVIDERS.catalog_api.get_region(region_id))
@validation.request_body_schema(schema.region_create_request_body)
@validation.response_body_schema(schema.region_show_response_body)
def put(self, region_id):
ENFORCER.enforce_call(action='identity:create_region')
region = self.request_body_json.get('region')
validation.lazy_validate(schema.region_create, region)
region = self._normalize_dict(region)
if 'id' not in region:
region['id'] = region_id
@@ -82,10 +89,11 @@ class RegionResource(ks_flask.ResourceBase):
)
return self.wrap_member(ref), http.client.CREATED
@validation.request_body_schema(schema.region_update_request_body)
@validation.response_body_schema(schema.region_show_response_body)
def patch(self, region_id):
ENFORCER.enforce_call(action='identity:update_region')
region = self.request_body_json.get('region')
validation.lazy_validate(schema.region_update, region)
self._require_matching_id(region)
return self.wrap_member(
PROVIDERS.catalog_api.update_region(
@@ -93,6 +101,8 @@ class RegionResource(ks_flask.ResourceBase):
)
)
@validation.request_body_schema(None)
@validation.response_body_schema(None)
def delete(self, region_id):
ENFORCER.enforce_call(action='identity:delete_region')
return (
@@ -106,8 +116,24 @@ class RegionResource(ks_flask.ResourceBase):
class RegionAPI(ks_flask.APIBase):
_name = 'regions'
_import_name = __name__
resources = [RegionResource]
resource_mapping = []
resource_mapping = [
ks_flask.construct_resource_map(
resource=RegionsResource,
url='/regions',
resource_kwargs={},
rel="regions",
path_vars=None,
),
ks_flask.construct_resource_map(
resource=RegionResource,
url='/regions/<string:region_id>',
resource_kwargs={},
rel="region",
path_vars={
'region_id': json_home.build_v3_parameter_relation("region_id")
},
),
]
APIs = (RegionAPI,)
@@ -54,6 +54,7 @@ boolean = {
description: dict[str, Any] = {
"type": "string",
"description": "The resource description.",
"maxLength": 255,
}
# User IDs can come from LDAP and LDAP has no constraints on length or format.
+93 -14
View File
@@ -16,26 +16,105 @@ from keystone.api.validation import parameter_types
from keystone.api.validation import response_types
from keystone.common import validation
_region_properties = {
'description': validation.nullable(parameter_types.description),
# Individual properties of `Region`
_region_properties: dict[str, Any] = {
"description": validation.nullable(parameter_types.description),
# NOTE(lbragstad): Regions use ID differently. The user can specify the ID
# or it will be generated automatically.
'id': {'type': 'string'},
'parent_region_id': {'type': ['string', 'null']},
"id": {**parameter_types.region_id, "description": "The region ID."},
"parent_region_id": {
**parameter_types.region_id,
"description": "To make this region a child of another region, set this parameter to the ID of the parent region.",
},
}
region_create = {
'type': 'object',
'properties': _region_properties,
'additionalProperties': True,
# NOTE(lbragstad): No parameters are required for creating regions.
# Common schema of the returned `Region` resource
region_schema: dict[str, Any] = {
"type": "object",
"description": "A region object.",
"properties": {
"links": response_types.resource_links,
**_region_properties,
},
"required": ["id", "parent_region_id", "description", "links"],
"additionalProperties": True,
}
region_update = {
'type': 'object',
'properties': _region_properties,
'minProperties': 1,
'additionalProperties': True,
# Response body of API operations returning a single region
# `GET /regions/{region_id}`, `POST /regions`, `PATCH /regions/{region_id}`,
# and `PUT /regions/{region_id}`
region_show_response_body: dict[str, Any] = {
"type": "object",
"properties": {"region": region_schema},
"required": ["region"],
"additionalProperties": False,
}
# Query parameters of the `GET /regions` API operation
# returning a list of regions
regions_index_request_query: dict[str, Any] = {
"type": "object",
"properties": {"parent_region_id": parameter_types.region_id},
"additionalProperties": False,
}
# Response body of the `GET /regions` API operation
# returning a list of regions
regions_index_response_body: dict[str, Any] = {
"type": "object",
"properties": {
"links": response_types.links,
"regions": {
"type": "array",
"items": region_schema,
"description": "A list of region objects.",
},
"truncated": response_types.truncated,
},
"required": ["regions"],
"additionalProperties": False,
}
# Request body of the `POST /regions` and `PUT /regions/{region_id}`
# API operations
region_create_request_body = {
"type": "object",
"properties": {
"region": {
"type": "object",
"properties": {
**_region_properties,
"id": {
**parameter_types.region_id,
# Empty ID is allowed
"minLength": 0,
"description": "The region ID.",
},
},
# NOTE(lbragstad): No parameters are required for creating regions.
"additionalProperties": True,
}
},
"required": ["region"],
"additonalProperties": False,
}
# Request body of the `PATCH /regions/{region_id}` operation
region_update_request_body = {
"type": "object",
"properties": {
"region": {
"type": "object",
# `id` is only allowed as a property if the value
# matches the `region_id` value in the path.
"properties": _region_properties,
"description": "A region object.",
"minProperties": 1,
"additionalProperties": True,
}
},
"required": ["region"],
"additionalProperties": False,
}
# Schema for Service v3
+25 -22
View File
@@ -1229,39 +1229,45 @@ class RegionValidationTestCase(unit.BaseTestCase):
self.region_name = 'My Region'
create = catalog_schema.region_create
update = catalog_schema.region_update
create = catalog_schema.region_create_request_body
update = catalog_schema.region_update_request_body
self.create_region_validator = validators.SchemaValidator(create)
self.update_region_validator = validators.SchemaValidator(update)
def test_validate_region_request(self):
def test_validate_region_create_request(self):
"""Test that we validate a basic region request."""
# Create_region doesn't take any parameters in the request so let's
# make sure we cover that case.
request_to_validate = {}
request_to_validate = {'region': {}}
self.create_region_validator.validate(request_to_validate)
def test_validate_region_create_request_with_parameters(self):
"""Test that we validate a region request with parameters."""
request_to_validate = {
'id': 'us-east',
'description': 'US East Region',
'parent_region_id': 'US Region',
'region': {
'id': 'us-east',
'description': 'US East Region',
'parent_region_id': 'US Region',
}
}
self.create_region_validator.validate(request_to_validate)
def test_validate_region_create_with_uuid(self):
"""Test that we validate a region request with a UUID as the id."""
request_to_validate = {
'id': uuid.uuid4().hex,
'description': 'US East Region',
'parent_region_id': uuid.uuid4().hex,
'region': {
'id': uuid.uuid4().hex,
'description': 'US East Region',
'parent_region_id': uuid.uuid4().hex,
}
}
self.create_region_validator.validate(request_to_validate)
def test_validate_region_create_fails_with_invalid_region_id(self):
"""Exception raised when passing invalid `id` in request."""
request_to_validate = {'id': 1234, 'description': 'US East Region'}
request_to_validate = {
'region': {'id': 1234, 'description': 'US East Region'}
}
self.assertRaises(
exception.SchemaValidationError,
self.create_region_validator.validate,
@@ -1270,32 +1276,29 @@ class RegionValidationTestCase(unit.BaseTestCase):
def test_validate_region_create_succeeds_with_extra_parameters(self):
"""Validate create region request with extra values."""
request_to_validate = {'other_attr': uuid.uuid4().hex}
self.create_region_validator.validate(request_to_validate)
def test_validate_region_create_succeeds_with_no_parameters(self):
"""Validate create region request with no parameters."""
request_to_validate = {}
request_to_validate = {'region': {'other_attr': uuid.uuid4().hex}}
self.create_region_validator.validate(request_to_validate)
def test_validate_region_update_succeeds(self):
"""Test that we validate a region update request."""
request_to_validate = {
'id': 'us-west',
'description': 'US West Region',
'parent_region_id': 'us-region',
'region': {
'id': 'us-west',
'description': 'US West Region',
'parent_region_id': 'us-region',
}
}
self.update_region_validator.validate(request_to_validate)
def test_validate_region_update_succeeds_with_extra_parameters(self):
"""Validate extra attributes in the region update request."""
request_to_validate = {'other_attr': uuid.uuid4().hex}
request_to_validate = {'region': {'other_attr': uuid.uuid4().hex}}
self.update_region_validator.validate(request_to_validate)
def test_validate_region_update_fails_with_no_parameters(self):
"""Exception raised when passing no parameters in a region update."""
# An update request should consist of at least one value to update
request_to_validate = {}
request_to_validate = {'region': {}}
self.assertRaises(
exception.SchemaValidationError,
self.update_region_validator.validate,