Skip to content

Commit 5279fae

Browse files
committed
feat(alerts): support additional fields in notification templates
1 parent a89c2d5 commit 5279fae

10 files changed

+190
-8
lines changed

sysdig/internal/client/v2/model.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,15 @@ type NotificationChannelOptionsV2 struct {
697697
}
698698

699699
type CustomNotificationTemplateV2 struct {
700-
Subject string `json:"subject"`
701-
PrependText string `json:"prependText"`
702-
AppendText string `json:"appendText"`
700+
Subject string `json:"subject"`
701+
PrependText string `json:"prependText"`
702+
AppendText string `json:"appendText"`
703+
AdditionalNotificationFields []CustomNotificationAdditionalField `json:"additionalNotificationFields,omitempty"`
704+
}
705+
706+
type CustomNotificationAdditionalField struct {
707+
Name string `json:"name"`
708+
Value string `json:"value"`
703709
}
704710

705711
type CaptureConfigV2 struct {

sysdig/resource_sysdig_monitor_alert_v2_common.go

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ func createAlertV2Schema(original map[string]*schema.Schema) map[string]*schema.
119119
Type: schema.TypeString,
120120
Optional: true,
121121
},
122+
"additional_field": {
123+
Type: schema.TypeSet,
124+
Optional: true,
125+
Elem: &schema.Resource{
126+
Schema: map[string]*schema.Schema{
127+
"name": {
128+
Type: schema.TypeString,
129+
Required: true,
130+
},
131+
"value": {
132+
Type: schema.TypeString,
133+
Required: true,
134+
},
135+
},
136+
},
137+
},
122138
},
123139
},
124140
},
@@ -271,12 +287,22 @@ func buildAlertV2CommonStruct(d *schema.ResourceData) *v2.AlertV2Common {
271287

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

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

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

380426
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)