-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource:
azurerm_monitor_alert_processing_rule_action_group
, `…
…azurerm_monitor_alert_processing_rule_suppression` (#17006)
- Loading branch information
Showing
36 changed files
with
4,085 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
260 changes: 260 additions & 0 deletions
260
internal/services/monitor/monitor_alert_processing_rule_action_group_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} | ||
} |
Oops, something went wrong.