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 #5 from gtema/f2
Browse files Browse the repository at this point in the history
feat: block storage additions
  • Loading branch information
gtema authored Jan 16, 2024
2 parents 394e3a5 + 49aa68b commit 3860c68
Show file tree
Hide file tree
Showing 16 changed files with 2,599 additions and 109 deletions.
7 changes: 4 additions & 3 deletions codegenerator/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,17 @@ def get_resource_names_from_url(path: str):
):
path_resource_names.pop()
if len(path_resource_names) == 0:
return ["Version"]
return ["version"]
if path.startswith("/v2/schemas/"):
# Image schemas should not be singularized (schema/images,
# schema/image)
if path.endswith("s") and not path_resource_names[-1].endswith("s"):
path_resource_names[-1] += "s"
path_resource_names[-1] = path_elements[-1]
if path.startswith("/v2/images") and path.endswith("/actions/deactivate"):
path_resource_names = ["image"]
if path.startswith("/v2/images") and path.endswith("/actions/reactivate"):
path_resource_names = ["image"]
if path_resource_names == ["volume_transfer", "accept"]:
path_resource_names = ["volume_transfer"]

return path_resource_names

Expand Down
19 changes: 10 additions & 9 deletions codegenerator/common/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Boolean(BasePrimitiveType):
original_data_type: BaseCompoundType | BaseCompoundType | None = None

def get_sample(self):
return "False"
return "false"


class Number(BasePrimitiveType):
Expand Down Expand Up @@ -70,12 +70,14 @@ def get_sample(self):


class Null(BasePrimitiveType):
type_hint: str = "Option<String>"
type_hint: str = "Value"
imports: set[str] = set(["serde_json::Value"])
builder_macros: set[str] = set(['default = "Value::Null"'])
clap_macros: set[str] = set()
original_data_type: BaseCompoundType | BaseCompoundType | None = None

def get_sample(self):
return "None::<String>"
return "Value::Null"


class String(BasePrimitiveType):
Expand All @@ -91,6 +93,9 @@ class JsonValue(BasePrimitiveType):
imports: set[str] = set(["serde_json::Value"])
builder_macros: set[str] = set(["setter(into)"])

def get_sample(self):
return "json!({})"


class Option(BaseCombinedType):
base_type: str = "Option"
Expand Down Expand Up @@ -435,12 +440,8 @@ def get_local_attribute_name(self, name: str) -> str:
attr_name = "_".join(
x.lower() for x in re.split(common.SPLIT_NAME_RE, name)
)
if attr_name == "type":
attr_name = "_type"
elif attr_name == "self":
attr_name = "_self"
elif attr_name == "enum":
attr_name = "_self"
if attr_name in ["type", "self", "enum", "ref"]:
attr_name = f"_{attr_name}"
return attr_name

def get_remote_attribute_name(self, name: str) -> str:
Expand Down
6 changes: 6 additions & 0 deletions codegenerator/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ def generate(
"/actions/reactivate"
):
operation_key = "reactivate"
elif (
args.service_type == "block-storage"
and "volume-transfers" in path
and path.endswith("}/accept")
):
operation_key = "accept"
elif (
len(
[
Expand Down
2 changes: 2 additions & 0 deletions codegenerator/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def parse_schema(
)
elif schema == {}:
return PrimitiveNull()
elif not type_ and "format" in schema:
return ConstraintString(**schema)
raise RuntimeError("Cannot determine type for %s", schema)

def parse_object(
Expand Down
109 changes: 54 additions & 55 deletions codegenerator/openapi/cinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

from ruamel.yaml.scalarstring import LiteralScalarString

from codegenerator.common.schema import ParameterSchema
from codegenerator.common.schema import SpecSchema
from codegenerator.common.schema import TypeSchema
from codegenerator.openapi.base import OpenStackServerSourceBase
from codegenerator.openapi import cinder_schemas
from codegenerator.openapi.utils import merge_api_ref_doc


Expand Down Expand Up @@ -97,6 +99,12 @@ def _generate(self, target_dir, args, *pargs, **kwargs):
),
)

# Set global parameters
for name, definition in cinder_schemas.VOLUME_PARAMETERS.items():
openapi_spec.components.parameters[name] = ParameterSchema(
**definition
)

for route in self.router.map.matchlist:
# if route.routepath.startswith("/{project"):
# continue
Expand All @@ -114,6 +122,42 @@ def _generate(self, target_dir, args, *pargs, **kwargs):

return impl_path

def _post_process_operation_hook(self, openapi_spec, operation_spec):
"""Hook to allow service specific generator to modify details"""
operationId = operation_spec.operationId

if operationId in [
"project_id/volumes:get",
"volumes:get",
"project_id/volumes/detail:get",
"volumes/detail:get",
]:
for pname in [
"all_tenants",
"sort",
"sort_key",
"sort_dir",
"limit",
"offset",
"marker",
"with_count",
"created_at",
"updated_at",
"consumes_quota",
]:
ref = f"#/components/parameters/{pname}"
if ref not in [x.ref for x in operation_spec.parameters]:
operation_spec.parameters.append(ParameterSchema(ref=ref))
elif operationId in [
"project_id/volumes/summary:get",
]:
for pname in [
"all_tenants",
]:
ref = f"#/components/parameters/{pname}"
if ref not in [x.ref for x in operation_spec.parameters]:
operation_spec.parameters.append(ParameterSchema(ref=ref))

def _get_schema_ref(
self,
openapi_spec,
Expand All @@ -123,68 +167,23 @@ def _get_schema_ref(
action_name=None,
):
mime_type: str = "application/json"
if name == "ServersCreateResponse":
if name == "VolumesListResponse":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(
description="Server Create response",
type="object",
properties={
"server": {
"type": "object",
"properties": {
"name": {"type": "string"},
"flavorRef": {"type": "string"},
"security_groups": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"}
},
},
},
},
}
},
),
name, TypeSchema(**cinder_schemas.VOLUMES_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "ServersListResponse":
if name == "VolumesDetailResponse":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(
description="Server List response",
type="object",
properties={
"servers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"name": {"type": "string"},
"links": {"type": "array"},
"server_links": {"type": "array"},
},
},
}
},
),
name, TypeSchema(**cinder_schemas.VOLUMES_DETAIL_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "FlavorsListResponse":
elif name in [
"VolumeShowResponse",
"VolumeUpdateResponse",
"VolumesCreateResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(
description="Dummy Flavors response",
type="object",
properties={
"flavors": {
"type": "array",
}
},
),
name, TypeSchema(**cinder_schemas.VOLUME_CONTAINER_SCHEMA)
)
ref = f"#/components/schemas/{name}"
else:
Expand Down
Loading

0 comments on commit 3860c68

Please sign in to comment.