Skip to content

Commit 2c7f2cc

Browse files
romainbergertherveskarimo
authored
[datadog_synthetics_global_variable] Add support for local variable extract from test (#1567)
* [Synthetics] Add support for local variable extract for global variable from test * Update docs * Update datadog/tests/resource_datadog_synthetics_global_variable_test.go Co-authored-by: skarimo <[email protected]> * [Synthetics] Fix getting required and optional value for GV Co-authored-by: Thomas Hervé <[email protected]> Co-authored-by: skarimo <[email protected]>
1 parent 8086e84 commit 2c7f2cc

5 files changed

+396
-15
lines changed

datadog/resource_datadog_synthetics_global_variable.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func resourceDatadogSyntheticsGlobalVariable() *schema.Resource {
8181
},
8282
"parser": {
8383
Type: schema.TypeList,
84-
Required: true,
84+
Optional: true,
8585
MaxItems: 1,
8686
Elem: &schema.Resource{
8787
Schema: map[string]*schema.Schema{
@@ -99,6 +99,11 @@ func resourceDatadogSyntheticsGlobalVariable() *schema.Resource {
9999
},
100100
},
101101
},
102+
"local_variable_name": {
103+
Type: schema.TypeString,
104+
Description: "When type is `local_variable`, name of the local variable to use to extract the value.",
105+
Optional: true,
106+
},
102107
},
103108
},
104109
},
@@ -240,14 +245,20 @@ func buildSyntheticsGlobalVariableStruct(d *schema.ResourceData) *datadogV1.Synt
240245
parseTestOptions.SetField(field.(string))
241246
}
242247

