diff --git a/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go b/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go index 5b8afe8189..5fd9989952 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go +++ b/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go @@ -109,12 +109,12 @@ func TestMigAdvancedCluster_shardedMigrationFromOldToNewSchema(t *testing.T) { Steps: []resource.TestStep{ { ExternalProviders: acc.ExternalProviders(versionBeforeISSRelease), - Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false), + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false), Check: checkShardedTransitionOldToNewSchema(false), }, { ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true), + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false), Check: checkShardedTransitionOldToNewSchema(true), }, }, @@ -228,6 +228,32 @@ func TestMigAdvancedCluster_partialAdvancedConf(t *testing.T) { }) } +func TestMigAdvancedCluster_newSchemaFromAutoscalingDisabledToEnabled(t *testing.T) { + acc.SkipIfTPFAdvancedCluster(t) + var ( + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + projectName = acc.RandomProjectName() + clusterName = acc.RandomClusterName() + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName), + CheckDestroy: acc.CheckDestroyCluster, + Steps: []resource.TestStep{ + { + ExternalProviders: acc.ExternalProviders("1.22.0"), // last version before cluster tier auto-scaling per shard was introduced + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false), + Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"), + }, + { + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, true), + Check: checkIndependentShardScalingMode(clusterName, "SHARD"), + }, + }, + }) +} + func configPartialAdvancedConfig(projectID, clusterName, extraArgs, autoScaling string) string { return fmt.Sprintf(` resource "mongodbatlas_advanced_cluster" "test" { diff --git a/internal/service/advancedcluster/resource_advanced_cluster_test.go b/internal/service/advancedcluster/resource_advanced_cluster_test.go index a53a3dac52..a6332855ae 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster_test.go +++ b/internal/service/advancedcluster/resource_advanced_cluster_test.go @@ -1,6 +1,7 @@ package advancedcluster_test import ( + "context" "fmt" "os" "regexp" @@ -708,11 +709,11 @@ func TestAccClusterAdvancedClusterConfig_shardedTransitionFromOldToNewSchema(t * CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { - Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false), + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false), Check: checkShardedTransitionOldToNewSchema(false), }, { - Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true), + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false), Check: checkShardedTransitionOldToNewSchema(true), }, }, @@ -866,6 +867,127 @@ func TestAccClusterAdvancedCluster_priorityNewSchema(t *testing.T) { }) } +func TestAccAdvancedCluster_oldToNewSchemaWithAutoscalingDisabled(t *testing.T) { + acc.SkipIfTPFAdvancedCluster(t) + var ( + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + projectName = acc.RandomProjectName() + clusterName = acc.RandomClusterName() + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName), + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + CheckDestroy: acc.CheckDestroyCluster, + Steps: []resource.TestStep{ + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false), + Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"), + }, + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false), + Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"), + }, + }, + }) +} + +func TestAccAdvancedCluster_oldToNewSchemaWithAutoscalingEnabled(t *testing.T) { + acc.SkipIfTPFAdvancedCluster(t) + var ( + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + projectName = acc.RandomProjectName() + clusterName = acc.RandomClusterName() + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName), + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + CheckDestroy: acc.CheckDestroyCluster, + Steps: []resource.TestStep{ + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, true), + Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"), + }, + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, true), + Check: checkIndependentShardScalingMode(clusterName, "SHARD"), + }, + }, + }) +} + +func TestAccAdvancedCluster_oldToNewSchemaWithAutoscalingDisabledToEnabled(t *testing.T) { + acc.SkipIfTPFAdvancedCluster(t) + var ( + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + projectName = acc.RandomProjectName() + clusterName = acc.RandomClusterName() + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName), + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + CheckDestroy: acc.CheckDestroyCluster, + Steps: []resource.TestStep{ + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false), + Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"), + }, + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false), + Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"), + }, + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, true), + Check: checkIndependentShardScalingMode(clusterName, "SHARD"), + }, + }, + }) +} + +func TestAccAdvancedCluster_newSchema(t *testing.T) { + acc.SkipIfTPFAdvancedCluster(t) + var ( + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + projectName = acc.RandomProjectName() + clusterName = acc.RandomClusterName() + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName), + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + CheckDestroy: acc.CheckDestroyCluster, + Steps: []resource.TestStep{ + { + Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false), + Check: checkIndependentShardScalingMode(clusterName, "SHARD"), + }, + }, + }) +} + +func checkIndependentShardScalingMode(clusterName, expectedMode string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("not found: %s", resourceName) + } + if rs.Primary.ID == "" { + return fmt.Errorf("no ID is set") + } + ids := conversion.DecodeStateID(rs.Primary.ID) + issMode, _, err := acc.GetIndependentShardScalingMode(context.Background(), ids["project_id"], clusterName) + if err != nil { + return fmt.Errorf("error getting independent shard scaling mode: %w", err) + } + if *issMode != expectedMode { + return fmt.Errorf("expected independent shard scaling mode to be %s, got %s", expectedMode, *issMode) + } + return nil + } +} + func checkAggr(attrsSet []string, attrsMap map[string]string, extra ...resource.TestCheckFunc) resource.TestCheckFunc { checks := []resource.TestCheckFunc{checkExists(resourceName)} checks = acc.AddAttrChecks(resourceName, checks, attrsMap) @@ -1908,11 +2030,19 @@ func checkGeoShardedNewSchema(includeThirdShardInFirstZone bool) resource.TestCh ) } -func configShardedTransitionOldToNewSchema(orgID, projectName, name string, useNewSchema bool) string { +func configShardedTransitionOldToNewSchema(orgID, projectName, name string, useNewSchema, autoscaling bool) string { var numShardsStr string if !useNewSchema { numShardsStr = `num_shards = 2` } + var autoscalingStr string + if autoscaling { + autoscalingStr = `auto_scaling { + compute_enabled = true + disk_gb_enabled = true + compute_max_instance_size = "M20" + }` + } replicationSpec := fmt.Sprintf(` replication_specs { %[1]s @@ -1928,9 +2058,10 @@ func configShardedTransitionOldToNewSchema(orgID, projectName, name string, useN provider_name = "AWS" priority = 7 region_name = "EU_WEST_1" + %[2]s } } - `, numShardsStr) + `, numShardsStr, autoscalingStr) var replicationSpecs string if useNewSchema { diff --git a/internal/testutil/acc/independent_shard_scaling.go b/internal/testutil/acc/independent_shard_scaling.go new file mode 100644 index 0000000000..7dc3f1e544 --- /dev/null +++ b/internal/testutil/acc/independent_shard_scaling.go @@ -0,0 +1,49 @@ +package acc + +import ( + "context" + "io" + "net/http" + "os" + + "github.com/mongodb-forks/digest" +) + +func GetIndependentShardScalingMode(ctx context.Context, projectID, clusterName string) (*string, *http.Response, error) { + baseURL := os.Getenv("MONGODB_ATLAS_BASE_URL") + req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"test/utils/auth/groups/"+projectID+"/clusters/"+clusterName+"/independentShardScalingMode", http.NoBody) + if err != nil { + return nil, nil, err + } + + req.Header.Add("Accept", "*/*") + + transport := digest.NewTransport(os.Getenv("MONGODB_ATLAS_PUBLIC_KEY"), os.Getenv("MONGODB_ATLAS_PRIVATE_KEY")) + httpClient, err := transport.Client() + if err != nil { + return nil, nil, err + } + + resp, err := httpClient.Do(req) + if err != nil || resp == nil { + return nil, resp, err + } + + var result *string + result, err = decode(resp.Body) + if err != nil { + return nil, resp, err + } + + return result, resp, nil +} + +func decode(body io.ReadCloser) (*string, error) { + buf, err := io.ReadAll(body) + _ = body.Close() + if err != nil { + return nil, err + } + result := string(buf) + return &result, nil +}