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/python-transform.mdx
+98-75Lines changed: 98 additions & 75 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,18 +18,46 @@ The goal of this guide is to develop a Python Transform and add it to Infrahub,
18
18
6. Add the repository to Infrahub as an external repository
19
19
7. Validate that the transform works by using the transform API endpoint
20
20
21
-
In this guide we are going to work with the builtin `Tag` objects in Infrahub. It won't provide a transform that is very useful, the goal is instead to show how the transforms are created. Once you have mastered the basics you will be ready to go on to create more advanced transforms.
21
+
## 1. Loading a schema
22
22
23
-
## 1. Creating a query to collect the desired data
23
+
In this guide we are going to work with a very simplistic network device model. I won't provide a transform that is very useful, the goal is instead to show how the transforms are created. Once you have mastered the basics you will be ready to go on to create more advanced transforms.
24
+
25
+
```yaml
26
+
---
27
+
version: "1.0"
28
+
nodes:
29
+
- name: Device
30
+
namespace: Network
31
+
display_labels:
32
+
- name__value
33
+
attributes:
34
+
- name: name
35
+
kind: Text
36
+
label: Name
37
+
optional: false
38
+
unique: true
39
+
- name: description
40
+
kind: Text
41
+
label: Description
42
+
optional: true
43
+
```
44
+
45
+
Store the schema as a YAML file on your local disk, and load the schema into Infrahub using the following command
46
+
47
+
```bash
48
+
infrahubctl schema load /path/to/schema.yml
49
+
```
50
+
51
+
## 2. Creating a query to collect the desired data
24
52
25
53
As the first step we need to have some data in the database to actually query.
26
54
27
-
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).
28
56
29
-
```graphql #1-2
30
-
mutationCreateTags {
31
-
BuiltinTagCreate(
32
-
data: {name: {value: "red"}, description: {value: "The red tag"}}
57
+
```graphql
58
+
mutationCreateDevice {
59
+
NetworkDeviceCreate(
60
+
data: {name: {value: "switch1"}, description: {value: "This is device switch1"}}
33
61
) {
34
62
ok
35
63
object {
@@ -41,9 +69,9 @@ mutation CreateTags {
41
69
42
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.
43
71
44
-
```graphql #1-2
45
-
queryTagsQuery {
46
-
BuiltinTag {
72
+
```graphql
73
+
queryDeviceQuery {
74
+
NetworkDevice {
47
75
edges {
48
76
node {
49
77
name {
@@ -63,35 +91,35 @@ Response to the tags query:
63
91
```json
64
92
{
65
93
"data": {
66
-
"BuiltinTag": {
94
+
"NetworkDevice": {
67
95
"edges": [
68
96
{
69
97
"node": {
70
98
"name": {
71
-
"value": "blue"
99
+
"value": "switch1"
72
100
},
73
101
"description": {
74
-
"value": "The blue tag"
102
+
"value": "This is device switch1"
75
103
}
76
104
}
77
105
},
78
106
{
79
107
"node": {
80
108
"name": {
81
-
"value": "green"
109
+
"value": "switch2"
82
110
},
83
111
"description": {
84
-
"value": "The green tag"
112
+
"value": "This is device switch2"
85
113
}
86
114
}
87
115
},
88
116
{
89
117
"node": {
90
118
"name": {
91
-
"value": "red"
119
+
"value": "switch3"
92
120
},
93
121
"description": {
94
-
"value": "The red tag"
122
+
"value": "This is device switch3"
95
123
}
96
124
}
97
125
}
@@ -101,19 +129,20 @@ Response to the tags query:
101
129
}
102
130
```
103
131
104
-
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.
105
133
106
-
Create a local directory on your computer:
134
+
Create a local directory on your computer.
107
135
108
136
```shell
109
-
mkdir tags_transform
137
+
mkdir device_config_render
110
138
```
111
139
112
-
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:
160
+
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:
132
161
133
162
```json
134
163
{
135
-
"tag": "red"
164
+
"name": "switch1"
136
165
}
137
166
```
138
167
139
168
## 2. Create the Python transform file
140
169
141
-
The next step is to create the actual Python transform. The transform is a Python class that inherits from InfrahubTransform from the [Python SDK]($(local_base_url_1)python-sdk). Create a file called `tags_transform.py`:
170
+
The next step is to create the actual Python transform. The transform is a Python class that inherits from InfrahubTransform from the [Python SDK]($(local_base_url_1)python-sdk). Create a file called `device_config.py`:
142
171
143
172
```python
144
173
from infrahub_sdk.transforms import InfrahubTransform
@@ -171,37 +199,36 @@ from infrahub_sdk.transforms import InfrahubTransform
171
199
2. We define our own class based on InfrahubTransform.
172
200
173
201
```python
174
-
classTagsTransform(InfrahubTransform):
202
+
classDeviceConfigTransform(InfrahubTransform):
175
203
```
176
204
177
205
Here we need to note the name of the class as we will need it later, optionally we can just call it `Transform` which is the default name.
178
206
179
207
3. We define where data comes from and what API endpoint to use.
180
208
181
209
```python
182
-
query ="tags_query"
183
-
url ="my-tags"
210
+
query ="device_config_query"
184
211
```
185
212
186
-
The query part refers to the file tags_query.gql that we created earlier. The URL parameter controls what the endpoint will be when you run this transform by targeting the API server.
213
+
The query part refers to the the query that we will define in the `.infrahub.yml` repository configuration file later in the guide.
187
214
188
-
With this configuration the endpoint of our transform will be [http://localhost:8000/api/transform/my-tags](http://localhost:8000/api/transform/my-tags).
215
+
With this configuration the endpoint of our transform will be [http://localhost:8000/api/transform/python/device_config_transform](http://localhost:8000/api/transform/python/device_config_transform).
When running the transform the `data` input variable will consist of the response to the query we created. In this case we return a JSON object consisting of two keys `tags_title` and `bold_description` where we have modified the data in some way. Here you would return data in the format you need.
231
+
When running the transform the `data` input variable will consist of the response to the query we created. In this case we return a JSON object consisting of two keys `device_hostname` and `device_description` where we have modified the data in some way. Here you would return data in the format you need.
205
232
206
233
:::info
207
234
@@ -210,9 +237,9 @@ If you are unsure of the format of the data you can set a debug marker when test
210
237
```python
211
238
asyncdeftransform(self, data):
212
239
breakpoint()
213
-
tag= data["BuiltinTag"]["edges"][0]["node"]
214
-
tag_name=tag["name"]["value"]
215
-
tag_description=tag["description"]["value"]
240
+
device= data["BuiltinTag"]["edges"][0]["node"]
241
+
device_name=device["name"]["value"]
242
+
device_description=device["description"]["value"]
216
243
```
217
244
218
245
:::
@@ -226,18 +253,18 @@ Create a `.infrahub.yml` file in the root of the directory.
226
253
```yaml
227
254
---
228
255
python_transforms:
229
-
- name: tags_transform
230
-
class_name: TagsTransform
231
-
file_path: "tags_transform.py"
256
+
- name: device_config_transform
257
+
class_name: DeviceConfigTransform
258
+
file_path: "device_config.py"
232
259
233
260
queries:
234
-
- name: tags_query
235
-
file_path: "tags_query.gql"
261
+
- name: device_config_query
262
+
file_path: "device_config.gql"
236
263
```
237
264
238
265
<Tabs>
239
266
<TabItem value="Python transform" default>
240
-
Two parts here are required, first the `name` of the transform which should be unique across Infrahub and also the `file_path` that should point to the Python file within the repository. In this example we have also defined `class_name`, the reason for this is that we gave our class the name `TagsTransform` instead of the default `Transform`.
267
+
Two parts here are required, first the `name` of the transform which should be unique across Infrahub and also the `file_path` that should point to the Python file within the repository. In this example we have also defined `class_name`, the reason for this is that we gave our class the name `DeviceConfigTransform` instead of the default `Transform`.
241
268
</TabItem>
242
269
<TabItem value="Queries">
243
270
Here the `name` refers to the query's name and `file_path` should point to the GraphQL file within the repository.
@@ -248,10 +275,10 @@ See [this topic](../topics/infrahub-yml) for a full explanation of everything th
248
275
249
276
## 4. Create a Git repository
250
277
251
-
Within the `tags_transform` folder you should now have 3 files:
278
+
Within the `device_config_render` folder you should now have 3 files:
252
279
253
-
* `tags_query.gql`: Contains the GraphQL query
254
-
* `tags_transform.py`: Contains the Python code for the transform
280
+
* `device_config.gql`: Contains the GraphQL query
281
+
* `device_config.py`: Contains the Python code for the transform
255
282
* `.infrahub.yml`: Contains the definition for the transform
256
283
257
284
Before we can test our transform we must add the files to a local Git repository.
@@ -268,29 +295,26 @@ Using infrahubctl you can first verify that the `.infrahub.yml` file is formatte
0 commit comments