Merge "Fix codegeneration for placement"

This commit is contained in:
Zuul 2024-11-07 19:49:06 +00:00 committed by Gerrit Code Review
commit b40adc5853
10 changed files with 153 additions and 51 deletions

View File

@ -585,6 +585,8 @@ def get_rust_service_type_from_str(xtype: str):
return "ObjectStore"
case "load-balancer":
return "LoadBalancer"
case "placement":
return "Placement"
case _:
return xtype

View File

@ -288,6 +288,22 @@ class MetadataGenerator(BaseGenerator):
and path.endswith("/details")
):
operation_key = "details"
elif (
args.service_type == "placement"
and resource_name == "allocation_candidate"
and method == "get"
):
operation_key = "list"
elif (
args.service_type == "placement"
and resource_name
in [
"resource_provider/aggregate",
"resource_provider/trait",
]
and method == "put"
):
operation_key = "update"
elif (
len(
[
@ -804,6 +820,10 @@ def post_process_operation(
operation = post_process_object_store_operation(
resource_name, operation_name, operation
)
elif service_type == "placement":
operation = post_process_placement_operation(
resource_name, operation_name, operation
)
return operation
@ -1197,3 +1217,26 @@ def post_process_dns_operation(
# operation.targets["rust-cli"].cli_full_command = "xfr"
return operation
def post_process_placement_operation(
resource_name: str, operation_name: str, operation
):
if resource_name == "allocation_candidate":
if operation_name == "list":
operation.operation_type = "show"
elif resource_name == "resource_provider/aggregate":
if operation_name == "list":
operation.operation_type = "show"
elif resource_name == "resource_provider/allocation":
if operation_name == "list":
operation.operation_type = "show"
elif resource_name == "resource_provider/trait":
if operation_name == "list":
operation.operation_type = "show"
elif resource_name == "usages":
if operation_name == "list":
operation.operation_type = "list_from_struct"
operation.targets["rust-cli"].response_key = "usages"
operation.targets["rust-sdk"].response_key = "usages"
return operation

View File

@ -263,6 +263,14 @@ class JsonSchemaParser:
parent=parent,
ignore_read_only=ignore_read_only,
)
if "anyOf" in schema:
return self.parse_anyOf(
schema,
results,
name=name,
parent=parent,
ignore_read_only=ignore_read_only,
)
if "allOf" in schema:
return self.parse_allOf(
schema,
@ -425,6 +433,15 @@ class JsonSchemaParser:
parent=parent,
ignore_read_only=ignore_read_only,
)
elif "anyOf" in schema:
# `"type": "object", "anyOf": []`
return self.parse_anyOf(
schema,
results,
name=name,
parent=parent,
ignore_read_only=ignore_read_only,
)
elif "allOf" in schema:
# `"type": "object", "anyOf": []`
return self.parse_allOf(
@ -519,6 +536,42 @@ class JsonSchemaParser:
results.append(obj)
return obj
def parse_anyOf(
self,
schema,
results: list[ADT],
name: str | None = None,
parent: Reference | None = None,
ignore_read_only: bool | None = False,
):
obj = OneOfType()
for kind in schema.get("anyOf"):
kind_schema = common._deep_merge(schema, kind)
kind_schema.pop("anyOf")
# todo: merge base props into the kind
kind_type = self.parse_schema(
kind_schema,
results,
name=name,
ignore_read_only=ignore_read_only,
)
if not kind_type:
raise NotImplementedError
ref: Reference | None = getattr(kind_type, "reference", None)
if ref:
obj.kinds.append(ref)
else:
obj.kinds.append(kind_type)
if name:
obj.reference = Reference(
name=name,
type=obj.__class__,
hash_=dicthash_(schema),
parent=parent,
)
results.append(obj)
return obj
def parse_typelist(
self,
schema,

View File

@ -98,10 +98,9 @@ ALLOCATION_RP_RESPONSE_SCHEMA: dict[str, Any] = {
}
},
"additionalProperties": False,
},
"required": ["resources"],
"additionalProperties": False,
}
},
"required": ["resources"],
"additionalProperties": False,
}
},
@ -112,6 +111,7 @@ ALLOCATION_RP_RESPONSE_SCHEMA: dict[str, Any] = {
},
},
"required": ["allocations", "resource_provider_generation"],
"additionalProperties": False,
}

View File

