From de62d20476fe60000a89878faf62d3e37e9e4c3e Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Wed, 2 Oct 2024 09:28:55 +0200 Subject: [PATCH] [fixlib][feature] Add the source of resource kind --- fixlib/fixlib/core/model_export.py | 10 ++++++++++ fixlib/test/core/model_export_test.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/fixlib/fixlib/core/model_export.py b/fixlib/fixlib/core/model_export.py index cde470610d..8d86066b69 100644 --- a/fixlib/fixlib/core/model_export.py +++ b/fixlib/fixlib/core/model_export.py @@ -137,6 +137,14 @@ def model_name(clazz: Union[type, Tuple[Any], None]) -> str: return "any" +def model_source(module_name: str) -> Optional[str]: + if module_name.startswith("fix_plugin_"): # Naming scheme for all fix plugins: fix_plugin_.xxx + return module_name.split(".")[0][11:] + elif module_name.startswith("fixlib.baseresources"): # All base kinds are defined in this package + return "base" + return None + + # define if a field should be exported or not. # Use python default: hide props starting with underscore. def should_export(field: Attribute) -> bool: # type: ignore @@ -267,6 +275,8 @@ def export_data_class(clazz: type) -> None: and isinstance(s, str) ): metadata["description"] = s + if root and (source := model_source(clazz.__module__)): + metadata["source"] = source model.append( { diff --git a/fixlib/test/core/model_export_test.py b/fixlib/test/core/model_export_test.py index 2708770efd..0dfb8489c5 100644 --- a/fixlib/test/core/model_export_test.py +++ b/fixlib/test/core/model_export_test.py @@ -4,7 +4,7 @@ from attrs import define, field -from fixlib.baseresources import ModelReference +from fixlib.baseresources import ModelReference, BaseAccessKey from fixlib.core.model_export import ( is_collection, type_arg, @@ -14,6 +14,7 @@ dataclasses_to_fixcore_model, model_name, dynamic_object_to_fixcore_model, + model_source, ) @@ -208,3 +209,11 @@ def test_config_export(): # All global config properties are defined config = {a["name"] for a in result_dict["config"]["properties"]} assert config == {"aws", "gcp"} + + +def test_module_source() -> None: + assert model_source(BaseAccessKey.__module__) == "base" + assert model_source(DataClassBase.__module__) is None + assert model_source("fix_plugin_aws.resource.sagemaker.AwsSagemakerAlgorithm") == "aws" + assert model_source("fix_plugin_digitalocean.resources.DigitalOceanApp") == "digitalocean" + assert model_source("fix_plugin_azure.resource.machinelearning.AzureMachineLearningCompute") == "azure"