diff --git a/CHANGELOG.md b/CHANGELOG.md index a9965a8bb..11c217004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ nav_order: 1 - Fix `aiven_organization_user_group` resource - `description` field is required - Use golang 1.22 - Output explicitly `termination_protection = true -> false` when service property is removed +- Fix `aiven_flink_application_deployment` deletion ## [4.13.3] - 2024-01-29 diff --git a/internal/plugin/util/wait.go b/internal/plugin/util/wait.go deleted file mode 100644 index a4393d46c..000000000 --- a/internal/plugin/util/wait.go +++ /dev/null @@ -1,20 +0,0 @@ -package util - -import ( - "context" - "time" - - "github.com/avast/retry-go" -) - -// WaitActive waits for resource activity (for example) -// Top timeout comes from the context, no need to parse timeouts from the object. -// But eventually (attempts + connection timeout) * delay makes less timeout than we usually use (20 minutes or more) -func WaitActive(ctx context.Context, retryableFunc retry.RetryableFunc) error { - return retry.Do( - retryableFunc, - retry.Context(ctx), - retry.Attempts(10), - retry.Delay(2*time.Second), - ) -} diff --git a/internal/sdkprovider/service/flink/flink_application_deployment.go b/internal/sdkprovider/service/flink/flink_application_deployment.go index 872a14fdd..71b127489 100644 --- a/internal/sdkprovider/service/flink/flink_application_deployment.go +++ b/internal/sdkprovider/service/flink/flink_application_deployment.go @@ -3,7 +3,7 @@ package flink import ( "context" - "time" + "fmt" "github.com/aiven/aiven-go-client/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -119,6 +119,10 @@ func resourceFlinkApplicationDeploymentCreate( d.SetId(schemautil.BuildResourceID(project, serviceName, applicationID, r.ID)) + if err = waitStatusChange(ctx, client, d, "INITIALIZING"); err != nil { + return diag.Errorf("error waiting for Flink Application Deployment to become initialized: %s", err) + } + return resourceFlinkApplicationDeploymentRead(ctx, d, m) } @@ -140,28 +144,7 @@ func resourceFlinkApplicationDeploymentDelete( return diag.Errorf("error cancelling Flink Application Deployment: %v", err) } - //goland:noinspection GoDeprecation - conf := &retry.StateChangeConf{ - Pending: []string{ - "CANCELLING", - }, - Target: []string{ - "CANCELED", - }, - Refresh: func() (interface{}, string, error) { - r, err := client.FlinkApplicationDeployments.Get(ctx, project, serviceName, applicationID, deploymentID) - if err != nil { - return nil, "", err - } - return r, r.Status, nil - }, - Delay: 1 * time.Second, - Timeout: d.Timeout(schema.TimeoutDelete), - MinTimeout: 1 * time.Second, - } - - _, err = conf.WaitForStateContext(ctx) - if err != nil { + if err = waitStatusChange(ctx, client, d, "CANCELLING"); err != nil { return diag.Errorf("error waiting for Flink Application Deployment to become canceled: %s", err) } @@ -170,6 +153,10 @@ func resourceFlinkApplicationDeploymentDelete( return diag.Errorf("error deleting Flink Application Deployment: %v", err) } + if err = waitStatusChange(ctx, client, d, "DELETING"); err != nil || aiven.IsNotFound(err) { + return diag.Errorf("error waiting for Flink Application Deployment to become deleted: %s", err) + } + return nil } @@ -225,3 +212,25 @@ func resourceFlinkApplicationDeploymentRead(ctx context.Context, d *schema.Resou return nil } + +// waitStatusChange waits for given status to change. +// Flink Application Deployment state machine is huge, better not to implement it here. +func waitStatusChange(ctx context.Context, client *aiven.Client, d *schema.ResourceData, status string) error { + project, serviceName, applicationID, deploymentID, err := schemautil.SplitResourceID4(d.Id()) + if err != nil { + return err + } + + return retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *retry.RetryError { + deployment, err := client.FlinkApplicationDeployments.Get(ctx, project, serviceName, applicationID, deploymentID) + if err != nil { + // The client has retries under the hood for 50x and other cases. + return retry.NonRetryableError(err) + } + + if status == deployment.FlinkApplicationDeployment.Status { + return retry.RetryableError(fmt.Errorf("expected %q status to change", status)) + } + return nil + }) +}