What are Transformations?
Transformations take data from Infrahub’s graph database (via GraphQL queries) and transform it into a desired output format. This could be:- Configuration files for network devices
- JSON/YAML data structures
- Markdown documentation
- Custom data formats for external systems
Types of Transformations
Infrahub supports two types of transformations:1. Jinja2 Transformations
Template-based transformations using the Jinja2 templating engine. Ideal for:- Generating text-based configuration files
- Creating structured output with familiar template syntax
- Simple data manipulation and formatting
2. Python Transformations
Code-based transformations using Python classes. Ideal for:- Complex data manipulation and business logic
- Integration with external APIs or libraries
- Type-safe transformations with validation
- Reusable transformation logic
Transformation Workflow
The transformation process follows these steps:- Define a GraphQL query to retrieve the data you need
- Create a transformation (Jinja2 template or Python class)
- Configure the transformation in
.infrahub.yml - Execute the transformation (manually, via artifact generation, or through generators)
Configuration in .infrahub.yml
Transformations are defined in your repository’s.infrahub.yml file:
Key Configuration Options
Common Options
- name: Unique identifier for the transformation
- query: Name or ID of the GraphQL query to execute
- timeout: Maximum execution time (default: 10 seconds)
Jinja2-Specific Options
- template_path: Path to the Jinja2 template file within the repository
- description: Human-readable description of the transformation
Python-Specific Options
- file_path: Path to the Python file containing the transform class
- class_name: Name of the Python class implementing
InfrahubTransform - convert_query_response: Convert GraphQL response to
InfrahubNodeobjects (default: false)
Query Response Conversion
By default, transformations receive raw GraphQL query responses as nested dictionaries. Whenconvert_query_response: true is set for Python transforms, the response is automatically converted to InfrahubNode objects:
Using Transformations
Transformations can be used in several contexts:1. Artifact Definitions
Link transformations to artifact definitions to automatically generate artifacts:2. Direct Execution
Transformations can be executed programmatically via the Infrahub SDK or API.3. Computed Attributes
Python transforms can be used to compute derived values for schema attributes.Timeouts and Performance
Transformations have configurable timeouts to prevent long-running operations:Best Practices
- Keep transformations focused: Each transformation should have a single, clear purpose
- Use appropriate type: Choose Jinja2 for templates, Python for complex logic
- Handle errors gracefully: Include error handling in Python transforms
- Test transformations: Write tests for your transform logic
- Optimize queries: Retrieve only the data you need in your GraphQL queries
- Set reasonable timeouts: Balance between allowing complex operations and preventing hangs
- Document transformations: Use clear names and descriptions
Related Topics
- Jinja2 Templates - Detailed guide on creating Jinja2 transformations
- Python Transforms - Guide to building Python transformations
- Artifacts - Using transformations to generate artifacts
- GraphQL Queries - Writing queries to retrieve data
Next Steps
- Learn how to create Jinja2 templates for text-based transformations
- Build Python transforms for complex data processing
- Generate artifacts using your transformations