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_action_rule_action_group, azurerm_monitor_action_rule_suppression #6563

Merged
merged 5 commits into from Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
156 changes: 156 additions & 0 deletions azurerm/internal/services/monitor/action_rule.go
@@ -0,0 +1,156 @@
package monitor

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"

"github.com/Azure/azure-sdk-for-go/services/preview/alertsmanagement/mgmt/2019-05-05/alertsmanagement"
)

var weekDays = []string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
}

var weekDayMap = map[string]int{
"Sunday": 0,
"Monday": 1,
"Tuesday": 2,
"Wednesday": 3,
"Thursday": 4,
"Friday": 5,
"Saturday": 6,
}

func schemaActionRuleCondition(operatorValidateFunc, valuesValidateFunc schema.SchemaValidateFunc) *schema.Schema {
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"operator": {
Type: schema.TypeString,
Required: true,
ValidateFunc: operatorValidateFunc,
},

"values": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: valuesValidateFunc,
},
},
},
},
}
}

func expandArmActionRuleCondition(input []interface{}) *alertsmanagement.Condition {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})
return &alertsmanagement.Condition{
Operator: alertsmanagement.Operator(v["operator"].(string)),
Values: utils.ExpandStringSlice(v["values"].(*schema.Set).List()),
}
}

func expandArmActionRuleScope(input []interface{}) *alertsmanagement.Scope {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})
return &alertsmanagement.Scope{
ScopeType: alertsmanagement.ScopeType(v["type"].(string)),
Values: utils.ExpandStringSlice(v["resource_ids"].(*schema.Set).List()),
}
}

func expandArmActionRuleConditions(input []interface{}) *alertsmanagement.Conditions {
if len(input) == 0 {
return nil
}
v := input[0].(map[string]interface{})

return &alertsmanagement.Conditions{
AlertContext: expandArmActionRuleCondition(v["alert_context"].([]interface{})),
AlertRuleID: expandArmActionRuleCondition(v["alert_rule_id"].([]interface{})),
Description: expandArmActionRuleCondition(v["description"].([]interface{})),
MonitorCondition: expandArmActionRuleCondition(v["monitor"].([]interface{})),
MonitorService: expandArmActionRuleCondition(v["monitor_service"].([]interface{})),
Severity: expandArmActionRuleCondition(v["severity"].([]interface{})),
TargetResourceType: expandArmActionRuleCondition(v["target_resource_type"].([]interface{})),
}
}

func flattenArmActionRuleCondition(input *alertsmanagement.Condition) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

var operator string
if input.Operator != "" {
operator = string(input.Operator)
}
return []interface{}{
map[string]interface{}{
"operator": operator,
"values": utils.FlattenStringSlice(input.Values),
},
}
}

func flattenArmActionRuleScope(input *alertsmanagement.Scope) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

var scopeType alertsmanagement.ScopeType
if input.ScopeType != "" {
scopeType = input.ScopeType
}
return []interface{}{
map[string]interface{}{
"type": scopeType,
"resource_ids": utils.FlattenStringSlice(input.Values),
},
}
}

func flattenArmActionRuleConditions(input *alertsmanagement.Conditions) []interface{} {
if input == nil {
return make([]interface{}, 0)
}
return []interface{}{
map[string]interface{}{
"alert_context": flattenArmActionRuleCondition(input.AlertContext),
"alert_rule_id": flattenArmActionRuleCondition(input.AlertRuleID),
"description": flattenArmActionRuleCondition(input.Description),
"monitor": flattenArmActionRuleCondition(input.MonitorCondition),
"monitor_service": flattenArmActionRuleCondition(input.MonitorService),
"severity": flattenArmActionRuleCondition(input.Severity),
"target_resource_type": flattenArmActionRuleCondition(input.TargetResourceType),
},
}
}

func FlattenInt32Slice(input *[]int32) []interface{} {
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
result := make([]interface{}, 0)
if input != nil {
for _, item := range *input {
result = append(result, item)
}
}
return result
}
8 changes: 8 additions & 0 deletions azurerm/internal/services/monitor/client/client.go
@@ -1,6 +1,7 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/alertsmanagement/mgmt/2019-05-05/alertsmanagement"
"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2019-06-01/insights"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)
Expand All @@ -9,6 +10,9 @@ type Client struct {
// Autoscale Settings
AutoscaleSettingsClient *insights.AutoscaleSettingsClient

// alerts management
ActionRulesClient *alertsmanagement.ActionRulesClient

// Monitor
ActionGroupsClient *insights.ActionGroupsClient
ActivityLogAlertsClient *insights.ActivityLogAlertsClient
Expand All @@ -24,6 +28,9 @@ func NewClient(o *common.ClientOptions) *Client {
AutoscaleSettingsClient := insights.NewAutoscaleSettingsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&AutoscaleSettingsClient.Client, o.ResourceManagerAuthorizer)

ActionRulesClient := alertsmanagement.NewActionRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ActionRulesClient.Client, o.ResourceManagerAuthorizer)

ActionGroupsClient := insights.NewActionGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ActionGroupsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -50,6 +57,7 @@ func NewClient(o *common.ClientOptions) *Client {

return &Client{
AutoscaleSettingsClient: &AutoscaleSettingsClient,
ActionRulesClient: &ActionRulesClient,
ActionGroupsClient: &ActionGroupsClient,
ActivityLogAlertsClient: &ActivityLogAlertsClient,
AlertRulesClient: &AlertRulesClient,
Expand Down