Skip to content

Commit

Permalink
Add and use 'internal/tfresource' package. (#15477)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit authored Oct 5, 2020
1 parent a7395fc commit afafdbf
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 16 deletions.
10 changes: 2 additions & 8 deletions aws/internal/keyvaluetags/create_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions aws/internal/keyvaluetags/generators/createtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,11 @@ import (
{{- end }}
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)
const EventualConsistencyTimeout = 5 * time.Minute
// Copied from aws/utils.go
// TODO: Export in shared package or add to Terraform Plugin SDK
func isResourceTimeoutError(err error) bool {
timeoutErr, ok := err.(*resource.TimeoutError)
return ok && timeoutErr.LastError == nil
}
{{- range .ServiceNames }}
// {{ . | Title }}CreateTags creates {{ . }} service tags for new resources.
Expand Down Expand Up @@ -161,7 +155,7 @@ func {{ . | Title }}CreateTags(conn {{ . | ClientType }}, identifier string{{ if
return nil
})
if isResourceTimeoutError(err) {
if tfresource.TimedOut(err) {
_, err = conn.{{ . | TagFunction }}(input)
}
{{- else }}
Expand Down
21 changes: 21 additions & 0 deletions aws/internal/tfresource/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tfresource

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

// NotFound returns true if the error represents a "resource not found" condition.
// Specifically, NotFound returns true if the error is of type resource.NotFoundError.
func NotFound(err error) bool {
_, ok := err.(*resource.NotFoundError)
return ok
}

// TimedOut returns true if the error represents a "wait timed out" condition.
// Specifically, TimedOut returns true if the error matches all these conditions:
// * err is of type resource.TimeoutError
// * TimeoutError.LastError is nil
func TimedOut(err error) bool {
timeoutErr, ok := err.(*resource.TimeoutError)
return ok && timeoutErr.LastError == nil
}
97 changes: 97 additions & 0 deletions aws/internal/tfresource/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package tfresource

import (
"errors"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestNotFound(t *testing.T) {
testCases := []struct {
Name string
Err error
Expected bool
}{
{
Name: "nil error",
Err: nil,
},
{
Name: "other error",
Err: errors.New("test"),
},
{
Name: "not found error",
Err: &resource.NotFoundError{LastError: errors.New("test")},
Expected: true,
},
{
Name: "wrapped other error",
Err: fmt.Errorf("test: %w", errors.New("test")),
},
{
Name: "wrapped not found error",
Err: fmt.Errorf("test: %w", &resource.NotFoundError{LastError: errors.New("test")}),
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
got := NotFound(testCase.Err)

if got != testCase.Expected {
t.Errorf("got %t, expected %t", got, testCase.Expected)
}
})
}
}

func TestTimedOut(t *testing.T) {
testCases := []struct {
Name string
Err error
Expected bool
}{
{
Name: "nil error",
Err: nil,
},
{
Name: "other error",
Err: errors.New("test"),
},
{
Name: "timeout error",
Err: &resource.TimeoutError{},
Expected: true,
},
{
Name: "timeout error non-nil last error",
Err: &resource.TimeoutError{LastError: errors.New("test")},
},
{
Name: "wrapped other error",
Err: fmt.Errorf("test: %w", errors.New("test")),
},
{
Name: "wrapped timeout error",
Err: fmt.Errorf("test: %w", &resource.TimeoutError{}),
},
{
Name: "wrapped timeout error non-nil last error",
Err: fmt.Errorf("test: %w", &resource.TimeoutError{LastError: errors.New("test")}),
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
got := TimedOut(testCase.Err)

if got != testCase.Expected {
t.Errorf("got %t, expected %t", got, testCase.Expected)
}
})
}
}

0 comments on commit afafdbf

Please sign in to comment.