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

Handle malformed 403 Forbidden errors on EP updates #858

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
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
Loading