Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Make error check return early when getting a 404
Browse files Browse the repository at this point in the history
  • Loading branch information
mLupine committed Feb 10, 2022
1 parent 560df72 commit 227913d
Show file tree
Hide file tree
Showing 42 changed files with 115 additions and 101 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/nordcloud/go-pagerduty v1.2.1
github.com/nordcloud/go-pagerduty v1.2.3
github.com/oklog/run v1.1.0 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nordcloud/go-pagerduty v1.2.1 h1:bdVZGNJKAML1EL15YwM3HRK7SZxCLO013W1X59aQTr4=
github.com/nordcloud/go-pagerduty v1.2.1/go.mod h1:ZB7WAA2rP1b5EGA7zqB2B9lN6a1L9AF6seOEDEr9hlI=
github.com/nordcloud/go-pagerduty v1.2.3 h1:ZtYGUcmBFW8yLPpp5LTlMCGD3b25PpS5FYm9nNc94J8=
github.com/nordcloud/go-pagerduty v1.2.3/go.mod h1:ZB7WAA2rP1b5EGA7zqB2B9lN6a1L9AF6seOEDEr9hlI=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func dataSourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interfa

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.BusinessServices.List()
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.BusinessService
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_escalation_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func dataSourcePagerDutyEscalationPoliciesRead(d *schema.ResourceData, meta inte

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.EscalationPolicies.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var ids []string
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func dataSourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interf

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.EscalationPolicies.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.EscalationPolicy
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_extension_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func dataSourcePagerDutyExtensionSchemaRead(d *schema.ResourceData, meta interfa

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.ExtensionSchemas.List(&pagerduty.ListExtensionSchemasOptions{Query: searchName})
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.ExtensionSchema
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func dataSourcePagerDutyPriorityRead(d *schema.ResourceData, meta interface{}) e

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Priorities.List()
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Priority
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func dataSourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) er

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Rulesets.List()
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Ruleset
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func dataSourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) e

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Schedules.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Schedule
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func dataSourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) er

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Services.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Service
Expand Down
8 changes: 4 additions & 4 deletions pagerduty/data_source_pagerduty_service_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func dataSourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta inte

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Services.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Service
Expand All @@ -74,8 +74,8 @@ func dataSourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta inte
for _, integration := range found.Integrations {
if strings.EqualFold(integration.Summary, integrationSummary) {
integrationDetails, _, err := client.Services.GetIntegration(found.ID, integration.ID, &pagerduty.GetIntegrationOptions{})
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}
d.SetId(integration.ID)
d.Set("service_name", found.Name)
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func dataSourcePagerDutyTagRead(d *schema.ResourceData, meta interface{}) error

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Tags.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Tag
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func dataSourcePagerDutyTeamRead(d *schema.ResourceData, meta interface{}) error

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Teams.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Team
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func dataSourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Users.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.User
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_user_contact_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func dataSourcePagerDutyUserContactMethodRead(d *schema.ResourceData, meta inter

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Users.ListContactMethods(userId)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.ContactMethod
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func dataSourcePagerDutyVendorRead(d *schema.ResourceData, meta interface{}) err
}
return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Vendors.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var found *pagerduty.Vendor
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/data_source_pagerduty_vendors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func dataSourcePagerDutyVendorsRead(d *schema.ResourceData, meta interface{}) er
o := &pagerduty.ListVendorsOptions{}
return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.Vendors.List(o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var ids []string
Expand Down
42 changes: 28 additions & 14 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pagerduty

import (
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"log"
Expand All @@ -12,6 +13,11 @@ import (
"github.com/nordcloud/go-pagerduty/pagerduty"
)

type ErrorResponse struct {
ShouldReturn bool
ReturnVal *resource.RetryError
}

// Provider represents a resource provider in Terraform
func Provider() *schema.Provider {
p := &schema.Provider{
Expand Down Expand Up @@ -106,10 +112,18 @@ func Provider() *schema.Provider {
}

func isErrCode(err error, code int) bool {
if e, ok := err.(*pagerduty.Error); ok && e.ErrorResponse.Response.StatusCode == code {
return true
currentErr := err
for errors.Unwrap(currentErr) != nil {
currentErr = errors.Unwrap(currentErr)

if e, ok := currentErr.(*pagerduty.Error); ok && e.ErrorResponse.Response.StatusCode == code {
log.Printf("[INFO] Error code matches expected %d", code)
return true
}
}

log.Printf("[INFO] Error code doesn't match expected %d", code)

return false
}

Expand All @@ -125,52 +139,52 @@ func genError(err error, d *schema.ResourceData) error {
return fmt.Errorf("error reading: %s: %s", resId, errStr)
}

func handleGenericErrors(err error, d *schema.ResourceData) *resource.RetryError {
func handleGenericErrors(err error, d *schema.ResourceData) ErrorResponse {
if err == nil {
return nil
return ErrorResponse{ShouldReturn: false}
}

if isErrCode(err, 429) {
// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return resource.RetryableError(err)
return ErrorResponse{ShouldReturn: true, ReturnVal: resource.RetryableError(err)}
}

if isErrCode(err, 404) {
log.Printf("[WARN] Removing %s because it's gone", d.Id())
d.SetId("")
return nil
return ErrorResponse{ShouldReturn: true, ReturnVal: nil}
}

generatedError := genError(err, d)
if generatedError != nil {
return resource.NonRetryableError(generatedError)
return ErrorResponse{ShouldReturn: true, ReturnVal: resource.NonRetryableError(generatedError)}
}

return nil
return ErrorResponse{ShouldReturn: false}
}

func handleRateLimitErrors(err error, d *schema.ResourceData) *resource.RetryError {
func handleRateLimitErrors(err error, d *schema.ResourceData) ErrorResponse {
if err == nil {
return nil
return ErrorResponse{ShouldReturn: false}
}

if isErrCode(err, 429) {
// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return resource.RetryableError(err)
return ErrorResponse{ShouldReturn: true, ReturnVal: resource.RetryableError(err)}
}
generatedError := genError(err, d)
if generatedError != nil {
return resource.NonRetryableError(generatedError)
return ErrorResponse{ShouldReturn: true, ReturnVal: resource.NonRetryableError(generatedError)}
}

return nil
return ErrorResponse{ShouldReturn: false}
}

func getErrorHandler(shouldHandle404Errors bool) func(err error, d *schema.ResourceData) *resource.RetryError {
func getErrorHandler(shouldHandle404Errors bool) func(err error, d *schema.ResourceData) ErrorResponse {
if shouldHandle404Errors {
return handleGenericErrors
}
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func fetchPagerDutyAddon(d *schema.ResourceData, meta interface{}, handle404Erro

return resource.Retry(5*time.Minute, func() *resource.RetryError {
addon, _, err := client.Addons.Get(d.Id())
if checkErr := getErrorHandler(handle404Errors)(err, d); checkErr != nil {
return checkErr
if checkErr := getErrorHandler(handle404Errors)(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

d.Set("name", addon.Name)
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_business_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func resourcePagerDutyBusinessServiceRead(d *schema.ResourceData, meta interface

retryErr := resource.Retry(5*time.Minute, func() *resource.RetryError {
businessService, _, err := client.BusinessServices.Get(d.Id())
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

if businessService != nil {
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_business_service_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func resourcePagerDutyBusinessServiceSubscriberRead(d *schema.ResourceData, meta

return resource.Retry(5*time.Minute, func() *resource.RetryError {
subscriberResponse, _, err := client.BusinessServiceSubscribers.List(businessServiceId)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

if subscriberResponse != nil {
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interfac

return resource.Retry(5*time.Minute, func() *resource.RetryError {
escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o)
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

d.Set("name", escalationPolicy.Name)
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_event_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func resourcePagerDutyEventRuleRead(d *schema.ResourceData, meta interface{}) er

return resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, _, err := client.EventRules.List()
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

var foundRule *pagerduty.EventRule
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func fetchPagerDutyExtension(d *schema.ResourceData, meta interface{}, handle404

return resource.Retry(5*time.Minute, func() *resource.RetryError {
extension, _, err := client.Extensions.Get(d.Id())
if checkErr := getErrorHandler(handle404Errors)(err, d); checkErr != nil {
return checkErr
if checkErr := getErrorHandler(handle404Errors)(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

d.Set("summary", extension.Summary)
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_extension_servicenow.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func fetchPagerDutyExtensionServiceNowCreate(d *schema.ResourceData, meta interf

return resource.Retry(5*time.Minute, func() *resource.RetryError {
extension, _, err := client.Extensions.Get(d.Id())
if checkErr := getErrorHandler(handle404Errors)(err, d); checkErr != nil {
return checkErr
if checkErr := getErrorHandler(handle404Errors)(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

d.Set("summary", extension.Summary)
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/resource_pagerduty_maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func resourcePagerDutyMaintenanceWindowRead(d *schema.ResourceData, meta interfa

return resource.Retry(5*time.Minute, func() *resource.RetryError {
window, _, err := client.MaintenanceWindows.Get(d.Id())
if checkErr := handleGenericErrors(err, d); checkErr != nil {
return checkErr
if checkErr := handleGenericErrors(err, d); checkErr.ShouldReturn {
return checkErr.ReturnVal
}

d.Set("description", window.Description)
Expand Down
Loading

0 comments on commit 227913d

Please sign in to comment.