Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for auto-mitigate for Monitor scheduled query rules alerts #13036 #13213

Merged
merged 18 commits into from
Sep 15, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func resourceMonitorScheduledQueryRulesAlert() *pluginsdk.Resource {
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
"auto_mitigation_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
ConflictsWith: []string{"throttling"},
},
"description": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -126,9 +132,10 @@ func resourceMonitorScheduledQueryRulesAlert() *pluginsdk.Resource {
ValidateFunc: validation.IntBetween(0, 4),
},
"throttling": {
Type: pluginsdk.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 10000),
Type: pluginsdk.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 10000),
ConflictsWith: []string{"auto_mitigation_enabled"},
},
"time_window": {
Type: pluginsdk.TypeInt,
Expand Down Expand Up @@ -239,6 +246,7 @@ func resourceMonitorScheduledQueryRulesAlertCreateUpdate(d *pluginsdk.ResourceDa
}
}

autoMitigate := d.Get("auto_mitigation_enabled").(bool)
description := d.Get("description").(string)
enabledRaw := d.Get("enabled").(bool)

Expand All @@ -257,11 +265,12 @@ func resourceMonitorScheduledQueryRulesAlertCreateUpdate(d *pluginsdk.ResourceDa
parameters := insights.LogSearchRuleResource{
Location: utils.String(location),
LogSearchRule: &insights.LogSearchRule{
Description: utils.String(description),
Enabled: enabled,
Source: source,
Schedule: schedule,
Action: action,
Description: utils.String(description),
Enabled: enabled,
Source: source,
Schedule: schedule,
Action: action,
AutoMitigate: utils.Bool(autoMitigate),
},
Tags: expandedTags,
}
Expand Down Expand Up @@ -310,6 +319,7 @@ func resourceMonitorScheduledQueryRulesAlertRead(d *pluginsdk.ResourceData, meta
d.Set("location", azure.NormalizeLocation(*location))
}

d.Set("auto_mitigation_enabled", resp.AutoMitigate)
d.Set("description", resp.Description)
if resp.Enabled == insights.EnabledTrue {
d.Set("enabled", true)
Expand Down Expand Up @@ -385,17 +395,19 @@ func expandMonitorScheduledQueryRulesAlertingAction(d *pluginsdk.ResourceData) *
alertAction := expandMonitorScheduledQueryRulesAlertAction(alertActionRaw)
severityRaw := d.Get("severity").(int)
severity := strconv.Itoa(severityRaw)
throttling := d.Get("throttling").(int)

triggerRaw := d.Get("trigger").([]interface{})
trigger := expandMonitorScheduledQueryRulesAlertTrigger(triggerRaw)

action := insights.AlertingAction{
AznsAction: alertAction,
Severity: insights.AlertSeverity(severity),
ThrottlingInMin: utils.Int32(int32(throttling)),
Trigger: trigger,
OdataType: insights.OdataTypeBasicActionOdataTypeMicrosoftWindowsAzureManagementMonitoringAlertsModelsMicrosoftAppInsightsNexusDataContractsResourcesScheduledQueryRulesAlertingAction,
AznsAction: alertAction,
Severity: insights.AlertSeverity(severity),
Trigger: trigger,
OdataType: insights.OdataTypeBasicActionOdataTypeMicrosoftWindowsAzureManagementMonitoringAlertsModelsMicrosoftAppInsightsNexusDataContractsResourcesScheduledQueryRulesAlertingAction,
}

if throttling, ok := d.Get("throttling").(int); ok && throttling != 0 {
action.ThrottlingInMin = utils.Int32(int32(throttling))
}

return &action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package monitor_test
import (
"context"
"fmt"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -85,6 +86,75 @@ func TestAccMonitorScheduledQueryRules_AlertingActionCrossResource(t *testing.T)
data.ImportStep(),
})
}
func TestAccMonitorScheduledQueryRules_AutoMitigate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_scheduled_query_rules_alert", "test")
r := MonitorScheduledQueryRulesResource{}
ts := time.Now().Format(time.RFC3339)

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.AlertingActionAutoMitigate(data, ts, false),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.AlertingActionConfigComplete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.AlertingActionAutoMitigate(data, ts, true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (MonitorScheduledQueryRulesResource) AlertingActionAutoMitigate(data acceptance.TestData, ts string, autoMitigate bool) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-monitor-%d"
location = "%s"
}
resource "azurerm_application_insights" "test" {
name = "acctestAppInsights-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
application_type = "web"
}
resource "azurerm_monitor_action_group" "test" {
name = "acctestActionGroup-%d"
resource_group_name = azurerm_resource_group.test.name
short_name = "acctestag"
}
resource "azurerm_monitor_scheduled_query_rules_alert" "test" {
name = "acctestsqr-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
data_source_id = azurerm_application_insights.test.id
query = <<-QUERY
let d=datatable(TimeGenerated: datetime, usage_percent: double) [ '%s', 25.4, '%s', 75.4 ];
d | summarize AggregatedValue=avg(usage_percent) by bin(TimeGenerated, 1h)
QUERY
frequency = 60
time_window = 60
auto_mitigation_enabled = %s
action {
action_group = [azurerm_monitor_action_group.test.id]
}
trigger {
operator = "GreaterThan"
threshold = 5000
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, ts, ts, strconv.FormatBool(autoMitigate))
}

func (MonitorScheduledQueryRulesResource) AlertingActionConfigBasic(data acceptance.TestData, ts string) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -220,6 +290,7 @@ resource "azurerm_monitor_scheduled_query_rules_alert" "test" {

severity = 3
throttling = 5

action {
action_group = [azurerm_monitor_action_group.test.id]
email_subject = "Custom alert email subject"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ The following arguments are supported:
* `trigger` - (Required) The condition that results in the alert rule being run.
* `action` - (Required) An `action` block as defined below.
* `authorized_resource_ids` - (Optional) List of Resource IDs referred into query.
* `auto_mitigation_enabled` - (Optional) Should the alerts in this Metric Alert be auto resolved? Defaults to `false`.
-> **NOTE** `auto_mitigation_enabled` and `throttling` are mutually exclusive and cannot both be set.
* `description` - (Optional) The description of the scheduled query rule.
* `enabled` - (Optional) Whether this scheduled query rule is enabled. Default is `true`.
* `severity` - (Optional) Severity of the alert. Possible values include: 0, 1, 2, 3, or 4.
Expand Down