Skip to content

Commit

Permalink
dsl: allow referencing embedded tosca templates
Browse files Browse the repository at this point in the history
  • Loading branch information
aszs committed Dec 17, 2024
1 parent 27f1a76 commit 8f5df64
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions tosca-package/tosca/_tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -1621,12 +1621,15 @@ def Artifact(


class _GetName:
# use this to lazily evaluate a template's name because might not be set correctly until yaml generation time.
def __init__(self, obj: Union["ToscaType", Type["ToscaType"]]):
self.obj = obj

def __str__(self) -> str:
if self.obj._type_name in ("inputs", "outputs"):
return f"root::{self.obj._type_name}"
if isinstance(self.obj, _OwnedToscaType):
return self.obj.get_embedded_name() or self.obj._name
return self.obj._name


Expand Down Expand Up @@ -2543,6 +2546,8 @@ def __post_init__(self):
elif getattr(field, "deferred_property_assignments", None):
for name, value in field.deferred_property_assignments.items():
setattr(val, name, value)
if isinstance(val, _ToscaType):
val._set_parent(self, field.name)
self._initialized = True

def _enforce_required_fields(self) -> bool:
Expand Down Expand Up @@ -2912,9 +2917,9 @@ def to_template_yaml(self, converter: "PythonToYaml") -> dict:
self._name + "_" + field.name + (str(i) if i else ""),
)
if shorthand or req:
body.setdefault("requirements", []).append(
{field.tosca_name: shorthand or req}
)
body.setdefault("requirements", []).append({
field.tosca_name: shorthand or req
})
elif field.section in ["capabilities", "artifacts"]:
if value:
assert isinstance(value, (CapabilityType, ArtifactType))
Expand Down Expand Up @@ -3135,7 +3140,7 @@ def _get_expr_prefix(
return ["", _GetName(cls_or_obj)]
elif isinstance(cls_or_obj, ToscaType):
return ["", _GetName(cls_or_obj)]
# XXX elif isinstance(cls_or_obj, type): return f"*[type={cls_or_obj._tosca_typename}]::"
# XXX elif isinstance(cls_or_obj, type): return f"*[.type={cls_or_obj._tosca_typename}]::"
return []


Expand Down Expand Up @@ -3251,6 +3256,11 @@ def _set_parent(self, parent: "_ToscaType", name: str):
self._node = parent
self._local_name = name

def get_embedded_name(self) -> str:
if self._node:
return f"{self._node._name}::{self._local_name}"
return self._name


class _BaseDataType(ToscaObject):
@classmethod
Expand All @@ -3266,7 +3276,7 @@ def get_tosca_datatype(cls):
class ValueType(_BaseDataType):
"ValueTypes are user-defined TOSCA data types that are derived from simple TOSCA datatypes, as opposed to complex TOSCA data types."

_template_section: ClassVar[str] = "data_types"
_type_section: ClassVar[str] = "data_types"
_constraints: ClassVar[Optional[List[dict]]] = None

@classmethod
Expand Down Expand Up @@ -3350,6 +3360,11 @@ def to_template_yaml(self, converter: "PythonToYaml") -> dict:
del tpl["type"]
return tpl

def get_embedded_name(self) -> str:
if self._node:
return f"{self._node._name}::.capabilities[.name={self._local_name}]"
return self._name


CapabilityType = CapabilityEntity

Expand Down Expand Up @@ -3388,6 +3403,11 @@ def __getitem__(self, target: Node) -> Self:
self._target = target
return self

def get_embedded_name(self) -> str:
if self._node:
return f"{self._node._name}::.requirements[.name={self._local_name}]"
return self._name


RelationshipType = Relationship

Expand Down Expand Up @@ -3442,6 +3462,11 @@ def execute(self, *args: ToscaInputs, **kw):
self.inputs = ToscaInputs._get_inputs(*args, **kw)
return self

def get_embedded_name(self) -> str:
if self._node:
return f"{self._node._name}::.artifacts::{self._local_name}"
return self._name


ArtifactType = ArtifactEntity # deprecated

Expand Down

0 comments on commit 8f5df64

Please sign in to comment.