Skip to content

Commit 79c16f1

Browse files
committed
feat(alerts): introduce duration attribute
1 parent 1effafc commit 79c16f1

18 files changed

+300
-57
lines changed

sysdig/internal/client/v2/model.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,13 @@ type AlertV2Common struct {
712712
type AlertV2ConfigPrometheus struct {
713713
Query string `json:"query"`
714714
KeepFiringForSec *int `json:"keepFiringForSec,omitempty"`
715+
716+
Duration int `json:"duration"`
715717
}
716718

717719
type AlertV2Prometheus struct {
718720
AlertV2Common
719-
DurationSec int `json:"durationSec"`
720-
Config AlertV2ConfigPrometheus `json:"config"`
721+
Config AlertV2ConfigPrometheus `json:"config"`
721722
}
722723

723724
type alertV2PrometheusWrapper struct {
@@ -755,12 +756,13 @@ type AlertV2ConfigEvent struct {
755756

756757
Filter string `json:"filter"`
757758
Tags []string `json:"tags"`
759+
760+
Range int `json:"range"`
758761
}
759762

760763
type AlertV2Event struct {
761764
AlertV2Common
762-
DurationSec int `json:"durationSec"`
763-
Config AlertV2ConfigEvent `json:"config"`
765+
Config AlertV2ConfigEvent `json:"config"`
764766
}
765767

766768
type alertV2EventWrapper struct {
@@ -796,11 +798,13 @@ type AlertV2ConfigMetric struct {
796798
TimeAggregation string `json:"timeAggregation"`
797799
Metric AlertMetricDescriptorV2 `json:"metric"`
798800
NoDataBehaviour string `json:"noDataBehaviour"`
801+
802+
Range int `json:"range"`
803+
Duration int `json:"duration"`
799804
}
800805

801806
type AlertV2Metric struct {
802807
AlertV2Common
803-
DurationSec int `json:"durationSec"`
804808
Config AlertV2ConfigMetric `json:"config"`
805809
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
806810
}
@@ -818,11 +822,12 @@ type AlertV2ConfigDowntime struct {
818822
GroupAggregation string `json:"groupAggregation"`
819823
TimeAggregation string `json:"timeAggregation"`
820824
Metric AlertMetricDescriptorV2 `json:"metric"`
825+
826+
Range int `json:"range"`
821827
}
822828

823829
type AlertV2Downtime struct {
824830
AlertV2Common
825-
DurationSec int `json:"durationSec"`
826831
Config AlertV2ConfigDowntime `json:"config"`
827832
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
828833
}
@@ -856,11 +861,12 @@ type AlertV2ConfigFormBasedPrometheus struct {
856861
WarningConditionOperator string `json:"warningConditionOperator,omitempty"`
857862
WarningThreshold *float64 `json:"warningThreshold,omitempty"`
858863
NoDataBehaviour string `json:"noDataBehaviour"`
864+
865+
Duration int `json:"duration"`
859866
}
860867

861868
type AlertV2FormBasedPrometheus struct {
862869
AlertV2Common
863-
DurationSec int `json:"durationSec"` // not really used but the api wants it set to 0 in POST/PUT
864870
Config AlertV2ConfigFormBasedPrometheus `json:"config"`
865871
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
866872
}
@@ -881,11 +887,12 @@ type AlertV2ConfigGroupOutlier struct {
881887
TimeAggregation string `json:"timeAggregation"`
882888
Metric AlertMetricDescriptorV2 `json:"metric"`
883889
NoDataBehaviour string `json:"noDataBehaviour"`
890+
891+
ObservationWindow int `json:"observationWindow"`
884892
}
885893

886894
type AlertV2GroupOutlier struct {
887895
AlertV2Common
888-
DurationSec int `json:"durationSec"` // Observation window should be greater than or equal to 10 minutes
889896
Config AlertV2ConfigGroupOutlier `json:"config"`
890897
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
891898
}
@@ -896,7 +903,6 @@ type alertV2GroupOutlierWrapper struct {
896903

897904
type AlertV2Change struct {
898905
AlertV2Common
899-
DurationSec int `json:"durationSec"` // not really used but the api wants it set to 0 in POST/PUT
900906
Config AlertV2ConfigChange `json:"config"`
901907
UnreportedAlertNotificationsRetentionSec *int `json:"unreportedAlertNotificationsRetentionSec"`
902908
}

sysdig/resource_sysdig_monitor_alert_v2_change.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ func buildAlertV2ChangeStruct(d *schema.ResourceData) (*v2.AlertV2Change, error)
312312

313313
alert := &v2.AlertV2Change{
314314
AlertV2Common: *alertV2Common,
315-
DurationSec: 0,
316315
Config: config,
317316
UnreportedAlertNotificationsRetentionSec: unreportedAlertNotificationsRetentionSec,
318317
}

sysdig/resource_sysdig_monitor_alert_v2_downtime.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@ func resourceSysdigMonitorAlertV2Downtime() *schema.Resource {
3333

3434
Schema: createScopedSegmentedAlertV2Schema(createAlertV2Schema(map[string]*schema.Schema{
3535
"trigger_after_minutes": {
36-
Type: schema.TypeInt,
37-
Required: true,
36+
Type: schema.TypeInt,
37+
Optional: true,
38+
Computed: true, // computed if range_seconds is defined
39+
Deprecated: "Use range_seconds instead",
40+
ValidateFunc: validation.IntAtLeast(1),
41+
},
42+
"range_seconds": {
43+
Type: schema.TypeInt,
44+
Optional: true,
45+
Computed: true, // computed if trigger_after_minutes is defined
46+
ExactlyOneOf: []string{"trigger_after_minutes"},
47+
ValidateFunc: validation.IntAtLeast(60),
3848
},
3949
"threshold": {
4050
Type: schema.TypeFloat,
@@ -175,6 +185,19 @@ func buildAlertV2DowntimeStruct(d *schema.ResourceData) *v2.AlertV2Downtime {
175185
metric := d.Get("metric").(string)
176186
config.Metric.ID = metric
177187

188+
if attr, ok := d.GetOk("range_seconds"); ok && attr != nil {
189+
config.Range = d.Get("range_seconds").(int)
190+
}
191+
192+
if d.HasChange("trigger_after_minutes") {
193+
// GetOk returns true even if the value is stored only in the state and not in the user config:
194+
// to avoid applying a trigger_after_minutes old value from the state even if the user removed it from the config
195+
// we use HasChange that is true only if the use has changed (or created) it - and so it must be in the config
196+
if attr, ok := d.GetOk("trigger_after_minutes"); ok && attr != nil {
197+
config.Range = minutesToSeconds(d.Get("trigger_after_minutes").(int))
198+
}
199+
}
200+
178201
var unreportedAlertNotificationsRetentionSec *int
179202
if unreportedAlertNotificationsRetentionSecInterface, ok := d.GetOk("unreported_alert_notifications_retention_seconds"); ok {
180203
u := unreportedAlertNotificationsRetentionSecInterface.(int)
@@ -183,7 +206,6 @@ func buildAlertV2DowntimeStruct(d *schema.ResourceData) *v2.AlertV2Downtime {
183206

184207
alert := &v2.AlertV2Downtime{
185208
AlertV2Common: *alertV2Common,
186-
DurationSec: minutesToSeconds(d.Get("trigger_after_minutes").(int)),
187209
Config: config,
188210
UnreportedAlertNotificationsRetentionSec: unreportedAlertNotificationsRetentionSec,
189211
}
@@ -201,7 +223,8 @@ func updateAlertV2DowntimeState(d *schema.ResourceData, alert *v2.AlertV2Downtim
201223
return err
202224
}
203225

204-
_ = d.Set("trigger_after_minutes", secondsToMinutes(alert.DurationSec))
226+
_ = d.Set("trigger_after_minutes", secondsToMinutes(alert.Config.Range))
227+
_ = d.Set("range_seconds", alert.Config.Range)
205228

206229
_ = d.Set("threshold", (1-alert.Config.Threshold)*100)
207230

sysdig/resource_sysdig_monitor_alert_v2_downtime_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func TestAccAlertV2Downtime(t *testing.T) {
2727
{
2828
Config: alertV2DowntimeWithName(rText()),
2929
},
30+
{
31+
Config: alertV2DowntimeWithTriggerAfterMinutes(rText()),
32+
},
3033
{
3134
Config: alertV2DowntimeWithWithUnreportedAlertNotificationsRetentionSec(rText()),
3235
},
@@ -56,6 +59,27 @@ resource "sysdig_monitor_alert_v2_downtime" "sample" {
5659
values = ["thom-cluster1", "demo-env-prom"]
5760
}
5861
62+
range_seconds = 600
63+
64+
}
65+
66+
`, name)
67+
}
68+
69+
func alertV2DowntimeWithTriggerAfterMinutes(name string) string {
70+
return fmt.Sprintf(`
71+
resource "sysdig_monitor_alert_v2_downtime" "sample" {
72+
73+
name = "TERRAFORM TEST - DOWNTIMEV2 %s"
74+
metric = "sysdig_container_up"
75+
threshold = 75
76+
77+
scope {
78+
label = "kube_cluster_name"
79+
operator = "in"
80+
values = ["thom-cluster1", "demo-env-prom"]
81+
}
82+
5983
trigger_after_minutes = 15
6084
6185
}
@@ -77,7 +101,7 @@ resource "sysdig_monitor_alert_v2_downtime" "sample" {
77101
values = ["thom-cluster1", "demo-env-prom"]
78102
}
79103
80-
trigger_after_minutes = 15
104+
range_seconds = 600
81105
unreported_alert_notifications_retention_seconds = 60 * 60 * 24 * 30
82106
83107
}
@@ -100,7 +124,7 @@ func alertV2DowntimeWithGroupBy(name string) string {
100124
values = ["thom-cluster1", "demo-env-prom"]
101125
}
102126
103-
trigger_after_minutes = 15
127+
range_seconds = 600
104128
105129
}
106130

sysdig/resource_sysdig_monitor_alert_v2_event.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ func resourceSysdigMonitorAlertV2Event() *schema.Resource {
3434

3535
Schema: createScopedSegmentedAlertV2Schema(createAlertV2Schema(map[string]*schema.Schema{
3636
"trigger_after_minutes": {
37-
Type: schema.TypeInt,
38-
Required: true,
37+
Type: schema.TypeInt,
38+
Optional: true,
39+
Computed: true, // computed if range_seconds is defined
40+
Deprecated: "Use range_seconds instead",
41+
ValidateFunc: validation.IntAtLeast(1),
42+
},
43+
"range_seconds": {
44+
Type: schema.TypeInt,
45+
Optional: true,
46+
Computed: true, // computed if trigger_after_minutes is defined
47+
ExactlyOneOf: []string{"trigger_after_minutes"},
48+
ValidateFunc: validation.IntAtLeast(60),
3949
},
4050
"operator": {
4151
Type: schema.TypeString,
@@ -203,9 +213,21 @@ func buildAlertV2EventStruct(d *schema.ResourceData) (*v2.AlertV2Event, error) {
203213
}
204214
config.Tags = tags
205215

216+
if attr, ok := d.GetOk("range_seconds"); ok && attr != nil {
217+
config.Range = d.Get("range_seconds").(int)
218+
}
219+
220+
if d.HasChange("trigger_after_minutes") {
221+
// GetOk returns true even if the value is stored only in the state and not in the user config:
222+
// to avoid applying a trigger_after_minutes old value from the state even if the user removed it from the config
223+
// we use HasChange that is true only if the use has changed (or created) it - and so it must be in the config
224+
if attr, ok := d.GetOk("trigger_after_minutes"); ok && attr != nil {
225+
config.Range = minutesToSeconds(d.Get("trigger_after_minutes").(int))
226+
}
227+
}
228+
206229
alert := &v2.AlertV2Event{
207230
AlertV2Common: *alertV2Common,
208-
DurationSec: minutesToSeconds(d.Get("trigger_after_minutes").(int)),
209231
Config: config,
210232
}
211233
return alert, nil
@@ -222,7 +244,8 @@ func updateAlertV2EventState(d *schema.ResourceData, alert *v2.AlertV2Event) err
222244
return err
223245
}
224246

225-
_ = d.Set("trigger_after_minutes", secondsToMinutes(alert.DurationSec))
247+
_ = d.Set("trigger_after_minutes", secondsToMinutes(alert.Config.Range))
248+
_ = d.Set("range_seconds", alert.Config.Range)
226249

227250
_ = d.Set("operator", alert.Config.ConditionOperator)
228251

sysdig/resource_sysdig_monitor_alert_v2_event_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func TestAccAlertV2Event(t *testing.T) {
2727
{
2828
Config: alertV2Event(rText()),
2929
},
30+
{
31+
Config: alertV2EventWithTriggerAfterMinutes(rText()),
32+
},
3033
{
3134
Config: alertV2EventWithSources(rText()),
3235
},
@@ -60,6 +63,28 @@ resource "sysdig_monitor_alert_v2_event" "sample" {
6063
values = ["thom-cluster1", "demo-env-prom"]
6164
}
6265
66+
range_seconds = 600
67+
68+
}
69+
70+
`, name)
71+
}
72+
73+
func alertV2EventWithTriggerAfterMinutes(name string) string {
74+
return fmt.Sprintf(`
75+
resource "sysdig_monitor_alert_v2_event" "sample" {
76+
77+
name = "TERRAFORM TEST - EVENTV2 %s"
78+
filter = "xxx"
79+
operator = ">="
80+
threshold = 50
81+
82+
scope {
83+
label = "kube_cluster_name"
84+
operator = "in"
85+
values = ["thom-cluster1", "demo-env-prom"]
86+
}
87+
6388
trigger_after_minutes = 15
6489
6590
}
@@ -83,7 +108,7 @@ resource "sysdig_monitor_alert_v2_event" "sample" {
83108
values = ["thom-cluster1", "demo-env-prom"]
84109
}
85110
86-
trigger_after_minutes = 15
111+
range_seconds = 600
87112
88113
}
89114
@@ -106,7 +131,7 @@ resource "sysdig_monitor_alert_v2_event" "sample" {
106131
values = ["thom-cluster1", "demo-env-prom"]
107132
}
108133
109-
trigger_after_minutes = 15
134+
range_seconds = 600
110135
111136
}
112137
@@ -129,7 +154,7 @@ resource "sysdig_monitor_alert_v2_event" "sample" {
129154
values = ["thom-cluster1", "demo-env-prom"]
130155
}
131156
132-
trigger_after_minutes = 15
157+
range_seconds = 600
133158
134159
}
135160

sysdig/resource_sysdig_monitor_alert_v2_form_based_prometheus.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func resourceSysdigMonitorAlertV2FormBasedPrometheus() *schema.Resource {
3333
},
3434

3535
Schema: createScopedSegmentedAlertV2Schema(createAlertV2Schema(map[string]*schema.Schema{
36+
"duration_seconds": {
37+
Type: schema.TypeInt,
38+
Optional: true,
39+
ValidateFunc: validation.IntAtLeast(0),
40+
},
3641
"operator": {
3742
Type: schema.TypeString,
3843
Required: true,
@@ -198,6 +203,10 @@ func buildAlertV2FormBasedPrometheusStruct(d *schema.ResourceData) (*v2.AlertV2F
198203

199204
config.NoDataBehaviour = d.Get("no_data_behaviour").(string)
200205

206+
if attr, ok := d.GetOk("duration_seconds"); ok && attr != nil {
207+
config.Duration = d.Get("duration_seconds").(int)
208+
}
209+
201210
var unreportedAlertNotificationsRetentionSec *int
202211
if unreportedAlertNotificationsRetentionSecInterface, ok := d.GetOk("unreported_alert_notifications_retention_seconds"); ok {
203212
u := unreportedAlertNotificationsRetentionSecInterface.(int)
@@ -206,7 +215,6 @@ func buildAlertV2FormBasedPrometheusStruct(d *schema.ResourceData) (*v2.AlertV2F
206215

207216
alert := &v2.AlertV2FormBasedPrometheus{
208217
AlertV2Common: *alertV2Common,
209-
DurationSec: 0,
210218
Config: config,
211219
UnreportedAlertNotificationsRetentionSec: unreportedAlertNotificationsRetentionSec,
212220
}
@@ -224,6 +232,8 @@ func updateAlertV2FormBasedPrometheusState(d *schema.ResourceData, alert *v2.Ale
224232
return err
225233
}
226234

235+
_ = d.Set("duration_seconds", alert.Config.Duration)
236+
227237
_ = d.Set("operator", alert.Config.ConditionOperator)
228238

229239
_ = d.Set("threshold", alert.Config.Threshold)

0 commit comments

Comments
 (0)