From 6526f9193d0fa9485142da1a5bf9d5a934b03c24 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Wed, 27 Nov 2024 15:50:27 +0000 Subject: [PATCH 1/5] use old API when the old schema structure is used --- .../model_advanced_cluster_test.go | 183 ++++++++++++++++++ .../model_sdk_version_conversion.go | 155 +++++++++++++++ .../resource_advanced_cluster.go | 34 +++- .../advancedclustertpf/resource_schema.go | 18 +- 4 files changed, 372 insertions(+), 18 deletions(-) diff --git a/internal/service/advancedcluster/model_advanced_cluster_test.go b/internal/service/advancedcluster/model_advanced_cluster_test.go index 7dd517c469..7ff1427595 100644 --- a/internal/service/advancedcluster/model_advanced_cluster_test.go +++ b/internal/service/advancedcluster/model_advanced_cluster_test.go @@ -8,6 +8,7 @@ import ( "testing" admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin" + admin20240805 "go.mongodb.org/atlas-sdk/v20240805005/admin" "go.mongodb.org/atlas-sdk/v20241023002/admin" "go.mongodb.org/atlas-sdk/v20241023002/mockadmin" @@ -463,3 +464,185 @@ func TestCheckRegionConfigsPriorityOrder(t *testing.T) { }) } } + +func TestConvertClusterDescription20241023to20240805(t *testing.T) { + var ( + clusterName = "clusterName" + clusterType = "REPLICASET" + earProvider = "AWS" + booleanValue = true + mongoDBMajorVersion = "7.0" + rootCertType = "rootCertType" + replicaSetScalingStrategy = "WORKLOAD_TYPE" + configServerManagementMode = "ATLAS_MANAGED" + readPreference = "primary" + zoneName = "z1" + id = "id1" + regionConfigProvider = "AWS" + region = "EU_WEST_1" + priority = 7 + instanceSize = "M10" + nodeCount = 3 + diskSizeGB = 30.3 + ebsVolumeType = "STANDARD" + diskIOPS = 100 + ) + testCases := []struct { + input *admin.ClusterDescription20240805 + expectedOutput *admin20240805.ClusterDescription20240805 + name string + }{ + { + name: "Converts cluster description from 20241023 to 20240805", + input: &admin.ClusterDescription20240805{ + Name: conversion.StringPtr(clusterName), + ClusterType: conversion.StringPtr(clusterType), + ReplicationSpecs: &[]admin.ReplicationSpec20240805{ + { + Id: conversion.StringPtr(id), + ZoneName: conversion.StringPtr(zoneName), + RegionConfigs: &[]admin.CloudRegionConfig20240805{ + { + ProviderName: conversion.StringPtr(regionConfigProvider), + RegionName: conversion.StringPtr(region), + BackingProviderName: conversion.StringPtr(regionConfigProvider), + Priority: conversion.IntPtr(priority), + AnalyticsSpecs: &admin.DedicatedHardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + ElectableSpecs: &admin.HardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + AutoScaling: &admin.AdvancedAutoScalingSettings{ + Compute: &admin.AdvancedComputeAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + MaxInstanceSize: conversion.Pointer(instanceSize), + MinInstanceSize: conversion.Pointer(instanceSize), + ScaleDownEnabled: conversion.Pointer(booleanValue), + }, + DiskGB: &admin.DiskGBAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + }, + }, + }, + }, + }, + }, + BackupEnabled: conversion.Pointer(booleanValue), + BiConnector: &admin.BiConnector{ + Enabled: conversion.Pointer(booleanValue), + ReadPreference: conversion.StringPtr(readPreference), + }, + EncryptionAtRestProvider: conversion.StringPtr(earProvider), + Labels: &[]admin.ComponentLabel{ + {Key: conversion.StringPtr("key1"), Value: conversion.StringPtr("value1")}, + {Key: conversion.StringPtr("key2"), Value: conversion.StringPtr("value2")}, + }, + Tags: &[]admin.ResourceTag{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + MongoDBMajorVersion: conversion.StringPtr(mongoDBMajorVersion), + PitEnabled: conversion.Pointer(booleanValue), + RootCertType: conversion.StringPtr(rootCertType), + TerminationProtectionEnabled: conversion.Pointer(booleanValue), + VersionReleaseSystem: conversion.StringPtr(""), + GlobalClusterSelfManagedSharding: conversion.Pointer(booleanValue), + ReplicaSetScalingStrategy: conversion.StringPtr(replicaSetScalingStrategy), + RedactClientLogData: conversion.Pointer(booleanValue), + ConfigServerManagementMode: conversion.StringPtr(configServerManagementMode), + }, + expectedOutput: &admin20240805.ClusterDescription20240805{ + Name: conversion.StringPtr(clusterName), + ClusterType: conversion.StringPtr(clusterType), + ReplicationSpecs: &[]admin20240805.ReplicationSpec20240805{ + { + Id: conversion.StringPtr(id), + ZoneName: conversion.StringPtr(zoneName), + RegionConfigs: &[]admin20240805.CloudRegionConfig20240805{ + { + ProviderName: conversion.StringPtr(regionConfigProvider), + RegionName: conversion.StringPtr(region), + BackingProviderName: conversion.StringPtr(regionConfigProvider), + Priority: conversion.IntPtr(priority), + AnalyticsSpecs: &admin20240805.DedicatedHardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + ElectableSpecs: &admin20240805.HardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + AutoScaling: &admin20240805.AdvancedAutoScalingSettings{ + Compute: &admin20240805.AdvancedComputeAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + MaxInstanceSize: conversion.Pointer(instanceSize), + MinInstanceSize: conversion.Pointer(instanceSize), + ScaleDownEnabled: conversion.Pointer(booleanValue), + }, + DiskGB: &admin20240805.DiskGBAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + }, + }, + }, + }, + }, + }, + BackupEnabled: conversion.Pointer(booleanValue), + BiConnector: &admin20240805.BiConnector{ + Enabled: conversion.Pointer(booleanValue), + ReadPreference: conversion.StringPtr(readPreference), + }, + EncryptionAtRestProvider: conversion.StringPtr(earProvider), + Labels: &[]admin20240805.ComponentLabel{ + {Key: conversion.StringPtr("key1"), Value: conversion.StringPtr("value1")}, + {Key: conversion.StringPtr("key2"), Value: conversion.StringPtr("value2")}, + }, + Tags: &[]admin20240805.ResourceTag{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + MongoDBMajorVersion: conversion.StringPtr(mongoDBMajorVersion), + PitEnabled: conversion.Pointer(booleanValue), + RootCertType: conversion.StringPtr(rootCertType), + TerminationProtectionEnabled: conversion.Pointer(booleanValue), + VersionReleaseSystem: conversion.StringPtr(""), + GlobalClusterSelfManagedSharding: conversion.Pointer(booleanValue), + ReplicaSetScalingStrategy: conversion.StringPtr(replicaSetScalingStrategy), + RedactClientLogData: conversion.Pointer(booleanValue), + ConfigServerManagementMode: conversion.StringPtr(configServerManagementMode), + }, + }, + { + name: "Converts cluster description from 20241023 to 20240805 with nil values", + input: &admin.ClusterDescription20240805{}, + expectedOutput: &admin20240805.ClusterDescription20240805{ + ReplicationSpecs: nil, + BiConnector: nil, + Labels: &[]admin20240805.ComponentLabel{}, + Tags: &[]admin20240805.ResourceTag{}, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := advancedcluster.ConvertClusterDescription20241023to20240805(tc.input) + assert.Equal(t, tc.expectedOutput, result) + }) + } +} diff --git a/internal/service/advancedcluster/model_sdk_version_conversion.go b/internal/service/advancedcluster/model_sdk_version_conversion.go index 292cc71136..151b6c2a40 100644 --- a/internal/service/advancedcluster/model_sdk_version_conversion.go +++ b/internal/service/advancedcluster/model_sdk_version_conversion.go @@ -2,6 +2,7 @@ package advancedcluster import ( admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin" + admin20240805 "go.mongodb.org/atlas-sdk/v20240805005/admin" "go.mongodb.org/atlas-sdk/v20241023002/admin" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -324,3 +325,157 @@ func convertClusterDescToLatestExcludeRepSpecs(oldClusterDesc *admin20240530.Adv Labels: convertLabelsToLatest(oldClusterDesc.Labels), } } + +func ConvertClusterDescription20241023to20240805(clusterDescription *admin.ClusterDescription20240805) *admin20240805.ClusterDescription20240805 { + return &admin20240805.ClusterDescription20240805{ + Name: clusterDescription.Name, + ClusterType: clusterDescription.ClusterType, + ReplicationSpecs: convertReplicationSpecs20241023to20240805(clusterDescription.ReplicationSpecs), + BackupEnabled: clusterDescription.BackupEnabled, + BiConnector: convertBiConnector20241023to20240805(clusterDescription.BiConnector), + EncryptionAtRestProvider: clusterDescription.EncryptionAtRestProvider, + Labels: convertLabels20241023to20240805(clusterDescription.Labels), + Tags: convertTag20241023to20240805(clusterDescription.Tags), + MongoDBMajorVersion: clusterDescription.MongoDBMajorVersion, + PitEnabled: clusterDescription.PitEnabled, + RootCertType: clusterDescription.RootCertType, + TerminationProtectionEnabled: clusterDescription.TerminationProtectionEnabled, + VersionReleaseSystem: clusterDescription.VersionReleaseSystem, + GlobalClusterSelfManagedSharding: clusterDescription.GlobalClusterSelfManagedSharding, + ReplicaSetScalingStrategy: clusterDescription.ReplicaSetScalingStrategy, + RedactClientLogData: clusterDescription.RedactClientLogData, + ConfigServerManagementMode: clusterDescription.ConfigServerManagementMode, + } +} + +func convertReplicationSpecs20241023to20240805(replicationSpecs *[]admin.ReplicationSpec20240805) *[]admin20240805.ReplicationSpec20240805 { + if replicationSpecs == nil { + return nil + } + result := make([]admin20240805.ReplicationSpec20240805, len(*replicationSpecs)) + for i, replicationSpec := range *replicationSpecs { + result[i] = admin20240805.ReplicationSpec20240805{ + Id: replicationSpec.Id, + ZoneName: replicationSpec.ZoneName, + ZoneId: replicationSpec.ZoneId, + RegionConfigs: convertCloudRegionConfig20241023to20240805(replicationSpec.RegionConfigs), + } + } + return &result +} + +func convertCloudRegionConfig20241023to20240805(cloudRegionConfig *[]admin.CloudRegionConfig20240805) *[]admin20240805.CloudRegionConfig20240805 { + if cloudRegionConfig == nil { + return nil + } + result := make([]admin20240805.CloudRegionConfig20240805, len(*cloudRegionConfig)) + for i, regionConfig := range *cloudRegionConfig { + result[i] = admin20240805.CloudRegionConfig20240805{ + ProviderName: regionConfig.ProviderName, + RegionName: regionConfig.RegionName, + BackingProviderName: regionConfig.BackingProviderName, + Priority: regionConfig.Priority, + ElectableSpecs: convertHardwareSpec20241023to20240805(regionConfig.ElectableSpecs), + ReadOnlySpecs: convertDedicatedHardwareSpec20241023to20240805(regionConfig.ReadOnlySpecs), + AnalyticsSpecs: convertDedicatedHardwareSpec20241023to20240805(regionConfig.AnalyticsSpecs), + AutoScaling: convertAdvancedAutoScalingSettings20241023to20240805(regionConfig.AutoScaling), + AnalyticsAutoScaling: convertAdvancedAutoScalingSettings20241023to20240805(regionConfig.AnalyticsAutoScaling), + } + } + return &result +} + +func convertAdvancedAutoScalingSettings20241023to20240805(advancedAutoScalingSettings *admin.AdvancedAutoScalingSettings) *admin20240805.AdvancedAutoScalingSettings { + if advancedAutoScalingSettings == nil { + return nil + } + return &admin20240805.AdvancedAutoScalingSettings{ + Compute: convertAdvancedComputeAutoScaling20241023to20240805(advancedAutoScalingSettings.Compute), + DiskGB: convertDiskGBAutoScaling20241023to20240805(advancedAutoScalingSettings.DiskGB), + } +} + +func convertDiskGBAutoScaling20241023to20240805(diskGBAutoScaling *admin.DiskGBAutoScaling) *admin20240805.DiskGBAutoScaling { + if diskGBAutoScaling == nil { + return nil + } + return &admin20240805.DiskGBAutoScaling{ + Enabled: diskGBAutoScaling.Enabled, + } +} + +func convertAdvancedComputeAutoScaling20241023to20240805(advancedComputeAutoScaling *admin.AdvancedComputeAutoScaling) *admin20240805.AdvancedComputeAutoScaling { + if advancedComputeAutoScaling == nil { + return nil + } + return &admin20240805.AdvancedComputeAutoScaling{ + Enabled: advancedComputeAutoScaling.Enabled, + MaxInstanceSize: advancedComputeAutoScaling.MaxInstanceSize, + MinInstanceSize: advancedComputeAutoScaling.MinInstanceSize, + ScaleDownEnabled: advancedComputeAutoScaling.ScaleDownEnabled, + } +} + +func convertHardwareSpec20241023to20240805(hardwareSpec *admin.HardwareSpec20240805) *admin20240805.HardwareSpec20240805 { + if hardwareSpec == nil { + return nil + } + return &admin20240805.HardwareSpec20240805{ + DiskSizeGB: hardwareSpec.DiskSizeGB, + NodeCount: hardwareSpec.NodeCount, + DiskIOPS: hardwareSpec.DiskIOPS, + EbsVolumeType: hardwareSpec.EbsVolumeType, + InstanceSize: hardwareSpec.InstanceSize, + } +} + +func convertDedicatedHardwareSpec20241023to20240805(hardwareSpec *admin.DedicatedHardwareSpec20240805) *admin20240805.DedicatedHardwareSpec20240805 { + if hardwareSpec == nil { + return nil + } + return &admin20240805.DedicatedHardwareSpec20240805{ + DiskSizeGB: hardwareSpec.DiskSizeGB, + NodeCount: hardwareSpec.NodeCount, + DiskIOPS: hardwareSpec.DiskIOPS, + EbsVolumeType: hardwareSpec.EbsVolumeType, + InstanceSize: hardwareSpec.InstanceSize, + } +} + +func convertBiConnector20241023to20240805(biConnector *admin.BiConnector) *admin20240805.BiConnector { + if biConnector == nil { + return nil + } + return &admin20240805.BiConnector{ + ReadPreference: biConnector.ReadPreference, + Enabled: biConnector.Enabled, + } +} + +func convertLabels20241023to20240805(labels *[]admin.ComponentLabel) *[]admin20240805.ComponentLabel { + if labels == nil { + return &[]admin20240805.ComponentLabel{} + } + result := make([]admin20240805.ComponentLabel, len(*labels)) + for i, label := range *labels { + result[i] = admin20240805.ComponentLabel{ + Key: label.Key, + Value: label.Value, + } + } + return &result +} + +func convertTag20241023to20240805(tags *[]admin.ResourceTag) *[]admin20240805.ResourceTag { + if tags == nil { + return &[]admin20240805.ResourceTag{} + } + result := make([]admin20240805.ResourceTag, len(*tags)) + for i, tag := range *tags { + result[i] = admin20240805.ResourceTag{ + Key: tag.Key, + Value: tag.Value, + } + } + return &result +} diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index 38b53ab5cc..b6d71bed0a 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -483,9 +483,25 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(err) } - cluster, _, err := connV2.ClustersApi.CreateCluster(ctx, projectID, params).Execute() - if err != nil { - return diag.FromErr(fmt.Errorf(errorCreate, err)) + var clusterName string + var clusterID string + var err error + if isUsingOldAPISchemaStructure(d) { + var cluster20240805 *admin20240805.ClusterDescription20240805 + cluster20240805, _, err = connV220240805.ClustersApi.CreateCluster(ctx, projectID, ConvertClusterDescription20241023to20240805(params)).Execute() + if err != nil { + return diag.FromErr(fmt.Errorf(errorCreate, err)) + } + clusterName = cluster20240805.GetName() + clusterID = cluster20240805.GetId() + } else { + var cluster *admin.ClusterDescription20240805 + cluster, _, err = connV2.ClustersApi.CreateCluster(ctx, projectID, params).Execute() + if err != nil { + return diag.FromErr(fmt.Errorf(errorCreate, err)) + } + clusterName = cluster.GetName() + clusterID = cluster.GetId() } timeout := d.Timeout(schema.TimeoutCreate) @@ -498,13 +514,13 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag. if ac, ok := d.GetOk("advanced_configuration"); ok { if aclist, ok := ac.([]any); ok && len(aclist) > 0 { params20240530, params := expandProcessArgs(d, aclist[0].(map[string]any), params.MongoDBMajorVersion) - _, _, err := connV220240530.ClustersApi.UpdateClusterAdvancedConfiguration(ctx, projectID, cluster.GetName(), ¶ms20240530).Execute() + _, _, err := connV220240530.ClustersApi.UpdateClusterAdvancedConfiguration(ctx, projectID, clusterName, ¶ms20240530).Execute() if err != nil { - return diag.FromErr(fmt.Errorf(errorConfigUpdate, cluster.GetName(), err)) + return diag.FromErr(fmt.Errorf(errorConfigUpdate, clusterName, err)) } - _, _, err = connV2.ClustersApi.UpdateClusterAdvancedConfiguration(ctx, projectID, cluster.GetName(), ¶ms).Execute() + _, _, err = connV2.ClustersApi.UpdateClusterAdvancedConfiguration(ctx, projectID, clusterName, ¶ms).Execute() if err != nil { - return diag.FromErr(fmt.Errorf(errorConfigUpdate, cluster.GetName(), err)) + return diag.FromErr(fmt.Errorf(errorConfigUpdate, clusterName, err)) } } } @@ -523,9 +539,9 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag. } d.SetId(conversion.EncodeStateID(map[string]string{ - "cluster_id": cluster.GetId(), + "cluster_id": clusterID, "project_id": projectID, - "cluster_name": cluster.GetName(), + "cluster_name": clusterName, })) return resourceRead(ctx, d, meta) diff --git a/internal/service/advancedclustertpf/resource_schema.go b/internal/service/advancedclustertpf/resource_schema.go index 424a6fa666..4bee9a8782 100644 --- a/internal/service/advancedclustertpf/resource_schema.go +++ b/internal/service/advancedclustertpf/resource_schema.go @@ -497,8 +497,8 @@ type TFModel struct { Labels types.Set `tfsdk:"labels"` ReplicationSpecs types.List `tfsdk:"replication_specs"` Tags types.Set `tfsdk:"tags"` - StateName types.String `tfsdk:"state_name"` - ConnectionStrings types.Object `tfsdk:"connection_strings"` + MongoDBVersion types.String `tfsdk:"mongo_db_version"` + BiConnectorConfig types.Object `tfsdk:"bi_connector_config"` CreateDate types.String `tfsdk:"create_date"` AcceptDataRisksAndForceReplicaSetReconfig types.String `tfsdk:"accept_data_risks_and_force_replica_set_reconfig"` EncryptionAtRestProvider types.String `tfsdk:"encryption_at_rest_provider"` @@ -507,22 +507,22 @@ type TFModel struct { ClusterID types.String `tfsdk:"cluster_id"` ConfigServerManagementMode types.String `tfsdk:"config_server_management_mode"` MongoDBMajorVersion types.String `tfsdk:"mongo_db_major_version"` - MongoDBVersion types.String `tfsdk:"mongo_db_version"` + StateName types.String `tfsdk:"state_name"` Name types.String `tfsdk:"name"` VersionReleaseSystem types.String `tfsdk:"version_release_system"` - BiConnectorConfig types.Object `tfsdk:"bi_connector_config"` + ConnectionStrings types.Object `tfsdk:"connection_strings"` ConfigServerType types.String `tfsdk:"config_server_type"` ReplicaSetScalingStrategy types.String `tfsdk:"replica_set_scaling_strategy"` ClusterType types.String `tfsdk:"cluster_type"` RootCertType types.String `tfsdk:"root_cert_type"` - RedactClientLogData types.Bool `tfsdk:"redact_client_log_data"` + AdvancedConfiguration types.Object `tfsdk:"advanced_configuration"` PitEnabled types.Bool `tfsdk:"pit_enabled"` TerminationProtectionEnabled types.Bool `tfsdk:"termination_protection_enabled"` Paused types.Bool `tfsdk:"paused"` RetainBackupsEnabled types.Bool `tfsdk:"retain_backups_enabled"` BackupEnabled types.Bool `tfsdk:"backup_enabled"` GlobalClusterSelfManagedSharding types.Bool `tfsdk:"global_cluster_self_managed_sharding"` - AdvancedConfiguration types.Object `tfsdk:"advanced_configuration"` + RedactClientLogData types.Bool `tfsdk:"redact_client_log_data"` } type TFBiConnectorModel struct { @@ -590,13 +590,13 @@ var LabelsObjType = types.ObjectType{AttrTypes: map[string]attr.Type{ }} type TFReplicationSpecsModel struct { - Id types.String `tfsdk:"id"` + RegionConfigs types.List `tfsdk:"region_configs"` ContainerId types.Map `tfsdk:"container_id"` + Id types.String `tfsdk:"id"` ExternalId types.String `tfsdk:"external_id"` - NumShards types.Int64 `tfsdk:"num_shards"` - RegionConfigs types.List `tfsdk:"region_configs"` ZoneId types.String `tfsdk:"zone_id"` ZoneName types.String `tfsdk:"zone_name"` + NumShards types.Int64 `tfsdk:"num_shards"` } var ReplicationSpecsObjType = types.ObjectType{AttrTypes: map[string]attr.Type{ From 71b9adbf255db3d5d33c9d270beca8c31067ab4e Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Wed, 27 Nov 2024 16:04:57 +0000 Subject: [PATCH 2/5] changelog --- .changelog/2836.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2836.txt diff --git a/.changelog/2836.txt b/.changelog/2836.txt new file mode 100644 index 0000000000..a6e20cc015 --- /dev/null +++ b/.changelog/2836.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/mongodbatlas_advanced_cluster: Ensures asymmetric auto-scaling is not defined in the cluster when using the old resource schema +``` From b2e170eed354ff9875baaef1ef3c47324aee3423 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Wed, 27 Nov 2024 16:06:24 +0000 Subject: [PATCH 3/5] changelog fix --- .changelog/2836.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/2836.txt b/.changelog/2836.txt index a6e20cc015..e7df7885c4 100644 --- a/.changelog/2836.txt +++ b/.changelog/2836.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -resource/mongodbatlas_advanced_cluster: Ensures asymmetric auto-scaling is not defined in the cluster when using the old resource schema +resource/mongodbatlas_advanced_cluster: Ensures asymmetric auto-scaling is not defined in the cluster when using the old sharding configuration ``` From bd76cda01e6a859d46c6a94adec76c15eba14919 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Thu, 28 Nov 2024 08:29:34 +0000 Subject: [PATCH 4/5] add feature to adv_cluster tpf todo --- internal/service/advancedclustertpf/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/service/advancedclustertpf/README.md b/internal/service/advancedclustertpf/README.md index 8275f6fd58..7d712dafb4 100644 --- a/internal/service/advancedclustertpf/README.md +++ b/internal/service/advancedclustertpf/README.md @@ -7,3 +7,4 @@ This package contains the WIP for `mongodbatlas_advanced_cluster` in TPF. Curren ## Changes in advancedcluster that needs to be added here (list changes done in advancedcluster which couldn't also be done here at that moment) +- feat: Ensures asymmetric auto-scaling is not defined in the cluster when using the old sharding configuration in mongodbatlas_advanced_cluster (https://github.com/mongodb/terraform-provider-mongodbatlas/pull/2836) \ No newline at end of file From 35d41ed0b71a1db2c43169f068a835bd5afebcf2 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Thu, 28 Nov 2024 10:08:43 +0000 Subject: [PATCH 5/5] pr comments --- .changelog/2836.txt | 2 +- .../model_advanced_cluster_test.go | 183 ----------------- .../model_sdk_version_conversion_test.go | 193 ++++++++++++++++++ .../resource_advanced_cluster.go | 8 +- 4 files changed, 198 insertions(+), 188 deletions(-) create mode 100644 internal/service/advancedcluster/model_sdk_version_conversion_test.go diff --git a/.changelog/2836.txt b/.changelog/2836.txt index e7df7885c4..5cc1efb27f 100644 --- a/.changelog/2836.txt +++ b/.changelog/2836.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -resource/mongodbatlas_advanced_cluster: Ensures asymmetric auto-scaling is not defined in the cluster when using the old sharding configuration +resource/mongodbatlas_advanced_cluster: Adjusts create operation to support cluster tier auto scaling per shard. ``` diff --git a/internal/service/advancedcluster/model_advanced_cluster_test.go b/internal/service/advancedcluster/model_advanced_cluster_test.go index 7ff1427595..7dd517c469 100644 --- a/internal/service/advancedcluster/model_advanced_cluster_test.go +++ b/internal/service/advancedcluster/model_advanced_cluster_test.go @@ -8,7 +8,6 @@ import ( "testing" admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin" - admin20240805 "go.mongodb.org/atlas-sdk/v20240805005/admin" "go.mongodb.org/atlas-sdk/v20241023002/admin" "go.mongodb.org/atlas-sdk/v20241023002/mockadmin" @@ -464,185 +463,3 @@ func TestCheckRegionConfigsPriorityOrder(t *testing.T) { }) } } - -func TestConvertClusterDescription20241023to20240805(t *testing.T) { - var ( - clusterName = "clusterName" - clusterType = "REPLICASET" - earProvider = "AWS" - booleanValue = true - mongoDBMajorVersion = "7.0" - rootCertType = "rootCertType" - replicaSetScalingStrategy = "WORKLOAD_TYPE" - configServerManagementMode = "ATLAS_MANAGED" - readPreference = "primary" - zoneName = "z1" - id = "id1" - regionConfigProvider = "AWS" - region = "EU_WEST_1" - priority = 7 - instanceSize = "M10" - nodeCount = 3 - diskSizeGB = 30.3 - ebsVolumeType = "STANDARD" - diskIOPS = 100 - ) - testCases := []struct { - input *admin.ClusterDescription20240805 - expectedOutput *admin20240805.ClusterDescription20240805 - name string - }{ - { - name: "Converts cluster description from 20241023 to 20240805", - input: &admin.ClusterDescription20240805{ - Name: conversion.StringPtr(clusterName), - ClusterType: conversion.StringPtr(clusterType), - ReplicationSpecs: &[]admin.ReplicationSpec20240805{ - { - Id: conversion.StringPtr(id), - ZoneName: conversion.StringPtr(zoneName), - RegionConfigs: &[]admin.CloudRegionConfig20240805{ - { - ProviderName: conversion.StringPtr(regionConfigProvider), - RegionName: conversion.StringPtr(region), - BackingProviderName: conversion.StringPtr(regionConfigProvider), - Priority: conversion.IntPtr(priority), - AnalyticsSpecs: &admin.DedicatedHardwareSpec20240805{ - InstanceSize: conversion.StringPtr(instanceSize), - NodeCount: conversion.IntPtr(nodeCount), - DiskSizeGB: conversion.Pointer(diskSizeGB), - EbsVolumeType: conversion.StringPtr(ebsVolumeType), - DiskIOPS: conversion.IntPtr(diskIOPS), - }, - ElectableSpecs: &admin.HardwareSpec20240805{ - InstanceSize: conversion.StringPtr(instanceSize), - NodeCount: conversion.IntPtr(nodeCount), - DiskSizeGB: conversion.Pointer(diskSizeGB), - EbsVolumeType: conversion.StringPtr(ebsVolumeType), - DiskIOPS: conversion.IntPtr(diskIOPS), - }, - AutoScaling: &admin.AdvancedAutoScalingSettings{ - Compute: &admin.AdvancedComputeAutoScaling{ - Enabled: conversion.Pointer(booleanValue), - MaxInstanceSize: conversion.Pointer(instanceSize), - MinInstanceSize: conversion.Pointer(instanceSize), - ScaleDownEnabled: conversion.Pointer(booleanValue), - }, - DiskGB: &admin.DiskGBAutoScaling{ - Enabled: conversion.Pointer(booleanValue), - }, - }, - }, - }, - }, - }, - BackupEnabled: conversion.Pointer(booleanValue), - BiConnector: &admin.BiConnector{ - Enabled: conversion.Pointer(booleanValue), - ReadPreference: conversion.StringPtr(readPreference), - }, - EncryptionAtRestProvider: conversion.StringPtr(earProvider), - Labels: &[]admin.ComponentLabel{ - {Key: conversion.StringPtr("key1"), Value: conversion.StringPtr("value1")}, - {Key: conversion.StringPtr("key2"), Value: conversion.StringPtr("value2")}, - }, - Tags: &[]admin.ResourceTag{ - {Key: "key1", Value: "value1"}, - {Key: "key2", Value: "value2"}, - }, - MongoDBMajorVersion: conversion.StringPtr(mongoDBMajorVersion), - PitEnabled: conversion.Pointer(booleanValue), - RootCertType: conversion.StringPtr(rootCertType), - TerminationProtectionEnabled: conversion.Pointer(booleanValue), - VersionReleaseSystem: conversion.StringPtr(""), - GlobalClusterSelfManagedSharding: conversion.Pointer(booleanValue), - ReplicaSetScalingStrategy: conversion.StringPtr(replicaSetScalingStrategy), - RedactClientLogData: conversion.Pointer(booleanValue), - ConfigServerManagementMode: conversion.StringPtr(configServerManagementMode), - }, - expectedOutput: &admin20240805.ClusterDescription20240805{ - Name: conversion.StringPtr(clusterName), - ClusterType: conversion.StringPtr(clusterType), - ReplicationSpecs: &[]admin20240805.ReplicationSpec20240805{ - { - Id: conversion.StringPtr(id), - ZoneName: conversion.StringPtr(zoneName), - RegionConfigs: &[]admin20240805.CloudRegionConfig20240805{ - { - ProviderName: conversion.StringPtr(regionConfigProvider), - RegionName: conversion.StringPtr(region), - BackingProviderName: conversion.StringPtr(regionConfigProvider), - Priority: conversion.IntPtr(priority), - AnalyticsSpecs: &admin20240805.DedicatedHardwareSpec20240805{ - InstanceSize: conversion.StringPtr(instanceSize), - NodeCount: conversion.IntPtr(nodeCount), - DiskSizeGB: conversion.Pointer(diskSizeGB), - EbsVolumeType: conversion.StringPtr(ebsVolumeType), - DiskIOPS: conversion.IntPtr(diskIOPS), - }, - ElectableSpecs: &admin20240805.HardwareSpec20240805{ - InstanceSize: conversion.StringPtr(instanceSize), - NodeCount: conversion.IntPtr(nodeCount), - DiskSizeGB: conversion.Pointer(diskSizeGB), - EbsVolumeType: conversion.StringPtr(ebsVolumeType), - DiskIOPS: conversion.IntPtr(diskIOPS), - }, - AutoScaling: &admin20240805.AdvancedAutoScalingSettings{ - Compute: &admin20240805.AdvancedComputeAutoScaling{ - Enabled: conversion.Pointer(booleanValue), - MaxInstanceSize: conversion.Pointer(instanceSize), - MinInstanceSize: conversion.Pointer(instanceSize), - ScaleDownEnabled: conversion.Pointer(booleanValue), - }, - DiskGB: &admin20240805.DiskGBAutoScaling{ - Enabled: conversion.Pointer(booleanValue), - }, - }, - }, - }, - }, - }, - BackupEnabled: conversion.Pointer(booleanValue), - BiConnector: &admin20240805.BiConnector{ - Enabled: conversion.Pointer(booleanValue), - ReadPreference: conversion.StringPtr(readPreference), - }, - EncryptionAtRestProvider: conversion.StringPtr(earProvider), - Labels: &[]admin20240805.ComponentLabel{ - {Key: conversion.StringPtr("key1"), Value: conversion.StringPtr("value1")}, - {Key: conversion.StringPtr("key2"), Value: conversion.StringPtr("value2")}, - }, - Tags: &[]admin20240805.ResourceTag{ - {Key: "key1", Value: "value1"}, - {Key: "key2", Value: "value2"}, - }, - MongoDBMajorVersion: conversion.StringPtr(mongoDBMajorVersion), - PitEnabled: conversion.Pointer(booleanValue), - RootCertType: conversion.StringPtr(rootCertType), - TerminationProtectionEnabled: conversion.Pointer(booleanValue), - VersionReleaseSystem: conversion.StringPtr(""), - GlobalClusterSelfManagedSharding: conversion.Pointer(booleanValue), - ReplicaSetScalingStrategy: conversion.StringPtr(replicaSetScalingStrategy), - RedactClientLogData: conversion.Pointer(booleanValue), - ConfigServerManagementMode: conversion.StringPtr(configServerManagementMode), - }, - }, - { - name: "Converts cluster description from 20241023 to 20240805 with nil values", - input: &admin.ClusterDescription20240805{}, - expectedOutput: &admin20240805.ClusterDescription20240805{ - ReplicationSpecs: nil, - BiConnector: nil, - Labels: &[]admin20240805.ComponentLabel{}, - Tags: &[]admin20240805.ResourceTag{}, - }, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - result := advancedcluster.ConvertClusterDescription20241023to20240805(tc.input) - assert.Equal(t, tc.expectedOutput, result) - }) - } -} diff --git a/internal/service/advancedcluster/model_sdk_version_conversion_test.go b/internal/service/advancedcluster/model_sdk_version_conversion_test.go new file mode 100644 index 0000000000..b496ccbba6 --- /dev/null +++ b/internal/service/advancedcluster/model_sdk_version_conversion_test.go @@ -0,0 +1,193 @@ +package advancedcluster_test + +import ( + "testing" + + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster" + "github.com/stretchr/testify/assert" + admin20240805 "go.mongodb.org/atlas-sdk/v20240805005/admin" + "go.mongodb.org/atlas-sdk/v20241023002/admin" +) + +func TestConvertClusterDescription20241023to20240805(t *testing.T) { + var ( + clusterName = "clusterName" + clusterType = "REPLICASET" + earProvider = "AWS" + booleanValue = true + mongoDBMajorVersion = "7.0" + rootCertType = "rootCertType" + replicaSetScalingStrategy = "WORKLOAD_TYPE" + configServerManagementMode = "ATLAS_MANAGED" + readPreference = "primary" + zoneName = "z1" + id = "id1" + regionConfigProvider = "AWS" + region = "EU_WEST_1" + priority = 7 + instanceSize = "M10" + nodeCount = 3 + diskSizeGB = 30.3 + ebsVolumeType = "STANDARD" + diskIOPS = 100 + ) + testCases := []struct { + input *admin.ClusterDescription20240805 + expectedOutput *admin20240805.ClusterDescription20240805 + name string + }{ + { + name: "Converts cluster description from 20241023 to 20240805", + input: &admin.ClusterDescription20240805{ + Name: conversion.StringPtr(clusterName), + ClusterType: conversion.StringPtr(clusterType), + ReplicationSpecs: &[]admin.ReplicationSpec20240805{ + { + Id: conversion.StringPtr(id), + ZoneName: conversion.StringPtr(zoneName), + RegionConfigs: &[]admin.CloudRegionConfig20240805{ + { + ProviderName: conversion.StringPtr(regionConfigProvider), + RegionName: conversion.StringPtr(region), + BackingProviderName: conversion.StringPtr(regionConfigProvider), + Priority: conversion.IntPtr(priority), + AnalyticsSpecs: &admin.DedicatedHardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + ElectableSpecs: &admin.HardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + AutoScaling: &admin.AdvancedAutoScalingSettings{ + Compute: &admin.AdvancedComputeAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + MaxInstanceSize: conversion.Pointer(instanceSize), + MinInstanceSize: conversion.Pointer(instanceSize), + ScaleDownEnabled: conversion.Pointer(booleanValue), + }, + DiskGB: &admin.DiskGBAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + }, + }, + }, + }, + }, + }, + BackupEnabled: conversion.Pointer(booleanValue), + BiConnector: &admin.BiConnector{ + Enabled: conversion.Pointer(booleanValue), + ReadPreference: conversion.StringPtr(readPreference), + }, + EncryptionAtRestProvider: conversion.StringPtr(earProvider), + Labels: &[]admin.ComponentLabel{ + {Key: conversion.StringPtr("key1"), Value: conversion.StringPtr("value1")}, + {Key: conversion.StringPtr("key2"), Value: conversion.StringPtr("value2")}, + }, + Tags: &[]admin.ResourceTag{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + MongoDBMajorVersion: conversion.StringPtr(mongoDBMajorVersion), + PitEnabled: conversion.Pointer(booleanValue), + RootCertType: conversion.StringPtr(rootCertType), + TerminationProtectionEnabled: conversion.Pointer(booleanValue), + VersionReleaseSystem: conversion.StringPtr(""), + GlobalClusterSelfManagedSharding: conversion.Pointer(booleanValue), + ReplicaSetScalingStrategy: conversion.StringPtr(replicaSetScalingStrategy), + RedactClientLogData: conversion.Pointer(booleanValue), + ConfigServerManagementMode: conversion.StringPtr(configServerManagementMode), + }, + expectedOutput: &admin20240805.ClusterDescription20240805{ + Name: conversion.StringPtr(clusterName), + ClusterType: conversion.StringPtr(clusterType), + ReplicationSpecs: &[]admin20240805.ReplicationSpec20240805{ + { + Id: conversion.StringPtr(id), + ZoneName: conversion.StringPtr(zoneName), + RegionConfigs: &[]admin20240805.CloudRegionConfig20240805{ + { + ProviderName: conversion.StringPtr(regionConfigProvider), + RegionName: conversion.StringPtr(region), + BackingProviderName: conversion.StringPtr(regionConfigProvider), + Priority: conversion.IntPtr(priority), + AnalyticsSpecs: &admin20240805.DedicatedHardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + ElectableSpecs: &admin20240805.HardwareSpec20240805{ + InstanceSize: conversion.StringPtr(instanceSize), + NodeCount: conversion.IntPtr(nodeCount), + DiskSizeGB: conversion.Pointer(diskSizeGB), + EbsVolumeType: conversion.StringPtr(ebsVolumeType), + DiskIOPS: conversion.IntPtr(diskIOPS), + }, + AutoScaling: &admin20240805.AdvancedAutoScalingSettings{ + Compute: &admin20240805.AdvancedComputeAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + MaxInstanceSize: conversion.Pointer(instanceSize), + MinInstanceSize: conversion.Pointer(instanceSize), + ScaleDownEnabled: conversion.Pointer(booleanValue), + }, + DiskGB: &admin20240805.DiskGBAutoScaling{ + Enabled: conversion.Pointer(booleanValue), + }, + }, + }, + }, + }, + }, + BackupEnabled: conversion.Pointer(booleanValue), + BiConnector: &admin20240805.BiConnector{ + Enabled: conversion.Pointer(booleanValue), + ReadPreference: conversion.StringPtr(readPreference), + }, + EncryptionAtRestProvider: conversion.StringPtr(earProvider), + Labels: &[]admin20240805.ComponentLabel{ + {Key: conversion.StringPtr("key1"), Value: conversion.StringPtr("value1")}, + {Key: conversion.StringPtr("key2"), Value: conversion.StringPtr("value2")}, + }, + Tags: &[]admin20240805.ResourceTag{ + {Key: "key1", Value: "value1"}, + {Key: "key2", Value: "value2"}, + }, + MongoDBMajorVersion: conversion.StringPtr(mongoDBMajorVersion), + PitEnabled: conversion.Pointer(booleanValue), + RootCertType: conversion.StringPtr(rootCertType), + TerminationProtectionEnabled: conversion.Pointer(booleanValue), + VersionReleaseSystem: conversion.StringPtr(""), + GlobalClusterSelfManagedSharding: conversion.Pointer(booleanValue), + ReplicaSetScalingStrategy: conversion.StringPtr(replicaSetScalingStrategy), + RedactClientLogData: conversion.Pointer(booleanValue), + ConfigServerManagementMode: conversion.StringPtr(configServerManagementMode), + }, + }, + { + name: "Converts cluster description from 20241023 to 20240805 with nil values", + input: &admin.ClusterDescription20240805{}, + expectedOutput: &admin20240805.ClusterDescription20240805{ + ReplicationSpecs: nil, + BiConnector: nil, + Labels: &[]admin20240805.ComponentLabel{}, + Tags: &[]admin20240805.ResourceTag{}, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := advancedcluster.ConvertClusterDescription20241023to20240805(tc.input) + assert.Equal(t, tc.expectedOutput, result) + }) + } +} diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index b6d71bed0a..2b35fcf397 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -486,7 +486,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag. var clusterName string var clusterID string var err error - if isUsingOldAPISchemaStructure(d) { + if isUsingOldShardingConfiguration(d) { var cluster20240805 *admin20240805.ClusterDescription20240805 cluster20240805, _, err = connV220240805.ClustersApi.CreateCluster(ctx, projectID, ConvertClusterDescription20241023to20240805(params)).Execute() if err != nil { @@ -568,7 +568,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di var clusterResp *admin.ClusterDescription20240805 var replicationSpecs []map[string]any - if isUsingOldAPISchemaStructure(d) { + if isUsingOldShardingConfiguration(d) { clusterOldSDK, resp, err := connV220240530.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() if err != nil { if resp != nil && resp.StatusCode == http.StatusNotFound { @@ -792,7 +792,7 @@ func setRootFields(d *schema.ResourceData, cluster *admin.ClusterDescription2024 } // For both read and update operations if old sharding schema structure is used (at least one replication spec with numShards > 1) we continue to invoke the old API -func isUsingOldAPISchemaStructure(d *schema.ResourceData) bool { +func isUsingOldShardingConfiguration(d *schema.ResourceData) bool { tfList := d.Get("replication_specs").([]any) for _, tfMapRaw := range tfList { tfMap, ok := tfMapRaw.(map[string]any) @@ -848,7 +848,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag. timeout := d.Timeout(schema.TimeoutUpdate) - if isUsingOldAPISchemaStructure(d) { + if isUsingOldShardingConfiguration(d) { req, diags := updateRequestOldAPI(d, clusterName) if diags != nil { return diags