From 2d4f51056d43a96d2871e7073c619c3182ea2342 Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Wed, 6 Nov 2024 11:21:03 +0100 Subject: [PATCH] Fix codegeneration for placement Actually start generating placement code Change-Id: I8ea5a43b5da797aadf479ec4d48164d873d12c7d --- codegenerator/common/__init__.py | 2 + codegenerator/metadata.py | 43 ++++++++++ codegenerator/model.py | 53 ++++++++++++ .../openapi/placement_schemas/allocation.py | 6 +- codegenerator/rust_cli.py | 5 +- codegenerator/rust_sdk.py | 2 +- codegenerator/templates/rust_sdk/find.rs.j2 | 2 +- codegenerator/templates/rust_sdk/mod.rs.j2 | 2 +- metadata/placement_metadata.yaml | 82 +++++++++---------- zuul.d/rust.yaml | 7 +- 10 files changed, 153 insertions(+), 51 deletions(-) diff --git a/codegenerator/common/__init__.py b/codegenerator/common/__init__.py index 67a24c6..36f34c7 100644 --- a/codegenerator/common/__init__.py +++ b/codegenerator/common/__init__.py @@ -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 diff --git a/codegenerator/metadata.py b/codegenerator/metadata.py index ee0654e..04bad38 100644 --- a/codegenerator/metadata.py +++ b/codegenerator/metadata.py @@ -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 diff --git a/codegenerator/model.py b/codegenerator/model.py index b906b45..ed6a7c3 100644 --- a/codegenerator/model.py +++ b/codegenerator/model.py @@ -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, diff --git a/codegenerator/openapi/placement_schemas/allocation.py b/codegenerator/openapi/placement_schemas/allocation.py index 50c1a41..6c38b9e 100644 --- a/codegenerator/openapi/placement_schemas/allocation.py +++ b/codegenerator/openapi/placement_schemas/allocation.py @@ -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, } diff --git a/codegenerator/rust_cli.py b/codegenerator/rust_cli.py index c967df8..267ce7a 100644 --- a/codegenerator/rust_cli.py +++ b/codegenerator/rust_cli.py @@ -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", diff --git a/codegenerator/rust_sdk.py b/codegenerator/rust_sdk.py index 50bb933..905db47 100644 --- a/codegenerator/rust_sdk.py +++ b/codegenerator/rust_sdk.py @@ -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::::new().into_iter()" def get_mandatory_init(self): diff --git a/codegenerator/templates/rust_sdk/find.rs.j2 b/codegenerator/templates/rust_sdk/find.rs.j2 index 1143f7c..cf93a5c 100644 --- a/codegenerator/templates/rust_sdk/find.rs.j2 +++ b/codegenerator/templates/rust_sdk/find.rs.j2 @@ -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, }; diff --git a/codegenerator/templates/rust_sdk/mod.rs.j2 b/codegenerator/templates/rust_sdk/mod.rs.j2 index f161d9a..cb80ed5 100644 --- a/codegenerator/templates/rust_sdk/mod.rs.j2 +++ b/codegenerator/templates/rust_sdk/mod.rs.j2 @@ -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 }}; diff --git a/metadata/placement_metadata.yaml b/metadata/placement_metadata.yaml index bff9257..06279da 100644 --- a/metadata/placement_metadata.yaml +++ b/metadata/placement_metadata.yaml @@ -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 diff --git a/zuul.d/rust.yaml b/zuul.d/rust.yaml index 460e13f..8d1ad2d 100644 --- a/zuul.d/rust.yaml +++ b/zuul.d/rust.yaml @@ -38,6 +38,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"] @@ -45,8 +48,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: