@@ -220,7 +220,7 @@ func setResourceEPProps(d *schema.ResourceData, escalationPolicy *pagerduty.Esca
220
220
return fmt .Errorf ("error setting teams: %s" , err )
221
221
}
222
222
223
- if err := d .Set ("rule" , flattenEscalationRules (escalationPolicy .EscalationRules )); err != nil {
223
+ if err := d .Set ("rule" , flattenEscalationRules (escalationPolicy .EscalationRules , d )); err != nil {
224
224
return err
225
225
}
226
226
return nil
@@ -353,10 +353,10 @@ func expandEscalationRuleAssignmentStrategy(v interface{}) *pagerduty.Escalation
353
353
return escalationRuleAssignmentStrategy
354
354
}
355
355
356
- func flattenEscalationRules (v []* pagerduty.EscalationRule ) []map [string ]interface {} {
356
+ func flattenEscalationRules (v []* pagerduty.EscalationRule , d * schema. ResourceData ) []map [string ]interface {} {
357
357
var escalationRules []map [string ]interface {}
358
358
359
- for _ , er := range v {
359
+ for i , er := range v {
360
360
escalationRule := map [string ]interface {}{
361
361
"id" : er .ID ,
362
362
"escalation_delay_in_minutes" : er .EscalationDelayInMinutes ,
@@ -370,14 +370,38 @@ func flattenEscalationRules(v []*pagerduty.EscalationRule) []map[string]interfac
370
370
escalationRule ["escalation_rule_assignment_strategy" ] = escalationRuleAssignmentStrategy
371
371
372
372
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
+ }
373
394
395
+ // Append targets not present in plan
374
396
for _ , ert := range er .Targets {
397
+ if _ , found := addedTargets [ert .ID ]; found {
398
+ continue
399
+ }
375
400
escalationRuleTarget := map [string ]interface {}{"id" : ert .ID , "type" : ert .Type }
376
401
targets = append (targets , escalationRuleTarget )
377
402
}
378
403
379
404
escalationRule ["target" ] = targets
380
-
381
405
escalationRules = append (escalationRules , escalationRule )
382
406
}
383
407
0 commit comments