Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fixlib][feature] Add the source of resource kind #2218

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading