Skip to content

Commit

Permalink
readability refactor of rule deletion from escalation policies when n…
Browse files Browse the repository at this point in the history
…eeded
  • Loading branch information
imjaroiswebdev committed Sep 9, 2022
1 parent af206af commit dbb8759
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pagerduty/resource_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,16 +621,23 @@ func removeScheduleFromEP(c *pagerduty.Client, scheduleID string, ep *pagerduty.
epr := ep.EscalationRules
for ri, r := range epr {
for index, target := range r.Targets {
if target.Type == "schedule_reference" && target.ID == scheduleID {
// Remove Schedule as a configured Target from the Escalation Rules
// slice
r.Targets = append(r.Targets[:index], r.Targets[index+1:]...)
isScheduleConfiguredInEscalationRule := target.Type == "schedule_reference" && target.ID == scheduleID
if !isScheduleConfiguredInEscalationRule {
continue
}

if isScheduleConfiguredInEscalationRule {
if len(r.Targets) > 1 {
// Removing Schedule as a configured Target from the Escalation Rules
// slice.
r.Targets = append(r.Targets[:index], r.Targets[index+1:]...)
} else {
// Removing Escalation Rules that will end up having no target configured.
epr = append(epr[:ri], epr[ri+1:]...)
}
needsToUpdate = true
}
}
if len(r.Targets) == 0 {
epr = append(epr[:ri], epr[ri+1:]...)
}
}
if !needsToUpdate {
return nil
Expand Down

0 comments on commit dbb8759

Please sign in to comment.