Skip to content

Commit

Permalink
New Resource: azurerm_monitor_alert_processing_rule_action_group, `…
Browse files Browse the repository at this point in the history
…azurerm_monitor_alert_processing_rule_suppression` (#17006)
  • Loading branch information
teowa authored Sep 22, 2022
1 parent 4ffbc68 commit 9269780
Show file tree
Hide file tree
Showing 36 changed files with 4,085 additions and 0 deletions.
654 changes: 654 additions & 0 deletions internal/services/monitor/alert_processing_rule.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions internal/services/monitor/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/preview/alertsmanagement/mgmt/2019-06-01-preview/alertsmanagement"
classic "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-07-01-preview/insights"
newActionGroupClient "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-09-01-preview/insights"
"github.com/hashicorp/go-azure-sdk/resource-manager/alertsmanagement/2021-08-08/alertprocessingrules"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionendpoints"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionruleassociations"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionrules"
Expand All @@ -24,6 +25,7 @@ type Client struct {

// alerts management
ActionRulesClient *alertsmanagement.ActionRulesClient
AlertProcessingRulesClient *alertprocessingrules.AlertProcessingRulesClient
SmartDetectorAlertRulesClient *alertsmanagement.SmartDetectorAlertRulesClient

// Monitor
Expand Down Expand Up @@ -53,6 +55,9 @@ func NewClient(o *common.ClientOptions) *Client {
ActionRulesClient := alertsmanagement.NewActionRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ActionRulesClient.Client, o.ResourceManagerAuthorizer)

AlertProcessingRulesClient := alertprocessingrules.NewAlertProcessingRulesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&AlertProcessingRulesClient.Client, o.ResourceManagerAuthorizer)

SmartDetectorAlertRulesClient := alertsmanagement.NewSmartDetectorAlertRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&SmartDetectorAlertRulesClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -106,6 +111,7 @@ func NewClient(o *common.ClientOptions) *Client {
ActionGroupsClient: &ActionGroupsClient,
ActivityLogAlertsClient: &ActivityLogAlertsClient,
AlertRulesClient: &AlertRulesClient,
AlertProcessingRulesClient: &AlertProcessingRulesClient,
DataCollectionEndpointsClient: &DataCollectionEndpointsClient,
DataCollectionRuleAssociationsClient: &DataCollectionRuleAssociationsClient,
DataCollectionRulesClient: &DataCollectionRulesClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
package monitor

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/alertsmanagement/2021-08-08/alertprocessingrules"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/monitor/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type AlertProcessingRuleActionGroupModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
AddActionGroupIds []string `tfschema:"add_action_group_ids"`
Scopes []string `tfschema:"scopes"`
Description string `tfschema:"description"`
Enabled bool `tfschema:"enabled"`
Condition []AlertProcessingRuleConditionModel `tfschema:"condition"`
Schedule []AlertProcessingRuleScheduleModel `tfschema:"schedule"`
Tags map[string]string `tfschema:"tags"`
}

type AlertProcessingRuleActionGroupResource struct{}

var _ sdk.ResourceWithUpdate = AlertProcessingRuleActionGroupResource{}

func (r AlertProcessingRuleActionGroupResource) ResourceType() string {
return "azurerm_monitor_alert_processing_rule_action_group"
}

func (r AlertProcessingRuleActionGroupResource) ModelObject() interface{} {
return &AlertProcessingRuleActionGroupModel{}
}

func (r AlertProcessingRuleActionGroupResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return alertprocessingrules.ValidateActionRuleID
}

func (r AlertProcessingRuleActionGroupResource) Arguments() map[string]*pluginsdk.Schema {
arguments := schemaAlertProcessingRule()
arguments["add_action_group_ids"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validate.ActionGroupID,
},
}
return arguments
}

func (r AlertProcessingRuleActionGroupResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r AlertProcessingRuleActionGroupResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model AlertProcessingRuleActionGroupModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}
client := metadata.Client.Monitor.AlertProcessingRulesClient
subscriptionId := metadata.Client.Account.SubscriptionId

