Skip to content

Commit 65d6042

Browse files
committed
updates Jinja2 transform guide
1 parent 48a18f4 commit 65d6042

File tree

1 file changed

+77
-44
lines changed

1 file changed

+77
-44
lines changed

docs/docs/guides/jinja2-transform.mdx

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,49 @@ The goal of this guide is to develop a Jinja Transform and add it to Infrahub, w
1616
6. Add the repository to Infrahub as an external repository
1717
7. Validate that the transform works by using the render API endpoint
1818

19-
In this guide we are going to work with the builtin tag objects in Infrahub. It won't provide a rendered template that is very useful, the goal is instead to show how the Jinja Rendering works. Once you have mastered the basics you will be ready to go on to create more advanced template.
2019

21-
## 1. Creating a query to collect the desired data
20+
## 1. Loading a schema
21+
22+
In this guide we are going to work with a very simplistic network device model. I won't provide a rendered template that is very useful, the goal is instead to show how the Jinja Rendering works. Once you have mastered the basics you will be ready to go on to create more advanced template.
23+
24+
```yaml
25+
---
26+
version: "1.0"
27+
nodes:
28+
- name: Device
29+
namespace: Network
30+
display_labels:
31+
- name__value
32+
attributes:
33+
- name: name
34+
kind: Text
35+
label: Name
36+
optional: false
37+
unique: true
38+
- name: description
39+
kind: Text
40+
label: Description
41+
optional: true
42+
```
43+
44+
Store the schema as a YAML file on your local disk, and load the schema into Infrahub using the following command
45+
46+
```bash
47+
infrahubctl schema load /path/to/schema.yml
48+
```
49+
50+
More information on loading schema files into Infrahub can be found [here](../guides/import-schema#load-a-schema-file).
51+
52+
## 2. Creating a query to collect the desired data
2253

2354
As the first step we need to have some data in the database to actually query.
2455

25-
Create three tags, called "red", "green", "blue", either using the frontend or by submitting three GraphQL mutations as per below (just swapping out the name of the color each time).
56+
Create three devices, called "switch1", "switch2", "switch3", either using the frontend or by submitting three GraphQL mutations as per below (just swapping out the name of the color each time).
2657

2758
```graphql
28-
mutation CreateTags {
29-
BuiltinTagCreate(
30-
data: {name: {value: "red"}, description: {value: "The red tag"}}
59+
mutation CreateDevice {
60+
NetworkDeviceCreate(
61+
data: {name: {value: "switch1"}, description: {value: "This is device switch1"}}
3162
) {
3263
ok
3364
object {
@@ -40,8 +71,8 @@ mutation CreateTags {
4071
The next step is to create a query that returns the data we just created. The rest of this guide assumes that the following query will return a response similar to the response below the query.
4172

4273
```graphql
43-
query TagsQuery {
44-
BuiltinTag {
74+
query DeviceQuery {
75+
NetworkDevice {
4576
edges {
4677
node {
4778
name {
@@ -61,35 +92,35 @@ Response to the tags query:
6192
```json
6293
{
6394
"data": {
64-
"BuiltinTag": {
95+
"NetworkDevice": {
6596
"edges": [
6697
{
6798
"node": {
6899
"name": {
69-
"value": "blue"
100+
"value": "switch1"
70101
},
71102
"description": {
72-
"value": "The blue tag"
103+
"value": "This is device switch1"
73104
}
74105
}
75106
},
76107
{
77108
"node": {
78109
"name": {
79-
"value": "green"
110+
"value": "switch2"
80111
},
81112
"description": {
82-
"value": "The green tag"
113+
"value": "This is device switch2"
83114
}
84115
}
85116
},
86117
{
87118
"node": {
88119
"name": {
89-
"value": "red"
120+
"value": "switch3"
90121
},
91122
"description": {
92-
"value": "The red tag"
123+
"value": "This is device switch3"
93124
}
94125
}
95126
}
@@ -99,19 +130,20 @@ Response to the tags query:
99130
}
100131
```
101132

102-
While it would be possible to create a transform that targets all of these tags, for example if you want to create a report, the goal for us is to be able to focus on one of these objects. For this reason we need to modify the query from above to take an input parameter so that we can filter the result to what we want.
133+
While it would be possible to create a transform that targets all of these devices, for example if you want to create a report, the goal for us is to be able to focus on one of these objects. For this reason we need to modify the query from above to take an input parameter so that we can filter the result to what we want.
103134

104135
Create a local directory on your computer.
105136

106137
```shell
107-
mkdir tags_render
138+
mkdir device_config_render
108139
```
109140

110-
Then save the below query as a text file named tags_query.gql.
141+
Then save the below query as a text file named `device_config.gql`.
142+
111143

112144
```graphql
113-
query TagsQuery($tag: String!) {
114-
BuiltinTag(name__value: $tag) {
145+
query DeviceQuery($name: String!) {
146+
NetworkDevice(name__value: $name) {
115147
edges {
116148
node {
117149
name {
@@ -126,30 +158,31 @@ query TagsQuery($tag: String!) {
126158
}
127159
```
128160

129-
Here the query will require an input parameter called `$name` what will refer to the name of each tag. When we want to query for the red tag the input variables to the query would look like this:
161+
Here the query will require an input parameter called `$name` what will refer to the name of each device. When we want to query for device switch1, the input variables to the query would look like this:
130162

131163
```json
132164
{
133-
"tag": "red"
165+
"name": "switch1"
134166
}
135167
```
136168

137-
## 2. Create the Jinja template
169+
## 3. Create the Jinja template
138170

139-
The next step is to create the actual Jinja Template file. Create a file called tags_tpl.j2
171+
The next step is to create the actual Jinja Template file. Create a file called `device_config.j2`.
140172

141173
```jinja2
142-
{% if data.BuiltinTag.edges and data.BuiltinTag.edges is iterable %}
143-
{% for tag in data["BuiltinTag"]["edges"] %}
144-
{% set tag_name = tag.node.name.value %}
145-
{% set tag_description = tag.node.description.value %}
146-
{{ tag_name }}
147-
description: {{ tag_description }}
174+
{% if data.NetworkDevice.edges and data.NetworkDevice.edges is iterable %}
175+
{% for device in data["NetworkDevice"]["edges"] %}
176+
{% set device_name = device.node.name.value %}
177+
{% set device_description = device.node.description.value %}
178+
hostname {{ device_name }}
179+
description "{{ device_description }}"
180+
end
148181
{% endfor %}
149182
{% endif %}
150183
```
151184

152-
## 3. Create a .infrahub.yml file
185+
## 4. Create a .infrahub.yml file
153186

154187
In the .infrahub.yml file you define what transforms you have in your repository that you want to make available for Infrahub.
155188

@@ -158,26 +191,26 @@ Create a .infrahub.yml file in the root of the directory.
158191
```yaml
159192
---
160193
jinja2_transforms:
161-
- name: my-jinja2-transform # Unique name for your transform
162-
description: "short description" # (optional)
163-
query: "tags_query" # Name or ID of the GraphQLQuery
164-
template_path: "tags_tpl.j2" # Path to the main Jinja2 template
194+
- name: device_config_transform # Unique name for your transform
195+
description: "device config transform" # (optional)
196+
query: "device_config_query" # Name or ID of the GraphQLQuery
197+
template_path: "device_config.j2" # Path to the main Jinja2 template
165198

166199
queries:
167-
- name: tags_query # Name of the GraphQLQuery
168-
file_path: "tags_query.gql" # Path to the main Jinja2 template
200+
- name: device_config_query # Name of the GraphQLQuery
201+
file_path: "device_config.gql" # Path to the main Jinja2 template
169202
```
170203
171204
> The main Jinja2 template can import other templates
172205
173206
Three parts here are required, first the `name` of the transform which should be unique across Infrahub, `query` the GraphqlQuery linked to our transform and also the `template_path` that should point to the Jinja2 file within the repository.
174207

175-
## 4. Create a Git repository
208+
## 5. Create a Git repository
176209

177-
Within the `tags_render` folder you should now have tree files:
210+
Within the `device_config_render` folder you should now have tree files:
178211

179-
* tags_query.gql: Contains the GraphQL query
180-
* tags_tpl.j2: Contains the Jinja2 Template
212+
* device_config.gql: Contains the GraphQL query
213+
* device_config.j2: Contains the Jinja2 Template
181214
* .infrahub.yml: Contains the definition for the transform
182215

183216
Before we can test our transform we must add the files to a local Git repository.
@@ -188,7 +221,7 @@ git add .
188221
git commit -m "First commit"
189222
```
190223

191-
## 5. Test the render using infrahubctl
224+
## 6. Test the render using infrahubctl
192225

193226
Using infrahubctl you can first verify that the `.infrahub.yml` file is formatted correctly by listing available transforms.
194227

@@ -224,11 +257,11 @@ If `--branch` is not provided it will automatically use the name of the local br
224257

225258
:::
226259

227-
## 6. Adding the repository to Infrahub
260+
## 7. Adding the repository to Infrahub
228261

229262
In order to avoid having the same instructions over and over please refer to the guide [adding a repository to Infrahub](../guides/repository) in order to sync the repository you created and make it available within Infrahub.
230263

231-
## 7. Accessing the Transform from the API
264+
## 8. Accessing the Transform from the API
232265

233266
A transform can be rendered on demand via the REST API with the endpoint: `https://<host>/api/transform/jinja2/<transform name or ID>`
234267

0 commit comments

Comments
 (0)