From bc4634b530672a8c58d791d6862a5819dace9f09 Mon Sep 17 00:00:00 2001 From: Tao <104055472+teowa@users.noreply.github.com> Date: Tue, 7 Feb 2023 05:36:34 +0800 Subject: [PATCH] `azurerm_monitor_scheduled_query_rules_alert` - fix `query_type` (#20290) resolves https://github.com/hashicorp/terraform-provider-azurerm/issues/20267 --- internal/services/monitor/common_monitor.go | 9 ++- ...heduled_query_rules_alert_resource_test.go | 57 +++++++++++++++++++ ..._scheduled_query_rules_alert.html.markdown | 2 +- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/internal/services/monitor/common_monitor.go b/internal/services/monitor/common_monitor.go index c51759b3150d..f1aeae91d067 100644 --- a/internal/services/monitor/common_monitor.go +++ b/internal/services/monitor/common_monitor.go @@ -22,15 +22,18 @@ func flattenAzureRmScheduledQueryRulesAlertAction(input *insights.AzNsActionGrou func expandMonitorScheduledQueryRulesCommonSource(d *pluginsdk.ResourceData) *insights.Source { authorizedResourceIDs := d.Get("authorized_resource_ids").(*pluginsdk.Set).List() dataSourceID := d.Get("data_source_id").(string) - query, ok := d.GetOk("query") + source := insights.Source{ AuthorizedResources: utils.ExpandStringSlice(authorizedResourceIDs), DataSourceID: utils.String(dataSourceID), - QueryType: insights.QueryTypeResultCount, } - if ok { + + if query, ok := d.GetOk("query"); ok { source.Query = utils.String(query.(string)) } + if queryType, ok := d.GetOk("query_type"); ok { + source.QueryType = insights.QueryType(queryType.(string)) + } return &source } diff --git a/internal/services/monitor/monitor_scheduled_query_rules_alert_resource_test.go b/internal/services/monitor/monitor_scheduled_query_rules_alert_resource_test.go index 122d4016823a..6fdb762d286a 100644 --- a/internal/services/monitor/monitor_scheduled_query_rules_alert_resource_test.go +++ b/internal/services/monitor/monitor_scheduled_query_rules_alert_resource_test.go @@ -32,6 +32,20 @@ func TestAccMonitorScheduledQueryRules_AlertingActionBasic(t *testing.T) { data.ImportStep(), }) } +func TestAccMonitorScheduledQueryRules_AlertingActionQueryTypeNumber(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_monitor_scheduled_query_rules_alert", "test") + r := MonitorScheduledQueryRulesResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.AlertingActionQueryTypeNumber(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} func TestAccMonitorScheduledQueryRules_AlertingActionUpdate(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_monitor_scheduled_query_rules_alert", "test") @@ -156,6 +170,49 @@ QUERY `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, ts, ts, strconv.FormatBool(autoMitigate)) } +func (MonitorScheduledQueryRulesResource) AlertingActionQueryTypeNumber(data acceptance.TestData) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-monitor-%d" + location = "%s" +} + +resource "azurerm_log_analytics_workspace" "test" { + name = "acctestWorkspace-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku = "PerGB2018" + retention_in_days = 30 +} + +resource "azurerm_monitor_action_group" "test" { + name = "acctestActionGroup-%[1]d" + resource_group_name = azurerm_resource_group.test.name + short_name = "acctestag" +} + +resource "azurerm_monitor_scheduled_query_rules_alert" "test" { + name = "acctestsqr-%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + data_source_id = azurerm_log_analytics_workspace.test.id + query_type = "Number" + query = <<-QUERY +Heartbeat | summarize AggregatedValue = count() by bin(TimeGenerated, 5m) +QUERY + frequency = 60 + time_window = 60 + auto_mitigation_enabled = true + action { + action_group = [azurerm_monitor_action_group.test.id] + } + trigger { + operator = "GreaterThan" + threshold = 5000 + } +}`, data.RandomInteger, data.Locations.Primary) +} + func (MonitorScheduledQueryRulesResource) AlertingActionConfigBasic(data acceptance.TestData, ts string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { diff --git a/website/docs/r/monitor_scheduled_query_rules_alert.html.markdown b/website/docs/r/monitor_scheduled_query_rules_alert.html.markdown index 7b58ac56fdaf..2831bf35f0fd 100644 --- a/website/docs/r/monitor_scheduled_query_rules_alert.html.markdown +++ b/website/docs/r/monitor_scheduled_query_rules_alert.html.markdown @@ -119,7 +119,7 @@ The following arguments are supported: -> **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`. -* `query_type` - (Optional) The type of query results. Possible values are `ResultCount` and `Number`. Default is `ResultCount`. +* `query_type` - (Optional) The type of query results. Possible values are `ResultCount` and `Number`. Default is `ResultCount`. If set to `Number`, `query` must include an `AggregatedValue` column of a numeric type, for example, `Heartbeat | summarize AggregatedValue = count() by bin(TimeGenerated, 5m)`. * `severity` - (Optional) Severity of the alert. Possible values include: 0, 1, 2, 3, or 4. * `throttling` - (Optional) Time (in minutes) for which Alerts should be throttled or suppressed. Values must be between 0 and 10000 (inclusive). * `tags` - (Optional) A mapping of tags to assign to the resource.