Skip to content

Commit

Permalink
azurerm_monitor_scheduled_query_rules_alert_v2 - correctly handling…
Browse files Browse the repository at this point in the history
… `identity` block if not specified (hashicorp#26364)
  • Loading branch information
teowa authored Jun 24, 2024
1 parent b4185b4 commit bf365b3
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (r ScheduledQueryRulesAlertV2Resource) Arguments() map[string]*pluginsdk.Sc
Required: true,
MinItems: 1,
Elem: &pluginsdk.Resource{

Schema: map[string]*pluginsdk.Schema{
"query": {
Type: pluginsdk.TypeString,
Expand Down Expand Up @@ -431,11 +430,6 @@ func (r ScheduledQueryRulesAlertV2Resource) Create() sdk.ResourceFunc {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

ExpanededIdentity, err := identity.ExpandSystemOrUserAssignedMapFromModel(model.Identity)
if err != nil {
return fmt.Errorf("expanding SystemOrUserAssigned Identity: %+v", err)
}

kind := scheduledqueryrules.KindLogAlert
properties := &scheduledqueryrules.ScheduledQueryRuleResource{
Kind: &kind,
Expand All @@ -449,8 +443,7 @@ func (r ScheduledQueryRulesAlertV2Resource) Create() sdk.ResourceFunc {
SkipQueryValidation: &model.SkipQueryValidation,
TargetResourceTypes: &model.TargetResourceTypes,
},
Identity: ExpanededIdentity,
Tags: &model.Tags,
Tags: &model.Tags,
}

properties.Properties.Actions = expandScheduledQueryRulesAlertV2ActionsModel(model.Actions)
Expand All @@ -469,6 +462,14 @@ func (r ScheduledQueryRulesAlertV2Resource) Create() sdk.ResourceFunc {
properties.Properties.EvaluationFrequency = &model.EvaluationFrequency
}

if len(model.Identity) != 0 {
ExpandedIdentity, err := identity.ExpandSystemOrUserAssignedMapFromModel(model.Identity)
if err != nil {
return fmt.Errorf("expanding SystemOrUserAssigned Identity: %+v", err)
}
properties.Identity = ExpandedIdentity
}

if model.MuteActionsDuration != "" {
if model.AutoMitigate {
return fmt.Errorf("auto mitigation must be disabled when mute action duration is set")
Expand Down Expand Up @@ -596,9 +597,13 @@ func (r ScheduledQueryRulesAlertV2Resource) Update() sdk.ResourceFunc {
}

if metadata.ResourceData.HasChange("identity") {
model.Identity, err = identity.ExpandSystemOrUserAssignedMapFromModel(resourceModel.Identity)
if err != nil {
return fmt.Errorf("expanding SystemOrUserAssigned Identity: %+v", err)
if len(resourceModel.Identity) != 0 {
model.Identity, err = identity.ExpandSystemOrUserAssignedMapFromModel(resourceModel.Identity)
if err != nil {
return fmt.Errorf("expanding SystemOrUserAssigned Identity: %+v", err)
}
} else {
model.Identity = nil
}
}

Expand Down

0 comments on commit bf365b3

Please sign in to comment.