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

New Resource: azurerm_monitor_scheduled_query_rules_alert & azurerm_monitor_scheduled_query_rules_log #5053

Merged
merged 31 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
81f2521
New Resource: `azurerm_monitor_scheduled_query_rules`
mcdafydd Sep 28, 2019
5303ecd
Acceptance tests passing
mcdafydd Dec 3, 2019
607a00d
Fix linting errors; minor cleanup
mcdafydd Dec 3, 2019
ebf84e9
Minor updates and bug fixes
mcdafydd Dec 5, 2019
ce59b1a
Remove heredoc in docs example
mcdafydd Dec 5, 2019
03cdd53
Merge remote-tracking branch 'upstream/master' into r/scheduled-query…
mcdafydd Dec 10, 2019
f3245e1
Merge remote-tracking branch 'upstream/master' into r/scheduled-query…
mcdafydd Dec 11, 2019
a1bf9ba
fix response import
mcdafydd Dec 11, 2019
7b785a0
Merge remote-tracking branch 'upstream/master' into r/scheduled-query…
mcdafydd Dec 12, 2019
e4e4c9e
Merge remote-tracking branch 'upstream/master' into r/scheduled-query…
mcdafydd Dec 27, 2019
8acb500
Update tests and docs to pass checks
mcdafydd Dec 27, 2019
7b25b37
Add moved tests to registration
mcdafydd Dec 28, 2019
88a2788
First pass update docs post-review
mcdafydd Jan 28, 2020
3dcea9c
First pass at separating into two resources
mcdafydd Jan 28, 2020
27f7f4f
Rename action to alert in docs
mcdafydd Jan 29, 2020
7dfdeb3
Fix code review items
mcdafydd Feb 1, 2020
7220ba4
Fix lint issues and minor typos
mcdafydd Feb 1, 2020
3fa8441
Fix lint issues #2
mcdafydd Feb 1, 2020
d2a15de
Fix lint issues #3
mcdafydd Feb 1, 2020
04d4a12
Fix documentation error
mcdafydd Feb 5, 2020
d835ced
Merge remote-tracking branch 'upstream/master' into r/scheduled-query…
mcdafydd Feb 6, 2020
78ddff0
Add monitor helpers
mcdafydd Feb 9, 2020
e9ce243
First pass address latest review
mcdafydd Feb 10, 2020
14d0825
Validate metric_trigger query; add example to docs
mcdafydd Feb 11, 2020
651c41f
Address review comments
mcdafydd Feb 15, 2020
db64f0d
Fix type assertions; simplify cross-resource query example
mcdafydd Feb 22, 2020
37b2c39
Review updates
mcdafydd Feb 22, 2020
347753a
Add MaxItems for cross-resource query
Feb 22, 2020
7689b82
Merge remote-tracking branch 'upstream/master' into r/scheduled-query…
mcdafydd Feb 23, 2020
27ad7ae
Address review comments
mcdafydd Mar 3, 2020
0625b05
Fix markdown formatting
mcdafydd Mar 3, 2020
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
116 changes: 116 additions & 0 deletions azurerm/helpers/azure/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,124 @@ package azure

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2019-06-01/insights"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func FlattenAzureRmScheduledQueryRulesAlertAction(input *insights.AzNsActionGroup) []interface{} {
mcdafydd marked this conversation as resolved.
Show resolved Hide resolved
result := make([]interface{}, 0)
v := make(map[string]interface{})

if input != nil {
if input.ActionGroup != nil {
v["action_group"] = *input.ActionGroup
}
v["email_subject"] = input.EmailSubject
v["custom_webhook_payload"] = input.CustomWebhookPayload
}
result = append(result, v)

return result
mcdafydd marked this conversation as resolved.
Show resolved Hide resolved
}

func ExpandMonitorScheduledQueryRulesCommonSource(d *schema.ResourceData) *insights.Source {
authorizedResourceIDs := d.Get("authorized_resource_ids").(*schema.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.ResultCount,
}
if ok {
source.Query = utils.String(query.(string))
}

return &source
}

func FlattenAzureRmScheduledQueryRulesAlertMetricTrigger(input *insights.LogMetricTrigger) []interface{} {
result := make(map[string]interface{})

if input == nil {
return []interface{}{}
}

result["operator"] = string(input.ThresholdOperator)

if input.Threshold != nil {
result["threshold"] = *input.Threshold
}

result["metric_trigger_type"] = string(input.MetricTriggerType)

if input.MetricColumn != nil {
result["metric_column"] = *input.MetricColumn
}
return []interface{}{result}
}

func FlattenAzureRmScheduledQueryRulesAlertTrigger(input *insights.TriggerCondition) []interface{} {
result := make(map[string]interface{})

result["operator"] = string(input.ThresholdOperator)

if input.Threshold != nil {
result["threshold"] = *input.Threshold
}

if input.MetricTrigger != nil {
result["metric_trigger"] = FlattenAzureRmScheduledQueryRulesAlertMetricTrigger(input.MetricTrigger)
}

return []interface{}{result}
}

func FlattenAzureRmScheduledQueryRulesLogCriteria(input *[]insights.Criteria) []interface{} {
result := make([]interface{}, 0)

if input != nil {
for _, criteria := range *input {
v := make(map[string]interface{})

v["dimension"] = FlattenAzureRmScheduledQueryRulesLogDimension(criteria.Dimensions)
v["metric_name"] = *criteria.MetricName

result = append(result, v)
}
}

return result
}

