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

Fix migration to terraform-plugin-testing causes panic in existing test suite #1167

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/NOTES-20230310-073619.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: NOTES
body: '''helper/resource: Deprecated `PrefixedUniqueId()` and `UniqueId()`. Use the
`helper/id` package instead.'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awkward as it may be, I think we should include enhancement entries for the new packages and target deprecations on minor versions so folks are less "surprised". I'm not sure if its worth calling out that the existing package code was converted to aliases to the migrated code in case that surfaces weirdness with tooling such as bflad/tfproviderlint or other AST-based tooling.

It may also be good if these deprecation notices call out that these deprecations are to assist in migrating to terraform-plugin-testing.

time: 2023-03-10T07:36:19.907948Z
custom:
Issue: "1167"
6 changes: 6 additions & 0 deletions .changes/unreleased/NOTES-20230310-073701.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: NOTES
body: '''helper/resource: Deprecated `RetryContext()`, `StateChangeConf`, and associated
`*Error` types. Use the `helper/retry` package instead.'
time: 2023-03-10T07:37:01.40605Z
custom:
Issue: "1167"
2 changes: 1 addition & 1 deletion helper/resource/id.go → helper/id/id.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package id

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion helper/resource/id_test.go → helper/id/id_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package id

import (
"regexp"
Expand Down
109 changes: 109 additions & 0 deletions helper/resource/aliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package resource

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add the exports from the new id package to this set of aliases as well?

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++1

)

// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
type NotFoundError retry.NotFoundError

// UnexpectedStateError is returned when Refresh returns a state that's neither in Target nor Pending
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
type UnexpectedStateError retry.UnexpectedStateError

// TimeoutError is returned when WaitForState times out
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
type TimeoutError retry.TimeoutError

// StateRefreshFunc is a function type used for StateChangeConf that is
// responsible for refreshing the item being watched for a state change.
//
// It returns three results. `result` is any object that will be returned
// as the final object after waiting for state change. This allows you to
// return the final updated object, for example an EC2 instance after refreshing
// it. A nil result represents not found.
//
// `state` is the latest state of that object. And `err` is any error that
// may have happened while refreshing the state.
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
type StateRefreshFunc retry.StateRefreshFunc

// StateChangeConf is the configuration struct used for `WaitForState`.
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
type StateChangeConf retry.StateChangeConf

// RetryFunc is the function retried until it succeeds.
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
type RetryFunc retry.RetryFunc

// RetryContext is a basic wrapper around StateChangeConf that will just retry
// a function until it no longer returns an error.
//
// Cancellation from the passed in context will propagate through to the
// underlying StateChangeConf
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
func RetryContext(ctx context.Context, timeout time.Duration, f RetryFunc) error {
return retry.RetryContext(ctx, timeout, retry.RetryFunc(f))
}

// Retry is a basic wrapper around StateChangeConf that will just retry
// a function until it no longer returns an error.
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
func Retry(timeout time.Duration, f RetryFunc) error {
return retry.Retry(timeout, retry.RetryFunc(f))
}

// RetryError is the required return type of RetryFunc. It forces client code
// to choose whether or not a given error is retryable.
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
type RetryError retry.RetryError

// RetryableError is a helper to create a RetryError that's retryable from a
// given error. To prevent logic errors, will return an error when passed a
// nil error.
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
func RetryableError(err error) *RetryError {
r := retry.RetryableError(err)

return &RetryError{
Err: r.Err,
Retryable: r.Retryable,
}
}

// NonRetryableError is a helper to create a RetryError that's _not_ retryable
// from a given error. To prevent logic errors, will return an error when
// passed a nil error.
//
// Deprecated: Use helper/retry package instead. This is required for migrating acceptance
// testing to terraform-plugin-testing.
func NonRetryableError(err error) *RetryError {
r := retry.NonRetryableError(err)

return &RetryError{
Err: err,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the return value as the called function may change the error.

Suggested change
Err: err,
Err: r.Err,

Retryable: r.Retryable,
}
}
1 change: 1 addition & 0 deletions helper/resource/testcase_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down
2 changes: 1 addition & 1 deletion helper/resource/error.go → helper/retry/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package retry

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion helper/resource/state.go → helper/retry/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package retry

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package retry

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion helper/resource/wait.go → helper/retry/wait.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package retry

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package retry

