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

Commit

Permalink
feat: remove few unnecessary clones in rust
Browse files Browse the repository at this point in the history
- remove some unnecessary clone() in rust generated code
- minor de-duplication of the keystone schemas
  • Loading branch information
gtema committed Mar 1, 2024
1 parent c4c9f5c commit d1d9e97
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 137 deletions.
13 changes: 11 additions & 2 deletions codegenerator/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,18 @@ def find_resource_schema(
try:
if "type" not in schema:
# Response of server create is a server or reservation_id
if "oneOf" in schema:
# if "oneOf" in schema:
# kinds = {}
# for kind in schema["oneOf"]:
# kinds.update(kind)
# schema["type"] = kinds["type"]
if "allOf" in schema:
# {'allOf': [
# {'type': 'integer', 'minimum': 0},
# {'default': 0}]
# }
kinds = {}
for kind in schema["oneOf"]:
for kind in schema["allOf"]:
kinds.update(kind)
schema["type"] = kinds["type"]
elif schema == {}:
Expand Down
27 changes: 11 additions & 16 deletions codegenerator/openapi/keystone_schemas/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
DOMAIN_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"id": {"type": "string", "format": "uuid", "readOnly": True},
**ks_schema._domain_properties,
},
"additionalProperties": True,
Expand Down Expand Up @@ -101,36 +101,31 @@ def _get_schema_ref(
mime_type: str = "application/json"
ref: str
# Domains
if name == "DomainsPostRequest":
if name in [
"DomainsPostResponse",
"DomainGetResponse",
"DomainPatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**ks_schema.domain_create)
"Domain", TypeSchema(**DOMAIN_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "DomainsPostResponse":
ref = "#/components/schemas/Domain"
elif name == "DomainsPostRequest":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**DOMAIN_SCHEMA)
name, TypeSchema(**ks_schema.domain_create)
)
ref = f"#/components/schemas/{name}"
elif name == "DomainPatchRequest":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**ks_schema.domain_update)
)
ref = f"#/components/schemas/{name}"
elif name == "DomainPatchResponse":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**DOMAIN_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "DomainsGetResponse":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**DOMAINS_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "DomainGetResponse":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**DOMAIN_SCHEMA)
)
ref = f"#/components/schemas/{name}"

# Domain Config
elif name in [
"DomainsConfigDefaultGetResponse",
Expand Down
16 changes: 4 additions & 12 deletions codegenerator/openapi/keystone_schemas/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"type": "string",
"format": "uuid",
"description": "The UUID of the service to which the endpoint belongs.",
"readOnly": True,
},
"interface": {
"type": "string",
Expand Down Expand Up @@ -98,10 +99,6 @@
"service_id",
"url",
]
ENDPOINT_UPDATE_SCHEMA: dict[str, Any] = copy.deepcopy(
ENDPOINT_CONTAINER_SCHEMA
)
ENDPOINT_UPDATE_SCHEMA["properties"]["endpoint"]["properties"].pop("id")


def _post_process_operation_hook(
Expand Down Expand Up @@ -140,26 +137,21 @@ def _get_schema_ref(
ref = f"#/components/schemas/{name}"
elif name in [
"EndpointGetResponse",
"EndpointsPostRequest",
"EndpointsPostResponse",
"EndpointPatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
"Endpoint",
TypeSchema(**ENDPOINT_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
ref = "#/components/schemas/Endpoint"
elif name == "EndpointsPostRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**ENDPOINT_CREATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "EndpointsPostRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**ENDPOINT_UPDATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"

else:
return (None, None, False)
Expand Down
53 changes: 11 additions & 42 deletions codegenerator/openapi/keystone_schemas/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import copy

from typing import Any

from keystone.resource import schema as ks_schema
Expand All @@ -24,7 +22,7 @@
PROJECT_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"id": {"type": "string", "format": "uuid", "readOnly": True},
**ks_schema._project_properties,
},
"additionalProperties": True,
Expand All @@ -36,25 +34,14 @@
"project": {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"id": {"type": "string", "format": "uuid", "readOnly": True},
**ks_schema._project_properties,
},
"additionalProperties": True,
},
},
}

PROJECT_CREATE_REQUEST_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {"project": copy.deepcopy(ks_schema.project_create)},
}

PROJECT_UPDATE_REQUEST_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {"project": copy.deepcopy(ks_schema.project_update)},
}