func FlattenAzureRmScheduledQueryRulesLogDimension(input *[]insights.Dimension) []interface{} {
result := make([]interface{}, 0)

if input != nil {
for _, dimension := range *input {
v := make(map[string]interface{})

if dimension.Name != nil {
v["name"] = *dimension.Name
}

if dimension.Operator != nil {
v["operator"] = *dimension.Operator
}

if dimension.Values != nil {
v["values"] = *dimension.Values
}

result = append(result, v)
}
}

return result
}

// ValidateThreshold checks that a threshold value is between 0 and 10000
// and is a whole number. The azure-sdk-for-go expects this value to be a float64
// but the user validation rules want an integer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,20 @@ func dataSourceArmMonitorScheduledQueryRulesAlertRead(d *schema.ResourceData, me
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Scheduled Query Rule %q was not found", name)
return fmt.Errorf("[DEBUG] Scheduled Query Rule %q was not found in Resource Group %q: %+v", name, resourceGroup, err)
}
return fmt.Errorf("Error reading Scheduled Query Rule: %+v", err)
return fmt.Errorf("Error getting Scheduled Query Rule %q (resource group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
Expand All @@ -175,9 +177,9 @@ func dataSourceArmMonitorScheduledQueryRulesAlertRead(d *schema.ResourceData, me

action, ok := resp.Action.(insights.AlertingAction)
if !ok {
return fmt.Errorf("Wrong action type in scheduled query rule %q (resource group %q): %T", name, resourceGroup, resp.Action)
return fmt.Errorf("Wrong action type in Scheduled Query Rule %q (resource group %q): %T", name, resourceGroup, resp.Action)
}
if err = d.Set("action", flattenAzureRmScheduledQueryRulesAlertAction(action.AznsAction)); err != nil {
if err = d.Set("action", azure.FlattenAzureRmScheduledQueryRulesAlertAction(action.AznsAction)); err != nil {
return fmt.Errorf("Error setting `action`: %+v", err)
}
severity, err := strconv.Atoi(string(action.Severity))
Expand All @@ -186,7 +188,7 @@ func dataSourceArmMonitorScheduledQueryRulesAlertRead(d *schema.ResourceData, me
}
d.Set("severity", severity)
d.Set("throttling", action.ThrottlingInMin)
if err = d.Set("trigger", flattenAzureRmScheduledQueryRulesAlertTrigger(action.Trigger)); err != nil {
if err = d.Set("trigger", azure.FlattenAzureRmScheduledQueryRulesAlertTrigger(action.Trigger)); err != nil {
return fmt.Errorf("Error setting `trigger`: %+v", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ func dataSourceArmMonitorScheduledQueryRulesLog() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"query_type": {
Type: schema.TypeString,
Computed: true,
},

"tags": tags.SchemaDataSource(),
},
Expand All @@ -100,18 +96,20 @@ func dataSourceArmMonitorScheduledQueryRulesLogRead(d *schema.ResourceData, meta
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Scheduled Query Rule %q was not found", name)
return fmt.Errorf("[DEBUG] Scheduled Query Rule %q was not found in Resource Group %q: %+v", name, resourceGroup, err)
}
return fmt.Errorf("Error reading Scheduled Query Rule: %+v", err)
return fmt.Errorf("Error getting Scheduled Query Rule %q (resource group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
Expand All @@ -126,9 +124,9 @@ func dataSourceArmMonitorScheduledQueryRulesLogRead(d *schema.ResourceData, meta

action, ok := resp.Action.(insights.LogToMetricAction)
if !ok {
return fmt.Errorf("Wrong action type in scheduled query rule %q (resource group %q): %T", name, resourceGroup, resp.Action)
return fmt.Errorf("Wrong action type in Scheduled Query Rule %q (resource group %q): %T", name, resourceGroup, resp.Action)
}
if err = d.Set("criteria", flattenAzureRmScheduledQueryRulesLogCriteria(action.Criteria)); err != nil {
if err = d.Set("criteria", azure.FlattenAzureRmScheduledQueryRulesLogCriteria(action.Criteria)); err != nil {
return fmt.Errorf("Error setting `criteria`: %+v", err)
}

Expand All @@ -148,7 +146,6 @@ func dataSourceArmMonitorScheduledQueryRulesLogRead(d *schema.ResourceData, meta
if source.DataSourceID != nil {
d.Set("data_source_id", source.DataSourceID)
}
d.Set("query_type", string(source.QueryType))
}

return tags.FlattenAndSet(d, resp.Tags)
Expand Down
3 changes: 0 additions & 3 deletions azurerm/internal/services/monitor/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_autoscale_setting": resourceArmAutoScaleSetting(),
"azurerm_metric_alertrule": resourceArmMetricAlertRule(),
"azurerm_monitor_autoscale_setting": resourceArmMonitorAutoScaleSetting(),
"azurerm_monitor_action_group": resourceArmMonitorActionGroup(),
"azurerm_monitor_activity_log_alert": resourceArmMonitorActivityLogAlert(),
"azurerm_monitor_diagnostic_setting": resourceArmMonitorDiagnosticSetting(),
"azurerm_monitor_log_profile": resourceArmMonitorLogProfile(),
"azurerm_monitor_metric_alert": resourceArmMonitorMetricAlert(),
"azurerm_monitor_metric_alertrule": resourceArmMonitorMetricAlertRule(),
"azurerm_monitor_scheduled_query_rules_alert": resourceArmMonitorScheduledQueryRulesAlert(),
"azurerm_monitor_scheduled_query_rules_log": resourceArmMonitorScheduledQueryRulesLog()}
}
Loading