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

refactor: use v2/helpers/retry instead of helper/resource #1592

Merged
merged 1 commit into from
Feb 13, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ nav_order: 1

<!-- TODO: uncomment when dragonfly is supported -->
<!-- - Dragonfly support -->
- Refactor deprecated `resource.StateChangeConf`/`resource.StateRefreshFunc` usage to their equivalent with `retry`
- Fix `aiven_kafka_schema` JSON object diff suppress function
- Add new user config generator
- Use `TypeSet` for `ip_filter`, `ip_filter_string` fields
Expand Down
12 changes: 5 additions & 7 deletions internal/schemautil/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"

"github.com/aiven/terraform-provider-aiven/internal/common"
)
Expand All @@ -19,9 +19,8 @@ type DatabaseDeleteWaiter struct {
Database string
}

// RefreshFunc will call the Aiven client and refresh it's state.
// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func (w *DatabaseDeleteWaiter) RefreshFunc() resource.StateRefreshFunc {
// RefreshFunc will call the Aiven client and refresh its state.
func (w *DatabaseDeleteWaiter) RefreshFunc() retry.StateRefreshFunc {
return func() (interface{}, string, error) {
err := w.Client.Databases.Delete(w.Context, w.ProjectName, w.ServiceName, w.Database)
if common.IsCritical(err) {
Expand All @@ -33,9 +32,8 @@ func (w *DatabaseDeleteWaiter) RefreshFunc() resource.StateRefreshFunc {
}

// Conf sets up the configuration to refresh.
// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func (w *DatabaseDeleteWaiter) Conf(timeout time.Duration) *resource.StateChangeConf {
return &resource.StateChangeConf{
func (w *DatabaseDeleteWaiter) Conf(timeout time.Duration) *retry.StateChangeConf {
return &retry.StateChangeConf{
Pending: []string{"REMOVING"},
Target: []string{"DELETED"},
Refresh: w.RefreshFunc(),
Expand Down
14 changes: 5 additions & 9 deletions internal/schemautil/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/common"
)
Expand All @@ -22,7 +22,6 @@ const (
aivenServicesStartingState = "WAITING_FOR_SERVICES"
)

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func WaitForServiceCreation(ctx context.Context, d *schema.ResourceData, m interface{}) (*aiven.Service, error) {
client := m.(*aiven.Client)

Expand All @@ -31,7 +30,7 @@ func WaitForServiceCreation(ctx context.Context, d *schema.ResourceData, m inter
timeout := d.Timeout(schema.TimeoutCreate)
log.Printf("[DEBUG] Service creation waiter timeout %.0f minutes", timeout.Minutes())

conf := &resource.StateChangeConf{
conf := &retry.StateChangeConf{
Pending: []string{aivenPendingState, aivenRebalancingState, aivenServicesStartingState},
Target: []string{aivenTargetState},
Delay: 10 * time.Second,
Expand Down Expand Up @@ -79,7 +78,6 @@ func WaitForServiceCreation(ctx context.Context, d *schema.ResourceData, m inter
return aux.(*aiven.Service), nil
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func WaitForServiceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) (*aiven.Service, error) {
client := m.(*aiven.Client)

Expand All @@ -88,7 +86,7 @@ func WaitForServiceUpdate(ctx context.Context, d *schema.ResourceData, m interfa
timeout := d.Timeout(schema.TimeoutCreate)
log.Printf("[DEBUG] Service update waiter timeout %.0f minutes", timeout.Minutes())

conf := &resource.StateChangeConf{
conf := &retry.StateChangeConf{
Pending: []string{"updating"},
Target: []string{"updated"},
Delay: 10 * time.Second,
Expand Down Expand Up @@ -131,12 +129,11 @@ func WaitForServiceUpdate(ctx context.Context, d *schema.ResourceData, m interfa
return aux.(*aiven.Service), nil
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func WaitStaticIpsDissassociation(ctx context.Context, d *schema.ResourceData, m interface{}) error {
timeout := d.Timeout(schema.TimeoutDelete)
log.Printf("[DEBUG] Static Ip dissassociation timeout %.0f minutes", timeout.Minutes())

conf := &resource.StateChangeConf{
conf := &retry.StateChangeConf{
Pending: []string{"doing"},
Target: []string{"done"},
Delay: 10 * time.Second,
Expand All @@ -161,7 +158,6 @@ func WaitStaticIpsDissassociation(ctx context.Context, d *schema.ResourceData, m
return nil
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func WaitForDeletion(ctx context.Context, d *schema.ResourceData, m interface{}) error {
client := m.(*aiven.Client)

Expand All @@ -170,7 +166,7 @@ func WaitForDeletion(ctx context.Context, d *schema.ResourceData, m interface{})
timeout := d.Timeout(schema.TimeoutDelete)
log.Printf("[DEBUG] Service deletion waiter timeout %.0f minutes", timeout.Minutes())

conf := &resource.StateChangeConf{
conf := &retry.StateChangeConf{
Pending: []string{"deleting"},
Target: []string{"deleted"},
Delay: 10 * time.Second,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
)
Expand Down Expand Up @@ -123,8 +123,6 @@ func resourceFlinkApplicationDeploymentCreate(
}

// resourceFlinkApplicationDeploymentDelete deletes an existing Flink Application Deployment resource.
//
//nolint:staticcheck // Ignore resource.StateChangeConf deprecation warning.
func resourceFlinkApplicationDeploymentDelete(
ctx context.Context,
d *schema.ResourceData,
Expand All @@ -143,7 +141,7 @@ func resourceFlinkApplicationDeploymentDelete(
}

//goland:noinspection GoDeprecation
conf := &resource.StateChangeConf{
conf := &retry.StateChangeConf{
Pending: []string{
"CANCELLING",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func resourceInfluxDBDatabaseDelete(ctx context.Context, d *schema.ResourceData,

timeout := d.Timeout(schema.TimeoutDelete)

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated WaitForStateContext.
_, err = waiter.Conf(timeout).WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for Aiven Database to be DELETED: %s", err)
Expand Down
4 changes: 1 addition & 3 deletions internal/sdkprovider/service/kafka/kafka_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
Expand Down Expand Up @@ -149,14 +148,13 @@ func flattenKafkaConnectorTasks(r *aiven.KafkaConnector) []map[string]interface{
return tasks
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func resourceKafkaConnectorRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
project, serviceName, connectorName, err := schemautil.SplitResourceID3(d.Id())
if err != nil {
return diag.FromErr(err)
}

stateChangeConf := &resource.StateChangeConf{
stateChangeConf := &retry.StateChangeConf{
Pending: []string{"IN_PROGRESS"},
Target: []string{"OK"},
Refresh: func() (interface{}, string, error) {
Expand Down
1 change: 0 additions & 1 deletion internal/sdkprovider/service/mysql/mysql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func resourceMySQLDatabaseDelete(ctx context.Context, d *schema.ResourceData, m

timeout := d.Timeout(schema.TimeoutDelete)

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated WaitForStateContext.
_, err = waiter.Conf(timeout).WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for Aiven Database to be DELETED: %s", err)
Expand Down
11 changes: 4 additions & 7 deletions internal/sdkprovider/service/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig/stateupgrader"
Expand Down Expand Up @@ -160,7 +160,6 @@ func resourceServicePGUpdate(ctx context.Context, d *schema.ResourceData, m inte
TaskID: t.Task.Id,
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated WaitForStateContext.
taskI, err := w.Conf(d.Timeout(schema.TimeoutDefault)).WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for Aiven service task to be DONE: %s", err)
Expand Down Expand Up @@ -191,8 +190,7 @@ type ServiceTaskWaiter struct {
}

// RefreshFunc will call the Aiven client and refresh its state.
// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func (w *ServiceTaskWaiter) RefreshFunc() resource.StateRefreshFunc {
func (w *ServiceTaskWaiter) RefreshFunc() retry.StateRefreshFunc {
return func() (interface{}, string, error) {
t, err := w.Client.ServiceTask.Get(
w.Context,
Expand All @@ -213,9 +211,8 @@ func (w *ServiceTaskWaiter) RefreshFunc() resource.StateRefreshFunc {
}

// Conf sets up the configuration to refresh.
// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func (w *ServiceTaskWaiter) Conf(timeout time.Duration) *resource.StateChangeConf {
return &resource.StateChangeConf{
func (w *ServiceTaskWaiter) Conf(timeout time.Duration) *retry.StateChangeConf {
return &retry.StateChangeConf{
Pending: []string{"IN_PROGRESS"},
Target: []string{"DONE"},
Refresh: w.RefreshFunc(),
Expand Down
1 change: 0 additions & 1 deletion internal/sdkprovider/service/pg/pg_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ func resourcePGDatabaseDelete(ctx context.Context, d *schema.ResourceData, m int

timeout := d.Timeout(schema.TimeoutDelete)

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated WaitForStateContext.
_, err = waiter.Conf(timeout).WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for Aiven Database to be DELETED: %s", err)
Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/schemautil/userconfig"
Expand Down Expand Up @@ -263,12 +263,11 @@ func resourceProjectAssignToBillingGroup(
return nil
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)

//goland:noinspection GoDeprecation
conf := &resource.StateChangeConf{
conf := &retry.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"target"},
Timeout: time.Minute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
Expand Down Expand Up @@ -292,7 +292,6 @@ func resourceServiceIntegrationCheckForPreexistingResource(ctx context.Context,
return nil, nil
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func resourceServiceIntegrationWaitUntilActive(ctx context.Context, d *schema.ResourceData, m interface{}) error {
const (
active = "ACTIVE"
Expand All @@ -305,7 +304,7 @@ func resourceServiceIntegrationWaitUntilActive(ctx context.Context, d *schema.Re
return err
}

stateChangeConf := &resource.StateChangeConf{
stateChangeConf := &retry.StateChangeConf{
Pending: []string{notActive},
Target: []string{active},
Refresh: func() (interface{}, string, error) {
Expand Down
5 changes: 2 additions & 3 deletions internal/sdkprovider/service/staticip/static_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
Expand Down Expand Up @@ -145,7 +145,6 @@ func resourceStaticIPDelete(ctx context.Context, d *schema.ResourceData, m inter
return nil
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func resourceStaticIPWait(ctx context.Context, d *schema.ResourceData, m interface{}) error {
client := m.(*aiven.Client)

Expand All @@ -154,7 +153,7 @@ func resourceStaticIPWait(ctx context.Context, d *schema.ResourceData, m interfa
return err
}

conf := resource.StateChangeConf{
conf := retry.StateChangeConf{
Target: []string{schemautil.StaticIPCreated},
Pending: []string{"waiting", schemautil.StaticIPCreating},
Timeout: d.Timeout(schema.TimeoutCreate),
Expand Down
14 changes: 5 additions & 9 deletions internal/sdkprovider/service/vpc/aws_privatelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
Expand Down Expand Up @@ -82,7 +82,6 @@ func resourceAWSPrivatelinkCreate(ctx context.Context, d *schema.ResourceData, m
ServiceName: serviceName,
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated WaitForStateContext.
_, err = w.Conf(d.Timeout(schema.TimeoutCreate)).WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("Error waiting for AWS privatelink creation: %s", err)
Expand Down Expand Up @@ -157,7 +156,6 @@ func resourceAWSPrivatelinkUpdate(ctx context.Context, d *schema.ResourceData, m
ServiceName: serviceName,
}

// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated WaitForStateContext.
_, err = w.Conf(d.Timeout(schema.TimeoutCreate)).WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("Error waiting for AWS privatelink to be updated: %s", err)
Expand Down Expand Up @@ -190,9 +188,8 @@ type AWSPrivatelinkWaiter struct {
ServiceName string
}

// RefreshFunc will call the Aiven client and refresh it's state.
// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func (w *AWSPrivatelinkWaiter) RefreshFunc() resource.StateRefreshFunc {
// RefreshFunc will call the Aiven client and refresh its state.
func (w *AWSPrivatelinkWaiter) RefreshFunc() retry.StateRefreshFunc {
return func() (interface{}, string, error) {
pc, err := w.Client.AWSPrivatelink.Get(w.Context, w.Project, w.ServiceName)
if err != nil {
Expand All @@ -206,11 +203,10 @@ func (w *AWSPrivatelinkWaiter) RefreshFunc() resource.StateRefreshFunc {
}

// Conf sets up the configuration to refresh.
// nolint:staticcheck // TODO: Migrate to helper/retry package to avoid deprecated resource.StateRefreshFunc.
func (w *AWSPrivatelinkWaiter) Conf(timeout time.Duration) *resource.StateChangeConf {
func (w *AWSPrivatelinkWaiter) Conf(timeout time.Duration) *retry.StateChangeConf {
log.Printf("[DEBUG] Create waiter timeout %.0f minutes", timeout.Minutes())

return &resource.StateChangeConf{
return &retry.StateChangeConf{
Pending: []string{"creating"},
Target: []string{"active"},
Refresh: w.RefreshFunc(),
Expand Down
Loading