import (
"context"
Expand Down
12 changes: 6 additions & 6 deletions website/docs/plugin/sdkv2/guides/v2-upgrade-guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Upgrade topics:
* [Louder `helper/schema.ResourceData.Set` Errors in Testing](#louder-helper-schema-resourcedata-set-errors-in-testing)
* [More Consistent Usage of `github.com/mitchellh/go-testing-interface`](#more-consistent-usage-of-github-com-mitchellh-go-testing-interface)
* [Corrected Results of `helper/acctest.RandIntRange`](#corrected-results-of-helper-acctest-randintrange)
* [Clearer Handling of `nil` for `helper/resource.NonRetryableError` and `helper/resource.RetryableError`](#clearer-handling-of-nil-for-helper-resource-nonretryableerror-and-helper-resource-retryableerror)
* [Clearer Handling of `nil` for `helper/retry.RetryableError` and `helper/retry.RetryableError`](#clearer-handling-of-nil-for-helper-resource-nonretryableerror-and-helper-resource-retryableerror)
* [More Robust Handling of `helper/schema.TypeSet` Hashes](#more-robust-handling-of-helper-schema-typeset-hashes)
* [Stronger Validation for `helper/schema.Schema.Computed` Fields](#stronger-validation-for-helper-schema-schema-computed-fields)
* [More Robust Validation of `helper/schema.TypeMap` `Elem`s](#more-robust-validation-of-helper-schema-typemap-elems)
Expand Down Expand Up @@ -287,13 +287,13 @@ integer between the arguments. It has been corrected to return an integer
between the arguments. Any usage reliant on the old, buggy behavior should be
updated.

## Clearer Handling of `nil` for `helper/resource.NonRetryableError` and `helper/resource.RetryableError`
## Clearer Handling of `nil` for `helper/retry.RetryableError` and `helper/retry.RetryableError`

Previously, passing a `nil` error to `helper/resource.RetryableError` and
`helper/resource.NonRetryableError` could lead to subtle bugs and even crashes.
Previously, passing a `nil` error to `helper/retry.RetryableError` and
`helper/retry.RetryableError` could lead to subtle bugs and even crashes.
It will now return an error. Providers should check that the error is not `nil`
before calling `helper/resource.NonRetryableError` or
`helper/resource.RetryableError`.
before calling `helper/retry.RetryableError` or
`helper/retry.RetryableError`.

## More Robust Handling of `helper/schema.TypeSet` Hashes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ If a CRUD function timeout is exceeded, the SDK will automatically return a `con
The retry helper takes a timeout and a retry function.

- The **timeout** value specifies the maximum time Terraform will invoke the retry function. You can retrieve the timeout from the `*schema.ResourceData` struct by passing the timeout key (`schema.TimeoutCreate`) to the `Timeout` method.
- The **retry function** returns either a `resource.NonRetryableError` for unexpected errors/states or a `resource.RetryableError` for expected errrors/states. If the function returns a `resource.RetryableError`, it will re-run the function.
- The **retry function** returns either a `retry.NonRetryableError` for unexpected errors/states or a `retry.RetryableError` for expected errrors/states. If the function returns a `retry.RetryableError`, it will re-run the function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This documentation wasn't doing it before, but it might be a good time to call out the import for this package since its different than any code example above.


In the context of a `CREATE` function, once the backend responds with the desired state, invoke the `READ` function. If `READ` errors, return that error wrapped with `resource.NonRetryableError`. Otherwise, return `nil` (no error) from the retry function.
In the context of a `CREATE` function, once the backend responds with the desired state, invoke the `READ` function. If `READ` errors, return that error wrapped with `retry.NonRetryableError`. Otherwise, return `nil` (no error) from the retry function.

```go
func resourceExampleInstanceCreate(d *schema.ResourceData, meta any) error {
Expand All @@ -90,20 +90,20 @@ func resourceExampleInstanceCreate(d *schema.ResourceData, meta any) error {
return fmt.Errorf("Error creating instance: %s", err)
}

return resource.Retry(d.Timeout(schema.TimeoutCreate) - time.Minute, func() *resource.RetryError {
return retry.Retry(d.Timeout(schema.TimeoutCreate) - time.Minute, func() *retry.RetryError {
resp, err := client.DescribeInstance(name)

if err != nil {
return resource.NonRetryableError(fmt.Errorf("Error describing instance: %s", err))
return retry.NonRetryableError(fmt.Errorf("Error describing instance: %s", err))
}

if resp.Status != "CREATED" {
return resource.RetryableError(fmt.Errorf("Expected instance to be created but was in state %s", resp.Status))
return retry.RetryableError(fmt.Errorf("Expected instance to be created but was in state %s", resp.Status))
}

err = resourceExampleInstanceRead(d, meta)
if err != nil {
return resource.NonRetryableError(err)
return retry.NonRetryableError(err)
} else {
return nil
}
Expand All @@ -115,17 +115,17 @@ func resourceExampleInstanceCreate(d *schema.ResourceData, meta any) error {

## StateChangeConf

`resource.Retry` is useful for simple scenarios, particularly when the API response is either success or failure, but sometimes handling an APIs latency or eventual consistency requires more fine tuning. `resource.Retry` is in fact a wrapper for a another helper: `resource.StateChangeConf`.
`retry.Retry` is useful for simple scenarios, particularly when the API response is either success or failure, but sometimes handling an APIs latency or eventual consistency requires more fine tuning. `retry.Retry` is in fact a wrapper for a another helper: `retry.StateChangeConf`.

Use `resource.StateChangeConf` when your resource has multiple states to progress though, you require fine grained control of retry and delay timing, or you want to ensure a minimum number of occurrences of a target state is reached (this is very common when dealing with eventually consistent APIs, where a response can reply back with an old state between calls before becoming consistent).
Use `retry.StateChangeConf` when your resource has multiple states to progress though, you require fine grained control of retry and delay timing, or you want to ensure a minimum number of occurrences of a target state is reached (this is very common when dealing with eventually consistent APIs, where a response can reply back with an old state between calls before becoming consistent).

```go
func resourceExampleInstanceCreate(d *schema.ResourceData, meta any) error {
name := d.Get("name").(string)
client := meta.(*ExampleClient)
resp, err := client.CreateInstance(name)

createStateConf := &resource.StateChangeConf{
createStateConf := &retry.StateChangeConf{
Pending: []string{
client.ExampleInstanceStateRequesting,
client.ExampleInstanceStatePending,
Expand Down