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

DWX-18776 Wait for dw aws cluster to become ready #159

Merged
merged 1 commit into from
Aug 28, 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
16 changes: 13 additions & 3 deletions resources/dw/cluster/aws/model_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package aws

import (
"time"

"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models"
Expand Down Expand Up @@ -45,12 +47,14 @@ type resourceModel struct {
Name types.String `tfsdk:"name"`
ClusterID types.String `tfsdk:"cluster_id"`
LastUpdated types.String `tfsdk:"last_updated"`
Status types.String `tfsdk:"status"`
NodeRoleCDWManagedPolicyArn types.String `tfsdk:"node_role_cdw_managed_policy_arn"`
DatabaseBackupRetentionDays types.Int64 `tfsdk:"database_backup_retention_days"`
CustomRegistryOptions *customRegistryOptions `tfsdk:"custom_registry_options"`
CustomSubdomain types.String `tfsdk:"custom_subdomain"`
NetworkSettings *networkResourceModel `tfsdk:"network_settings"`
InstanceSettings *instanceResourceModel `tfsdk:"instance_settings"`
PollingOptions *utils.PollingOptions `tfsdk:"polling_options"`
}

func (p *resourceModel) convertToCreateAwsClusterRequest() *models.CreateAwsClusterRequest {
Expand Down Expand Up @@ -105,10 +109,16 @@ func (p *resourceModel) getAdditionalInstanceTypes() []string {
func (p *resourceModel) getCustomRegistryOptions() *models.CustomRegistryOptions {
if cro := p.CustomRegistryOptions; cro != nil {
return &models.CustomRegistryOptions{
DisableImageVerification: true, // option will be deprecated, we should disallow image verification
RegistryType: p.CustomRegistryOptions.RegistryType.ValueString(),
RepositoryURL: p.CustomRegistryOptions.RepositoryURL.ValueString(),
RegistryType: p.CustomRegistryOptions.RegistryType.ValueString(),
RepositoryURL: p.CustomRegistryOptions.RepositoryURL.ValueString(),
}
}
return nil
}

func (p *resourceModel) getPollingTimeout() time.Duration {
if p.PollingOptions != nil {
return time.Duration(p.PollingOptions.PollingTimeout.ValueInt64()) * time.Minute
}
return 40 * time.Minute
}
67 changes: 67 additions & 0 deletions resources/dw/cluster/aws/model_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 Cloudera. All Rights Reserved.
//
// This file is licensed under the Apache License Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
//
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. Refer to the License for the specific
// permissions and limitations governing your use of the file.

package aws

import (
"context"
models2 "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/stretchr/testify/suite"
"testing"
"time"
)

type DwClusterModelTestSuite struct {
suite.Suite
rm *resourceModel
}

func TestDwModelClusterTestSuite(t *testing.T) {
suite.Run(t, new(DwClusterModelTestSuite))
}

func (s *DwClusterModelTestSuite) SetupSuite() {
req := resource.CreateRequest{
Plan: tfsdk.Plan{
Raw: createRawClusterResource(),
Schema: testDwClusterSchema,
},
}
rm := &resourceModel{}
req.Plan.Get(context.Background(), &rm)
s.rm = rm
}

func (s *DwClusterModelTestSuite) TestConvertToCreateAwsClusterRequest() {
awsCluster := s.rm.convertToCreateAwsClusterRequest()
s.Equal("crn", *awsCluster.EnvironmentCrn)
s.Equal(true, awsCluster.UseOverlayNetwork)
s.Equal([]string{"cidr-1", "cidr-2", "cidr-3"}, awsCluster.WhitelistK8sClusterAccessIPCIDRs)
s.Equal([]string{"cidr-4", "cidr-5", "cidr-6"}, awsCluster.WhitelistWorkloadAccessIPCIDRs)
s.Equal(true, awsCluster.UsePrivateLoadBalancer)
s.Equal(false, awsCluster.UsePublicWorkerNode)
s.Equal([]string{"subnet-1", "subnet-2", "subnet-3"}, awsCluster.WorkerSubnetIds)
s.Equal([]string{"subnet-4", "subnet-5", "subnet-6"}, awsCluster.LbSubnetIds)
s.Equal("", awsCluster.NodeRoleCDWManagedPolicyArn)
s.Equal(int32(0), *awsCluster.DatabaseBackupRetentionPeriod)
s.Equal("", awsCluster.CustomSubdomain)
s.Equal(models2.CustomRegistryOptions{RegistryType: "", RepositoryURL: ""}, *awsCluster.CustomRegistryOptions)
s.Equal(false, *awsCluster.EnableSpotInstances)
s.Equal("", awsCluster.CustomAmiID)
s.Equal([]string{}, awsCluster.ComputeInstanceTypes)
s.Equal([]string{}, awsCluster.AdditionalInstanceTypes)
}

func (s *DwClusterModelTestSuite) TestGetPollingTimeout() {
timeout := s.rm.getPollingTimeout()
s.Equal(90*time.Minute, timeout)
}
92 changes: 83 additions & 9 deletions resources/dw/cluster/aws/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ package aws

import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"

"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp"
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations"
Expand Down Expand Up @@ -62,23 +65,25 @@ func (r *dwClusterResource) Create(ctx context.Context, req resource.CreateReque
clusterParams := operations.NewCreateAwsClusterParamsWithContext(ctx).
WithInput(plan.convertToCreateAwsClusterRequest())

// Create new aws cluster
// Create new AWS cluster
response, err := r.client.Dw.Operations.CreateAwsCluster(clusterParams)
if err != nil {
resp.Diagnostics.AddError(
"Error creating data warehouse aws cluster",
"Error creating Data Warehouse AWS cluster",
"Could not create cluster, unexpected error: "+err.Error(),
)
return
}

payload := response.GetPayload()
clusterID := &payload.ClusterID
plan.ClusterID = types.StringValue(*clusterID)

desc := operations.NewDescribeClusterParamsWithContext(ctx).
WithInput(&models.DescribeClusterRequest{ClusterID: &payload.ClusterID})
WithInput(&models.DescribeClusterRequest{ClusterID: clusterID})
describe, err := r.client.Dw.Operations.DescribeCluster(desc)
if err != nil {
resp.Diagnostics.AddError(
"Error creating data warehouse aws cluster",
"Error creating Data Warehouse AWS cluster",
"Could not describe cluster, unexpected error: "+err.Error(),
)
return
Expand All @@ -90,7 +95,7 @@ func (r *dwClusterResource) Create(ctx context.Context, req resource.CreateReque
plan.ID = types.StringValue(cluster.Cluster.EnvironmentCrn)
plan.Crn = types.StringValue(cluster.Cluster.EnvironmentCrn)
plan.Name = types.StringValue(cluster.Cluster.Name)
plan.ClusterID = types.StringValue(cluster.Cluster.ID)
plan.Status = types.StringValue(cluster.Cluster.Status)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

// Set state to fully populated data
Expand All @@ -100,6 +105,29 @@ func (r *dwClusterResource) Create(ctx context.Context, req resource.CreateReque
return
}

if plan.PollingOptions.Async.ValueBool() {
callFailedCount := 0
stateConf := &retry.StateChangeConf{
Pending: []string{"Accepted", "Creating", "Created", "Starting"},
Target: []string{"Running"},
Delay: 30 * time.Second,
Timeout: plan.getPollingTimeout(),
PollInterval: 30 * time.Second,
Refresh: r.stateRefresh(ctx, clusterID, &callFailedCount, int(plan.PollingOptions.CallFailureThreshold.ValueInt64())),
}
if _, err = stateConf.WaitForStateContext(ctx); err != nil {
resp.Diagnostics.AddError(
"Error waiting for Data Warehouse AWS cluster",
"Could not create cluster, unexpected error: "+err.Error(),
)
return
}
plan.Status = types.StringValue(cluster.Cluster.Status)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
}

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
}

func (r *dwClusterResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand All @@ -120,17 +148,63 @@ func (r *dwClusterResource) Delete(ctx context.Context, req resource.DeleteReque
return
}

clusterID := state.ClusterID.ValueStringPointer()
op := operations.NewDeleteClusterParamsWithContext(ctx).
WithInput(&models.DeleteClusterRequest{
ClusterID: state.ClusterID.ValueStringPointer(),
// Force: true,
ClusterID: clusterID,
})

if _, err := r.client.Dw.Operations.DeleteCluster(op); err != nil {
resp.Diagnostics.AddError(
"Error deleting data warehouse aws cluster",
"Error deleting Data Warehouse AWS cluster",
"Could not delete cluster, unexpected error: "+err.Error(),
)
return
}

if state.PollingOptions.Async.ValueBool() {
callFailedCount := 0
stateConf := &retry.StateChangeConf{
Pending: []string{"Deleting", "Running"},
Target: []string{"Deleted"}, // This is not an actual state, we added it to fake the state change
Delay: 30 * time.Second,
Timeout: state.getPollingTimeout(),
PollInterval: 30 * time.Second,
Refresh: r.stateRefresh(ctx, clusterID, &callFailedCount, int(state.PollingOptions.CallFailureThreshold.ValueInt64())),
}
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
resp.Diagnostics.AddError(
"Esrror waiting for Data Warehouse AWS cluster",
"Could not delete cluster, unexpected error: "+err.Error(),
)
return
}
}
}

func (r *dwClusterResource) stateRefresh(ctx context.Context, clusterID *string, callFailedCount *int, callFailureThreshold int) func() (any, string, error) {
return func() (any, string, error) {
tflog.Debug(ctx, "About to describe cluster")
params := operations.NewDescribeClusterParamsWithContext(ctx).
WithInput(&models.DescribeClusterRequest{ClusterID: clusterID})
resp, err := r.client.Dw.Operations.DescribeCluster(params)
if err != nil {
if strings.Contains(err.Error(), "NOT_FOUND") {
return &models.DescribeClusterResponse{}, "Deleted", nil
}
*callFailedCount++
if *callFailedCount <= callFailureThreshold {
tflog.Warn(ctx, fmt.Sprintf("could not describe Data Warehouse AWS cluster "+
"due to [%s] but threshold limit is not reached yet (%d out of %d).", err.Error(), callFailedCount, callFailureThreshold))
return nil, "", nil
}
tflog.Error(ctx, fmt.Sprintf("error describing Data Warehouse AWS cluster due to [%s] "+
"failure threshold limit exceeded.", err.Error()))
return nil, "", err
}
*callFailedCount = 0
cluster := resp.GetPayload()
tflog.Debug(ctx, fmt.Sprintf("Described cluster %s with status %s", *clusterID, cluster.Cluster.Status))
return cluster, cluster.Cluster.Status, nil
}
}
Loading
Loading