Skip to content

Commit

Permalink
[fixlib][feature] Add the source of resource kind (#2218)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikita Melkozerov <[email protected]>
  • Loading branch information
aquamatthias and meln1k authored Oct 2, 2024
1 parent a285340 commit 76f1e08
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions fixlib/fixlib/core/model_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_<source>.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
Expand Down Expand Up @@ -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(
{
Expand Down
11 changes: 10 additions & 1 deletion fixlib/test/core/model_export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,6 +14,7 @@
dataclasses_to_fixcore_model,
model_name,
dynamic_object_to_fixcore_model,
model_source,
)


Expand Down Expand Up @@ -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"

0 comments on commit 76f1e08

Please sign in to comment.