Skip to content

Commit fc89502

Browse files
Merge pull request #937 from cjgajard/tfprovdev-106
Try to ensure escalation rule targets are added in same order as the plan
2 parents 536f3c8 + ab8697f commit fc89502

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

pagerduty/resource_pagerduty_escalation_policy.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func setResourceEPProps(d *schema.ResourceData, escalationPolicy *pagerduty.Esca
220220
return fmt.Errorf("error setting teams: %s", err)
221221
}
222222

223-
if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
223+
if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules, d)); err != nil {
224224
return err
225225
}
226226
return nil
@@ -353,10 +353,10 @@ func expandEscalationRuleAssignmentStrategy(v interface{}) *pagerduty.Escalation
353353
return escalationRuleAssignmentStrategy
354354
}
355355

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

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

372372
var targets []map[string]interface{}
373+
addedTargets := map[string]struct{}{}
374+
375+
// Append targets in same orden as plan, then mark them as added
376+
if d != nil {
377+
targetsPlan := d.Get(fmt.Sprintf("rule.%d.target", i)).([]any)
378+
for _, tpValue := range targetsPlan {
379+
var ert *pagerduty.EscalationTargetReference
380+
for _, t := range er.Targets {
381+
if t.ID == tpValue.(map[string]any)["id"].(string) {
382+
ert = t
383+
break
384+
}
385+
}
386+
if ert == nil {
387+
continue
388+
}
389+
escalationRuleTarget := map[string]interface{}{"id": ert.ID, "type": ert.Type}
390+
targets = append(targets, escalationRuleTarget)
391+
addedTargets[ert.ID] = struct{}{}
392+
}
393+
}
373394

395+
// Append targets not present in plan
374396
for _, ert := range er.Targets {
397+
if _, found := addedTargets[ert.ID]; found {
398+
continue
399+
}
375400
escalationRuleTarget := map[string]interface{}{"id": ert.ID, "type": ert.Type}
376401
targets = append(targets, escalationRuleTarget)
377402
}
378403

379404
escalationRule["target"] = targets
380-
381405
escalationRules = append(escalationRules, escalationRule)
382406
}
383407

pagerduty/resource_pagerduty_response_play.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func flattenResponders(rlist []*pagerduty.Responder) []interface{} {
446446
// EscalationRules
447447
if r.EscalationRules != nil {
448448
// flattenEscalationRules in resource_pagerduty_escalation_policy
449-
flattenedR["escalation_rules"] = flattenEscalationRules(r.EscalationRules)
449+
flattenedR["escalation_rules"] = flattenEscalationRules(r.EscalationRules, nil)
450450
}
451451
// Services
452452
if r.Services != nil {

0 commit comments

Comments
 (0)