diff --git a/sysdig/internal/client/v2/model.go b/sysdig/internal/client/v2/model.go index b483285e..fb796aee 100644 --- a/sysdig/internal/client/v2/model.go +++ b/sysdig/internal/client/v2/model.go @@ -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"` diff --git a/sysdig/resource_sysdig_monitor_alert_v2_metric.go b/sysdig/resource_sysdig_monitor_alert_v2_metric.go index 593d8bee..b60ffdc5 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_metric.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_metric.go @@ -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, @@ -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) _ = d.Set("no_data_behaviour", alert.Config.NoDataBehaviour) diff --git a/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go b/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go index 575b0d23..6bd87384 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go @@ -4,6 +4,7 @@ package sysdig_test import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -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" {