@ -1303,7 +1303,8 @@ class RustCliGenerator(BaseGenerator):
root_type = response_type_manager.get_root_data_type()
mod_import_name = "openstack_sdk::api::" + "::".join(
f"r#{x}" if x in ["type"] else x for x in sdk_mod_path
f"r#{x}" if x in ["trait", "type"] else x
for x in sdk_mod_path
)
if not (
@ -1323,7 +1324,7 @@ class RustCliGenerator(BaseGenerator):
[
"openstack_sdk::api",
"::".join(
f"r#{x}" if x in ["type"] else x
f"r#{x}" if x in ["trait", "type"] else x
for x in sdk_mod_path[:-1]
),
"find",

View File

@ -188,7 +188,7 @@ class BTreeMap(common_rust.Dictionary):
else:
type_hint = self.value_type.type_hint.replace(
"Cow<'a, str>", "String"
)
).replace("<'a>", "<'_>")
return f"BTreeMap::<String, {type_hint}>::new().into_iter()"
def get_mandatory_init(self):

View File

@ -25,7 +25,7 @@ use crate::api::{ApiError, RestClient};
use tracing::trace;
{%- endif %}
use crate::api::{{ mod_path | join("::") | replace("::type", "::r#type") }}::{
use crate::api::{{ mod_path | join("::") | replace("::type", "::r#type") | replace("::trait", "::r#trait") }}::{
get as Get,
{{ list_mod }} as List,
};

View File

@ -21,7 +21,7 @@
{%- endif %}
{%- for mod in mod_list|sort %}
{%- if mod in ["type"] %}
{%- if mod in ["trait", "type"] %}
pub mod r#{{ mod }};
{%- else %}
pub mod {{ mod }};

View File

@ -213,40 +213,40 @@ resources:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1
operations:
get:
list:
operation_id: resource_providers/uuid/aggregates:get
operation_type: get
operation_type: show
targets:
rust-sdk:
module_name: get
module_name: list
rust-cli:
module_name: get
sdk_mod_name: get
cli_full_command: resource-provider aggregate get
aggregates:
module_name: list
sdk_mod_name: list
cli_full_command: resource-provider aggregate list
update:
operation_id: resource_providers/uuid/aggregates:put
operation_type: action
operation_type: set
targets:
rust-sdk:
module_name: aggregates
module_name: set
rust-cli:
module_name: aggregates
sdk_mod_name: aggregates
cli_full_command: resource-provider aggregate aggregates
module_name: set
sdk_mod_name: set
cli_full_command: resource-provider aggregate set
placement.resource_provider/allocation:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1
operations:
get:
list:
operation_id: resource_providers/uuid/allocations:get
operation_type: get
operation_type: show
targets:
rust-sdk:
module_name: get
module_name: list
rust-cli:
module_name: get
sdk_mod_name: get
cli_full_command: resource-provider allocation get
module_name: list
sdk_mod_name: list
cli_full_command: resource-provider allocation list
placement.allocation:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1
@ -295,16 +295,16 @@ resources:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1
operations:
show:
list:
operation_id: allocation_candidates:get
operation_type: show
targets:
rust-sdk:
module_name: get
module_name: list
rust-cli:
module_name: show
sdk_mod_name: get
cli_full_command: allocation-candidate show
module_name: list
sdk_mod_name: list
cli_full_command: allocation-candidate list
placement.trait:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1
@ -353,26 +353,26 @@ resources:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1
operations:
get:
list:
operation_id: resource_providers/uuid/traits:get
operation_type: get
operation_type: show
targets:
rust-sdk:
module_name: get
module_name: list
rust-cli:
module_name: get
sdk_mod_name: get
cli_full_command: resource-provider trait get
traits:
module_name: list
sdk_mod_name: list
cli_full_command: resource-provider trait list
update:
operation_id: resource_providers/uuid/traits:put
operation_type: action
operation_type: set
targets:
rust-sdk:
module_name: traits
module_name: set
rust-cli:
module_name: traits
sdk_mod_name: traits
cli_full_command: resource-provider trait traits
module_name: set
sdk_mod_name: set
cli_full_command: resource-provider trait set
delete:
operation_id: resource_providers/uuid/traits:delete
operation_type: delete
@ -387,16 +387,16 @@ resources:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1
operations:
get:
list:
operation_id: usages:get
operation_type: get
operation_type: list
targets:
rust-sdk:
module_name: get
module_name: list
rust-cli:
module_name: get
sdk_mod_name: get
cli_full_command: usage get
module_name: list
sdk_mod_name: list
cli_full_command: usage list
placement.reshaper:
spec_file: wrk/openapi_specs/placement/v1.yaml
api_version: v1

View File

@ -36,6 +36,9 @@
- service: "object-store"
metadata: "metadata/object-store_metadata.yaml"
targets: ["rust-sdk"]
- service: "placement"
metadata: "metadata/placement_metadata.yaml"
targets: ["rust-sdk", "rust-cli"]
# - service: "shared-file-system"
# metadata: "metadata/shared-file-system_metadata.yaml"
# targets: ["rust-sdk"]
@ -43,8 +46,8 @@
- job:
name: codegenerator-rust-all
parent: codegenerator-rust-base
# It takes a while to compile the project
timeout: 3600
# It takes a while to optimize and compile the project
timeout: 5400
description: |
Generate Rust SDK/CLI
dependencies: