Skip to content

Commit

Permalink
CDPCP-11100 Remove polling from environment and datalake resource cre…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
daszabo committed Jan 25, 2024
1 parent 002445b commit 635a873
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 86 deletions.
36 changes: 0 additions & 36 deletions resources/datalake/resource_aws_datalake.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package datalake
import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -133,11 +132,6 @@ func (r *awsDatalakeResource) Create(ctx context.Context, req resource.CreateReq
return
}

if err := waitForDatalakeToBeRunning(ctx, state.DatalakeName.ValueString(), time.Hour, r.client.Datalake, state.PollingOptions); err != nil {
utils.AddDatalakeDiagnosticsError(err, &resp.Diagnostics, "create AWS Datalake")
return
}

descParams := operations.NewDescribeDatalakeParamsWithContext(ctx)
descParams.WithInput(&datalakemodels.DescribeDatalakeRequest{DatalakeName: state.DatalakeName.ValueStringPointer()})
descResponseOk, err := client.Operations.DescribeDatalake(descParams)
Expand All @@ -156,36 +150,6 @@ func (r *awsDatalakeResource) Create(ctx context.Context, req resource.CreateReq
}
}

func waitForDatalakeToBeRunning(ctx context.Context, datalakeName string, fallbackPollingTimeout time.Duration, client *client.Datalake, options *utils.PollingOptions) error {
timeout, err := utils.CalculateTimeoutOrDefault(ctx, options, fallbackPollingTimeout)
if err != nil {
return err
}
stateConf := &retry.StateChangeConf{
Pending: []string{"REQUESTED", "WAIT_FOR_ENVIRONMENT", "ENVIRONMENT_CREATED", "STACK_CREATION_IN_PROGRESS",
"STACK_CREATION_FINISHED", "EXTERNAL_DATABASE_CREATION_IN_PROGRESS", "EXTERNAL_DATABASE_CREATED",
},
Target: []string{"RUNNING"},
Delay: 5 * time.Second,
Timeout: *timeout,
PollInterval: 10 * time.Second,
Refresh: func() (interface{}, string, error) {
log.Printf("About to describe datalake")
params := operations.NewDescribeDatalakeParamsWithContext(ctx)
params.WithInput(&datalakemodels.DescribeDatalakeRequest{DatalakeName: &datalakeName})
resp, err := client.Operations.DescribeDatalake(params)
if err != nil {
log.Printf("Error describing datalake: %s", err)
return nil, "", err
}
log.Printf("Described datalake: %s", resp.GetPayload().Datalake.Status)
return checkResponseStatusForError(resp)
},
}
_, err = stateConf.WaitForStateContext(ctx)
return err
}

