Skip to content

Commit 3e71b80

Browse files
authored
fix(r/trigger): validate threshold when baseline_details in use (#708)
Adds validation support for baseline triggers so that basic constraints can be caught prior to the `apply` phase. - Closes #704
1 parent 4f6a7da commit 3e71b80

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

internal/provider/trigger_resource.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,39 @@ func (r *triggerResource) ValidateConfig(ctx context.Context, req resource.Valid
661661
)
662662
}
663663
}
664+
665+
// ensure baseline_details conditions are met
666+
if !data.BaselineDetails.IsNull() && !data.BaselineDetails.IsUnknown() {
667+
threshold := expandTriggerThreshold(ctx, data.Threshold, &resp.Diagnostics)
668+
if resp.Diagnostics.HasError() {
669+
return
670+
}
671+
672+
switch threshold.Op {
673+
case client.TriggerThresholdOpGreaterThanOrEqual:
674+
if threshold.Value < 0 {
675+
resp.Diagnostics.AddAttributeError(
676+
path.Root("threshold").AtName("value"),
677+
"Trigger validation error",
678+
"Triggers with baseline_details and a threshold operator of '>=' must have a value greater than or equal to 0.",
679+
)
680+
}
681+
case client.TriggerThresholdOpLessThanOrEqual:
682+
if threshold.Value > 0 {
683+
resp.Diagnostics.AddAttributeError(
684+
path.Root("threshold").AtName("value"),
685+
"Trigger validation error",
686+
"Triggers with baseline_details and a threshold operator of '<=' must have a value less than or equal to 0.",
687+
)
688+
}
689+
default:
690+
resp.Diagnostics.AddAttributeError(
691+
path.Root("threshold").AtName("op"),
692+
"Trigger validation error",
693+
"Triggers with baseline_details must use a threshold operator of '>=' or '<='.",
694+
)
695+
}
696+
}
664697
}
665698

666699
func expandTriggerThreshold(

internal/provider/trigger_resource_test.go

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,151 @@ func TestAcc_TriggerResource(t *testing.T) {
192192
})
193193
})
194194

195-
t.Run("trigger with partial baseline_details errors", func(t *testing.T) {
195+
t.Run("trigger with baseline_details validates", func(t *testing.T) {
196196
resource.Test(t, resource.TestCase{
197197
PreCheck: testAccPreCheck(t),
198198
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
199199
Steps: []resource.TestStep{
200200
{
201201
Config: testAccConfigBasicTriggerWithBaselineDetailsTest(dataset, name, true, true),
202202
ExpectError: regexp.MustCompile(`The argument "offset_minutes" is required, but no definition was found.`),
203+
PlanOnly: true,
204+
},
205+
{
206+
Config: `
207+
data "honeycombio_query_specification" "test" {
208+
calculation {
209+
op = "AVG"
210+
column = "duration_ms"
211+
}
212+
213+
time_range = 1200
214+
}
215+
216+
resource "honeycombio_trigger" "test" {
217+
name = "Test trigger with baseline_details errors"
218+
dataset = "foobar"
219+
220+
description = "My nice description"
221+
222+
query_json = data.honeycombio_query_specification.test.json
223+
224+
threshold {
225+
op = ">"
226+
value = 100
227+
}
228+
229+
baseline_details {
230+
type = "percentage"
231+
offset_minutes = 1440
232+
}
233+
234+
frequency = 1200
235+
}`,
236+
ExpectError: regexp.MustCompile(`must use a threshold operator of '>=' or '<='`),
237+
PlanOnly: true,
238+
},
239+
{
240+
Config: `
241+
data "honeycombio_query_specification" "test" {
242+
calculation {
243+
op = "AVG"
244+
column = "duration_ms"
245+
}
246+
247+
time_range = 1200
248+
}
249+
250+
resource "honeycombio_trigger" "test" {
251+
name = "Test trigger with baseline_details errors"
252+
dataset = "foobar"
253+
254+
description = "My nice description"
255+
256+
query_json = data.honeycombio_query_specification.test.json
257+
258+
threshold {
259+
op = ">="
260+
value = -838
261+
}
262+
263+
baseline_details {
264+
type = "percentage"
265+
offset_minutes = 1440
266+
}
267+
268+
frequency = 1200
269+
}`,
270+
ExpectError: regexp.MustCompile(`value greater than or equal to 0`),
271+
PlanOnly: true,
272+
},
273+
{
274+
Config: `
275+
data "honeycombio_query_specification" "test" {
276+
calculation {
277+
op = "AVG"
278+
column = "duration_ms"
279+
}
280+
281+
time_range = 1200
282+
}
283+
284+
resource "honeycombio_trigger" "test" {
285+
name = "Test trigger with baseline_details errors"
286+
dataset = "foobar"
287+
288+
description = "My nice description"
289+
290+
query_json = data.honeycombio_query_specification.test.json
291+
292+
threshold {
293+
op = "<"
294+
value = 100
295+
}
296+
297+
baseline_details {
298+
type = "percentage"
299+
offset_minutes = 1440
300+
}
301+
302+
frequency = 1200
303+
}`,
304+
ExpectError: regexp.MustCompile(`must use a threshold operator of '>=' or '<='`),
305+
PlanOnly: true,
306+
},
307+
{
308+
Config: `
309+
data "honeycombio_query_specification" "test" {
310+
calculation {
311+
op = "AVG"
312+
column = "duration_ms"
313+
}
314+
315+
time_range = 1200
316+
}
317+
318+
resource "honeycombio_trigger" "test" {
319+
name = "Test trigger with baseline_details errors"
320+
dataset = "foobar"
321+
322+
description = "My nice description"
323+
324+
query_json = data.honeycombio_query_specification.test.json
325+
326+
threshold {
327+
op = "<="
328+
value = 100
329+
}
330+
331+
baseline_details {
332+
type = "percentage"
333+
offset_minutes = 1440
334+
}
335+
336+
frequency = 1200
337+
}`,
338+
ExpectError: regexp.MustCompile(`value less than or equal to 0`),
339+
PlanOnly: true,
203340
},
204341
},
205342
})

0 commit comments

Comments
 (0)