Closed
Description
Component
Python SDK, infrahubctl
Infrahub SDK version
0.14.0
Current Behavior
When you want to import functions (or classes, variables) from other python modules into python transformations or generators then there is inconsistency on how this can be achieved.
resource | infrahubctl (relative) | infrahubctl (absolute) | Infrahub (relative) | Infrahub (absolute) |
---|---|---|---|---|
transform | ❌ | ❌ | ✅ | ❌ |
generator | ❌ | ✅ | ✅ | ❌ |
Expected Behavior
Ideally we can import functions / classes variables from modules using relative imports when doing this in tranformations and generators, running locally or within Infrahub.
This way common code in transformations and generators can be shared in modules that are located anywhere within the repository.
Steps to Reproduce
- load an instance of Infrahub with the demo schema
- create a BuiltinTag node
- create an external repository
- define the following
.infrahub.yml
file
---
queries:
- name: test
file_path: test.gql
python_transforms:
- name: configuration
file_path: transformers/configuration.py
class_name: Configuration
- create a directory transformers in the root of the repository
- create a file
transformers/configurations.py
with the following contents
from typing import Any, Dict
from infrahub_sdk.transforms import InfrahubTransform
from .utils import get_value
class Configuration(InfrahubTransform):
query: str = "test"
async def transform(self, data: Dict[str, Any]) -> Dict[str, Any]:
return {
"utils.get_value": get_value(),
}
- create a file
transformers/utils.py
with the following contents
def get_value() -> str:
return "value"
- create a file
test.gql
at the root of the repository with the following contents
query test($name: String!) {
BuiltinTag(name__value: $name) {
edges {
node {
name {
value
}
}
}
}
}
- stage all the newly created files and commit them
- try running the transformation using
infrahubctl transform configuration
> issue importing the get_value function - push the changes to a git remote
- import the repository into Infrahub
- run the transformation through the REST API > works
Additional Information
No response