func checkResponseStatusForError(resp *operations.DescribeDatalakeOK) (interface{}, string, error) {
if utils.ContainsAsSubstring([]string{"FAILED", "ERROR"}, resp.GetPayload().Datalake.Status) {
return nil, "", fmt.Errorf("unexpected Data Lake status: %s. Reason: %s", resp.GetPayload().Datalake.Status, resp.GetPayload().Datalake.StatusReason)
Expand Down
5 changes: 0 additions & 5 deletions resources/datalake/resource_azure_datalake.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ func (r *azureDatalakeResource) Create(ctx context.Context, req resource.CreateR
return
}

if err := waitForDatalakeToBeRunning(ctx, state.DatalakeName.ValueString(), time.Hour, r.client.Datalake, state.PollingOptions); err != nil {
utils.AddDatalakeDiagnosticsError(err, &resp.Diagnostics, "create Azure Datalake")
return
}

descParams := operations.NewDescribeDatalakeParamsWithContext(ctx)
descParams.WithInput(&datalakemodels.DescribeDatalakeRequest{DatalakeName: state.DatalakeName.ValueStringPointer()})
descResponseOk, err := client.Operations.DescribeDatalake(descParams)
Expand Down
5 changes: 0 additions & 5 deletions resources/datalake/resource_gcp_datalake.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ func (r *gcpDatalakeResource) Create(ctx context.Context, req resource.CreateReq
return
}

if err := waitForDatalakeToBeRunning(ctx, state.DatalakeName.ValueString(), time.Hour, r.client.Datalake, state.PollingOptions); err != nil {
utils.AddDatalakeDiagnosticsError(err, &resp.Diagnostics, "create GCP Datalake")
return
}

descParams := operations.NewDescribeDatalakeParamsWithContext(ctx)
descParams.WithInput(&datalakemodels.DescribeDatalakeRequest{DatalakeName: state.DatalakeName.ValueStringPointer()})
descResponseOk, err := client.Operations.DescribeDatalake(descParams)
Expand Down
37 changes: 6 additions & 31 deletions resources/environments/environment_action_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp"
Expand All @@ -28,7 +30,7 @@ const (
timeoutOneHour = time.Hour * 1
)

func describeEnvironmentWithDiagnosticHandle(envName string, id string, ctx context.Context, client *cdp.Client, resp *resource.ReadResponse) (*environmentsmodels.Environment, error) {
func describeEnvironmentWithDiagnosticHandle(envName string, id string, ctx context.Context, client *cdp.Client, diags *diag.Diagnostics, state *tfsdk.State) (*environmentsmodels.Environment, error) {
tflog.Info(ctx, "About to describe environment '"+envName+"'.")
params := operations.NewDescribeEnvironmentParamsWithContext(ctx)
params.WithInput(&environmentsmodels.DescribeEnvironmentRequest{
Expand All @@ -38,14 +40,14 @@ func describeEnvironmentWithDiagnosticHandle(envName string, id string, ctx cont
if err != nil {
tflog.Warn(ctx, "Something happened during environment fetch: "+err.Error())
if isEnvNotFoundError(err) {
resp.Diagnostics.AddWarning("Resource not found on provider", "Environment not found, removing from state.")
diags.AddWarning("Resource not found on provider", "Environment not found, removing from state.")
tflog.Warn(ctx, "Environment not found, removing from state", map[string]interface{}{
"id": id,
})
resp.State.RemoveResource(ctx)
state.RemoveResource(ctx)
return nil, err
}
utils.AddEnvironmentDiagnosticsError(err, &resp.Diagnostics, "read Environment")
utils.AddEnvironmentDiagnosticsError(err, diags, "read Environment")
return nil, err
}
return utils.LogEnvironmentSilently(ctx, descEnvResp.GetPayload().Environment, describeLogPrefix), nil
Expand Down Expand Up @@ -76,30 +78,3 @@ func isEnvNotFoundError(err error) bool {
}
return false
}

func waitForCreateEnvironmentWithDiagnosticHandle(ctx context.Context, client *cdp.Client, id string, envName string, resp *resource.CreateResponse, options *utils.PollingOptions) (*operations.DescribeEnvironmentOK, error) {
if err := waitForEnvironmentToBeAvailable(id, timeoutOneHour, client.Environments, ctx, options); err != nil {
utils.AddEnvironmentDiagnosticsError(err, &resp.Diagnostics, "create Environment failed")
return nil, err
}

environmentName := envName
descParams := operations.NewDescribeEnvironmentParamsWithContext(ctx)
descParams.WithInput(&environmentsmodels.DescribeEnvironmentRequest{
EnvironmentName: &environmentName,
})
descEnvResp, err := client.Environments.Operations.DescribeEnvironment(descParams)
if err != nil {
if isEnvNotFoundError(err) {
resp.Diagnostics.AddWarning("Resource not found on provider", "Environment not found, removing from state.")
tflog.Warn(ctx, "Environment not found, removing from state", map[string]interface{}{
"id": id,
})
resp.State.RemoveResource(ctx)
return nil, err
}
utils.AddEnvironmentDiagnosticsError(err, &resp.Diagnostics, "create Environment failed")
return nil, err
}
return descEnvResp, nil
}
7 changes: 4 additions & 3 deletions resources/environments/resource_aws_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package environments

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -76,12 +77,12 @@ func (r *awsEnvironmentResource) Create(ctx context.Context, req resource.Create
return
}

descEnvResp, err := waitForCreateEnvironmentWithDiagnosticHandle(ctx, r.client, data.ID.ValueString(), data.EnvironmentName.ValueString(), resp, data.PollingOptions)
descEnvResp, err := describeEnvironmentWithDiagnosticHandle(data.EnvironmentName.ValueString(), data.ID.ValueString(), ctx, r.client, &resp.Diagnostics, &resp.State)
if err != nil {
return
}

toAwsEnvironmentResource(ctx, utils.LogEnvironmentSilently(ctx, descEnvResp.GetPayload().Environment, describeLogPrefix), &data, data.PollingOptions, &resp.Diagnostics)
toAwsEnvironmentResource(ctx, utils.LogEnvironmentSilently(ctx, descEnvResp, describeLogPrefix), &data, data.PollingOptions, &resp.Diagnostics)
diags = resp.State.Set(ctx, data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -97,7 +98,7 @@ func (r *awsEnvironmentResource) Read(ctx context.Context, req resource.ReadRequ
return
}

env, err := describeEnvironmentWithDiagnosticHandle(state.EnvironmentName.ValueString(), state.ID.ValueString(), ctx, r.client, resp)
env, err := describeEnvironmentWithDiagnosticHandle(state.EnvironmentName.ValueString(), state.ID.ValueString(), ctx, r.client, &resp.Diagnostics, &resp.State)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions resources/environments/resource_azure_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func (r *azureEnvironmentResource) Create(ctx context.Context, req resource.Crea
return
}

descEnvResp, err := waitForCreateEnvironmentWithDiagnosticHandle(ctx, r.client, data.ID.ValueString(), data.EnvironmentName.ValueString(), resp, data.PollingOptions)
descEnvResp, err := describeEnvironmentWithDiagnosticHandle(data.EnvironmentName.ValueString(), data.ID.ValueString(), ctx, r.client, &resp.Diagnostics, &resp.State)
if err != nil {
return
}

toAzureEnvironmentResource(ctx, descEnvResp.GetPayload().Environment, &data, data.PollingOptions, &resp.Diagnostics)
toAzureEnvironmentResource(ctx, descEnvResp, &data, data.PollingOptions, &resp.Diagnostics)
diags = resp.State.Set(ctx, data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -99,7 +99,7 @@ func (r *azureEnvironmentResource) Read(ctx context.Context, req resource.ReadRe
return
}

descEnvResp, err := describeEnvironmentWithDiagnosticHandle(state.EnvironmentName.ValueString(), state.ID.ValueString(), ctx, r.client, resp)
descEnvResp, err := describeEnvironmentWithDiagnosticHandle(state.EnvironmentName.ValueString(), state.ID.ValueString(), ctx, r.client, &resp.Diagnostics, &resp.State)
if err != nil {
return
}
Expand Down
7 changes: 4 additions & 3 deletions resources/environments/resource_gcp_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package environments

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"

Expand Down Expand Up @@ -70,12 +71,12 @@ func (r *gcpEnvironmentResource) Create(ctx context.Context, req resource.Create
return
}

descEnvResp, err := waitForCreateEnvironmentWithDiagnosticHandle(ctx, r.client, data.ID.ValueString(), data.EnvironmentName.ValueString(), resp, data.PollingOptions)
descEnvResp, err := describeEnvironmentWithDiagnosticHandle(data.EnvironmentName.ValueString(), data.ID.ValueString(), ctx, r.client, &resp.Diagnostics, &resp.State)
if err != nil {
return
}

toGcpEnvironmentResource(ctx, utils.LogEnvironmentSilently(ctx, descEnvResp.GetPayload().Environment, describeLogPrefix), &data, data.PollingOptions, &resp.Diagnostics)
toGcpEnvironmentResource(ctx, utils.LogEnvironmentSilently(ctx, descEnvResp, describeLogPrefix), &data, data.PollingOptions, &resp.Diagnostics)
diags = resp.State.Set(ctx, data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -91,7 +92,7 @@ func (r *gcpEnvironmentResource) Read(ctx context.Context, req resource.ReadRequ
return
}

descEnvResp, err := describeEnvironmentWithDiagnosticHandle(state.EnvironmentName.ValueString(), state.ID.ValueString(), ctx, r.client, resp)
descEnvResp, err := describeEnvironmentWithDiagnosticHandle(state.EnvironmentName.ValueString(), state.ID.ValueString(), ctx, r.client, &resp.Diagnostics, &resp.State)
if err != nil {
return
}
Expand Down

0 comments on commit 635a873

Please sign in to comment.