Skip to content

Commit

Permalink
Merge pull request #846 from imjaroiswebdev/issue-819-403-querying-ep
Browse files Browse the repository at this point in the history
Address: 403 Forbidden error when updating existing pagerduty_escalation_policy's target
  • Loading branch information
imjaroiswebdev authored Apr 5, 2024
2 parents 0cfe90e + 711972f commit 3e196bc
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,23 @@ func fetchEscalationPolicy(d *schema.ResourceData, meta interface{}, errCallback

o := &pagerduty.GetEscalationPolicyOptions{Includes: []string{"escalation_rule_assignment_strategies"}}

var escalationPolicyFirstAttempt *pagerduty.EscalationPolicy

escalationPolicyFirstAttempt, _, err = client.EscalationPolicies.Get(d.Id(), o)
if err != nil && isErrCode(err, http.StatusForbidden) {
// Removing the inclusion of escalation_rule_assignment_strategies for
// accounts wihtout the required entitlements.
o = nil
}

if err == nil && escalationPolicyFirstAttempt == nil {
return setResourceEPProps(d, escalationPolicyFirstAttempt)
}

return retry.Retry(5*time.Minute, func() *retry.RetryError {
escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o)
if err != nil {
if isErrCode(err, http.StatusBadRequest) {
if isErrCode(err, http.StatusBadRequest) || isErrCode(err, http.StatusForbidden) {
return retry.NonRetryableError(err)
}

Expand All @@ -189,22 +202,30 @@ func fetchEscalationPolicy(d *schema.ResourceData, meta interface{}, errCallback
return nil
}

d.Set("name", escalationPolicy.Name)
d.Set("description", escalationPolicy.Description)
d.Set("num_loops", escalationPolicy.NumLoops)

if err := d.Set("teams", flattenTeams(escalationPolicy.Teams)); err != nil {
return retry.NonRetryableError(fmt.Errorf("error setting teams: %s", err))
}

if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
err = setResourceEPProps(d, escalationPolicy)
if err != nil {
return retry.NonRetryableError(err)
}

return nil
})
}

func setResourceEPProps(d *schema.ResourceData, escalationPolicy *pagerduty.EscalationPolicy) error {
d.Set("name", escalationPolicy.Name)
d.Set("description", escalationPolicy.Description)
d.Set("num_loops", escalationPolicy.NumLoops)

if err := d.Set("teams", flattenTeams(escalationPolicy.Teams)); err != nil {
return fmt.Errorf("error setting teams: %s", err)
}

if err := d.Set("rule", flattenEscalationRules(escalationPolicy.EscalationRules)); err != nil {
return err
}
return nil
}

func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).Client()
if err != nil {
Expand Down

0 comments on commit 3e196bc

Please sign in to comment.