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
1 change: 1 addition & 0 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ type AlertV2ConfigMetric struct {
GroupAggregation string `json:"groupAggregation"`
TimeAggregation string `json:"timeAggregation"`
Metric AlertMetricDescriptorV2 `json:"metric"`
MetricID string `json:"metricId,omitempty"` // Legacy API field, used as fallback when Metric.ID is empty
NoDataBehaviour string `json:"noDataBehaviour"`

Range int `json:"range"`
Expand Down
11 changes: 8 additions & 3 deletions sysdig/resource_sysdig_monitor_alert_v2_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func resourceSysdigMonitorAlertV2Metric() *schema.Resource {
Default: "",
},
"metric": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"time_aggregation": {
Type: schema.TypeString,
Expand Down Expand Up @@ -274,7 +275,11 @@ func updateAlertV2MetricState(d *schema.ResourceData, alert *v2.AlertV2Metric) e

_ = d.Set("group_aggregation", alert.Config.GroupAggregation)

_ = d.Set("metric", alert.Config.Metric.ID)
metricID := alert.Config.Metric.ID
if metricID == "" {
metricID = alert.Config.MetricID
}
_ = d.Set("metric", metricID)
Comment thread
tembleking marked this conversation as resolved.

_ = d.Set("no_data_behaviour", alert.Config.NoDataBehaviour)

Expand Down
34 changes: 34 additions & 0 deletions sysdig/resource_sysdig_monitor_alert_v2_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sysdig_test

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -108,6 +109,39 @@ func TestAccAlertV2Metric(t *testing.T) {
})
}

func TestAccAlertV2MetricRejectsEmptyMetric(t *testing.T) {
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) }

resource.ParallelTest(t, resource.TestCase{
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigIBMMonitorAPIKeyEnv),
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
},
},
Steps: []resource.TestStep{
{
Config: alertV2MetricWithEmptyMetric(rText()),
ExpectError: regexp.MustCompile(`expected "metric" to not be an empty string`),
},
},
})
}

func alertV2MetricWithEmptyMetric(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_alert_v2_metric" "sample" {
name = "TERRAFORM TEST - METRICV2 %s"
metric = ""
group_aggregation = "avg"
time_aggregation = "avg"
operator = ">="
threshold = 50
range_seconds = 600
}
`, name)
}

func alertV2Metric(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_alert_v2_metric" "sample" {
Expand Down
Loading