Skip to content

Commit

Permalink
azurerm_monitor_metric_alert - fix crash when `dynamic_criteria.0.i…
Browse files Browse the repository at this point in the history
…gnore_data_before` isn't set (#21446)
  • Loading branch information
teowa authored Apr 18, 2023
1 parent c10e467 commit 070744f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
20 changes: 11 additions & 9 deletions internal/services/monitor/monitor_metric_alert_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down
59 changes: 59 additions & 0 deletions internal/services/monitor/monitor_metric_alert_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 070744f

Please sign in to comment.