From 441f363a9d6b85f7dd3ebd3bd90131830790ff37 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:17:26 +0800 Subject: [PATCH 1/4] fix metric alert crack with dynamic_criteria.ignore_data_before --- .../monitor/monitor_metric_alert_resource.go | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/internal/services/monitor/monitor_metric_alert_resource.go b/internal/services/monitor/monitor_metric_alert_resource.go index 21c4f1e22b67..22bff1e875e0 100644 --- a/internal/services/monitor/monitor_metric_alert_resource.go +++ b/internal/services/monitor/monitor_metric_alert_resource.go @@ -647,14 +647,8 @@ func expandMonitorMetricAlertMultiResourceMultiMetricForDynamicMetricCriteria(in for i, item := range input { v := item.(map[string]interface{}) dimensions := expandMonitorMetricDimension(v["dimension"].([]interface{})) - var ignoreDataBefore *date.Time - if v := v["ignore_data_before"].(string); v != "" { - // Guaranteed in schema validation func. - t, _ := time.Parse(time.RFC3339, v) - ignoreDataBefore = &date.Time{Time: t} - } - criteria = append(criteria, metricalerts.DynamicMetricCriteria{ + dynamicMetricCriteria := metricalerts.DynamicMetricCriteria{ Name: fmt.Sprintf("Metric%d", i+1), MetricNamespace: utils.String(v["metric_namespace"].(string)), MetricName: v["metric_name"].(string), @@ -666,9 +660,17 @@ func expandMonitorMetricAlertMultiResourceMultiMetricForDynamicMetricCriteria(in NumberOfEvaluationPeriods: float64(v["evaluation_total_count"].(int)), MinFailingPeriodsToAlert: float64(v["evaluation_failure_count"].(int)), }, - IgnoreDataBefore: pointer.To(ignoreDataBefore.String()), SkipMetricValidation: utils.Bool(v["skip_metric_validation"].(bool)), - }) + } + + if datetime := v["ignore_data_before"].(string); datetime != "" { + // Guaranteed in schema validation func. + t, _ := time.Parse(time.RFC3339, datetime) + ignoreDataBefore := &date.Time{Time: t} + dynamicMetricCriteria.IgnoreDataBefore = pointer.To(ignoreDataBefore.String()) + } + + criteria = append(criteria, dynamicMetricCriteria) } return &metricalerts.MetricAlertMultipleResourceMultipleMetricCriteria{ AllOf: &criteria, From 786f262fb0a0196bb39e95f86fb6af1bbcfbe733 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:42:59 +0800 Subject: [PATCH 2/4] add test --- .../monitor_metric_alert_resource_test.go | 69 +++++++++++++++++-- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/internal/services/monitor/monitor_metric_alert_resource_test.go b/internal/services/monitor/monitor_metric_alert_resource_test.go index 7032acf0cfb1..51a4e3ad5af0 100644 --- a/internal/services/monitor/monitor_metric_alert_resource_test.go +++ b/internal/services/monitor/monitor_metric_alert_resource_test.go @@ -78,6 +78,21 @@ func TestAccMonitorMetricAlert_complete(t *testing.T) { }) } +func TestAccMonitorMetricAlert_dynamicCriteria(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_monitor_metric_alert", "test") + r := MonitorMetricAlertResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.dynamicCriteria(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccMonitorMetricAlert_basicAndCompleteUpdate(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_monitor_metric_alert", "test") r := MonitorMetricAlertResource{} @@ -227,6 +242,50 @@ resource "azurerm_monitor_metric_alert" "import" { `, r.basic(data)) } +func (MonitorMetricAlertResource) dynamicCriteria(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_storage_account" "test" { + name = "acctestsa1%[3]s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + account_tier = "Standard" + account_replication_type = "LRS" +} + +resource "azurerm_monitor_metric_alert" "test" { + name = "acctestMetricAlert-%[1]d" + resource_group_name = azurerm_resource_group.test.name + scopes = [azurerm_storage_account.test.id] + + dynamic_criteria { + metric_namespace = "Microsoft.Storage/storageAccounts" + metric_name = "Availability" + aggregation = "Minimum" + operator = "GreaterThan" + alert_sensitivity = "High" + + dimension { + name = "ApiName" + operator = "Include" + values = ["*"] + } + + evaluation_total_count = 4 + evaluation_failure_count = 1 + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomString) +} + func (MonitorMetricAlertResource) multiCriteria(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { @@ -448,12 +507,10 @@ resource "azurerm_monitor_metric_alert" "test" { metric_name = "CPU Credits Consumed" aggregation = "Average" - operator = "GreaterOrLessThan" - alert_sensitivity = "Medium" - skip_metric_validation = true - evaluation_failure_count = 4 - evaluation_total_count = 5 - ignore_data_before = "2022-03-02T15:04:05Z" + operator = "GreaterOrLessThan" + alert_sensitivity = "Medium" + skip_metric_validation = true + } window_size = "PT5M" frequency = "PT5M" From a11c9a8351d010bebf0627262e2e2a00c2cb19e4 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Tue, 18 Apr 2023 13:43:40 +0800 Subject: [PATCH 3/4] fix --- .../services/monitor/monitor_metric_alert_resource_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/services/monitor/monitor_metric_alert_resource_test.go b/internal/services/monitor/monitor_metric_alert_resource_test.go index 51a4e3ad5af0..97fe21ab8c86 100644 --- a/internal/services/monitor/monitor_metric_alert_resource_test.go +++ b/internal/services/monitor/monitor_metric_alert_resource_test.go @@ -510,7 +510,9 @@ resource "azurerm_monitor_metric_alert" "test" { operator = "GreaterOrLessThan" alert_sensitivity = "Medium" skip_metric_validation = true - + evaluation_failure_count = 4 + evaluation_total_count = 5 + ignore_data_before = "2022-03-02T15:04:05Z" } window_size = "PT5M" frequency = "PT5M" From 7cc2ee1fde763f665cb8d3f3cc4b6cb879e2a420 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Tue, 18 Apr 2023 13:48:22 +0800 Subject: [PATCH 4/4] fmt --- .../monitor/monitor_metric_alert_resource_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/services/monitor/monitor_metric_alert_resource_test.go b/internal/services/monitor/monitor_metric_alert_resource_test.go index 97fe21ab8c86..a510bbb4bf46 100644 --- a/internal/services/monitor/monitor_metric_alert_resource_test.go +++ b/internal/services/monitor/monitor_metric_alert_resource_test.go @@ -507,11 +507,11 @@ resource "azurerm_monitor_metric_alert" "test" { metric_name = "CPU Credits Consumed" aggregation = "Average" - operator = "GreaterOrLessThan" - alert_sensitivity = "Medium" - skip_metric_validation = true - evaluation_failure_count = 4 - evaluation_total_count = 5 + operator = "GreaterOrLessThan" + alert_sensitivity = "Medium" + skip_metric_validation = true + evaluation_failure_count = 4 + evaluation_total_count = 5 ignore_data_before = "2022-03-02T15:04:05Z" } window_size = "PT5M"