id := alertprocessingrules.NewActionRuleID(subscriptionId, model.ResourceGroupName, model.Name)
existing, err := client.AlertProcessingRulesGetByName(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

alertProcessingRule := alertprocessingrules.AlertProcessingRule{
// Location support "global" only
Location: "global",
Properties: &alertprocessingrules.AlertProcessingRuleProperties{
Actions: []alertprocessingrules.Action{
alertprocessingrules.AddActionGroups{
ActionGroupIds: model.AddActionGroupIds,
}},
Conditions: expandAlertProcessingRuleConditions(model.Condition),
Description: utils.String(model.Description),
Enabled: utils.Bool(model.Enabled),
Schedule: expandAlertProcessingRuleSchedule(model.Schedule),
Scopes: model.Scopes,
},
Tags: &model.Tags,
}

if _, err := client.AlertProcessingRulesCreateOrUpdate(ctx, id, alertProcessingRule); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
}
}

func (r AlertProcessingRuleActionGroupResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.AlertProcessingRulesClient

id, err := alertprocessingrules.ParseActionRuleID(metadata.ResourceData.Id())
if err != nil {
return err
}

var resourceModel AlertProcessingRuleActionGroupModel
if err := metadata.Decode(&resourceModel); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

resp, err := client.AlertProcessingRulesGetByName(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

if resp.Model == nil {
return fmt.Errorf("unexpected null model of %s", *id)
}
model := resp.Model
if model.Properties == nil {
return fmt.Errorf("unexpected null properties of %s", *id)
}

if metadata.ResourceData.HasChange("add_action_group_ids") {
model.Properties.Actions = []alertprocessingrules.Action{
alertprocessingrules.AddActionGroups{
ActionGroupIds: resourceModel.AddActionGroupIds,
}}
}

if metadata.ResourceData.HasChange("condition") {
model.Properties.Conditions = expandAlertProcessingRuleConditions(resourceModel.Condition)
}

if metadata.ResourceData.HasChange("description") {
model.Properties.Description = utils.String(resourceModel.Description)
}

if metadata.ResourceData.HasChange("enabled") {
model.Properties.Enabled = utils.Bool(resourceModel.Enabled)
}

if metadata.ResourceData.HasChange("schedule") {
model.Properties.Schedule = expandAlertProcessingRuleSchedule(resourceModel.Schedule)
}

if metadata.ResourceData.HasChange("scopes") {
model.Properties.Scopes = resourceModel.Scopes
}

if metadata.ResourceData.HasChange("tags") {
model.Tags = &resourceModel.Tags
}

model.SystemData = nil
if _, err := client.AlertProcessingRulesCreateOrUpdate(ctx, *id, *model); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}
return nil
},
}
}

func (r AlertProcessingRuleActionGroupResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.AlertProcessingRulesClient

id, err := alertprocessingrules.ParseActionRuleIDInsensitively(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.AlertProcessingRulesGetByName(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

state := AlertProcessingRuleActionGroupModel{
Name: id.AlertProcessingRuleName,
ResourceGroupName: id.ResourceGroupName,
}

model := resp.Model
if model == nil {
return fmt.Errorf("retrieving %s: model is null", *id)
}
properties := model.Properties
if properties == nil {
return fmt.Errorf("retrieving %s: property is null", *id)
}

addActionGroupID, err := flattenAlertProcessingRuleAddActionGroupId(properties.Actions)
if err != nil {
return err
}
state.AddActionGroupIds = addActionGroupID

if model.Tags != nil {
state.Tags = *model.Tags
}

if properties.Description != nil {
state.Description = *properties.Description
}

if properties.Enabled != nil {
state.Enabled = *properties.Enabled
}

state.Scopes = properties.Scopes

if properties.Conditions != nil {
state.Condition = flattenAlertProcessingRuleConditions(properties.Conditions)
}

if properties.Schedule != nil {
state.Schedule = flattenAlertProcessingRuleSchedule(properties.Schedule)
}

return metadata.Encode(&state)
},
}
}
func (r AlertProcessingRuleActionGroupResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Monitor.AlertProcessingRulesClient

id, err := alertprocessingrules.ParseActionRuleIDInsensitively(metadata.ResourceData.Id())
if err != nil {
return err
}

if _, err := client.AlertProcessingRulesDelete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
},
}
}
Loading

0 comments on commit 9269780

Please sign in to comment.