Skip to content

Commit

Permalink
azurerm_monitor_scheduled_query_rules_alert - fix query_type (#20290
Browse files Browse the repository at this point in the history
)

resolves #20267
  • Loading branch information
teowa committed Feb 6, 2023
1 parent 61c43e3 commit bc4634b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
9 changes: 6 additions & 3 deletions internal/services/monitor/common_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit bc4634b

Please sign in to comment.