You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/guides/jinja2-transform.mdx
+77-44Lines changed: 77 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,18 +16,49 @@ The goal of this guide is to develop a Jinja Transform and add it to Infrahub, w
16
16
6. Add the repository to Infrahub as an external repository
17
17
7. Validate that the transform works by using the render API endpoint
18
18
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.
20
19
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
22
53
23
54
As the first step we need to have some data in the database to actually query.
24
55
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).
26
57
27
58
```graphql
28
-
mutationCreateTags {
29
-
BuiltinTagCreate(
30
-
data: {name: {value: "red"}, description: {value: "The red tag"}}
59
+
mutationCreateDevice {
60
+
NetworkDeviceCreate(
61
+
data: {name: {value: "switch1"}, description: {value: "This is device switch1"}}
31
62
) {
32
63
ok
33
64
object {
@@ -40,8 +71,8 @@ mutation CreateTags {
40
71
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.
41
72
42
73
```graphql
43
-
queryTagsQuery {
44
-
BuiltinTag {
74
+
queryDeviceQuery {
75
+
NetworkDevice {
45
76
edges {
46
77
node {
47
78
name {
@@ -61,35 +92,35 @@ Response to the tags query:
61
92
```json
62
93
{
63
94
"data": {
64
-
"BuiltinTag": {
95
+
"NetworkDevice": {
65
96
"edges": [
66
97
{
67
98
"node": {
68
99
"name": {
69
-
"value": "blue"
100
+
"value": "switch1"
70
101
},
71
102
"description": {
72
-
"value": "The blue tag"
103
+
"value": "This is device switch1"
73
104
}
74
105
}
75
106
},
76
107
{
77
108
"node": {
78
109
"name": {
79
-
"value": "green"
110
+
"value": "switch2"
80
111
},
81
112
"description": {
82
-
"value": "The green tag"
113
+
"value": "This is device switch2"
83
114
}
84
115
}
85
116
},
86
117
{
87
118
"node": {
88
119
"name": {
89
-
"value": "red"
120
+
"value": "switch3"
90
121
},
91
122
"description": {
92
-
"value": "The red tag"
123
+
"value": "This is device switch3"
93
124
}
94
125
}
95
126
}
@@ -99,19 +130,20 @@ Response to the tags query:
99
130
}
100
131
```
101
132
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.
103
134
104
135
Create a local directory on your computer.
105
136
106
137
```shell
107
-
mkdir tags_render
138
+
mkdir device_config_render
108
139
```
109
140
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`.
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:
130
162
131
163
```json
132
164
{
133
-
"tag": "red"
165
+
"name": "switch1"
134
166
}
135
167
```
136
168
137
-
## 2. Create the Jinja template
169
+
## 3. Create the Jinja template
138
170
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`.
140
172
141
173
```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
148
181
{% endfor %}
149
182
{% endif %}
150
183
```
151
184
152
-
## 3. Create a .infrahub.yml file
185
+
## 4. Create a .infrahub.yml file
153
186
154
187
In the .infrahub.yml file you define what transforms you have in your repository that you want to make available for Infrahub.
155
188
@@ -158,26 +191,26 @@ Create a .infrahub.yml file in the root of the directory.
158
191
```yaml
159
192
---
160
193
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
query: "device_config_query"# Name or ID of the GraphQLQuery
197
+
template_path: "device_config.j2"# Path to the main Jinja2 template
165
198
166
199
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
169
202
```
170
203
171
204
> The main Jinja2 template can import other templates
172
205
173
206
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.
174
207
175
-
## 4. Create a Git repository
208
+
## 5. Create a Git repository
176
209
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:
178
211
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
181
214
* .infrahub.yml: Contains the definition for the transform
182
215
183
216
Before we can test our transform we must add the files to a local Git repository.
@@ -188,7 +221,7 @@ git add .
188
221
git commit -m "First commit"
189
222
```
190
223
191
-
## 5. Test the render using infrahubctl
224
+
## 6. Test the render using infrahubctl
192
225
193
226
Using infrahubctl you can first verify that the `.infrahub.yml` file is formatted correctly by listing available transforms.
194
227
@@ -224,11 +257,11 @@ If `--branch` is not provided it will automatically use the name of the local br
224
257
225
258
:::
226
259
227
-
## 6. Adding the repository to Infrahub
260
+
## 7. Adding the repository to Infrahub
228
261
229
262
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.
230
263
231
-
## 7. Accessing the Transform from the API
264
+
## 8. Accessing the Transform from the API
232
265
233
266
A transform can be rendered on demand via the REST API with the endpoint: `https://<host>/api/transform/jinja2/<transform name or ID>`
0 commit comments