243-
parser := datadogV1.SyntheticsVariableParser{}
244-
parser.SetType(datadogV1.SyntheticsGlobalVariableParserType(d.Get("parse_test_options.0.parser.0.type").(string)))
248+
if _, ok := d.GetOk("parse_test_options.0.parser"); ok {
249+
parser := datadogV1.SyntheticsVariableParser{}
250+
parser.SetType(datadogV1.SyntheticsGlobalVariableParserType(d.Get("parse_test_options.0.parser.0.type").(string)))
245251

246-
if value, ok := d.GetOk("parse_test_options.0.parser.0.value"); ok {
247-
parser.SetValue(value.(string))
252+
if value, ok := d.GetOk("parse_test_options.0.parser.0.value"); ok {
253+
parser.SetValue(value.(string))
254+
}
255+
256+
parseTestOptions.SetParser(parser)
248257
}
249258

250-
parseTestOptions.SetParser(parser)
259+
if localVariableName, ok := d.GetOk("parse_test_options.0.local_variable_name"); ok {
260+
parseTestOptions.SetLocalVariableName(localVariableName.(string))
261+
}
251262

252263
syntheticsGlobalVariable.SetParseTestOptions(parseTestOptions)
253264
}
@@ -289,19 +300,28 @@ func updateSyntheticsGlobalVariableLocalState(d *schema.ResourceData, synthetics
289300
localParser := make(map[string]string)
290301

291302
parseTestOptions := syntheticsGlobalVariable.GetParseTestOptions()
292-
parser := parseTestOptions.GetParser()
293-
294-
if v, ok := parser.GetTypeOk(); ok {
295-
localParser["type"] = string(*v)
296-
}
297-
298-
localParser["value"] = parser.GetValue()
299303

300304
localParseTestOptions["type"] = parseTestOptions.GetType()
305+
301306
if v, ok := parseTestOptions.GetFieldOk(); ok {
302307
localParseTestOptions["field"] = string(*v)
303308
}
304-
localParseTestOptions["parser"] = []map[string]string{localParser}
309+
310+
if parseTestOptions.HasParser() {
311+
parser := parseTestOptions.GetParser()
312+
313+
localParser["type"] = string(parser.GetType())
314+
315+
if value, ok := parser.GetValueOk(); ok {
316+
localParser["value"] = string(*value)
317+
}
318+
319+
localParseTestOptions["parser"] = []map[string]string{localParser}
320+
}
321+
322+
if parseTestOptions.HasLocalVariableName() {
323+
localParseTestOptions["local_variable_name"] = parseTestOptions.GetLocalVariableName()
324+
}
305325

306326
d.Set("parse_test_options", []map[string]interface{}{localParseTestOptions})
307327
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2022-09-13T20:00:51.367545+02:00
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: |
6+
{"config":{"assertions":[],"configVariables":[],"steps":[{"allowFailure":true,"assertions":[{"operator":"is","target":200,"type":"statusCode"}],"extractedValues":[{"field":"content-length","name":"LOCAL_VAR_EXTRACT","parser":{"type":"regex","value":".*"},"type":"http_header"}],"isCritical":false,"name":"First api step","request":{"allow_insecure":true,"body":"this is a body","follow_redirects":true,"method":"GET","timeout":30,"url":"https://www.datadoghq.com"},"retry":{"count":5,"interval":1000},"subtype":"http"}]},"locations":["aws:eu-central-1"],"message":"","name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","options":{"follow_redirects":true,"min_location_failed":1,"monitor_options":{"renotify_interval":120},"tick_every":60},"status":"paused","subtype":"multi","tags":[],"type":"api"}
7+
form: {}
8+
headers:
9+
Accept:
10+
- application/json
11+
Content-Type:
12+
- application/json
13+
url: https://api.datadoghq.com/api/v1/synthetics/tests/api
14+
method: POST
15+
response:
16+
body: |
17+
{"status":"paused","public_id":"9bw-vck-5p9","tags":[],"org_id":321813,"locations":["aws:eu-central-1"],"message":"","deleted_at":null,"name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","monitor_id":86719615,"type":"api","created_at":"2022-09-13T18:00:54.595386+00:00","modified_at":"2022-09-13T18:00:54.595386+00:00","subtype":"multi","config":{"steps":[{"retry":{"count":5,"interval":1000},"name":"First api step","request":{"body":"this is a body","allow_insecure":true,"url":"https://www.datadoghq.com","follow_redirects":true,"timeout":30,"method":"GET"},"subtype":"http","allowFailure":true,"assertions":[{"operator":"is","type":"statusCode","target":200}],"isCritical":false,"id":"94v-6t6-psr","extractedValues":[{"field":"content-length","parser":{"type":"regex","value":".*"},"type":"http_header","name":"LOCAL_VAR_EXTRACT"}]}],"assertions":[],"configVariables":[]},"options":{"follow_redirects":true,"monitor_options":{"include_tags":true,"renotify_interval":120,"notify_audit":false,"new_host_delay":300,"on_missing_data":"show_no_data"},"min_location_failed":1,"tick_every":60}}
18+
headers:
19+
Content-Type:
20+
- application/json
21+
status: 200 OK
22+
code: 200
23+
duration: ""
24+
- request:
25+
body: ""
26+
form: {}
27+
headers:
28+
Accept:
29+
- application/json
30+
url: https://api.datadoghq.com/api/v1/synthetics/tests/api/9bw-vck-5p9
31+
method: GET
32+
response:
33+
body: |
34+
{"status":"paused","public_id":"9bw-vck-5p9","tags":[],"locations":["aws:eu-central-1"],"message":"","name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","monitor_id":86719615,"type":"api","created_at":"2022-09-13T18:00:54.595386+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:54.595386+00:00","subtype":"multi","config":{"steps":[{"retry":{"count":5,"interval":1000},"name":"First api step","request":{"body":"this is a body","allow_insecure":true,"url":"https://www.datadoghq.com","follow_redirects":true,"timeout":30,"method":"GET"},"subtype":"http","allowFailure":true,"assertions":[{"operator":"is","type":"statusCode","target":200}],"isCritical":false,"id":"94v-6t6-psr","extractedValues":[{"field":"content-length","parser":{"type":"regex","value":".*"},"type":"http_header","name":"LOCAL_VAR_EXTRACT"}]}],"assertions":[],"configVariables":[]},"options":{"follow_redirects":true,"monitor_options":{"include_tags":true,"renotify_interval":120,"notify_audit":false,"new_host_delay":300,"on_missing_data":"show_no_data"},"min_location_failed":1,"tick_every":60}}
35+
headers:
36+
Content-Type:
37+
- application/json
38+
status: 200 OK
39+
code: 200
40+
duration: ""
41+
- request:
42+
body: |
43+
{"description":"a global variable from multistep test","name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","parse_test_options":{"localVariableName":"LOCAL_VAR_EXTRACT","type":"local_variable"},"parse_test_public_id":"9bw-vck-5p9","tags":["foo:bar","baz"],"value":{"secure":false,"value":""}}
44+
form: {}
45+
headers:
46+
Accept:
47+
- application/json
48+
Content-Type:
49+
- application/json
50+
url: https://api.datadoghq.com/api/v1/synthetics/variables
51+
method: POST
52+
response:
53+
body: |
54+
{"parse_test_options":{"type":"local_variable","localVariableName":"LOCAL_VAR_EXTRACT"},"parse_test_extracted_at":null,"description":"a global variable from multistep test","tags":["foo:bar","baz"],"last_error":null,"is_totp":null,"value":{"secure":false,"value":""},"parse_test_public_id":"9bw-vck-5p9","parse_test_name":null,"type":"variable","id":"819f1d49-7f73-4c05-8567-3626a453c9a3","name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051"}
55+
headers:
56+
Content-Type:
57+
- application/json
58+
status: 200 OK
59+
code: 200
60+
duration: ""
61+
- request:
62+
body: ""
63+
form: {}
64+
headers:
65+
Accept:
66+
- application/json
67+
url: https://api.datadoghq.com/api/v1/synthetics/variables/819f1d49-7f73-4c05-8567-3626a453c9a3
68+
method: GET
69+
response:
70+
body: '{"parse_test_extracted_at":null,"description":"a global variable from multistep test","tags":["foo:bar","baz"],"last_error":null,"parse_test_public_id":"9bw-vck-5p9","is_totp":null,"id":"819f1d49-7f73-4c05-8567-3626a453c9a3","parse_test_options":{"type":"local_variable","localVariableName":"LOCAL_VAR_EXTRACT"},"name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","created_at":"2022-09-13T18:00:55.115052+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:55.115052+00:00","value":{"secure":false,"value":""},"parse_test_name":null,"type":"variable"}'
71+
headers:
72+
Content-Type:
73+
- application/json
74+
status: 200 OK
75+
code: 200
76+
duration: ""
77+
- request:
78+
body: ""
79+
form: {}
80+
headers:
81+
Accept:
82+
- application/json
83+
url: https://api.datadoghq.com/api/v1/synthetics/variables/819f1d49-7f73-4c05-8567-3626a453c9a3
84+
method: GET
85+
response:
86+
body: '{"parse_test_extracted_at":null,"description":"a global variable from multistep test","tags":["foo:bar","baz"],"last_error":null,"parse_test_public_id":"9bw-vck-5p9","is_totp":null,"id":"819f1d49-7f73-4c05-8567-3626a453c9a3","parse_test_options":{"type":"local_variable","localVariableName":"LOCAL_VAR_EXTRACT"},"name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","created_at":"2022-09-13T18:00:55.115052+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:55.115052+00:00","value":{"secure":false,"value":""},"parse_test_name":null,"type":"variable"}'
87+
headers:
88+
Content-Type:
89+
- application/json
90+
status: 200 OK
91+
code: 200
92+
duration: ""
93+
- request:
94+
body: ""
95+
form: {}
96+
headers:
97+
Accept:
98+
- application/json
99+
url: https://api.datadoghq.com/api/v1/synthetics/variables/819f1d49-7f73-4c05-8567-3626a453c9a3
100+
method: GET
101+
response:
102+
body: '{"parse_test_extracted_at":null,"description":"a global variable from multistep test","tags":["foo:bar","baz"],"last_error":null,"parse_test_public_id":"9bw-vck-5p9","is_totp":null,"id":"819f1d49-7f73-4c05-8567-3626a453c9a3","parse_test_options":{"type":"local_variable","localVariableName":"LOCAL_VAR_EXTRACT"},"name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","created_at":"2022-09-13T18:00:55.115052+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:55.115052+00:00","value":{"secure":false,"value":""},"parse_test_name":null,"type":"variable"}'
103+
headers:
104+
Content-Type:
105+
- application/json
106+
status: 200 OK
107+
code: 200
108+
duration: ""
109+
- request:
110+
body: ""
111+
form: {}
112+
headers:
113+
Accept:
114+
- application/json
115+
url: https://api.datadoghq.com/api/v1/synthetics/tests/9bw-vck-5p9
116+
method: GET
117+
response:
118+
body: |
119+
{"status":"paused","public_id":"9bw-vck-5p9","tags":[],"locations":["aws:eu-central-1"],"message":"","name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","monitor_id":86719615,"type":"api","created_at":"2022-09-13T18:00:54.595386+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:54.595386+00:00","subtype":"multi","config":{"steps":[{"retry":{"count":5,"interval":1000},"name":"First api step","request":{"body":"this is a body","allow_insecure":true,"url":"https://www.datadoghq.com","follow_redirects":true,"timeout":30,"method":"GET"},"subtype":"http","allowFailure":true,"assertions":[{"operator":"is","type":"statusCode","target":200}],"isCritical":false,"id":"94v-6t6-psr","extractedValues":[{"field":"content-length","parser":{"type":"regex","value":".*"},"type":"http_header","name":"LOCAL_VAR_EXTRACT"}]}],"assertions":[],"configVariables":[]},"options":{"follow_redirects":true,"monitor_options":{"include_tags":true,"renotify_interval":120,"notify_audit":false,"new_host_delay":300,"on_missing_data":"show_no_data"},"tick_every":60,"min_location_failed":1}}
120+
headers:
121+
Content-Type:
122+
- application/json
123+
status: 200 OK
124+
code: 200
125+
duration: ""
126+
- request:
127+
body: ""
128+
form: {}
129+
headers:
130+
Accept:
131+
- application/json
132+
url: https://api.datadoghq.com/api/v1/synthetics/tests/9bw-vck-5p9
133+
method: GET
134+
response:
135+
body: |
136+
{"status":"paused","public_id":"9bw-vck-5p9","tags":[],"locations":["aws:eu-central-1"],"message":"","name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","monitor_id":86719615,"type":"api","created_at":"2022-09-13T18:00:54.595386+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:54.595386+00:00","subtype":"multi","config":{"steps":[{"retry":{"count":5,"interval":1000},"name":"First api step","request":{"body":"this is a body","allow_insecure":true,"url":"https://www.datadoghq.com","follow_redirects":true,"timeout":30,"method":"GET"},"subtype":"http","allowFailure":true,"assertions":[{"operator":"is","type":"statusCode","target":200}],"isCritical":false,"id":"94v-6t6-psr","extractedValues":[{"field":"content-length","parser":{"type":"regex","value":".*"},"type":"http_header","name":"LOCAL_VAR_EXTRACT"}]}],"assertions":[],"configVariables":[]},"options":{"follow_redirects":true,"monitor_options":{"include_tags":true,"renotify_interval":120,"notify_audit":false,"new_host_delay":300,"on_missing_data":"show_no_data"},"tick_every":60,"min_location_failed":1}}
137+
headers:
138+
Content-Type:
139+
- application/json
140+
status: 200 OK
141+
code: 200
142+
duration: ""
143+
- request:
144+
body: ""
145+
form: {}
146+
headers:
147+
Accept:
148+
- application/json
149+
url: https://api.datadoghq.com/api/v1/synthetics/tests/api/9bw-vck-5p9
150+
method: GET
151+
response:
152+
body: |
153+
{"status":"paused","public_id":"9bw-vck-5p9","tags":[],"locations":["aws:eu-central-1"],"message":"","name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","monitor_id":86719615,"type":"api","created_at":"2022-09-13T18:00:54.595386+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:54.595386+00:00","subtype":"multi","config":{"steps":[{"retry":{"count":5,"interval":1000},"name":"First api step","request":{"body":"this is a body","allow_insecure":true,"url":"https://www.datadoghq.com","follow_redirects":true,"timeout":30,"method":"GET"},"subtype":"http","allowFailure":true,"assertions":[{"operator":"is","type":"statusCode","target":200}],"isCritical":false,"id":"94v-6t6-psr","extractedValues":[{"field":"content-length","parser":{"type":"regex","value":".*"},"type":"http_header","name":"LOCAL_VAR_EXTRACT"}]}],"assertions":[],"configVariables":[]},"options":{"follow_redirects":true,"monitor_options":{"include_tags":true,"renotify_interval":120,"notify_audit":false,"new_host_delay":300,"on_missing_data":"show_no_data"},"tick_every":60,"min_location_failed":1}}
154+
headers:
155+
Content-Type:
156+
- application/json
157+
status: 200 OK
158+
code: 200
159+
duration: ""
160+
- request:
161+
body: ""
162+
form: {}
163+
headers:
164+
Accept:
165+
- application/json
166+
url: https://api.datadoghq.com/api/v1/synthetics/variables/819f1d49-7f73-4c05-8567-3626a453c9a3
167+
method: GET
168+
response:
169+
body: '{"parse_test_extracted_at":null,"description":"a global variable from multistep test","tags":["foo:bar","baz"],"last_error":null,"parse_test_public_id":"9bw-vck-5p9","is_totp":null,"id":"819f1d49-7f73-4c05-8567-3626a453c9a3","parse_test_options":{"type":"local_variable","localVariableName":"LOCAL_VAR_EXTRACT"},"name":"TF_TESTACCDATADOGSYNTHETICSGLOBALVARIABLEFROMTEST_LOCALVARIABLE_LOCAL_1663092051","created_at":"2022-09-13T18:00:55.115052+00:00","creator":{"handle":"[email protected]","name":null,"email":"[email protected]"},"modified_at":"2022-09-13T18:00:55.115052+00:00","value":{"secure":false,"value":""},"parse_test_name":null,"type":"variable"}'
170+
headers:
171+
Content-Type:
172+
- application/json
173+
status: 200 OK
174+
code: 200
175+
duration: ""
176+
- request:
177+
body: ""
178+
form: {}
179+
headers:
180+
Accept:
181+
- '*/*'
182+
url: https://api.datadoghq.com/api/v1/synthetics/variables/819f1d49-7f73-4c05-8567-3626a453c9a3
183+
method: DELETE
184+
response:
185+
body: '{}'
186+
headers:
187+
Content-Type:
188+
- application/json
189+
status: 200 OK
190+
code: 200
191+
duration: ""
192+
- request:
193+
body: |
194+
{"public_ids":["9bw-vck-5p9"]}
195+
form: {}
196+
headers:
197+
Accept:
198+
- application/json
199+
Content-Type:
200+
- application/json
201+
url: https://api.datadoghq.com/api/v1/synthetics/tests/delete
202+
method: POST
203+
response:
204+
body: |
205+
{"deleted_tests":[{"deleted_at":"2022-09-13T18:01:03.488705+00:00","public_id":"9bw-vck-5p9"}]}
206+
headers:
207+
Content-Type:
208+
- application/json
209+
status: 200 OK
210+
code: 200
211+
duration: ""
212+
- request:
213+
body: ""
214+
form: {}
215+
headers:
216+
Accept:
217+
- application/json
218+
url: https://api.datadoghq.com/api/v1/synthetics/tests/9bw-vck-5p9
219+
method: GET
220+
response:
221+
body: '{"errors":["Synthetics test not found"]}'
222+
headers:
223+
Content-Type:
224+
- application/json
225+
status: 404 Not Found
226+
code: 404
227+
duration: ""
228+
- request:
229+
body: ""
230+
form: {}
231+
headers:
232+
Accept:
233+
- application/json
234+
url: https://api.datadoghq.com/api/v1/synthetics/variables/819f1d49-7f73-4c05-8567-3626a453c9a3
235+
method: GET
236+
response:
237+
body: '{"errors": ["Synthetics global variable not found"]}'
238+
headers:
239+
Content-Type:
240+
- application/json
241+
status: 404 Not Found
242+
code: 404
243+
duration: ""

0 commit comments

Comments
 (0)