Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
62 changes: 54 additions & 8 deletions sysdig/resource_sysdig_monitor_alert_v2_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
81 changes: 81 additions & 0 deletions sysdig/resource_sysdig_monitor_alert_v2_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ func TestAccAlertV2Metric(t *testing.T) {
{
Config: alertV2MetricWithCustomNotifications(rText()),
},
{
Config: alertV2MetricWithCustomNotifications2(rText()),
},
{
Config: alertV2MetricWithCustomNotifications3(rText()),
},
{
Config: alertV2MetricWithCustomNotifications4(rText()),
},
{
Config: alertV2MetricWithCapture(rText()),
},
Expand Down Expand Up @@ -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" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/monitor_alert_v2_change.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/monitor_alert_v2_downtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/monitor_alert_v2_event.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/monitor_alert_v2_form_based_prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/monitor_alert_v2_group_outlier.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
19 changes: 19 additions & 0 deletions website/docs/r/monitor_alert_v2_metric.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"
}
}

}

```
Expand Down Expand Up @@ -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`

Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/monitor_alert_v2_prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Loading