Skip to content

Commit

Permalink
Merge pull request #858 from imjaroiswebdev/issue-819-repatch-handle-…
Browse files Browse the repository at this point in the history
…malformed-403

Handle malformed 403 Forbidden errors on EP updates
  • Loading branch information
imjaroiswebdev authored Apr 16, 2024
2 parents 072bf18 + 7484028 commit 78d9bf5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 13 additions & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,23 @@ func isErrCode(err error, code int) bool {
func isMalformedNotFoundError(err error) bool {
// There are some errors that doesn't stick to expected error interface and
// fallback to a simple text error message that can be capture by this regexp.
if err == nil {
return false
}

re := regexp.MustCompile(".*: 404 Not Found$")
return re.Match([]byte(err.Error()))
}

func isMalformedForbiddenError(err error) bool {
if err == nil {
return false
}

re := regexp.MustCompile(".*: 403 Forbidden$")
return re.Match([]byte(err.Error()))
}

func genError(err error, d *schema.ResourceData) error {
return fmt.Errorf("Error reading: %s: %s", d.Id(), err)
}
Expand Down
16 changes: 8 additions & 8 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func fetchEscalationPolicy(d *schema.ResourceData, meta interface{}, errCallback
var escalationPolicyFirstAttempt *pagerduty.EscalationPolicy

escalationPolicyFirstAttempt, _, err = client.EscalationPolicies.Get(d.Id(), o)
if err != nil && isErrCode(err, http.StatusForbidden) {
if err != nil && isErrCode(err, http.StatusForbidden) || isMalformedForbiddenError(err) {
// Removing the inclusion of escalation_rule_assignment_strategies for
// accounts wihtout the required entitlements.
o = nil
Expand All @@ -188,7 +188,7 @@ func fetchEscalationPolicy(d *schema.ResourceData, meta interface{}, errCallback
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) || isErrCode(err, http.StatusForbidden) {
if isErrCode(err, http.StatusBadRequest) || isErrCode(err, http.StatusForbidden) || isMalformedForbiddenError(err) {
return retry.NonRetryableError(err)
}

Expand Down Expand Up @@ -237,7 +237,11 @@ func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interf
log.Printf("[INFO] Updating PagerDuty escalation policy: %s", d.Id())

_, _, err = client.EscalationPolicies.Update(d.Id(), escalationPolicy)
if err != nil && isErrCode(err, http.StatusForbidden) {
if err == nil {
return nil
}

if isErrCode(err, http.StatusForbidden) || isMalformedForbiddenError(err) {
// Removing the inclusion of escalation_rule_assignment_strategies for
// accounts wihtout the required entitlements.
for idx, er := range escalationPolicy.EscalationRules {
Expand All @@ -250,13 +254,9 @@ func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interf
}
}

if err == nil {
return nil
}

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

Expand Down

0 comments on commit 78d9bf5

Please sign in to comment.