-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use 'internal/tfresource' package. (#15477)
- Loading branch information
Showing
4 changed files
with
122 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |