Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to ensure escalation rule targets are added in same order as the plan #937

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading