Computed attributes
The computed attributes feature enables the dynamic calculation of attribute values based on user-defined logic.
Currently, Infrahub supports two main types of computed attributes:
- Jinja2 – Lightweight, but limited in relationship handling.
- Python – More flexible and powerful, but requires async execution.
Computed attributes have some inherent restrictions due to system constraints and performance considerations. Keep these in mind when designing your schema:
- Computed attributes can only be used on nodes, not generics.
- Only
URL
andText
attribute kinds are supported for computed attributes.
Jinja2 computed attributes
Users can input a concise Jinja2 template directly within the schema definition. Any change to any field used to compute the value will automatically update it.
See the guide for instructions on creating Jinja2 computed attribute in Infrahub.
Restrictions
- Jinja2 computed attributes cannot reference relationships with cardinality
many
. - Only direct relationships can be used (i.e., a relationship of a relationship is not accessible).
Python computed attributes
Python computed attributes leverage Infrahub's Python transform feature. Users can define which transformation to apply directly within the schema. The computation of the value is delegated to workers as asynchronous tasks, which may cause the value to take a few seconds to update.
Python scripts do not have the limitations of Jinja2 computed attributes and can include more complex logic, including relationships with cardinality many
and nested relationships.
See the guide for instructions on creating Python computed attribute in Infrahub.
Restrictions
- Mandatory attributes cannot use Python computed attributes.
- Complex scripts may impact system performance.
Choosing Between Jinja2 and Python
Computed Attribute Kind | Mandatory Attribute Support | Relationship Constraints | Complexity Handling |
---|---|---|---|
Jinja2 | ✅ Supported | Cannot use many cardinality; only direct relationships | Best for straightforward operations |
Python | ❌ Not supported | No restrictions | Suitable for complex logic |
See the guide for instructions on creating computed attribute in Infrahub.