PROJECTS_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {"projects": {"type": "array", "items": PROJECT_SCHEMA}},
Expand Down Expand Up @@ -124,41 +111,23 @@ def _get_schema_ref(
mime_type: str = "application/json"
ref: str
# Projects
if name == "ProjectsPostRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**PROJECT_CREATE_REQUEST_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "ProjectsPostResponse":
if name in [
"ProjectsPostRequest",
"ProjectsPostResponse",
"ProjectPatchRequest",
"ProjectPatchResponse",
"ProjectGetResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
"Project",
TypeSchema(**PROJECT_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "ProjectPatchRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**PROJECT_UPDATE_REQUEST_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "ProjectPatchResponse":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**PROJECT_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
ref = "#/components/schemas/Project"
elif name == "ProjectsGetResponse":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**PROJECTS_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "ProjectGetResponse":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**PROJECT_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"

# Project Tags
elif name == "ProjectsTagPutRequest":
Expand Down
5 changes: 3 additions & 2 deletions codegenerator/openapi/keystone_schemas/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"type": "string",
"format": "uuid",
"description": "The ID for the region.",
"readOnly": True,
},
"parent_id": {
"type": "string",
Expand Down Expand Up @@ -97,10 +98,10 @@ def _get_schema_ref(
"RegionPatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
"Region",
TypeSchema(**REGION_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
ref = "#/components/schemas/Region"

else:
return (None, None, False)
Expand Down
39 changes: 16 additions & 23 deletions codegenerator/openapi/keystone_schemas/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@

ROLE_SCHEMA: dict[str, Any] = {
"type": "object",
"description": "A role object.",
"properties": {
"id": {
"type": "string",
"format": "uuid",
"description": "The role ID.",
"readOnly": True,
},
"links": {
"type": "object",
"additionalProperties": {
"type": ["string", "null"],
"format": "uri",
},
"readOnly": True,
},
**assignment_schema._role_properties,
},
Expand Down Expand Up @@ -66,6 +69,10 @@
},
}

ROLE_CONTAINER_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {"role": ROLE_SCHEMA},
}

ROLES_SCHEMA: dict[str, Any] = {
"type": "object",
Expand Down Expand Up @@ -354,31 +361,17 @@ def _get_schema_ref(
name, TypeSchema(**ROLES_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "RoleGetResponse":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**ROLE_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "RolesPostRequest":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**assignment_schema.role_create)
)
ref = f"#/components/schemas/{name}"
elif name == "RolesPostResponse":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**ROLE_SCHEMA)
)
ref = f"#/components/schemas/{name}"
elif name == "RolePatchRequest":
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**assignment_schema.role_update)
)
ref = f"#/components/schemas/{name}"
elif name == "RolePatchResponse":
elif name in [
"RolesPostRequest",
"RolesPostResponse",
"RoleGetResponse",
"RolePatchRequest",
"RolePatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name, TypeSchema(**ROLE_SCHEMA)
"Role", TypeSchema(**ROLE_CONTAINER_SCHEMA)
)
ref = f"#/components/schemas/{name}"
ref = "#/components/schemas/Role"

# Role Implies
elif name == "RolesImpliesGetResponse":
Expand Down
30 changes: 6 additions & 24 deletions codegenerator/openapi/keystone_schemas/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import copy

from typing import Any

from codegenerator.common.schema import TypeSchema
Expand All @@ -33,6 +31,7 @@
"type": "string",
"format": "uuid",
"description": "The UUID of the service to which the endpoint belongs.",
"readOnly": True,
},
"name": {
"type": "string",
Expand All @@ -50,13 +49,6 @@
"properties": {"service": SERVICE_SCHEMA},
}

SERVICE_CREATE_SCHEMA: dict[str, Any] = copy.deepcopy(SERVICE_CONTAINER_SCHEMA)
SERVICE_CREATE_SCHEMA["properties"]["service"]["properties"].pop("id")
SERVICE_CREATE_SCHEMA["properties"]["service"]["required"] = ["type"]
SERVICE_UPDATE_SCHEMA: dict[str, Any] = copy.deepcopy(SERVICE_CONTAINER_SCHEMA)
SERVICE_UPDATE_SCHEMA["properties"]["service"]["properties"].pop("id")


SERVICES_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {"services": {"type": "array", "items": SERVICE_SCHEMA}},
Expand Down Expand Up @@ -107,27 +99,17 @@ def _get_schema_ref(
)
ref = f"#/components/schemas/{name}"
elif name in [
"ServiceGetResponse",
"ServicesPostRequest",
"ServicesPostResponse",
"ServiceGetResponse",
"ServicePatchRequest",
"ServicePatchResponse",
]:
openapi_spec.components.schemas.setdefault(
name,
"Service",
TypeSchema(**SERVICE_CONTAINER_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "ServicesPostRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**SERVICE_CREATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"
elif name == "ServicePatchRequest":
openapi_spec.components.schemas.setdefault(
name,
TypeSchema(**SERVICE_UPDATE_SCHEMA),
)
ref = f"#/components/schemas/{name}"
ref = "#/components/schemas/Service"

else:
return (None, None, False)
Expand Down
2 changes: 1 addition & 1 deletion codegenerator/templates/rust_cli/set_body_parameters.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
{%- elif v.data_type.format is defined and v.data_type.format == "password" %}
if let Some(val) = &args.{{ v.local_name }} {
{{ macros.set_request_data_from_input(builder_name, v, "val") }}
{{ builder_name }}.{{ v.remote_name }}(val);
} else {
let secret = Password::new()
{%- if v.description %}
Expand Down
Loading

0 comments on commit d1d9e97

Please sign in to comment.