Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from gtema/dev
Browse files Browse the repository at this point in the history
Implement identity region/service/endpoint schemas
  • Loading branch information
gtema authored Feb 9, 2024
2 parents bfbe141 + 96823d5 commit a63d548
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 36 deletions.
23 changes: 8 additions & 15 deletions codegenerator/common/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,17 @@ def type_hint(self):

@property
def clap_macros(self) -> set[str]:
""" "Return clap macros"""
"""Return clap macros"""
return set()

@property
def builder_macros(self) -> set[str]:
"""Return builder macros"""
return set()

def get_sample(self):
"""Generate sample data"""
variant = list(self.variants.keys())[0]
variant = list(sorted(self.variants.keys()))[0]
return f"{self.name}::{variant}"

def variant_serde_macros(self, variant: str):
Expand All @@ -381,7 +386,7 @@ class RequestParameter(BaseModel):
remote_name: str
local_name: str
location: str
data_type: BaseCombinedType | BasePrimitiveType
data_type: BaseCombinedType | BasePrimitiveType | BaseCompoundType
description: str | None = None
is_required: bool = False
setter_name: str | None = None
Expand Down Expand Up @@ -930,8 +935,6 @@ def set_parameters(self, parameters: list[model.RequestParameter]) -> None:
"""Set OpenAPI operation parameters into typemanager for conversion"""
for parameter in parameters:
data_type = self.convert_model(parameter.data_type)
# if not parameter.is_required:
# data_type = Option(item_type=data_type)
param = self.request_parameter_class(
remote_name=self.get_remote_attribute_name(parameter.name),
local_name=self.get_local_attribute_name(parameter.name),
Expand All @@ -940,16 +943,6 @@ def set_parameters(self, parameters: list[model.RequestParameter]) -> None:
description=sanitize_rust_docstrings(parameter.description),
is_required=parameter.is_required,
)
if isinstance(data_type, CommaSeparatedList):
param.setter_name = param.local_name
param.setter_type = "csv"
if isinstance(data_type, BTreeSet):
param.setter_name = param.local_name
param.setter_type = "set"
if isinstance(data_type, Array):
param.setter_name = param.local_name
param.setter_type = "list"

self.parameters[param.local_name] = param

def get_parameters(
Expand Down
6 changes: 6 additions & 0 deletions codegenerator/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ConstraintString(PrimitiveType):
minLength: int | None = None
maxLength: int | None = None
pattern: str | None = None
enum: list[Any] | None = None


class PrimitiveNumber(PrimitiveType):
Expand Down Expand Up @@ -588,6 +589,11 @@ def parse_parameter(self, schema) -> RequestParameter:
if len(param_typ) == 1:
param_typ = param_typ[0]
if param_typ == "string":
# NOTE: this is commented out so far since most of enums are just
# too wrong to treat them as enums here
# if "enum" in param_schema:
# dt = Enum(literals=param_schema["enum"], base_types=[ConstraintString])
# else:
dt = ConstraintString(**param_schema)
elif param_typ == "number":
dt = ConstraintNumber(**param_schema)
Expand Down
8 changes: 5 additions & 3 deletions codegenerator/openapi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,11 @@ def process_operation(
(schema_ref, mime_type) = self._get_schema_ref(
openapi_spec,
schema_name,
description=f"Response of the {operation_spec.operationId} operation"
if not action_name
else f"Response of the {operation_spec.operationId}:{action_name} action", # noqa
description=(
f"Response of the {operation_spec.operationId} operation"
if not action_name
else f"Response of the {operation_spec.operationId}:{action_name} action"
), # noqa
schema_def=ser_schema,
action_name=action_name,
)
Expand Down
114 changes: 114 additions & 0 deletions codegenerator/openapi/keystone.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,48 @@ def _post_process_operation_hook(self, openapi_spec, operation_spec):
if ref not in [x.ref for x in operation_spec.parameters]:
operation_spec.parameters.append(ParameterSchema(ref=ref))

elif operationId == "services:get":
for (
key,
val,
) in keystone_schemas.SERVICES_LIST_PARAMETERS.items():
openapi_spec.components.parameters.setdefault(
key, ParameterSchema(**val)
)
ref = f"#/components/parameters/{key}"
if ref not in [x.ref for x in operation_spec.parameters]:
operation_spec.parameters.append(ParameterSchema(ref=ref))

elif operationId == "endpoints:get":
for (
key,
val,
) in keystone_schemas.ENDPOINTS_LIST_PARAMETERS.items():
openapi_spec.components.parameters.setdefault(
key, ParameterSchema(**val)
)
ref = f"#/components/parameters/{key}"
if ref not in [x.ref for x in operation_spec.parameters]:
operation_spec.parameters.append(ParameterSchema(ref=ref))

elif operationId == "regions:get":
for (
key,
val,
) in keystone_schemas.REGIONS_LIST_PARAMETERS.items():
openapi_spec.components.parameters.setdefault(
key, ParameterSchema(**val)
)
ref = f"#/components/parameters/{key}"
if ref not in [x.ref for x in operation_spec.parameters]:
operation_spec.parameters.append(ParameterSchema(ref=ref))

elif operationId in [
"OS-FEDERATION/projects:get",
"OS-FEDERATION/projects:head",
"OS-FEDERATION/domains:get",
"OS-FEDERATION/domains:head",
"endpoints/endpoint_id/OS-ENDPOINT-POLICY/policy:get",
]:
operation_spec.deprecated = True

Expand Down Expand Up @@ -1034,6 +1071,83 @@ def _get_schema_ref(
),
)
ref = f"#/components/schemas/{name}"
# ### Services
elif name == "ServicesGetResponse":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.SERVICES_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name in [
"ServiceGetResponse",
"ServicesPostResponse",
"ServicePatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.SERVICE_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "ServicesPostRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.SERVICE_CREATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "ServicePatchRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.SERVICE_UPDATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"
# ### Endpoints
elif name == "EndpointsGetResponse":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.ENDPOINTS_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name in [
"EndpointGetResponse",
"EndpointsPostResponse",
"EndpointPatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.ENDPOINT_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "EndpointsPostRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.ENDPOINT_CREATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "EndpointsPostRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.ENDPOINT_UPDATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"
# ### Regions
elif name == "RegionsGetResponse":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.REGIONS_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name in [
"RegionGetResponse",
"RegionsPostRequest",
"RegionsPostResponse",
"RegionPatchRequest",
"RegionPatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**keystone_schemas.REGION_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
# Default
else:
(ref, mime_type) = super()._get_schema_ref(
Expand Down
Loading

0 comments on commit a63d548

Please sign in to comment.