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 826b60f
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 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 @@ -2787,7 +2792,7 @@ def _operation2yaml(cls_or_self, operation, converter: Optional["PythonToYaml"])
for key in ("outputs", "entry_state", "invoke"):
impl_val = getattr(operation, key, None)
if impl_val is not None:
op_def[key] = impl_val
op_def[key] = to_tosca_value(impl_val, dict_cls)
return op_def

@classmethod
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 @@ -2947,7 +2952,7 @@ def to_template_yaml(self, converter: "PythonToYaml") -> dict:
f"{field.tosca_field_type.name} \"{field.name}\"'s value has wrong type: it's a {type(value)}, not a {field.type}."
)
body.setdefault(field.section, {})[field.tosca_name] = to_tosca_value(
value
value, dict_cls
)
elif field.section:
assert False, "unexpected section in {field}"
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 @@ -3190,7 +3195,7 @@ def to_template_yaml(self, converter: "PythonToYaml") -> dict:
if self._directives:
tpl["directives"] = self._directives
if self._node_filter:
tpl["node_filter"] = self._node_filter
tpl["node_filter"] = to_tosca_value(self._node_filter)
return tpl

def find_artifact(self, name_or_tpl) -> Optional["ArtifactType"]:
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,9 @@ 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."

# we need this because this class isn't derived from ToscaType:
_template_section: ClassVar[str] = "data_types"
_type_section: ClassVar[str] = "data_types"
_constraints: ClassVar[Optional[List[dict]]] = None

@classmethod
Expand Down Expand Up @@ -3296,7 +3308,7 @@ def _cls_to_yaml(cls, converter: Optional["PythonToYaml"]) -> dict:
body[cls.tosca_type_name()]["description"] = doc
body[cls.tosca_type_name()]["type"] = cls.simple_tosca_type()
if cls._constraints:
body[cls.tosca_type_name()]["constraints"] = cls._constraints
body[cls.tosca_type_name()]["constraints"] = to_tosca_value(cls._constraints)
return body


Expand Down Expand Up @@ -3350,6 +3362,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 +3405,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 @@ -3435,13 +3457,18 @@ def to_template_yaml(self, converter: "PythonToYaml") -> dict:
for field in self._builtin_fields:
val = getattr(self, field, None)
if val is not None:
tpl[field] = val
tpl[field] = to_tosca_value(val)
return tpl

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 826b60f

Please sign in to comment.