Skip to content
Draft
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
14 changes: 13 additions & 1 deletion docs/resources/dw_aws_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ output "name" {
### Read-Only

- `cluster_id` (String) The id of the cluster.
- `default_database_catalog` (Attributes) (see [below for nested schema](#nestedatt--default_database_catalog))
- `id` (String) The ID of this resource.
- `last_updated` (String) Timestamp of the last Terraform update of the order.
- `name` (String) The name of the cluster matches the environment name.
- `status` (String) The status of the cluster.
- `version` (String) The version of the cluster.

<a id="nestedatt--network_settings"></a>
### Nested Schema for `network_settings`
Expand Down Expand Up @@ -119,7 +121,6 @@ Required:

Optional:

- `additional_instance_types` (List of String) The additional instance types that the environment is allowed to use, listed in their priority order. They will be used instead of the primary compute instance type in case it is unavailable. You cannot include any instance type that was already indicated in computeInstanceTypes.
- `compute_instance_types` (List of String) The compute instance types that the environment is restricted to use. This affects the creation of virtual warehouses where this restriction will apply. Select an instance type that meets your computing, memory, networking, or storage needs. As of now, only a single instance type can be listed.
- `custom_ami_id` (String) The custom AMI ID to use for worker nodes.
- `enable_spot_instances` (Boolean) Whether to use spot instances for worker nodes.
Expand All @@ -133,3 +134,14 @@ Optional:
- `async` (Boolean) Boolean value that specifies if Terraform should wait for resource creation/deletion.
- `call_failure_threshold` (Number) Threshold value that specifies how many times should a single call failure happen before giving up the polling.
- `polling_timeout` (Number) Timeout value in minutes that specifies for how long should the polling go for resource creation/deletion.


<a id="nestedatt--default_database_catalog"></a>
### Nested Schema for `default_database_catalog`

Read-Only:

- `id` (String) The ID of the database catalog.
- `last_updated` (String) Timestamp of the last Terraform update of the order.
- `name` (String) The name of the database catalog.
- `status` (String) The status of the database catalog.
115 changes: 82 additions & 33 deletions resources/dw/cluster/aws/model_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
package aws

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"

"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models"
"github.com/cloudera/terraform-provider-cdp/utils"
Expand All @@ -35,10 +39,9 @@ type customRegistryOptions struct {
}

type instanceResourceModel struct {
CustomAmiID types.String `tfsdk:"custom_ami_id"`
EnableSpotInstances types.Bool `tfsdk:"enable_spot_instances"`
ComputeInstanceTypes types.List `tfsdk:"compute_instance_types"`
AdditionalInstanceTypes types.List `tfsdk:"additional_instance_types"`
CustomAmiID types.String `tfsdk:"custom_ami_id"`
EnableSpotInstances types.Bool `tfsdk:"enable_spot_instances"`
ComputeInstanceTypes types.List `tfsdk:"compute_instance_types"`
}

type resourceModel struct {
Expand All @@ -48,16 +51,31 @@ type resourceModel struct {
ClusterID types.String `tfsdk:"cluster_id"`
LastUpdated types.String `tfsdk:"last_updated"`
Status types.String `tfsdk:"status"`
Version types.String `tfsdk:"version"`
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"`
InstanceSettings types.Object `tfsdk:"instance_settings"`
DefaultDatabaseCatalog types.Object `tfsdk:"default_database_catalog"`
PollingOptions *utils.PollingOptions `tfsdk:"polling_options"`
}

func (p *resourceModel) convertToCreateAwsClusterRequest() *models.CreateAwsClusterRequest {
func (p *resourceModel) convertToCreateAwsClusterRequest(ctx context.Context) (*models.CreateAwsClusterRequest, diag.Diagnostics) {
enableSpotInstances, diags := p.getEnableSpotInstances(ctx)
if diags.HasError() {
return nil, diags
}
customAmiID, diags := p.getCustomAmiID(ctx)
if diags.HasError() {
return nil, diags
}
computeInstanceTypes, diags := p.getComputeInstanceTypes(ctx)
if diags.HasError() {
return nil, diags
}

return &models.CreateAwsClusterRequest{
EnvironmentCrn: p.Crn.ValueStringPointer(),
UseOverlayNetwork: p.NetworkSettings.UseOverlayNetwork.ValueBool(),
Expand All @@ -71,31 +89,28 @@ func (p *resourceModel) convertToCreateAwsClusterRequest() *models.CreateAwsClus
DatabaseBackupRetentionPeriod: utils.Int64To32Pointer(p.DatabaseBackupRetentionDays),
CustomSubdomain: p.CustomSubdomain.ValueString(),
CustomRegistryOptions: p.getCustomRegistryOptions(),
EnableSpotInstances: p.getEnableSpotInstances(),
CustomAmiID: p.getCustomAmiID(),
ComputeInstanceTypes: p.getComputeInstanceTypes(),
}
EnableSpotInstances: enableSpotInstances,
CustomAmiID: customAmiID,
ComputeInstanceTypes: computeInstanceTypes,
}, diags
}

func (p *resourceModel) getEnableSpotInstances() *bool {
if i := p.InstanceSettings; i != nil {
return i.EnableSpotInstances.ValueBoolPointer()
}
return nil
func (p *resourceModel) getEnableSpotInstances(ctx context.Context) (*bool, diag.Diagnostics) {
var irm instanceResourceModel
diags := p.InstanceSettings.As(ctx, &irm, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})
return irm.EnableSpotInstances.ValueBoolPointer(), diags
}

func (p *resourceModel) getCustomAmiID() string {
if i := p.InstanceSettings; i != nil {
return p.InstanceSettings.CustomAmiID.ValueString()
}
return ""
func (p *resourceModel) getCustomAmiID(ctx context.Context) (string, diag.Diagnostics) {
var irm instanceResourceModel
diags := p.InstanceSettings.As(ctx, &irm, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})
return irm.CustomAmiID.ValueString(), diags
}

func (p *resourceModel) getComputeInstanceTypes() []string {
if i := p.InstanceSettings; i != nil {
return utils.FromListValueToStringList(p.InstanceSettings.ComputeInstanceTypes)
}
return nil
func (p *resourceModel) getComputeInstanceTypes(ctx context.Context) ([]string, diag.Diagnostics) {
var irm instanceResourceModel
diags := p.InstanceSettings.As(ctx, &irm, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})
return utils.FromListValueToStringList(irm.ComputeInstanceTypes), diags
}

func (p *resourceModel) getCustomRegistryOptions() *models.CustomRegistryOptions {
Expand All @@ -108,16 +123,50 @@ func (p *resourceModel) getCustomRegistryOptions() *models.CustomRegistryOptions
return nil
}

func (p *resourceModel) getPollingTimeout() time.Duration {
if p.PollingOptions != nil {
return time.Duration(p.PollingOptions.PollingTimeout.ValueInt64()) * time.Minute
func (p *resourceModel) GetPollingOptions() *utils.PollingOptions {
return p.PollingOptions
}

func (p *resourceModel) setResourceModel(ctx context.Context, resp *models.DescribeClusterResponse) diag.Diagnostics {
p.ID = types.StringValue(resp.Cluster.EnvironmentCrn)
p.Crn = types.StringValue(resp.Cluster.EnvironmentCrn)
p.Name = types.StringValue(resp.Cluster.Name)
p.Status = types.StringValue(resp.Cluster.Status)
p.Version = types.StringValue(resp.Cluster.Version)
p.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
var irm instanceResourceModel
diags := p.InstanceSettings.As(ctx, &irm, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})
if diags.HasError() {
return diags
}
return 40 * time.Minute
attributeTypes := map[string]attr.Type{
"custom_ami_id": types.StringType,
"enable_spot_instances": types.BoolType,
"compute_instance_types": types.ListType{ElemType: types.StringType},
}
attributes := map[string]attr.Value{
"custom_ami_id": irm.CustomAmiID,
"enable_spot_instances": basetypes.NewBoolValue(resp.Cluster.EnableSpotInstances),
"compute_instance_types": utils.FromStringListToListValue(resp.Cluster.ComputeInstanceTypes),
}
p.InstanceSettings, diags = basetypes.NewObjectValue(attributeTypes, attributes)
return diags
}

func (p *resourceModel) getCallFailureThreshold() int {
if p.PollingOptions != nil {
return int(p.PollingOptions.CallFailureThreshold.ValueInt64())
func (p *resourceModel) setDefaultDatabaseCatalog(catalog *models.DbcSummary) diag.Diagnostics {
attributeTypes := map[string]attr.Type{
"id": types.StringType,
"name": types.StringType,
"last_updated": types.StringType,
"status": types.StringType,
}
attributes := map[string]attr.Value{
"id": basetypes.NewStringValue(catalog.ID),
"name": basetypes.NewStringValue(catalog.Name),
"last_updated": basetypes.NewStringValue(time.Now().Format(time.RFC850)),
"status": basetypes.NewStringValue(catalog.Status),
}
return 3
dbc, diags := basetypes.NewObjectValue(attributeTypes, attributes)
p.DefaultDatabaseCatalog = dbc
return diags
}
13 changes: 1 addition & 12 deletions resources/dw/cluster/aws/model_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package aws
import (
"context"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
Expand Down Expand Up @@ -44,7 +43,7 @@ func (s *DwClusterModelTestSuite) SetupSuite() {
}

func (s *DwClusterModelTestSuite) TestConvertToCreateAwsClusterRequest() {
awsCluster := s.rm.convertToCreateAwsClusterRequest()
awsCluster, _ := s.rm.convertToCreateAwsClusterRequest(context.TODO())
s.Equal("crn", *awsCluster.EnvironmentCrn)
s.Equal(true, awsCluster.UseOverlayNetwork)
s.Equal([]string{"cidr-1", "cidr-2", "cidr-3"}, awsCluster.WhitelistK8sClusterAccessIPCIDRs)
Expand All @@ -61,13 +60,3 @@ func (s *DwClusterModelTestSuite) TestConvertToCreateAwsClusterRequest() {
s.Equal("", awsCluster.CustomAmiID)
s.Equal([]string{}, awsCluster.ComputeInstanceTypes)
}

func (s *DwClusterModelTestSuite) TestGetPollingTimeout() {
timeout := s.rm.getPollingTimeout()
s.Equal(90*time.Minute, timeout)
}

func (s *DwClusterModelTestSuite) TestGetCallFailureThreshold() {
out := s.rm.getCallFailureThreshold()
s.Equal(3, out)
}
Loading