diff --git a/sysdig/internal/client/v2/model.go b/sysdig/internal/client/v2/model.go index 480be46de..bf6ee3d81 100644 --- a/sysdig/internal/client/v2/model.go +++ b/sysdig/internal/client/v2/model.go @@ -619,9 +619,15 @@ type NotificationChannelOptionsV2 struct { } type CustomNotificationTemplateV2 struct { - Subject string `json:"subject"` - PrependText string `json:"prependText"` - AppendText string `json:"appendText"` + Subject string `json:"subject"` + PrependText string `json:"prependText"` + AppendText string `json:"appendText"` + AdditionalNotificationFields []CustomNotificationAdditionalField `json:"additionalNotificationFields,omitempty"` +} + +type CustomNotificationAdditionalField struct { + Name string `json:"name"` + Value string `json:"value"` } type CaptureConfigV2 struct { diff --git a/sysdig/resource_sysdig_monitor_alert_v2_common.go b/sysdig/resource_sysdig_monitor_alert_v2_common.go index 5ef605c4f..e1e28971b 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_common.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_common.go @@ -120,6 +120,22 @@ func createAlertV2Schema(original map[string]*schema.Schema) map[string]*schema. Type: schema.TypeString, Optional: true, }, + "additional_field": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "value": { + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, }, }, }, @@ -270,12 +286,22 @@ func buildAlertV2CommonStruct(d *schema.ResourceData) *v2.AlertV2Common { customNotification := v2.CustomNotificationTemplateV2{} if attr, ok := d.GetOk("custom_notification"); ok && attr != nil { - if len(attr.([]any)) > 0 { - m := attr.([]any)[0].(map[string]any) + if len(attr.([]interface{})) > 0 && attr.([]interface{})[0] != nil { + m := attr.([]interface{})[0].(map[string]interface{}) customNotification.Subject = m["subject"].(string) customNotification.AppendText = m["append"].(string) customNotification.PrependText = m["prepend"].(string) + customNotification.AdditionalNotificationFields = []v2.CustomNotificationAdditionalField{} + if m["additional_field"] != nil { + for _, field := range m["additional_field"].(*schema.Set).List() { + fieldMap := field.(map[string]interface{}) + customNotification.AdditionalNotificationFields = append(customNotification.AdditionalNotificationFields, v2.CustomNotificationAdditionalField{ + Name: fieldMap["name"].(string), + Value: fieldMap["value"].(string), + }) + } + } } } alert.CustomNotificationTemplate = &customNotification @@ -365,15 +391,35 @@ func updateAlertV2CommonState(d *schema.ResourceData, alert *v2.AlertV2Common) ( } _ = d.Set("notification_channels", notificationChannels) - if alert.CustomNotificationTemplate != nil && (alert.CustomNotificationTemplate.Subject != "" || - alert.CustomNotificationTemplate.AppendText != "" || - alert.CustomNotificationTemplate.PrependText != "") { - customNotification := map[string]any{} + if alert.CustomNotificationTemplate != nil && + (alert.CustomNotificationTemplate.Subject != "" || + alert.CustomNotificationTemplate.AppendText != "" || + alert.CustomNotificationTemplate.PrependText != "" || + len(alert.CustomNotificationTemplate.AdditionalNotificationFields) != 0) { + customNotification := map[string]interface{}{} customNotification["subject"] = alert.CustomNotificationTemplate.Subject customNotification["append"] = alert.CustomNotificationTemplate.AppendText customNotification["prepend"] = alert.CustomNotificationTemplate.PrependText - - _ = d.Set("custom_notification", []any{customNotification}) + additionalFields := []interface{}{} + for _, field := range alert.CustomNotificationTemplate.AdditionalNotificationFields { + additionalFields = append(additionalFields, map[string]interface{}{ + "name": field.Name, + "value": field.Value, + }) + } + customNotification["additional_field"] = additionalFields + _ = d.Set("custom_notification", []interface{}{customNotification}) + } else { + // if the custom notification template has all empty fields, we don't set it in the state + // this because, even if the alert was created without custom notification template, the api returs: + // ``` + // "customNotificationTemplate" : { + // "subject" : "" + // } + // ``` + // and it would triggert a diff compared to the empty state defined in the schema. + // (an empty subject creates a notification with default title anyway, so it is equal to no subject definition) + _ = d.Set("custom_notification", []interface{}{}) } if alert.CaptureConfig != nil { diff --git a/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go b/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go index 58c9f4788..3f01a9416 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go @@ -60,6 +60,15 @@ func TestAccAlertV2Metric(t *testing.T) { { Config: alertV2MetricWithCustomNotifications(rText()), }, + { + Config: alertV2MetricWithCustomNotifications2(rText()), + }, + { + Config: alertV2MetricWithCustomNotifications3(rText()), + }, + { + Config: alertV2MetricWithCustomNotifications4(rText()), + }, { Config: alertV2MetricWithCapture(rText()), }, @@ -319,6 +328,78 @@ resource "sysdig_monitor_alert_v2_metric" "sample" { `, name) } +func alertV2MetricWithCustomNotifications2(name string) string { + return fmt.Sprintf(` +resource "sysdig_monitor_alert_v2_metric" "sample" { + + name = "TERRAFORM TEST - METRICV2 %s" + metric = "sysdig_container_cpu_used_percent" + group_aggregation = "avg" + time_aggregation = "avg" + operator = ">=" + threshold = 50 + range_seconds = 600 + custom_notification { + subject = "test" + prepend = "pre" + append = "post" + additional_field { + name = "test_field" + value = "test_value" + } + additional_field { + name = "test_field2" + value = "test_value2" + } + } +} +`, name) +} + +func alertV2MetricWithCustomNotifications3(name string) string { + return fmt.Sprintf(` +resource "sysdig_monitor_alert_v2_metric" "sample" { + + name = "TERRAFORM TEST - METRICV2 %s" + metric = "sysdig_container_cpu_used_percent" + group_aggregation = "avg" + time_aggregation = "avg" + operator = ">=" + threshold = 50 + range_seconds = 600 + custom_notification { + subject = "test" + prepend = "pre" + append = "post" + additional_field { + name = "test_field2" + value = "test_value2" + } + } +} +`, name) +} + +func alertV2MetricWithCustomNotifications4(name string) string { + return fmt.Sprintf(` +resource "sysdig_monitor_alert_v2_metric" "sample" { + + name = "TERRAFORM TEST - METRICV2 %s" + metric = "sysdig_container_cpu_used_percent" + group_aggregation = "avg" + time_aggregation = "avg" + operator = ">=" + threshold = 50 + range_seconds = 600 + custom_notification { + subject = "test" + prepend = "pre" + append = "post" + } +} +`, name) +} + func alertV2MetricWithCapture(name string) string { return fmt.Sprintf(` resource "sysdig_monitor_alert_v2_metric" "sample" { diff --git a/website/docs/r/monitor_alert_v2_change.md b/website/docs/r/monitor_alert_v2_change.md index 923c6aadc..0affb3503 100644 --- a/website/docs/r/monitor_alert_v2_change.md +++ b/website/docs/r/monitor_alert_v2_change.md @@ -86,6 +86,11 @@ By defining this field, the user can modify the title and the body of the messag * `subject` - (Optional) Sets the title of the alert. * `prepend` - (Optional) Text to add before the alert template. * `append` - (Optional) Text to add after the alert template. +* `additional_field` - (Optional) Set of additional fields to add to the notification. + +#### `additional_field` +* `name` - (Required) field name. +* `value` - (Required) field value. ### `link` diff --git a/website/docs/r/monitor_alert_v2_downtime.md b/website/docs/r/monitor_alert_v2_downtime.md index a5fe88598..4c21cd3f1 100644 --- a/website/docs/r/monitor_alert_v2_downtime.md +++ b/website/docs/r/monitor_alert_v2_downtime.md @@ -73,6 +73,11 @@ By defining this field, the user can modify the title and the body of the messag * `subject` - (Optional) Sets the title of the alert. * `prepend` - (Optional) Text to add before the alert template. * `append` - (Optional) Text to add after the alert template. +* `additional_field` - (Optional) Set of additional fields to add to the notification. + +#### `additional_field` +* `name` - (Required) field name. +* `value` - (Required) field value. ### `link` diff --git a/website/docs/r/monitor_alert_v2_event.md b/website/docs/r/monitor_alert_v2_event.md index f29719032..96f9dbc96 100644 --- a/website/docs/r/monitor_alert_v2_event.md +++ b/website/docs/r/monitor_alert_v2_event.md @@ -85,6 +85,11 @@ By defining this field, the user can modify the title and the body of the messag * `subject` - (Optional) Sets the title of the alert. * `prepend` - (Optional) Text to add before the alert template. * `append` - (Optional) Text to add after the alert template. +* `additional_field` - (Optional) Set of additional fields to add to the notification. + +#### `additional_field` +* `name` - (Required) field name. +* `value` - (Required) field value. ### `link` diff --git a/website/docs/r/monitor_alert_v2_form_based_prometheus.md b/website/docs/r/monitor_alert_v2_form_based_prometheus.md index cac1faf1a..aa885ca5b 100644 --- a/website/docs/r/monitor_alert_v2_form_based_prometheus.md +++ b/website/docs/r/monitor_alert_v2_form_based_prometheus.md @@ -65,6 +65,11 @@ By defining this field, the user can modify the title and the body of the messag * `subject` - (Optional) Sets the title of the alert. * `prepend` - (Optional) Text to add before the alert template. * `append` - (Optional) Text to add after the alert template. +* `additional_field` - (Optional) Set of additional fields to add to the notification. + +#### `additional_field` +* `name` - (Required) field name. +* `value` - (Required) field value. ### `link` diff --git a/website/docs/r/monitor_alert_v2_group_outlier.md b/website/docs/r/monitor_alert_v2_group_outlier.md index bd97b7067..d4bc9caa8 100644 --- a/website/docs/r/monitor_alert_v2_group_outlier.md +++ b/website/docs/r/monitor_alert_v2_group_outlier.md @@ -86,6 +86,11 @@ By defining this field, the user can modify the title and the body of the messag * `subject` - (Optional) Sets the title of the alert. * `prepend` - (Optional) Text to add before the alert template. * `append` - (Optional) Text to add after the alert template. +* `additional_field` - (Optional) Set of additional fields to add to the notification. + +#### `additional_field` +* `name` - (Required) field name. +* `value` - (Required) field value. ### `link` diff --git a/website/docs/r/monitor_alert_v2_metric.md b/website/docs/r/monitor_alert_v2_metric.md index b85ae7d48..76a19c364 100644 --- a/website/docs/r/monitor_alert_v2_metric.md +++ b/website/docs/r/monitor_alert_v2_metric.md @@ -47,6 +47,20 @@ resource "sysdig_monitor_alert_v2_metric" "sample" { range_seconds = 60 + custom_notification { + subject = "{{__alert_name__}} for pod {{kube_pod_name}}" + prepend = "Attention!!" + append = "Please investigate the issue. Escalate to the on-call team if needed." + additional_field { + name = "pod" + value = "{{kube_pod_name}}" + } + additional_field { + name = "cluster" + value = "{{kube_cluster_name}}" + } + } + } ``` @@ -88,6 +102,11 @@ By defining this field, the user can modify the title and the body of the messag * `subject` - (Optional) Sets the title of the alert. * `prepend` - (Optional) Text to add before the alert template. * `append` - (Optional) Text to add after the alert template. +* `additional_field` - (Optional) Set of additional fields to add to the notification. + +#### `additional_field` +* `name` - (Required) field name. +* `value` - (Required) field value. ### `link` diff --git a/website/docs/r/monitor_alert_v2_prometheus.md b/website/docs/r/monitor_alert_v2_prometheus.md index ae19b567a..aae059700 100644 --- a/website/docs/r/monitor_alert_v2_prometheus.md +++ b/website/docs/r/monitor_alert_v2_prometheus.md @@ -66,6 +66,11 @@ By defining this field, the user can modify the title and the body of the messag * `subject` - (Optional) Sets the title of the alert. * `prepend` - (Optional) Text to add before the alert template. * `append` - (Optional) Text to add after the alert template. +* `additional_field` - (Optional) Set of additional fields to add to the notification. + +#### `additional_field` +* `name` - (Required) field name. +* `value` - (Required) field value. ### `link`