Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

### Changed

- Use NODE_TLS_REJECT_UNAUTHORIZED environment variable instead of passing TLS options to SDK when tls_insecure is enabled. ([#46](https://github.com/opsmill/infrahub-vscode/issues/46))
- Use NODE_TLS_REJECT_UNAUTHORIZED environment variable instead of passing TLS options to SDK when <!-- vale off -->tls_insecure<!-- vale on --> is enabled. ([#46](https://github.com/opsmill/infrahub-vscode/issues/46))

## [1.0.5](https://github.com/opsmill/infrahub-vscode/tree/v1.0.5) - 2025-11-07

Expand Down
1 change: 1 addition & 0 deletions changelog/30.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added transform execution support for Infrahub YAML configuration files. The extension now detects and displays transform types (Jinja2 or Python) in the YAML tree view, automatically determines the appropriate `infrahubctl` command (`render` for Jinja2 transforms, `transform` for Python transforms), and provides context-sensitive run commands for artifact definitions. Enhanced tree view provider with improved code structure and better type detection logic.
10 changes: 5 additions & 5 deletions docs/docs/guides/configure-multiple-servers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ Restart VSCode after setting environment variables.

When executing GraphQL queries:

1. Right-click on a query in the Infrahub YAML tree view
2. Select "Execute GraphQL Query"
3. You'll be prompted to select a server from your configured list
4. Choose the appropriate environment
5. Select the branch to query against
1. Select the play icon next to the query
2. You'll be prompted to select a server from your configured list
3. Choose the appropriate environment
4. Select the branch to query against

## Advanced configuration

Expand Down Expand Up @@ -215,4 +214,5 @@ If you encounter certificate errors with development servers:

- [How to Execute GraphQL Queries](./execute-graphql-queries.mdx)
- [How to Manage Branches](./manage-branches.mdx)
- [Security Configuration and Best Practices](../topics/security-configuration.mdx)
- [Extension Commands Reference](../reference/commands-settings.mdx)
269 changes: 269 additions & 0 deletions docs/docs/guides/running-transforms.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
---
title: How to Run Transforms and Artifacts
description: Execute Jinja2 and Python transforms directly from VSCode with automatic command selection and variable support
---

This guide shows you how to execute Infrahub transforms (both Jinja2 and Python) directly from VSCode. The extension automatically detects transform types and uses the appropriate `infrahubctl` command for execution.

## Prerequisites

- Infrahub VSCode extension installed and configured
- At least one Infrahub server configured
- A workspace with `.infrahub.yml` file containing transforms and artifact definitions
- `infrahubctl` CLI tool installed and available in your system PATH

## Understanding Transform Types

The extension supports two types of transforms:

- **Jinja2 Transforms**: Template-based transforms using Jinja2 syntax (executed with `infrahubctl render`)
- **Python Transforms**: Code-based transforms using Python classes (executed with `infrahubctl transform`)

## Step 1: Configure transforms in .infrahub.yml

### Jinja2 Transforms

Define Jinja2 transforms in your `.infrahub.yml`:

```yaml
jinja2_transforms:
- name: topology_clab
description: Template to generate a containerlab topology
query: topology_simulator
template_path: templates/clab_topology.j2
```

### Python Transforms

Define Python transforms in your `.infrahub.yml`:

```yaml
python_transforms:
- name: leaf
class_name: Leaf
file_path: transforms/leaf.py
- name: spine
class_name: Spine
file_path: transforms/spine.py
- name: edge
class_name: Edge
file_path: transforms/edge.py
```

### Artifact Definitions

Define artifact definitions that reference your transforms:

```yaml
artifact_definitions:
- name: leaf_config
artifact_name: leaf
content_type: text/plain
targets: leafs
transformation: leaf # References python_transforms
parameters:
device: name__value

- name: Containerlab Topology
artifact_name: containerlab-topology
content_type: text/plain
targets: topologies_clab
transformation: topology_clab # References jinja2_transforms
parameters:
name: name__value
```

## Step 2: Execute transforms from VSCode

### Method 1: From Artifact Definitions

1. Open the **Infrahub YAML** tree view in VSCode
2. Expand your `.infrahub.yml` file
3. Navigate to **artifact_definitions**
4. You'll see each artifact with its transform type displayed in parentheses:
- `leaf_config (python)`
- `Containerlab Topology (jinja)`
5. Click the play icon next to the desired artifact

### Method 2: From Transform Definitions

1. In the **Infrahub YAML** tree view, navigate to:
- **jinja2_transforms** for Jinja2 templates, or
- **python_transforms** for Python transforms
2. Select the play icon to run the transform directly

### Transform Execution Process

When you run a transform, the extension will:

1. **Auto-detect transform type**: The extension automatically determines whether to use `infrahubctl render` (Jinja2) or `infrahubctl transform` (Python)

2. **Prompt for branch selection**: Choose which Infrahub branch to execute against

3. **Collect transform variables**: Enter any required variables in `key=value` format:
```
site=nyc
device=router01
environment=production
```

4. **Execute the appropriate command**:
- For Jinja2: `infrahubctl render topology_clab site=nyc --branch main`
- For Python: `infrahubctl transform leaf device=router01 --branch main`

## Step 3: Working with transform variables

### Adding Variables

When prompted for variables:

1. Enter each variable in `key=value` format
2. Press Enter to add another variable
3. Leave empty and press Enter to finish

### Variable Examples

```
# Network configuration
site=atl01
rack=A12
vlan=100

# Device specifics
device=spine01
role=spine
asn=65001

# Environment settings
environment=production
region=us-east
```

### Variable Validation

The extension validates variable format:
- ✅ `device=router01` (valid)
- ✅ `site=nyc` (valid)
- ❌ `device=` (invalid - empty value)
- ❌ `=router01` (invalid - empty key)
- ❌ `devicerouter01` (invalid - missing =)

## Step 4: Understanding command execution

### Automatic Command Selection

The extension intelligently chooses the correct `infrahubctl` command:

| Transform Type | Command Used | Example |
|---|---|---|
| Jinja2 | `infrahubctl render` | `infrahubctl render topology_clab --branch main` |
| Python | `infrahubctl transform` | `infrahubctl transform leaf device=spine01 --branch main` |

### Transform Type Detection

The extension determines transform types by:

1. **For artifact definitions**: Looking up the `transformation` field in both `jinja2_transforms` and `python_transforms` sections
2. **For direct transforms**: Using the section they're defined in (`jinja2_transforms` vs `python_transforms`)

### Terminal Integration

Commands execute in the VSCode integrated terminal, allowing you to:

- See real-time output
- Monitor progress
- Debug any errors
- Access command history

## Step 5: Example workflow

Here's a complete example of setting up and running transforms:

### 1. Create directory structure
```bash
mkdir -p transforms templates
```

### 2. Define transforms in .infrahub.yml
```yaml
---
jinja2_transforms:
- name: device_config
description: Generate device configuration
query: device_query
template_path: templates/device.j2

python_transforms:
- name: topology_builder
class_name: TopologyBuilder
file_path: transforms/topology.py

artifact_definitions:
- name: router_config
artifact_name: router-config
content_type: text/plain
targets: routers
transformation: device_config # Jinja2 transform
parameters:
device: name__value

- name: network_topology
artifact_name: topology
content_type: application/json
targets: networks
transformation: topology_builder # Python transform
parameters:
network: name__value
```

### 3. Execute from VSCode
<!-- vale off -->
1. Navigate to **artifact_definitions** → **router_config (jinja)**
<!-- vale on -->
2. Click play icon
3. Select branch: `main`
4. Add variables: `device=router01`, `site=nyc`
5. Command executes: `infrahubctl render device_config device=router01 site=nyc --branch main`

## Troubleshooting

### Common Issues

**"Transform type not determined"**
- Verify the `transformation` field matches a name in `jinja2_transforms` or `python_transforms`
- Check YAML syntax and indentation

**"infrahubctl command not found"**
- Ensure `infrahubctl` is installed: `pip install infrahubctl`
- Verify it's in your system PATH
- Restart VSCode after installation

**"No transform selected"**
- Ensure your artifact definition has a valid `transformation` field
- Verify the referenced transform exists in your configuration

**Transform execution fails**
- Check the terminal output for specific error messages
- Verify branch exists and is accessible
- Ensure required variables are provided
- Check transform syntax (Jinja2 templates or Python code)

### Best Practices

1. **Organize transforms logically**: Group related transforms in clearly named sections
2. **Use descriptive names**: Transform names should clearly indicate their purpose
3. **Document variables**: Add comments in your `.infrahub.yml` describing expected variables
4. **Test incrementally**: Start with simple transforms and add complexity gradually
5. **Version control**: Keep transform files and `.infrahub.yml` in version control

## Next Steps

- **[Managing Branches](./manage-branches.mdx)**: Learn how to work with different Infrahub branches
- **[Configure Multiple Servers](./configure-multiple-servers.mdx)**: Set up development, staging, and production environments
- **[Extension Commands Reference](../reference/commands-settings.mdx)**: Complete list of available commands

## Further Resources

- [Infrahub Transforms Documentation](https://docs.infrahub.app/topics/transforms)
- [infrahubctl CLI Reference](https://docs.infrahub.app/reference/cli)
- [Jinja2 Template Documentation](https://jinja.palletsprojects.com/)
8 changes: 4 additions & 4 deletions docs/docs/guides/snippets.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
title: How to Use Infrahub Snippets in VSCode
title: How to use Infrahub snippets in VSCode
description: Step-by-step guide to inserting and customizing Infrahub YAML and automation snippets in Visual Studio Code
---

# How to Use Infrahub Snippets in VSCode
# How to use Infrahub snippets in VSCode

This guide shows you how to quickly insert and customize Infrahub YAML objects and automation scripts using built-in snippets in Visual Studio Code. By following these steps, you’ll save time and reduce errors when authoring Infrahub resources.

Expand All @@ -15,7 +15,7 @@ This guide shows you how to quickly insert and customize Infrahub YAML objects a

## Steps

### 1. Insert a Infrahub Object Snippet
### 1. Insert an Infrahub object snippet

1. Open any `.yaml` or `.yml` file in your project.
2. Type `infrahubobject` and select the snippet from the suggestion list.
Expand Down Expand Up @@ -43,7 +43,7 @@ This guide shows you how to quickly insert and customize Infrahub YAML objects a
- `infrahubcheck` for a check
3. Fill in the placeholders as needed to scaffold your automation script.

## Related Resources
## Related resources

- [How to Configure Multiple Servers](./configure-multiple-servers.mdx)
- [How to Execute GraphQL Queries](./execute-graphql-queries.mdx)
3 changes: 2 additions & 1 deletion docs/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Welcome to the comprehensive documentation for the Infrahub VSCode Extension. Th

## What is the Infrahub VSCode extension?

The Infrahub VSCode Extension provides intelligent tooling that connects directly to your Infrahub servers, enabling you to develop infrastructure schemas, execute GraphQL queries, and manage branch-based workflows without leaving your IDE. It brings the power of Infrahub's graph database and version control directly into your development workflow.
The Infrahub VSCode Extension provides intelligent tooling that connects directly to your Infrahub servers, enabling you to develop infrastructure schemas, execute GraphQL queries, run Jinja2 and Python transforms, and manage branch-based workflows without leaving your IDE. It brings the power of Infrahub's graph database and version control directly into your development workflow.

## Documentation structure

Expand All @@ -25,6 +25,7 @@ This documentation is organized following the [Diataxis framework](https://diata

- [How to Configure Multiple Infrahub Servers](./guides/configure-multiple-servers.mdx) - Set up dev, staging, and production environments
- [How to Execute GraphQL Queries](./guides/execute-graphql-queries.mdx) - Run queries with variables and branch selection
- [How to Run Transforms and Artifacts](./guides/running-transforms.mdx) - Execute Jinja2 and Python transforms with automatic command selection
- [How to Manage Branches](./guides/manage-branches.mdx) - Create, delete, and work with branches
- [How to use Infrahub Snippets](./guides/snippets.mdx) - Insert and customize Infrahub YAML and automation snippets

Expand Down
Loading