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/artifact.mdx
+58-15Lines changed: 58 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,56 @@ import ReactPlayer from 'react-player/youtube'
8
8
9
9
:::note
10
10
11
-
This guide has the assumption that you followed the [Creating a Jinja Rendered File (Transform)](./jinja2-transform.mdx) or [Creating a Python transform](./python-transform.mdx) guide as well as [Creating a Group](./groups.mdx) guide
12
-
This is a prerequisite.
11
+
This guide has the assumption that you followed the [Creating a Jinja Rendered File (Transform)](./jinja2-transform.mdx) or [Creating a Python transform](./python-transform.mdx) guide. This is a prerequisite.
13
12
14
13
:::
15
14
16
15
The goal of this guide is to define an artifact for a Jinja rendered Transform or Python transform in Infrahub. We will be using the following steps.
17
16
18
-
1. Creating an artifact definition
19
-
2. Accessing the artifacts
17
+
1. Modifying the schema
18
+
2. Creating a group and adding member nodes
19
+
2. Creating an artifact definition
20
+
3. Accessing the artifacts
21
+
22
+
## Modifying the node schema
23
+
24
+
Before we can generate an artifact for a node, we need to modify the schema. The node will need to inherit from the `CoreArtifactTarget` generic.
25
+
26
+
```yaml
27
+
---
28
+
version: "1.0"
29
+
nodes:
30
+
- name: Device
31
+
namespace: Network
32
+
display_labels:
33
+
- name__value
34
+
inherit_from:
35
+
- CoreArtifactTarget
36
+
attributes:
37
+
- name: name
38
+
kind: Text
39
+
label: Name
40
+
optional: false
41
+
unique: true
42
+
- name: description
43
+
kind: Text
44
+
label: Description
45
+
optional: true
46
+
```
47
+
48
+
We then have to load the modified schema definition into Infrahub.
49
+
50
+
```bash
51
+
infrahubctl schema load /tmp/schema.yml
52
+
```
53
+
54
+
## Creating a group and adding members
55
+
56
+
We need to create a group for every Artifact Definition that we will define. An artifact will be generated for every member that is part of that group.
57
+
58
+
Create a Standard Group with the name `DeviceGroup` and add `NetworkDevice``switch1`, `switch2` and `switch3` as a member.
59
+
60
+
More information can be found in the [Creating a group guide](./groups.mdx).
@@ -26,23 +67,23 @@ The goal of this guide is to define an artifact for a Jinja rendered Transform o
26
67
27
68
In the last step we need to define an Artifact definition, which groups together a [transformation](../topics/transformation.mdx) with a target group and forms the definition of the artifact. Artifact definitions can be created via the frontend, via GraphQL or via a [Git repository](../topics/repository.mdx). In this guide we will be using the Git repository.
28
69
29
-
Add the following contents to the end of the `.infrahub.yml` file at the root of the `tags_render` repository.
70
+
Add the following contents to the end of the `.infrahub.yml` file at the root of the `device_config_render` repository.
30
71
31
72
```yaml
32
73
artifact_definitions:
33
-
- name: "tags_config_file"
34
-
artifact_name: "Tags configuration file"
74
+
- name: "device_configuration"
75
+
artifact_name: "Device configuration file"
35
76
parameters:
36
-
tag: "name__value"
77
+
name: "name__value"
37
78
content_type: "text/plain"
38
-
targets: "TagConfigGroup"
39
-
transformation: "my-transform"
79
+
targets: "DeviceGroup"
80
+
transformation: "device_config_transform"
40
81
```
41
82
42
83
This defines an artifact with the following properties:
43
84
44
85
- **name**: a unique name for the artifact
45
-
- **parameters**: the parameter to pass to the transformation GraphQL query, in this case this we will pass the name of the object (tag) as the tag parameter
86
+
- **parameters**: the parameter to pass to the transformation GraphQL query, in this case this we will pass the name of the object (device) as the name parameter
46
87
- **content type**: the content type for the resulting artifact
47
88
- **targets**: the name of a group of which the members will be a target for this artifact
48
89
- **transformation**: the Jinja2 or Python transformation that should be used
@@ -53,11 +94,11 @@ Commit the changes to the repository and push them to the Git server
The artifact definition will be created in the database, when the Task worker(s) notice the change in the Git repository. The `tags_config_file` should now be visible in the Artifact Definition view in the web interface.
101
+
The artifact definition will be created in the database, when the Task worker(s) notice the change in the Git repository. The `device_configuration` should now be visible in the Artifact Definition view in the web interface.
You can download the artifact by clicking on the `Storage Id` or alternatively through the rest API endpoint `http://<INFRAHUB_HOST:INFRAHUB_PORT>/api/storage/object/<storage_id>`
113
+
You can download the artifact by clicking on the download button, or by using the REST API endpoint `http://<INFRAHUB_HOST:INFRAHUB_PORT>/api/storage/object/<storage_id>`. The `Storage Id` can be copied from the menu next to the artifact name.
Optionally, Infrahub can create a relation between a node and an artifact, by inheriting from the CoreArtifactTarget generic in the schema of the node. This is outside the scope of this guide, since the BuiltinTag node does not inherit from CoreArtifactTarget. More information can be found in the [artifact](../topics/artifact.mdx) topic.
117
+
Alternatively we can access the artifact of an object, by navigating to the object detail view. Navigate to device `switch1`'s detail page, from the *Artifacts* tab you can access all the artifacts that were generated for this object.
Copy file name to clipboardExpand all lines: docs/docs/guides/jinja2-transform.mdx
+75-44Lines changed: 75 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,18 +16,48 @@ 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.
19
+
## 1. Loading a schema
20
20
21
-
## 1. Creating a query to collect the desired data
21
+
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.
22
+
23
+
```yaml
24
+
---
25
+
version: "1.0"
26
+
nodes:
27
+
- name: Device
28
+
namespace: Network
29
+
display_labels:
30
+
- name__value
31
+
attributes:
32
+
- name: name
33
+
kind: Text
34
+
label: Name
35
+
optional: false
36
+
unique: true
37
+
- name: description
38
+
kind: Text
39
+
label: Description
40
+
optional: true
41
+
```
42
+
43
+
Store the schema as a YAML file on your local disk, and load the schema into Infrahub using the following command
44
+
45
+
```bash
46
+
infrahubctl schema load /path/to/schema.yml
47
+
```
48
+
49
+
More information on loading schema files into Infrahub can be found [here](../guides/import-schema#load-a-schema-file).
50
+
51
+
## 2. Creating a query to collect the desired data
22
52
23
53
As the first step we need to have some data in the database to actually query.
24
54
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).
55
+
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
56
27
57
```graphql
28
-
mutationCreateTags {
29
-
BuiltinTagCreate(
30
-
data: {name: {value: "red"}, description: {value: "The red tag"}}
58
+
mutationCreateDevice {
59
+
NetworkDeviceCreate(
60
+
data: {name: {value: "switch1"}, description: {value: "This is device switch1"}}
31
61
) {
32
62
ok
33
63
object {
@@ -40,8 +70,8 @@ mutation CreateTags {
40
70
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
71
42
72
```graphql
43
-
queryTagsQuery {
44
-
BuiltinTag {
73
+
queryDeviceQuery {
74
+
NetworkDevice {
45
75
edges {
46
76
node {
47
77
name {
@@ -61,35 +91,35 @@ Response to the tags query:
61
91
```json
62
92
{
63
93
"data": {
64
-
"BuiltinTag": {
94
+
"NetworkDevice": {
65
95
"edges": [
66
96
{
67
97
"node": {
68
98
"name": {
69
-
"value": "blue"
99
+
"value": "switch1"
70
100
},
71
101
"description": {
72
-
"value": "The blue tag"
102
+
"value": "This is device switch1"
73
103
}
74
104
}
75
105
},
76
106
{
77
107
"node": {
78
108
"name": {
79
-
"value": "green"
109
+
"value": "switch2"
80
110
},
81
111
"description": {
82
-
"value": "The green tag"
112
+
"value": "This is device switch2"
83
113
}
84
114
}
85
115
},
86
116
{
87
117
"node": {
88
118
"name": {
89
-
"value": "red"
119
+
"value": "switch3"
90
120
},
91
121
"description": {
92
-
"value": "The red tag"
122
+
"value": "This is device switch3"
93
123
}
94
124
}
95
125
}
@@ -99,19 +129,19 @@ Response to the tags query:
99
129
}
100
130
```
101
131
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.
132
+
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
133
104
134
Create a local directory on your computer.
105
135
106
136
```shell
107
-
mkdir tags_render
137
+
mkdir device_config_render
108
138
```
109
139
110
-
Then save the below query as a text file named tags_query.gql.
140
+
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:
159
+
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
160
131
161
```json
132
162
{
133
-
"tag": "red"
163
+
"name": "switch1"
134
164
}
135
165
```
136
166
137
-
## 2. Create the Jinja template
167
+
## 3. Create the Jinja template
138
168
139
-
The next step is to create the actual Jinja Template file. Create a file called tags_tpl.j2
169
+
The next step is to create the actual Jinja Template file. Create a file called `device_config.j2`.
140
170
141
171
```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 }}
172
+
{% if data.NetworkDevice.edges and data.NetworkDevice.edges is iterable %}
173
+
{% for device in data["NetworkDevice"]["edges"] %}
174
+
{% set device_name = device.node.name.value %}
175
+
{% set device_description = device.node.description.value %}
176
+
hostname {{ device_name }}
177
+
description "{{ device_description }}"
178
+
end
148
179
{% endfor %}
149
180
{% endif %}
150
181
```
151
182
152
-
## 3. Create a .infrahub.yml file
183
+
## 4. Create a .infrahub.yml file
153
184
154
185
In the .infrahub.yml file you define what transforms you have in your repository that you want to make available for Infrahub.
155
186
@@ -158,26 +189,26 @@ Create a .infrahub.yml file in the root of the directory.
158
189
```yaml
159
190
---
160
191
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
192
+
- name: device_config_transform # Unique name for your transform
query: "device_config_query"# Name or ID of the GraphQLQuery
195
+
template_path: "device_config.j2"# Path to the main Jinja2 template
165
196
166
197
queries:
167
-
- name: tags_query# Name of the GraphQLQuery
168
-
file_path: "tags_query.gql"# Path to the main Jinja2 template
198
+
- name: device_config_query# Name of the GraphQLQuery
199
+
file_path: "device_config.gql"# Path to the main Jinja2 template
169
200
```
170
201
171
202
> The main Jinja2 template can import other templates
172
203
173
204
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
205
175
-
## 4. Create a Git repository
206
+
## 5. Create a Git repository
176
207
177
-
Within the `tags_render` folder you should now have tree files:
208
+
Within the `device_config_render` folder you should now have tree files:
178
209
179
-
* tags_query.gql: Contains the GraphQL query
180
-
* tags_tpl.j2: Contains the Jinja2 Template
210
+
* device_config.gql: Contains the GraphQL query
211
+
* device_config.j2: Contains the Jinja2 Template
181
212
* .infrahub.yml: Contains the definition for the transform
182
213
183
214
Before we can test our transform we must add the files to a local Git repository.
@@ -188,7 +219,7 @@ git add .
188
219
git commit -m "First commit"
189
220
```
190
221
191
-
## 5. Test the render using infrahubctl
222
+
## 6. Test the render using infrahubctl
192
223
193
224
Using infrahubctl you can first verify that the `.infrahub.yml` file is formatted correctly by listing available transforms.
194
225
@@ -224,11 +255,11 @@ If `--branch` is not provided it will automatically use the name of the local br
224
255
225
256
:::
226
257
227
-
## 6. Adding the repository to Infrahub
258
+
## 7. Adding the repository to Infrahub
228
259
229
260
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
261
231
-
## 7. Accessing the Transform from the API
262
+
## 8. Accessing the Transform from the API
232
263
233
264
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