Skip to content

Commit e5f40db

Browse files
dkutsanovdbonf
andauthored
feat(alerts): support additional fields in notification templates (#654)
* feat(alerts): support additional fields in notification templates * applying De Morgan's law to fix linter --------- Co-authored-by: Diego Bonfigli <diego.bonfigli@sysdig.com>
1 parent b3274f6 commit e5f40db

10 files changed

+193
-11
lines changed

sysdig/internal/client/v2/model.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,15 @@ type NotificationChannelOptionsV2 struct {
619619
}
620620

621621
type CustomNotificationTemplateV2 struct {
622-
Subject string `json:"subject"`
623-
PrependText string `json:"prependText"`
624-
AppendText string `json:"appendText"`
622+
Subject string `json:"subject"`
623+
PrependText string `json:"prependText"`
624+
AppendText string `json:"appendText"`
625+
AdditionalNotificationFields []CustomNotificationAdditionalField `json:"additionalNotificationFields,omitempty"`
626+
}
627+
628+
type CustomNotificationAdditionalField struct {
629+
Name string `json:"name"`
630+
Value string `json:"value"`
625631
}
626632

627633
type CaptureConfigV2 struct {

sysdig/resource_sysdig_monitor_alert_v2_common.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ func createAlertV2Schema(original map[string]*schema.Schema) map[string]*schema.
120120
Type: schema.TypeString,
121121
Optional: true,
122122
},
123+
"additional_field": {
124+
Type: schema.TypeSet,
125+
Optional: true,
126+
Elem: &schema.Resource{
127+
Schema: map[string]*schema.Schema{
128+
"name": {
129+
Type: schema.TypeString,
130+
Required: true,
131+
},
132+
"value": {
133+
Type: schema.TypeString,
134+
Required: true,
135+
},
136+
},
137+
},
138+
},
123139
},
124140
},
125141
},
@@ -270,12 +286,22 @@ func buildAlertV2CommonStruct(d *schema.ResourceData) *v2.AlertV2Common {
270286

271287
customNotification := v2.CustomNotificationTemplateV2{}
272288
if attr, ok := d.GetOk("custom_notification"); ok && attr != nil {
273-
if len(attr.([]any)) > 0 {
274-
m := attr.([]any)[0].(map[string]any)
289+
if len(attr.([]interface{})) > 0 && attr.([]interface{})[0] != nil {
290+
m := attr.([]interface{})[0].(map[string]interface{})
275291

276292
customNotification.Subject = m["subject"].(string)
277293
customNotification.AppendText = m["append"].(string)
278294
customNotification.PrependText = m["prepend"].(string)
295+
customNotification.AdditionalNotificationFields = []v2.CustomNotificationAdditionalField{}
296+
if m["additional_field"] != nil {
297+
for _, field := range m["additional_field"].(*schema.Set).List() {
298+
fieldMap := field.(map[string]interface{})
299+
customNotification.AdditionalNotificationFields = append(customNotification.AdditionalNotificationFields, v2.CustomNotificationAdditionalField{
300+
Name: fieldMap["name"].(string),
301+
Value: fieldMap["value"].(string),
302+
})
303+
}
304+
}
279305
}
280306
}
281307
alert.CustomNotificationTemplate = &customNotification
@@ -365,15 +391,35 @@ func updateAlertV2CommonState(d *schema.ResourceData, alert *v2.AlertV2Common) (
365391
}
366392
_ = d.Set("notification_channels", notificationChannels)
367393

368-
if alert.CustomNotificationTemplate != nil && (alert.CustomNotificationTemplate.Subject != "" ||
369-
alert.CustomNotificationTemplate.AppendText != "" ||
370-
alert.CustomNotificationTemplate.PrependText != "") {
371-
customNotification := map[string]any{}
394+
if alert.CustomNotificationTemplate != nil &&
395+
(alert.CustomNotificationTemplate.Subject != "" ||
396+
alert.CustomNotificationTemplate.AppendText != "" ||
397+
alert.CustomNotificationTemplate.PrependText != "" ||
398+
len(alert.CustomNotificationTemplate.AdditionalNotificationFields) != 0) {
399+
customNotification := map[string]interface{}{}
372400
customNotification["subject"] = alert.CustomNotificationTemplate.Subject
373401
customNotification["append"] = alert.CustomNotificationTemplate.AppendText
374402
customNotification["prepend"] = alert.CustomNotificationTemplate.PrependText
375-
376-
_ = d.Set("custom_notification", []any{customNotification})
403+
additionalFields := []interface{}{}
404+
for _, field := range alert.CustomNotificationTemplate.AdditionalNotificationFields {
405+
additionalFields = append(additionalFields, map[string]interface{}{
406+
"name": field.Name,
407+
"value": field.Value,
408+
})
409+
}
410+
customNotification["additional_field"] = additionalFields
411+
_ = d.Set("custom_notification", []interface{}{customNotification})
412+
} else {
413+
// if the custom notification template has all empty fields, we don't set it in the state
414+
// this because, even if the alert was created without custom notification template, the api returs:
415+
// ```
416+
// "customNotificationTemplate" : {
417+
// "subject" : ""
418+
// }
419+
// ```
420+
// and it would triggert a diff compared to the empty state defined in the schema.
421+
// (an empty subject creates a notification with default title anyway, so it is equal to no subject definition)
422+
_ = d.Set("custom_notification", []interface{}{})
377423
}
378424

379425
if alert.CaptureConfig != nil {

sysdig/resource_sysdig_monitor_alert_v2_metric_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ func TestAccAlertV2Metric(t *testing.T) {
6060
{
6161
Config: alertV2MetricWithCustomNotifications(rText()),
6262
},
63+
{
64+
Config: alertV2MetricWithCustomNotifications2(rText()),
65+
},
66+
{
67+
Config: alertV2MetricWithCustomNotifications3(rText()),
68+
},
69+
{
70+
Config: alertV2MetricWithCustomNotifications4(rText()),
71+
},
6372
{
6473
Config: alertV2MetricWithCapture(rText()),
6574
},
@@ -319,6 +328,78 @@ resource "sysdig_monitor_alert_v2_metric" "sample" {
319328
`, name)
320329
}
321330

331+
func alertV2MetricWithCustomNotifications2(name string) string {
332+
return fmt.Sprintf(`
333+
resource "sysdig_monitor_alert_v2_metric" "sample" {
334+
335+
name = "TERRAFORM TEST - METRICV2 %s"
336+
metric = "sysdig_container_cpu_used_percent"
337+
group_aggregation = "avg"
338+
time_aggregation = "avg"
339+
operator = ">="
340+
threshold = 50
341+
range_seconds = 600
342+
custom_notification {
343+
subject = "test"
344+
prepend = "pre"
345+
append = "post"
346+
additional_field {
347+
name = "test_field"
348+
value = "test_value"
349+
}
350+
additional_field {
351+
name = "test_field2"
352+
value = "test_value2"
353+
}
354+
}
355+
}
356+
`, name)
357+
}
358+
359+
func alertV2MetricWithCustomNotifications3(name string) string {
360+
return fmt.Sprintf(`
361+
resource "sysdig_monitor_alert_v2_metric" "sample" {
362+
363+
name = "TERRAFORM TEST - METRICV2 %s"
364+
metric = "sysdig_container_cpu_used_percent"
365+
group_aggregation = "avg"
366+
time_aggregation = "avg"
367+
operator = ">="
368+
threshold = 50
369+
range_seconds = 600
370+
custom_notification {
371+
subject = "test"
372+
prepend = "pre"
373+
append = "post"
374+
additional_field {
375+
name = "test_field2"
376+
value = "test_value2"
377+
}
378+
}
379+
}
380+
`, name)
381+
}
382+
383+
func alertV2MetricWithCustomNotifications4(name string) string {
384+
return fmt.Sprintf(`
385+
resource "sysdig_monitor_alert_v2_metric" "sample" {
386+
387+
name = "TERRAFORM TEST - METRICV2 %s"
388+
metric = "sysdig_container_cpu_used_percent"
389+
group_aggregation = "avg"
390+
time_aggregation = "avg"
391+
operator = ">="
392+
threshold = 50
393+
range_seconds = 600
394+
custom_notification {
395+
subject = "test"
396+
prepend = "pre"
397+
append = "post"
398+
}
399+
}
400+
`, name)
401+
}
402+
322403
func alertV2MetricWithCapture(name string) string {
323404
return fmt.Sprintf(`
324405
resource "sysdig_monitor_alert_v2_metric" "sample" {

website/docs/r/monitor_alert_v2_change.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ By defining this field, the user can modify the title and the body of the messag
8686
* `subject` - (Optional) Sets the title of the alert.
8787
* `prepend` - (Optional) Text to add before the alert template.
8888
* `append` - (Optional) Text to add after the alert template.
89+
* `additional_field` - (Optional) Set of additional fields to add to the notification.
90+
91+
#### `additional_field`
92+
* `name` - (Required) field name.
93+
* `value` - (Required) field value.
8994

9095
### `link`
9196

website/docs/r/monitor_alert_v2_downtime.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ By defining this field, the user can modify the title and the body of the messag
7373
* `subject` - (Optional) Sets the title of the alert.
7474
* `prepend` - (Optional) Text to add before the alert template.
7575
* `append` - (Optional) Text to add after the alert template.
76+
* `additional_field` - (Optional) Set of additional fields to add to the notification.
77+
78+
#### `additional_field`
79+
* `name` - (Required) field name.
80+
* `value` - (Required) field value.
7681

7782
### `link`
7883

website/docs/r/monitor_alert_v2_event.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ By defining this field, the user can modify the title and the body of the messag
8585
* `subject` - (Optional) Sets the title of the alert.
8686
* `prepend` - (Optional) Text to add before the alert template.
8787
* `append` - (Optional) Text to add after the alert template.
88+
* `additional_field` - (Optional) Set of additional fields to add to the notification.
89+
90+
#### `additional_field`
91+
* `name` - (Required) field name.
92+
* `value` - (Required) field value.
8893

8994
### `link`
9095

website/docs/r/monitor_alert_v2_form_based_prometheus.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ By defining this field, the user can modify the title and the body of the messag
6565
* `subject` - (Optional) Sets the title of the alert.
6666
* `prepend` - (Optional) Text to add before the alert template.
6767
* `append` - (Optional) Text to add after the alert template.
68+
* `additional_field` - (Optional) Set of additional fields to add to the notification.
69+
70+
#### `additional_field`
71+
* `name` - (Required) field name.
72+
* `value` - (Required) field value.
6873

6974
### `link`
7075

website/docs/r/monitor_alert_v2_group_outlier.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ By defining this field, the user can modify the title and the body of the messag
8686
* `subject` - (Optional) Sets the title of the alert.
8787
* `prepend` - (Optional) Text to add before the alert template.
8888
* `append` - (Optional) Text to add after the alert template.
89+
* `additional_field` - (Optional) Set of additional fields to add to the notification.
90+
91+
#### `additional_field`
92+
* `name` - (Required) field name.
93+
* `value` - (Required) field value.
8994

9095
### `link`
9196

website/docs/r/monitor_alert_v2_metric.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ resource "sysdig_monitor_alert_v2_metric" "sample" {
4747
4848
range_seconds = 60
4949
50+
custom_notification {
51+
subject = "{{__alert_name__}} for pod {{kube_pod_name}}"
52+
prepend = "Attention!!"
53+
append = "Please investigate the issue. Escalate to the on-call team if needed."
54+
additional_field {
55+
name = "pod"
56+
value = "{{kube_pod_name}}"
57+
}
58+
additional_field {
59+
name = "cluster"
60+
value = "{{kube_cluster_name}}"
61+
}
62+
}
63+
5064
}
5165
5266
```
@@ -88,6 +102,11 @@ By defining this field, the user can modify the title and the body of the messag
88102
* `subject` - (Optional) Sets the title of the alert.
89103
* `prepend` - (Optional) Text to add before the alert template.
90104
* `append` - (Optional) Text to add after the alert template.
105+
* `additional_field` - (Optional) Set of additional fields to add to the notification.
106+
107+
#### `additional_field`
108+
* `name` - (Required) field name.
109+
* `value` - (Required) field value.
91110

92111
### `link`
93112

website/docs/r/monitor_alert_v2_prometheus.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ By defining this field, the user can modify the title and the body of the messag
6666
* `subject` - (Optional) Sets the title of the alert.
6767
* `prepend` - (Optional) Text to add before the alert template.
6868
* `append` - (Optional) Text to add after the alert template.
69+
* `additional_field` - (Optional) Set of additional fields to add to the notification.
70+
71+
#### `additional_field`
72+
* `name` - (Required) field name.
73+
* `value` - (Required) field value.
6974

7075
### `link`
7176

0 commit comments

Comments
 (0)