Skip to content

Commit

Permalink
Try to ensure escalation rule targets are added in same order as the …
Browse files Browse the repository at this point in the history
…plan
  • Loading branch information
cjgajard committed Oct 9, 2024
1 parent 536f3c8 commit ab8697f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 28 additions & 4 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func setResourceEPProps(d *schema.ResourceData, escalationPolicy *pagerduty.Esca
return fmt.Errorf("error setting teams: %s", err)
}

if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules, d)); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -353,10 +353,10 @@ func expandEscalationRuleAssignmentStrategy(v interface{}) *pagerduty.Escalation
return escalationRuleAssignmentStrategy
}

func flattenEscalationRules(v []*pagerduty.EscalationRule) []map[string]interface{} {
func flattenEscalationRules(v []*pagerduty.EscalationRule, d *schema.ResourceData) []map[string]interface{} {
var escalationRules []map[string]interface{}

for _, er := range v {
for i, er := range v {
escalationRule := map[string]interface{}{
"id": er.ID,
"escalation_delay_in_minutes": er.EscalationDelayInMinutes,
Expand All @@ -370,14 +370,38 @@ func flattenEscalationRules(v []*pagerduty.EscalationRule) []map[string]interfac
escalationRule["escalation_rule_assignment_strategy"] = escalationRuleAssignmentStrategy

var targets []map[string]interface{}
addedTargets := map[string]struct{}{}

// Append targets in same orden as plan, then mark them as added
if d != nil {
targetsPlan := d.Get(fmt.Sprintf("rule.%d.target", i)).([]any)
for _, tpValue := range targetsPlan {
var ert *pagerduty.EscalationTargetReference
for _, t := range er.Targets {
if t.ID == tpValue.(map[string]any)["id"].(string) {
ert = t
break
}
}
if ert == nil {
continue
}
escalationRuleTarget := map[string]interface{}{"id": ert.ID, "type": ert.Type}
targets = append(targets, escalationRuleTarget)
addedTargets[ert.ID] = struct{}{}
}
}

// Append targets not present in plan
for _, ert := range er.Targets {
if _, found := addedTargets[ert.ID]; found {
continue
}
escalationRuleTarget := map[string]interface{}{"id": ert.ID, "type": ert.Type}
targets = append(targets, escalationRuleTarget)
}

escalationRule["target"] = targets

escalationRules = append(escalationRules, escalationRule)
}

Expand Down
2 changes: 1 addition & 1 deletion pagerduty/resource_pagerduty_response_play.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func flattenResponders(rlist []*pagerduty.Responder) []interface{} {
// EscalationRules
if r.EscalationRules != nil {
// flattenEscalationRules in resource_pagerduty_escalation_policy
flattenedR["escalation_rules"] = flattenEscalationRules(r.EscalationRules)
flattenedR["escalation_rules"] = flattenEscalationRules(r.EscalationRules, nil)
}
// Services
if r.Services != nil {
Expand Down

0 comments on commit ab8697f

Please sign in to comment.