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

azurerm_monitor_scheduled_query_rules_alert - fix query_type #20290

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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