From a34ef6de4bb0c47bc4bc75242690472868ad4171 Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Thu, 27 Jun 2024 12:18:06 +0100 Subject: [PATCH 1/8] update flatteners and add conversion methods --- .../data_source_advanced_cluster.go | 38 ++++- .../data_source_advanced_clusters.go | 18 ++- .../advancedcluster/model_advanced_cluster.go | 67 +++++--- .../model_advanced_cluster_test.go | 24 +-- .../model_sdk_version_conversion.go | 148 +++++++++++++++++- internal/service/advancedcluster/rep_spec.go | 66 ++++++++ .../resource_advanced_cluster.go | 22 ++- 7 files changed, 328 insertions(+), 55 deletions(-) create mode 100644 internal/service/advancedcluster/rep_spec.go diff --git a/internal/service/advancedcluster/data_source_advanced_cluster.go b/internal/service/advancedcluster/data_source_advanced_cluster.go index 38b15521a4..8d87dc5b40 100644 --- a/internal/service/advancedcluster/data_source_advanced_cluster.go +++ b/internal/service/advancedcluster/data_source_advanced_cluster.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" @@ -20,6 +21,10 @@ func DataSource() *schema.Resource { Type: schema.TypeString, Required: true, }, + "use_independent_shards": { + Type: schema.TypeBool, + Optional: true, + }, "advanced_configuration": SchemaAdvancedConfigDS(), "backup_enabled": { Type: schema.TypeBool, @@ -108,6 +113,14 @@ func DataSource() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "zone_id": { // new API only + Type: schema.TypeString, + Computed: true, + }, + "external_id": { // new API only + Type: schema.TypeString, + Computed: true, + }, "num_shards": { Type: schema.TypeInt, Computed: true, @@ -235,10 +248,24 @@ func DataSource() *schema.Resource { func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { connV2 := meta.(*config.MongoDBClient).AtlasV2 + connLatest := meta.(*config.MongoDBClient).AtlasV2Preview + projectID := d.Get("project_id").(string) clusterName := d.Get("name").(string) + // useIndependentShards := false + + // if v, ok := d.GetOk("use_independent_shards"); ok { + // useIndependentShards = v.(bool) + // } + // if !useIndependentShards { cluster, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() + + // } else { + // cluster, resp, err := connPreview.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() //var cluster *adminPreview.ClusterDescription20240710 + + // } + if err != nil { if resp != nil && resp.StatusCode == http.StatusNotFound { return nil @@ -250,7 +277,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "backup_enabled", clusterName, err)) } - if err := d.Set("bi_connector_config", flattenBiConnectorConfig(cluster.GetBiConnector())); err != nil { + if err := d.Set("bi_connector_config", flattenBiConnectorConfig(convertBiConnectToLatest(cluster.GetBiConnector()))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "bi_connector_config", clusterName, err)) } @@ -258,7 +285,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "cluster_type", clusterName, err)) } - if err := d.Set("connection_strings", flattenConnectionStrings(cluster.GetConnectionStrings())); err != nil { + if err := d.Set("connection_strings", flattenConnectionStrings(convertConnectionStringToLatest(cluster.GetConnectionStrings()))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "connection_strings", clusterName, err)) } @@ -274,11 +301,11 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "encryption_at_rest_provider", clusterName, err)) } - if err := d.Set("labels", flattenLabels(cluster.GetLabels())); err != nil { + if err := d.Set("labels", flattenLabels(convertLabelsToLatest(cluster.GetLabels()))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "labels", clusterName, err)) } - if err := d.Set("tags", conversion.FlattenTags(cluster.GetTags())); err != nil { + if err := d.Set("tags", flattenTags(*convertTagsToLatest(cluster.Tags))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "tags", clusterName, err)) } @@ -302,7 +329,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err)) } - replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV2) + replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connLatest) if err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) } @@ -328,6 +355,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "global_cluster_self_managed_sharding", clusterName, err)) } + // TODO: update to use connLatest to call below API processArgs, _, err := connV2.ClustersApi.GetClusterAdvancedConfiguration(ctx, projectID, clusterName).Execute() if err != nil { return diag.FromErr(fmt.Errorf(ErrorAdvancedConfRead, clusterName, err)) diff --git a/internal/service/advancedcluster/data_source_advanced_clusters.go b/internal/service/advancedcluster/data_source_advanced_clusters.go index 867b0b5cdd..c6da6fbfc0 100644 --- a/internal/service/advancedcluster/data_source_advanced_clusters.go +++ b/internal/service/advancedcluster/data_source_advanced_clusters.go @@ -6,13 +6,16 @@ import ( "log" "net/http" + admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" + "go.mongodb.org/atlas-sdk/v20240530001/admin" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" - "go.mongodb.org/atlas-sdk/v20231115014/admin" ) func PluralDataSource() *schema.Resource { @@ -246,6 +249,7 @@ func PluralDataSource() *schema.Resource { func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { connV2 := meta.(*config.MongoDBClient).AtlasV2 + connLatest := meta.(*config.MongoDBClient).AtlasV2Preview projectID := d.Get("project_id").(string) d.SetId(id.UniqueId()) @@ -256,14 +260,14 @@ func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any) } return diag.FromErr(fmt.Errorf("error reading advanced cluster list for project(%s): %s", projectID, err)) } - if err := d.Set("results", flattenAdvancedClusters(ctx, connV2, list.GetResults(), d)); err != nil { + if err := d.Set("results", flattenAdvancedClusters(ctx, connV2, connLatest, list.GetResults(), d)); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "results", d.Id(), err)) } return nil } -func flattenAdvancedClusters(ctx context.Context, connV2 *admin.APIClient, clusters []admin.AdvancedClusterDescription, d *schema.ResourceData) []map[string]any { +func flattenAdvancedClusters(ctx context.Context, connV2 *admin20231115.APIClient, connLatest *admin.APIClient, clusters []admin20231115.AdvancedClusterDescription, d *schema.ResourceData) []map[string]any { results := make([]map[string]any, 0, len(clusters)) for i := range clusters { cluster := &clusters[i] @@ -271,7 +275,7 @@ func flattenAdvancedClusters(ctx context.Context, connV2 *admin.APIClient, clust if err != nil { log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err) } - replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), nil, d, connV2) + replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), nil, d, connLatest) if err != nil { log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err) } @@ -279,13 +283,13 @@ func flattenAdvancedClusters(ctx context.Context, connV2 *admin.APIClient, clust result := map[string]any{ "advanced_configuration": flattenProcessArgs(processArgs), "backup_enabled": cluster.GetBackupEnabled(), - "bi_connector_config": flattenBiConnectorConfig(cluster.GetBiConnector()), + "bi_connector_config": flattenBiConnectorConfig(convertBiConnectToLatest(cluster.GetBiConnector())), "cluster_type": cluster.GetClusterType(), "create_date": conversion.TimePtrToStringPtr(cluster.CreateDate), - "connection_strings": flattenConnectionStrings(cluster.GetConnectionStrings()), + "connection_strings": flattenConnectionStrings(convertConnectionStringToLatest(cluster.GetConnectionStrings())), "disk_size_gb": cluster.GetDiskSizeGB(), "encryption_at_rest_provider": cluster.GetEncryptionAtRestProvider(), - "labels": flattenLabels(cluster.GetLabels()), + "labels": flattenLabels(convertLabelsToLatest(cluster.GetLabels())), "tags": conversion.FlattenTags(cluster.GetTags()), "mongo_db_major_version": cluster.GetMongoDBMajorVersion(), "mongo_db_version": cluster.GetMongoDBVersion(), diff --git a/internal/service/advancedcluster/model_advanced_cluster.go b/internal/service/advancedcluster/model_advanced_cluster.go index 48de83f807..ba4575f504 100644 --- a/internal/service/advancedcluster/model_advanced_cluster.go +++ b/internal/service/advancedcluster/model_advanced_cluster.go @@ -9,14 +9,16 @@ import ( "slices" "strings" + admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" + "go.mongodb.org/atlas-sdk/v20240530001/admin" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/spf13/cast" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" - "github.com/spf13/cast" - admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" - "go.mongodb.org/atlas-sdk/v20240530001/admin" ) var ( @@ -340,7 +342,7 @@ func FormatMongoDBMajorVersion(val any) string { return fmt.Sprintf("%.1f", cast.ToFloat32(val)) } -func flattenLabels(l []admin20231115.ComponentLabel) []map[string]string { +func flattenLabels(l []admin.ComponentLabel) []map[string]string { labels := make([]map[string]string, 0, len(l)) for _, item := range l { if item.GetKey() == ignoreLabel { @@ -354,7 +356,18 @@ func flattenLabels(l []admin20231115.ComponentLabel) []map[string]string { return labels } -func flattenConnectionStrings(str admin20231115.ClusterConnectionStrings) []map[string]any { +func flattenTags(tags []admin.ResourceTag) []map[string]string { + ret := make([]map[string]string, len(tags)) + for i, tag := range tags { + ret[i] = map[string]string{ + "key": tag.GetKey(), + "value": tag.GetValue(), + } + } + return ret +} + +func flattenConnectionStrings(str admin.ClusterConnectionStrings) []map[string]any { return []map[string]any{ { "standard": str.GetStandard(), @@ -366,7 +379,7 @@ func flattenConnectionStrings(str admin20231115.ClusterConnectionStrings) []map[ } } -func flattenPrivateEndpoint(privateEndpoints []admin20231115.ClusterDescriptionConnectionStringsPrivateEndpoint) []map[string]any { +func flattenPrivateEndpoint(privateEndpoints []admin.ClusterDescriptionConnectionStringsPrivateEndpoint) []map[string]any { endpoints := make([]map[string]any, 0, len(privateEndpoints)) for _, endpoint := range privateEndpoints { endpoints = append(endpoints, map[string]any{ @@ -380,7 +393,7 @@ func flattenPrivateEndpoint(privateEndpoints []admin20231115.ClusterDescriptionC return endpoints } -func flattenEndpoints(listEndpoints []admin20231115.ClusterDescriptionConnectionStringsPrivateEndpointEndpoint) []map[string]any { +func flattenEndpoints(listEndpoints []admin.ClusterDescriptionConnectionStringsPrivateEndpointEndpoint) []map[string]any { endpoints := make([]map[string]any, 0, len(listEndpoints)) for _, endpoint := range listEndpoints { endpoints = append(endpoints, map[string]any{ @@ -392,7 +405,7 @@ func flattenEndpoints(listEndpoints []admin20231115.ClusterDescriptionConnection return endpoints } -func flattenBiConnectorConfig(biConnector admin20231115.BiConnector) []map[string]any { +func flattenBiConnectorConfig(biConnector admin.BiConnector) []map[string]any { return []map[string]any{ { "enabled": biConnector.GetEnabled(), @@ -435,8 +448,8 @@ func flattenProcessArgs(p *admin20231115.ClusterDescriptionProcessArgs) []map[st } } -func FlattenAdvancedReplicationSpecs(ctx context.Context, apiObjects []admin20231115.ReplicationSpec, tfMapObjects []any, - d *schema.ResourceData, connV2 *admin20231115.APIClient) ([]map[string]any, error) { +func FlattenAdvancedReplicationSpecsOldSDK(ctx context.Context, apiObjects []admin20231115.ReplicationSpec, tfMapObjects []any, + d *schema.ResourceData, connV2 *admin.APIClient) ([]map[string]any, error) { if len(apiObjects) == 0 { return nil, nil } @@ -456,7 +469,7 @@ func FlattenAdvancedReplicationSpecs(ctx context.Context, apiObjects []admin2023 continue } - advancedReplicationSpec, err := flattenAdvancedReplicationSpec(ctx, &apiObjects[j], tfMapObject, d, connV2) + advancedReplicationSpec, err := flattenAdvancedReplicationSpecOldSDK(ctx, &apiObjects[j], tfMapObject, d, connV2) if err != nil { return nil, err @@ -480,7 +493,7 @@ func FlattenAdvancedReplicationSpecs(ctx context.Context, apiObjects []admin2023 } j := slices.IndexFunc(wasAPIObjectUsed, func(isUsed bool) bool { return !isUsed }) - advancedReplicationSpec, err := flattenAdvancedReplicationSpec(ctx, &apiObjects[j], tfMapObject, d, connV2) + advancedReplicationSpec, err := flattenAdvancedReplicationSpecOldSDK(ctx, &apiObjects[j], tfMapObject, d, connV2) if err != nil { return nil, err @@ -497,8 +510,8 @@ func doesAdvancedReplicationSpecMatchAPI(tfObject map[string]any, apiObject *adm return tfObject["id"] == apiObject.GetId() || (tfObject["id"] == nil && tfObject["zone_name"] == apiObject.GetZoneName()) } -func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *admin20231115.ReplicationSpec, tfMapObject map[string]any, - d *schema.ResourceData, connV2 *admin20231115.APIClient) (map[string]any, error) { +func flattenAdvancedReplicationSpecOldSDK(ctx context.Context, apiObject *admin20231115.ReplicationSpec, tfMapObject map[string]any, + d *schema.ResourceData, connV2 *admin.APIClient) (map[string]any, error) { if apiObject == nil { return nil, nil } @@ -507,14 +520,14 @@ func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *admin2023111 tfMap["num_shards"] = apiObject.GetNumShards() tfMap["id"] = apiObject.GetId() if tfMapObject != nil { - object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), tfMapObject["region_configs"].([]any), d, connV2) + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, *convertRegionConfigSliceToLatest(apiObject.RegionConfigs), tfMapObject["region_configs"].([]any), d, connV2) if err != nil { return nil, err } tfMap["region_configs"] = object tfMap["container_id"] = containerIDs } else { - object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), nil, d, connV2) + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, *convertRegionConfigSliceToLatest(apiObject.RegionConfigs), nil, d, connV2) if err != nil { return nil, err } @@ -526,8 +539,8 @@ func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *admin2023111 return tfMap, nil } -func flattenAdvancedReplicationSpecRegionConfigs(ctx context.Context, apiObjects []admin20231115.CloudRegionConfig, tfMapObjects []any, - d *schema.ResourceData, connV2 *admin20231115.APIClient) (tfResult []map[string]any, containersIDs map[string]string, err error) { +func flattenAdvancedReplicationSpecRegionConfigs(ctx context.Context, apiObjects []admin.CloudRegionConfig, tfMapObjects []any, + d *schema.ResourceData, connV2 *admin.APIClient) (tfResult []map[string]any, containersIDs map[string]string, err error) { if len(apiObjects) == 0 { return nil, nil, nil } @@ -545,7 +558,7 @@ func flattenAdvancedReplicationSpecRegionConfigs(ctx context.Context, apiObjects } if apiObject.GetProviderName() != "TENANT" { - params := &admin20231115.ListPeeringContainerByCloudProviderApiParams{ + params := &admin.ListPeeringContainerByCloudProviderApiParams{ GroupId: d.Get("project_id").(string), ProviderName: apiObject.ProviderName, } @@ -562,7 +575,7 @@ func flattenAdvancedReplicationSpecRegionConfigs(ctx context.Context, apiObjects return tfList, containerIDs, nil } -func flattenAdvancedReplicationSpecRegionConfig(apiObject *admin20231115.CloudRegionConfig, tfMapObject map[string]any) map[string]any { +func flattenAdvancedReplicationSpecRegionConfig(apiObject *admin.CloudRegionConfig, tfMapObject map[string]any) map[string]any { if apiObject == nil { return nil } @@ -600,11 +613,11 @@ func flattenAdvancedReplicationSpecRegionConfig(apiObject *admin20231115.CloudRe return tfMap } -func hwSpecToDedicatedHwSpec(apiObject *admin20231115.HardwareSpec) *admin20231115.DedicatedHardwareSpec { +func hwSpecToDedicatedHwSpec(apiObject *admin.HardwareSpec) *admin.DedicatedHardwareSpec { if apiObject == nil { return nil } - return &admin20231115.DedicatedHardwareSpec{ + return &admin.DedicatedHardwareSpec{ NodeCount: apiObject.NodeCount, DiskIOPS: apiObject.DiskIOPS, EbsVolumeType: apiObject.EbsVolumeType, @@ -625,7 +638,7 @@ func dedicatedHwSpecToHwSpec(apiObject *admin.DedicatedHardwareSpec) *admin.Hard } } -func flattenAdvancedReplicationSpecRegionConfigSpec(apiObject *admin20231115.DedicatedHardwareSpec, providerName string, tfMapObjects []any) []map[string]any { +func flattenAdvancedReplicationSpecRegionConfigSpec(apiObject *admin.DedicatedHardwareSpec, providerName string, tfMapObjects []any) []map[string]any { if apiObject == nil { return nil } @@ -644,6 +657,9 @@ func flattenAdvancedReplicationSpecRegionConfigSpec(apiObject *admin20231115.Ded tfMap["ebs_volume_type"] = apiObject.GetEbsVolumeType() } } + if _, ok := tfMapObject["disk_size_gb"]; ok { + tfMap["disk_size_gb"] = apiObject.GetDiskSizeGB() + } if _, ok := tfMapObject["node_count"]; ok { tfMap["node_count"] = apiObject.GetNodeCount() } @@ -652,6 +668,7 @@ func flattenAdvancedReplicationSpecRegionConfigSpec(apiObject *admin20231115.Ded tfList = append(tfList, tfMap) } } else { + tfMap["disk_size_gb"] = apiObject.GetDiskSizeGB() tfMap["disk_iops"] = apiObject.GetDiskIOPS() tfMap["ebs_volume_type"] = apiObject.GetEbsVolumeType() tfMap["node_count"] = apiObject.GetNodeCount() @@ -661,7 +678,7 @@ func flattenAdvancedReplicationSpecRegionConfigSpec(apiObject *admin20231115.Ded return tfList } -func flattenAdvancedReplicationSpecAutoScaling(apiObject *admin20231115.AdvancedAutoScalingSettings) []map[string]any { +func flattenAdvancedReplicationSpecAutoScaling(apiObject *admin.AdvancedAutoScalingSettings) []map[string]any { if apiObject == nil { return nil } @@ -680,7 +697,7 @@ func flattenAdvancedReplicationSpecAutoScaling(apiObject *admin20231115.Advanced return tfList } -func getAdvancedClusterContainerID(containers []admin20231115.CloudProviderContainer, cluster *admin20231115.CloudRegionConfig) string { +func getAdvancedClusterContainerID(containers []admin.CloudProviderContainer, cluster *admin.CloudRegionConfig) string { if len(containers) == 0 { return "" } diff --git a/internal/service/advancedcluster/model_advanced_cluster_test.go b/internal/service/advancedcluster/model_advanced_cluster_test.go index a63e0e648b..be9a8283f8 100644 --- a/internal/service/advancedcluster/model_advanced_cluster_test.go +++ b/internal/service/advancedcluster/model_advanced_cluster_test.go @@ -6,14 +6,18 @@ import ( "net/http" "testing" + "go.mongodb.org/atlas-sdk/v20231115014/admin" + "go.mongodb.org/atlas-sdk/v20231115014/mockadmin" + adminLatest "go.mongodb.org/atlas-sdk/v20240530001/admin" + mockAdminLatest "go.mongodb.org/atlas-sdk/v20240530001/mockadmin" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "go.mongodb.org/atlas-sdk/v20231115014/admin" - "go.mongodb.org/atlas-sdk/v20231115014/mockadmin" + + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster" ) var ( @@ -122,18 +126,18 @@ func TestFlattenReplicationSpecs(t *testing.T) { } for name, tc := range testCases { t.Run(name, func(t *testing.T) { - peeringAPI := mockadmin.NetworkPeeringApi{} + peeringAPI := mockAdminLatest.NetworkPeeringApi{} - peeringAPI.EXPECT().ListPeeringContainerByCloudProviderWithParams(mock.Anything, mock.Anything).Return(admin.ListPeeringContainerByCloudProviderApiRequest{ApiService: &peeringAPI}) - containerResult := []admin.CloudProviderContainer{{Id: conversion.StringPtr("c1"), RegionName: ®ionName, ProviderName: &providerName}} - peeringAPI.EXPECT().ListPeeringContainerByCloudProviderExecute(mock.Anything).Return(&admin.PaginatedCloudProviderContainer{Results: &containerResult}, nil, nil) + peeringAPI.EXPECT().ListPeeringContainerByCloudProviderWithParams(mock.Anything, mock.Anything).Return(adminLatest.ListPeeringContainerByCloudProviderApiRequest{ApiService: &peeringAPI}) + containerResult := []adminLatest.CloudProviderContainer{{Id: conversion.StringPtr("c1"), RegionName: ®ionName, ProviderName: &providerName}} + peeringAPI.EXPECT().ListPeeringContainerByCloudProviderExecute(mock.Anything).Return(&adminLatest.PaginatedCloudProviderContainer{Results: &containerResult}, nil, nil) - client := &admin.APIClient{ + client := &adminLatest.APIClient{ NetworkPeeringApi: &peeringAPI, } resourceData := schema.TestResourceDataRaw(t, testSchema, map[string]any{"project_id": "p1"}) - tfOutputSpecs, err := advancedcluster.FlattenAdvancedReplicationSpecs(context.Background(), tc.adminSpecs, tc.tfInputSpecs, resourceData, client) + tfOutputSpecs, err := advancedcluster.FlattenAdvancedReplicationSpecsOldSDK(context.Background(), tc.adminSpecs, tc.tfInputSpecs, resourceData, client) require.NoError(t, err) assert.Len(t, tfOutputSpecs, tc.expectedLen) diff --git a/internal/service/advancedcluster/model_sdk_version_conversion.go b/internal/service/advancedcluster/model_sdk_version_conversion.go index 355da41876..2338a4f2a8 100644 --- a/internal/service/advancedcluster/model_sdk_version_conversion.go +++ b/internal/service/advancedcluster/model_sdk_version_conversion.go @@ -1,9 +1,10 @@ package advancedcluster import ( - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" "go.mongodb.org/atlas-sdk/v20240530001/admin" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" ) // Conversions from one SDK model version to another are used to avoid duplicating our flatten/expand conversion functions. @@ -36,6 +37,73 @@ func convertBiConnectToOldSDK(biconnector *admin.BiConnector) *admin20231115.BiC } } +func convertBiConnectToLatest(biconnector admin20231115.BiConnector) admin.BiConnector { + return admin.BiConnector{ + Enabled: biconnector.Enabled, + ReadPreference: biconnector.ReadPreference, + } +} + +func convertConnectionStringToLatest(connStrings admin20231115.ClusterConnectionStrings) admin.ClusterConnectionStrings { + return admin.ClusterConnectionStrings{ + AwsPrivateLink: connStrings.AwsPrivateLink, + AwsPrivateLinkSrv: connStrings.AwsPrivateLinkSrv, + Private: connStrings.Private, + PrivateEndpoint: convertPrivateEndpointToLatest(connStrings.PrivateEndpoint), + PrivateSrv: connStrings.PrivateSrv, + Standard: connStrings.Standard, + StandardSrv: connStrings.StandardSrv, + } +} + +func convertPrivateEndpointToLatest(privateEndpoints *[]admin20231115.ClusterDescriptionConnectionStringsPrivateEndpoint) *[]admin.ClusterDescriptionConnectionStringsPrivateEndpoint { + if privateEndpoints == nil { + return nil + } + peSlice := *privateEndpoints + results := make([]admin.ClusterDescriptionConnectionStringsPrivateEndpoint, len(peSlice)) + for i := range len(peSlice) { + pe := peSlice[i] + results[i] = admin.ClusterDescriptionConnectionStringsPrivateEndpoint{ + ConnectionString: pe.ConnectionString, + Endpoints: convertEndpointsToLatest(pe.Endpoints), + SrvConnectionString: pe.SrvConnectionString, + SrvShardOptimizedConnectionString: pe.SrvShardOptimizedConnectionString, + Type: pe.Type, + } + } + return &results +} + +func convertEndpointsToLatest(privateEndpoints *[]admin20231115.ClusterDescriptionConnectionStringsPrivateEndpointEndpoint) *[]admin.ClusterDescriptionConnectionStringsPrivateEndpointEndpoint { + if privateEndpoints == nil { + return nil + } + peSlice := *privateEndpoints + results := make([]admin.ClusterDescriptionConnectionStringsPrivateEndpointEndpoint, len(peSlice)) + for i := range len(peSlice) { + pe := peSlice[i] + results[i] = admin.ClusterDescriptionConnectionStringsPrivateEndpointEndpoint{ + EndpointId: pe.EndpointId, + ProviderName: pe.ProviderName, + Region: pe.Region, + } + } + return &results +} + +func convertLabelsToLatest(labels []admin20231115.ComponentLabel) []admin.ComponentLabel { + results := make([]admin.ComponentLabel, len(labels)) + for i := range len(labels) { + label := labels[i] + results[i] = admin.ComponentLabel{ + Key: label.Key, + Value: label.Value, + } + } + return results +} + func convertLabelSliceToOldSDK(slice []admin.ComponentLabel, err diag.Diagnostics) ([]admin20231115.ComponentLabel, diag.Diagnostics) { if err != nil { return nil, err @@ -128,3 +196,81 @@ func convertDedicatedHardwareSpecToOldSDK(spec *admin.DedicatedHardwareSpec) *ad InstanceSize: spec.InstanceSize, } } + +func convertDedicatedHwSpecToLatest(spec *admin20231115.DedicatedHardwareSpec) *admin.DedicatedHardwareSpec { + if spec == nil { + return nil + } + return &admin.DedicatedHardwareSpec{ + NodeCount: spec.NodeCount, + DiskIOPS: spec.DiskIOPS, + EbsVolumeType: spec.EbsVolumeType, + InstanceSize: spec.InstanceSize, + } +} + +func convertAdvancedAutoScalingSettingsToLatest(settings *admin20231115.AdvancedAutoScalingSettings) *admin.AdvancedAutoScalingSettings { + if settings == nil { + return nil + } + return &admin.AdvancedAutoScalingSettings{ + Compute: convertAdvancedComputeAutoScalingToLatest(settings.Compute), + DiskGB: convertDiskGBAutoScalingToLatest(settings.DiskGB), + } +} + +func convertAdvancedComputeAutoScalingToLatest(settings *admin20231115.AdvancedComputeAutoScaling) *admin.AdvancedComputeAutoScaling { + if settings == nil { + return nil + } + return &admin.AdvancedComputeAutoScaling{ + Enabled: settings.Enabled, + MaxInstanceSize: settings.MaxInstanceSize, + MinInstanceSize: settings.MinInstanceSize, + ScaleDownEnabled: settings.ScaleDownEnabled, + } +} + +func convertDiskGBAutoScalingToLatest(settings *admin20231115.DiskGBAutoScaling) *admin.DiskGBAutoScaling { + if settings == nil { + return nil + } + return &admin.DiskGBAutoScaling{ + Enabled: settings.Enabled, + } +} + +func convertHardwareSpecToLatest(hwspec *admin20231115.HardwareSpec) *admin.HardwareSpec { + if hwspec == nil { + return nil + } + return &admin.HardwareSpec{ + DiskIOPS: hwspec.DiskIOPS, + EbsVolumeType: hwspec.EbsVolumeType, + InstanceSize: hwspec.InstanceSize, + NodeCount: hwspec.NodeCount, + } +} + +func convertRegionConfigSliceToLatest(slice *[]admin20231115.CloudRegionConfig) *[]admin.CloudRegionConfig { + if slice == nil { + return nil + } + cloudRegionSlice := *slice + results := make([]admin.CloudRegionConfig, len(cloudRegionSlice)) + for i := range len(cloudRegionSlice) { + cloudRegion := cloudRegionSlice[i] + results[i] = admin.CloudRegionConfig{ + ElectableSpecs: convertHardwareSpecToLatest(cloudRegion.ElectableSpecs), + Priority: cloudRegion.Priority, + ProviderName: cloudRegion.ProviderName, + RegionName: cloudRegion.RegionName, + AnalyticsAutoScaling: convertAdvancedAutoScalingSettingsToLatest(cloudRegion.AnalyticsAutoScaling), + AnalyticsSpecs: convertDedicatedHwSpecToLatest(cloudRegion.AnalyticsSpecs), + AutoScaling: convertAdvancedAutoScalingSettingsToLatest(cloudRegion.AutoScaling), + ReadOnlySpecs: convertDedicatedHwSpecToLatest(cloudRegion.ReadOnlySpecs), + BackingProviderName: cloudRegion.BackingProviderName, + } + } + return &results +} diff --git a/internal/service/advancedcluster/rep_spec.go b/internal/service/advancedcluster/rep_spec.go new file mode 100644 index 0000000000..06f4b2106f --- /dev/null +++ b/internal/service/advancedcluster/rep_spec.go @@ -0,0 +1,66 @@ +package advancedcluster + +import ( + "context" + + "go.mongodb.org/atlas-sdk/v20240530001/admin" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// func FlattenAdvancedReplicationSpecsDS0710(ctx context.Context, apiRepSpecs []admin.ReplicationSpec20240710, connV2 *admin.APIClient) ([]map[string]any, error) { +// if len(apiRepSpecs) == 0 { +// return nil, nil +// } + +// tfList := make([]map[string]any, len(apiRepSpecs)) + +// for i, apiRepSpec := range apiRepSpecs { +// tfListObj := map[string]any{ +// "external_id": apiRepSpec.GetId(), +// "zone_id": apiRepSpec.GetZoneId(), +// "zone_name": apiRepSpec.GetZoneName(), +// // "region_configs":apiRepSpec.RegionConfigs, +// // "container_id": flattenEndpoints(endpoint.GetEndpoints()), +// } +// object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigsDS0710(ctx, apiRepSpec.GetRegionConfigs(), connV2) +// if err != nil { +// return nil, err +// } + +// tfListObj["region_configs"] = object +// tfListObj["container_id"] = containerIDs + +// tfList[i] = tfListObj +// } +// return tfList, nil +// } + +func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *admin.ReplicationSpec20240710, tfMapObject map[string]any, + d *schema.ResourceData, connV2 *admin.APIClient) (map[string]any, error) { + if apiObject == nil { + return nil, nil + } + + tfMap := map[string]any{} + tfMap["external_id"] = apiObject.GetId() + if tfMapObject != nil { + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), tfMapObject["region_configs"].([]any), d, connV2) + if err != nil { + return nil, err + } + tfMap["region_configs"] = object + tfMap["container_id"] = containerIDs + } else { + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), nil, d, connV2) + if err != nil { + return nil, err + } + tfMap["region_configs"] = object + tfMap["container_id"] = containerIDs + } + tfMap["zone_name"] = apiObject.GetZoneName() + tfMap["zone_id"] = apiObject.GetZoneId() + + return tfMap, nil +} diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index 7249367a05..500707ea5b 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -12,17 +12,19 @@ import ( "strings" "time" + admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" + "go.mongodb.org/atlas-sdk/v20240530001/admin" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/spf13/cast" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" - "github.com/spf13/cast" - admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" - "go.mongodb.org/atlas-sdk/v20240530001/admin" ) const ( @@ -339,6 +341,11 @@ func schemaSpecs() *schema.Schema { Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ + "disk_size_gb": { // new API only + Type: schema.TypeFloat, + Optional: true, + Computed: true, + }, "disk_iops": { Type: schema.TypeInt, Optional: true, @@ -484,6 +491,7 @@ func CreateStateChangeConfig(ctx context.Context, connV2 *admin20231115.APIClien func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { connV2 := meta.(*config.MongoDBClient).AtlasV2 + connPreview := meta.(*config.MongoDBClient).AtlasV2Preview ids := conversion.DecodeStateID(d.Id()) projectID := ids["project_id"] clusterName := ids["cluster_name"] @@ -505,7 +513,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "backup_enabled", clusterName, err)) } - if err := d.Set("bi_connector_config", flattenBiConnectorConfig(cluster.GetBiConnector())); err != nil { + if err := d.Set("bi_connector_config", flattenBiConnectorConfig(convertBiConnectToLatest(cluster.GetBiConnector()))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "bi_connector_config", clusterName, err)) } @@ -513,7 +521,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "cluster_type", clusterName, err)) } - if err := d.Set("connection_strings", flattenConnectionStrings(cluster.GetConnectionStrings())); err != nil { + if err := d.Set("connection_strings", flattenConnectionStrings(convertConnectionStringToLatest(cluster.GetConnectionStrings()))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "connection_strings", clusterName, err)) } @@ -529,7 +537,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "encryption_at_rest_provider", clusterName, err)) } - if err := d.Set("labels", flattenLabels(cluster.GetLabels())); err != nil { + if err := d.Set("labels", flattenLabels(convertLabelsToLatest(cluster.GetLabels()))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "labels", clusterName, err)) } @@ -557,7 +565,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err)) } - replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV2) + replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connPreview) if err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) } From e3e4b77317d9c518557f4015447f03f2cb2f28ff Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Thu, 27 Jun 2024 13:21:21 +0100 Subject: [PATCH 2/8] update singulat data source to use new API --- .../data_source_advanced_cluster.go | 121 +++++++++++------- .../data_source_advanced_clusters.go | 6 +- .../advancedcluster/model_advanced_cluster.go | 55 +++++++- .../model_sdk_version_conversion.go | 46 +++++-- internal/service/advancedcluster/rep_spec.go | 66 ---------- .../resource_advanced_cluster.go | 6 +- 6 files changed, 171 insertions(+), 129 deletions(-) delete mode 100644 internal/service/advancedcluster/rep_spec.go diff --git a/internal/service/advancedcluster/data_source_advanced_cluster.go b/internal/service/advancedcluster/data_source_advanced_cluster.go index 8d87dc5b40..f3570c2e32 100644 --- a/internal/service/advancedcluster/data_source_advanced_cluster.go +++ b/internal/service/advancedcluster/data_source_advanced_cluster.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" + "go.mongodb.org/atlas-sdk/v20240530001/admin" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -21,7 +23,7 @@ func DataSource() *schema.Resource { Type: schema.TypeString, Required: true, }, - "use_independent_shards": { + "use_replication_spec_per_shard": { Type: schema.TypeBool, Optional: true, }, @@ -113,11 +115,11 @@ func DataSource() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "zone_id": { // new API only + "zone_id": { Type: schema.TypeString, Computed: true, }, - "external_id": { // new API only + "external_id": { Type: schema.TypeString, Computed: true, }, @@ -252,32 +254,87 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. projectID := d.Get("project_id").(string) clusterName := d.Get("name").(string) - // useIndependentShards := false + useReplicationSpecPerShard := false + var replicationSpecs []map[string]any + var clusterId string - // if v, ok := d.GetOk("use_independent_shards"); ok { - // useIndependentShards = v.(bool) - // } + if v, ok := d.GetOk("use_replication_spec_per_shard"); ok { + useReplicationSpecPerShard = v.(bool) + } - // if !useIndependentShards { - cluster, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() + if !useReplicationSpecPerShard { + clusterDescOld, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() + if err != nil { + if resp != nil && resp.StatusCode == http.StatusNotFound { + return nil + } + return diag.FromErr(fmt.Errorf(errorRead, clusterName, err)) + } - // } else { - // cluster, resp, err := connPreview.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() //var cluster *adminPreview.ClusterDescription20240710 + clusterId = clusterDescOld.GetId() - // } + replicationSpecs, err = FlattenAdvancedReplicationSpecsOldSDK(ctx, clusterDescOld.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connLatest) + if err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) + } - if err != nil { - if resp != nil && resp.StatusCode == http.StatusNotFound { - return nil + diags := setCommonSchemaFields(d, convertClusterDescToLatestExcludeRepSpecs(clusterDescOld)) + if diags.HasError() { + return diags + } + + if err := d.Set("disk_size_gb", clusterDescOld.GetDiskSizeGB()); err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err)) + } + + } else { + clusterDescLatest, resp, err := connLatest.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() + if err != nil { + if resp != nil && resp.StatusCode == http.StatusNotFound { + return nil + } + return diag.FromErr(fmt.Errorf(errorRead, clusterName, err)) + } + + clusterId = clusterDescLatest.GetId() + + replicationSpecs, err = flattenAdvancedReplicationSpecsDS(ctx, clusterDescLatest.GetReplicationSpecs(), d, connLatest) + if err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) + } + + diags := setCommonSchemaFields(d, clusterDescLatest) + if diags.HasError() { + return diags } - return diag.FromErr(fmt.Errorf(errorRead, clusterName, err)) } + if err := d.Set("replication_specs", replicationSpecs); err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) + } + + // TODO: update to use connLatest to call below API + processArgs, _, err := connV2.ClustersApi.GetClusterAdvancedConfiguration(ctx, projectID, clusterName).Execute() + if err != nil { + return diag.FromErr(fmt.Errorf(ErrorAdvancedConfRead, clusterName, err)) + } + + if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs)); err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "advanced_configuration", clusterName, err)) + } + + d.SetId(clusterId) + return nil +} + +func setCommonSchemaFields(d *schema.ResourceData, cluster *admin.ClusterDescription20240710) diag.Diagnostics { + clusterName := *cluster.Name + if err := d.Set("backup_enabled", cluster.GetBackupEnabled()); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "backup_enabled", clusterName, err)) } - if err := d.Set("bi_connector_config", flattenBiConnectorConfig(convertBiConnectToLatest(cluster.GetBiConnector()))); err != nil { + if err := d.Set("bi_connector_config", flattenBiConnectorConfig(cluster.BiConnector)); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "bi_connector_config", clusterName, err)) } @@ -285,7 +342,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "cluster_type", clusterName, err)) } - if err := d.Set("connection_strings", flattenConnectionStrings(convertConnectionStringToLatest(cluster.GetConnectionStrings()))); err != nil { + if err := d.Set("connection_strings", flattenConnectionStrings(*cluster.ConnectionStrings)); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "connection_strings", clusterName, err)) } @@ -293,19 +350,15 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "create_date", clusterName, err)) } - if err := d.Set("disk_size_gb", cluster.GetDiskSizeGB()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err)) - } - if err := d.Set("encryption_at_rest_provider", cluster.GetEncryptionAtRestProvider()); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "encryption_at_rest_provider", clusterName, err)) } - if err := d.Set("labels", flattenLabels(convertLabelsToLatest(cluster.GetLabels()))); err != nil { + if err := d.Set("labels", flattenLabels(*cluster.Labels)); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "labels", clusterName, err)) } - if err := d.Set("tags", flattenTags(*convertTagsToLatest(cluster.Tags))); err != nil { + if err := d.Set("tags", flattenTags(cluster.Tags)); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "tags", clusterName, err)) } @@ -329,15 +382,6 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err)) } - replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connLatest) - if err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) - } - - if err := d.Set("replication_specs", replicationSpecs); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) - } - if err := d.Set("root_cert_type", cluster.GetRootCertType()); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "state_name", clusterName, err)) } @@ -355,16 +399,5 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "global_cluster_self_managed_sharding", clusterName, err)) } - // TODO: update to use connLatest to call below API - processArgs, _, err := connV2.ClustersApi.GetClusterAdvancedConfiguration(ctx, projectID, clusterName).Execute() - if err != nil { - return diag.FromErr(fmt.Errorf(ErrorAdvancedConfRead, clusterName, err)) - } - - if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs)); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "advanced_configuration", clusterName, err)) - } - - d.SetId(cluster.GetId()) return nil } diff --git a/internal/service/advancedcluster/data_source_advanced_clusters.go b/internal/service/advancedcluster/data_source_advanced_clusters.go index c6da6fbfc0..64ff559f19 100644 --- a/internal/service/advancedcluster/data_source_advanced_clusters.go +++ b/internal/service/advancedcluster/data_source_advanced_clusters.go @@ -283,13 +283,13 @@ func flattenAdvancedClusters(ctx context.Context, connV2 *admin20231115.APIClien result := map[string]any{ "advanced_configuration": flattenProcessArgs(processArgs), "backup_enabled": cluster.GetBackupEnabled(), - "bi_connector_config": flattenBiConnectorConfig(convertBiConnectToLatest(cluster.GetBiConnector())), + "bi_connector_config": flattenBiConnectorConfig(convertBiConnectToLatest(cluster.BiConnector)), "cluster_type": cluster.GetClusterType(), "create_date": conversion.TimePtrToStringPtr(cluster.CreateDate), - "connection_strings": flattenConnectionStrings(convertConnectionStringToLatest(cluster.GetConnectionStrings())), + "connection_strings": flattenConnectionStrings(*convertConnectionStringToLatest(cluster.ConnectionStrings)), "disk_size_gb": cluster.GetDiskSizeGB(), "encryption_at_rest_provider": cluster.GetEncryptionAtRestProvider(), - "labels": flattenLabels(convertLabelsToLatest(cluster.GetLabels())), + "labels": flattenLabels(*convertLabelsToLatest(cluster.Labels)), "tags": conversion.FlattenTags(cluster.GetTags()), "mongo_db_major_version": cluster.GetMongoDBMajorVersion(), "mongo_db_version": cluster.GetMongoDBVersion(), diff --git a/internal/service/advancedcluster/model_advanced_cluster.go b/internal/service/advancedcluster/model_advanced_cluster.go index ba4575f504..8a4c3e1110 100644 --- a/internal/service/advancedcluster/model_advanced_cluster.go +++ b/internal/service/advancedcluster/model_advanced_cluster.go @@ -356,9 +356,10 @@ func flattenLabels(l []admin.ComponentLabel) []map[string]string { return labels } -func flattenTags(tags []admin.ResourceTag) []map[string]string { - ret := make([]map[string]string, len(tags)) - for i, tag := range tags { +func flattenTags(tags *[]admin.ResourceTag) []map[string]string { + tagSlice := *tags + ret := make([]map[string]string, len(tagSlice)) + for i, tag := range tagSlice { ret[i] = map[string]string{ "key": tag.GetKey(), "value": tag.GetValue(), @@ -405,7 +406,7 @@ func flattenEndpoints(listEndpoints []admin.ClusterDescriptionConnectionStringsP return endpoints } -func flattenBiConnectorConfig(biConnector admin.BiConnector) []map[string]any { +func flattenBiConnectorConfig(biConnector *admin.BiConnector) []map[string]any { return []map[string]any{ { "enabled": biConnector.GetEnabled(), @@ -950,3 +951,49 @@ func expandRegionConfigAutoScaling(tfList []any) *admin.AdvancedAutoScalingSetti } return &settings } + +func flattenAdvancedReplicationSpecsDS(ctx context.Context, apiRepSpecs []admin.ReplicationSpec20240710, d *schema.ResourceData, connV2 *admin.APIClient) ([]map[string]any, error) { + if len(apiRepSpecs) == 0 { + return nil, nil + } + + tfList := make([]map[string]any, len(apiRepSpecs)) + + for i, apiRepSpec := range apiRepSpecs { + tfReplicationSpec, err := flattenAdvancedReplicationSpec(ctx, &apiRepSpec, nil, d, connV2) + if err != nil { + return nil, err + } + tfList[i] = tfReplicationSpec + } + return tfList, nil +} + +func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *admin.ReplicationSpec20240710, tfMapObject map[string]any, + d *schema.ResourceData, connV2 *admin.APIClient) (map[string]any, error) { + if apiObject == nil { + return nil, nil + } + + tfMap := map[string]any{} + tfMap["external_id"] = apiObject.GetId() + if tfMapObject != nil { + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), tfMapObject["region_configs"].([]any), d, connV2) + if err != nil { + return nil, err + } + tfMap["region_configs"] = object + tfMap["container_id"] = containerIDs + } else { + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), nil, d, connV2) + if err != nil { + return nil, err + } + tfMap["region_configs"] = object + tfMap["container_id"] = containerIDs + } + tfMap["zone_name"] = apiObject.GetZoneName() + tfMap["zone_id"] = apiObject.GetZoneId() + + return tfMap, nil +} diff --git a/internal/service/advancedcluster/model_sdk_version_conversion.go b/internal/service/advancedcluster/model_sdk_version_conversion.go index 2338a4f2a8..b158f6f5cf 100644 --- a/internal/service/advancedcluster/model_sdk_version_conversion.go +++ b/internal/service/advancedcluster/model_sdk_version_conversion.go @@ -37,15 +37,15 @@ func convertBiConnectToOldSDK(biconnector *admin.BiConnector) *admin20231115.BiC } } -func convertBiConnectToLatest(biconnector admin20231115.BiConnector) admin.BiConnector { - return admin.BiConnector{ +func convertBiConnectToLatest(biconnector *admin20231115.BiConnector) *admin.BiConnector { + return &admin.BiConnector{ Enabled: biconnector.Enabled, ReadPreference: biconnector.ReadPreference, } } -func convertConnectionStringToLatest(connStrings admin20231115.ClusterConnectionStrings) admin.ClusterConnectionStrings { - return admin.ClusterConnectionStrings{ +func convertConnectionStringToLatest(connStrings *admin20231115.ClusterConnectionStrings) *admin.ClusterConnectionStrings { + return &admin.ClusterConnectionStrings{ AwsPrivateLink: connStrings.AwsPrivateLink, AwsPrivateLinkSrv: connStrings.AwsPrivateLinkSrv, Private: connStrings.Private, @@ -92,16 +92,17 @@ func convertEndpointsToLatest(privateEndpoints *[]admin20231115.ClusterDescripti return &results } -func convertLabelsToLatest(labels []admin20231115.ComponentLabel) []admin.ComponentLabel { - results := make([]admin.ComponentLabel, len(labels)) - for i := range len(labels) { - label := labels[i] +func convertLabelsToLatest(labels *[]admin20231115.ComponentLabel) *[]admin.ComponentLabel { + labelSlice := *labels + results := make([]admin.ComponentLabel, len(labelSlice)) + for i := range len(labelSlice) { + label := labelSlice[i] results[i] = admin.ComponentLabel{ Key: label.Key, Value: label.Value, } } - return results + return &results } func convertLabelSliceToOldSDK(slice []admin.ComponentLabel, err diag.Diagnostics) ([]admin20231115.ComponentLabel, diag.Diagnostics) { @@ -274,3 +275,30 @@ func convertRegionConfigSliceToLatest(slice *[]admin20231115.CloudRegionConfig) } return &results } + +func convertClusterDescToLatestExcludeRepSpecs(oldClusterDesc *admin20231115.AdvancedClusterDescription) *admin.ClusterDescription20240710 { + return &admin.ClusterDescription20240710{ + BackupEnabled: oldClusterDesc.BackupEnabled, + AcceptDataRisksAndForceReplicaSetReconfig: oldClusterDesc.AcceptDataRisksAndForceReplicaSetReconfig, + ClusterType: oldClusterDesc.ClusterType, + CreateDate: oldClusterDesc.CreateDate, + DiskWarmingMode: oldClusterDesc.DiskWarmingMode, + EncryptionAtRestProvider: oldClusterDesc.EncryptionAtRestProvider, + GlobalClusterSelfManagedSharding: oldClusterDesc.GlobalClusterSelfManagedSharding, + GroupId: oldClusterDesc.GroupId, + Id: oldClusterDesc.Id, + MongoDBMajorVersion: oldClusterDesc.MongoDBMajorVersion, + MongoDBVersion: oldClusterDesc.MongoDBVersion, + Name: oldClusterDesc.Name, + Paused: oldClusterDesc.Paused, + PitEnabled: oldClusterDesc.PitEnabled, + RootCertType: oldClusterDesc.RootCertType, + StateName: oldClusterDesc.StateName, + TerminationProtectionEnabled: oldClusterDesc.TerminationProtectionEnabled, + VersionReleaseSystem: oldClusterDesc.VersionReleaseSystem, + Tags: convertTagsToLatest(oldClusterDesc.Tags), + BiConnector: convertBiConnectToLatest(oldClusterDesc.BiConnector), + ConnectionStrings: convertConnectionStringToLatest(oldClusterDesc.ConnectionStrings), + Labels: convertLabelsToLatest(oldClusterDesc.Labels), + } +} diff --git a/internal/service/advancedcluster/rep_spec.go b/internal/service/advancedcluster/rep_spec.go deleted file mode 100644 index 06f4b2106f..0000000000 --- a/internal/service/advancedcluster/rep_spec.go +++ /dev/null @@ -1,66 +0,0 @@ -package advancedcluster - -import ( - "context" - - "go.mongodb.org/atlas-sdk/v20240530001/admin" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -) - -// func FlattenAdvancedReplicationSpecsDS0710(ctx context.Context, apiRepSpecs []admin.ReplicationSpec20240710, connV2 *admin.APIClient) ([]map[string]any, error) { -// if len(apiRepSpecs) == 0 { -// return nil, nil -// } - -// tfList := make([]map[string]any, len(apiRepSpecs)) - -// for i, apiRepSpec := range apiRepSpecs { -// tfListObj := map[string]any{ -// "external_id": apiRepSpec.GetId(), -// "zone_id": apiRepSpec.GetZoneId(), -// "zone_name": apiRepSpec.GetZoneName(), -// // "region_configs":apiRepSpec.RegionConfigs, -// // "container_id": flattenEndpoints(endpoint.GetEndpoints()), -// } -// object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigsDS0710(ctx, apiRepSpec.GetRegionConfigs(), connV2) -// if err != nil { -// return nil, err -// } - -// tfListObj["region_configs"] = object -// tfListObj["container_id"] = containerIDs - -// tfList[i] = tfListObj -// } -// return tfList, nil -// } - -func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *admin.ReplicationSpec20240710, tfMapObject map[string]any, - d *schema.ResourceData, connV2 *admin.APIClient) (map[string]any, error) { - if apiObject == nil { - return nil, nil - } - - tfMap := map[string]any{} - tfMap["external_id"] = apiObject.GetId() - if tfMapObject != nil { - object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), tfMapObject["region_configs"].([]any), d, connV2) - if err != nil { - return nil, err - } - tfMap["region_configs"] = object - tfMap["container_id"] = containerIDs - } else { - object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), nil, d, connV2) - if err != nil { - return nil, err - } - tfMap["region_configs"] = object - tfMap["container_id"] = containerIDs - } - tfMap["zone_name"] = apiObject.GetZoneName() - tfMap["zone_id"] = apiObject.GetZoneId() - - return tfMap, nil -} diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index 500707ea5b..ad25cce1fb 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -513,7 +513,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "backup_enabled", clusterName, err)) } - if err := d.Set("bi_connector_config", flattenBiConnectorConfig(convertBiConnectToLatest(cluster.GetBiConnector()))); err != nil { + if err := d.Set("bi_connector_config", flattenBiConnectorConfig(convertBiConnectToLatest(cluster.BiConnector))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "bi_connector_config", clusterName, err)) } @@ -521,7 +521,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "cluster_type", clusterName, err)) } - if err := d.Set("connection_strings", flattenConnectionStrings(convertConnectionStringToLatest(cluster.GetConnectionStrings()))); err != nil { + if err := d.Set("connection_strings", flattenConnectionStrings(*convertConnectionStringToLatest(cluster.ConnectionStrings))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "connection_strings", clusterName, err)) } @@ -537,7 +537,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "encryption_at_rest_provider", clusterName, err)) } - if err := d.Set("labels", flattenLabels(convertLabelsToLatest(cluster.GetLabels()))); err != nil { + if err := d.Set("labels", flattenLabels(*convertLabelsToLatest(cluster.Labels))); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "labels", clusterName, err)) } From c72e9130061244e97a1d7656b26e064e57e43fc3 Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Thu, 27 Jun 2024 13:26:22 +0100 Subject: [PATCH 3/8] lint fix --- .../advancedcluster/data_source_advanced_cluster.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/service/advancedcluster/data_source_advanced_cluster.go b/internal/service/advancedcluster/data_source_advanced_cluster.go index f3570c2e32..efa8cc46e8 100644 --- a/internal/service/advancedcluster/data_source_advanced_cluster.go +++ b/internal/service/advancedcluster/data_source_advanced_cluster.go @@ -256,7 +256,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. clusterName := d.Get("name").(string) useReplicationSpecPerShard := false var replicationSpecs []map[string]any - var clusterId string + var clusterID string if v, ok := d.GetOk("use_replication_spec_per_shard"); ok { useReplicationSpecPerShard = v.(bool) @@ -271,7 +271,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(errorRead, clusterName, err)) } - clusterId = clusterDescOld.GetId() + clusterID = clusterDescOld.GetId() replicationSpecs, err = FlattenAdvancedReplicationSpecsOldSDK(ctx, clusterDescOld.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connLatest) if err != nil { @@ -286,7 +286,6 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. if err := d.Set("disk_size_gb", clusterDescOld.GetDiskSizeGB()); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err)) } - } else { clusterDescLatest, resp, err := connLatest.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() if err != nil { @@ -296,7 +295,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(errorRead, clusterName, err)) } - clusterId = clusterDescLatest.GetId() + clusterID = clusterDescLatest.GetId() replicationSpecs, err = flattenAdvancedReplicationSpecsDS(ctx, clusterDescLatest.GetReplicationSpecs(), d, connLatest) if err != nil { @@ -323,7 +322,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "advanced_configuration", clusterName, err)) } - d.SetId(clusterId) + d.SetId(clusterID) return nil } From b4e85f183d1b83bc7ffa519b538da82f793a5c41 Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Thu, 27 Jun 2024 15:57:22 +0100 Subject: [PATCH 4/8] minor --- internal/service/advancedcluster/model_advanced_cluster.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/service/advancedcluster/model_advanced_cluster.go b/internal/service/advancedcluster/model_advanced_cluster.go index 8a4c3e1110..bf0162bb07 100644 --- a/internal/service/advancedcluster/model_advanced_cluster.go +++ b/internal/service/advancedcluster/model_advanced_cluster.go @@ -976,6 +976,7 @@ func flattenAdvancedReplicationSpec(ctx context.Context, apiObject *admin.Replic } tfMap := map[string]any{} + tfMap["id"] = apiObject.GetId() tfMap["external_id"] = apiObject.GetId() if tfMapObject != nil { object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, apiObject.GetRegionConfigs(), tfMapObject["region_configs"].([]any), d, connV2) From 8b7f8bf8b0c090b891bad8f9c9efebec0b158f31 Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Thu, 27 Jun 2024 16:00:04 +0100 Subject: [PATCH 5/8] minor --- internal/service/advancedcluster/resource_advanced_cluster.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index ad25cce1fb..f12a7f54fc 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -341,7 +341,7 @@ func schemaSpecs() *schema.Schema { Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "disk_size_gb": { // new API only + "disk_size_gb": { Type: schema.TypeFloat, Optional: true, Computed: true, From f3f19d3d114574ddfb080c353d9b78ff5d218040 Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Fri, 28 Jun 2024 10:32:21 +0100 Subject: [PATCH 6/8] address PR comments --- .../data_source_advanced_cluster.go | 9 +++++++-- .../advancedcluster/model_advanced_cluster.go | 9 +++++++-- .../model_sdk_version_conversion.go | 14 ++++++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/internal/service/advancedcluster/data_source_advanced_cluster.go b/internal/service/advancedcluster/data_source_advanced_cluster.go index efa8cc46e8..746c25787d 100644 --- a/internal/service/advancedcluster/data_source_advanced_cluster.go +++ b/internal/service/advancedcluster/data_source_advanced_cluster.go @@ -265,8 +265,13 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. if !useReplicationSpecPerShard { clusterDescOld, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() if err != nil { - if resp != nil && resp.StatusCode == http.StatusNotFound { - return nil + if resp != nil { + if resp.StatusCode == http.StatusNotFound { + return nil + } + if err.Error() == "ASYMMETRIC_SHARD_UNSUPPORTED" { + return diag.FromErr(fmt.Errorf("please add `use_replication_spec_per_shard = true` to your data source configuration to enable asymmetric shard support. Refer to documentation for more details. %s", err)) + } } return diag.FromErr(fmt.Errorf(errorRead, clusterName, err)) } diff --git a/internal/service/advancedcluster/model_advanced_cluster.go b/internal/service/advancedcluster/model_advanced_cluster.go index bf0162bb07..ba14faa4e4 100644 --- a/internal/service/advancedcluster/model_advanced_cluster.go +++ b/internal/service/advancedcluster/model_advanced_cluster.go @@ -516,19 +516,23 @@ func flattenAdvancedReplicationSpecOldSDK(ctx context.Context, apiObject *admin2 if apiObject == nil { return nil, nil } + var rootDiskSizeGB *float64 + if v, ok := d.GetOk("disk_size_gb"); ok { + rootDiskSizeGB = conversion.Pointer(v.(float64)) + } tfMap := map[string]any{} tfMap["num_shards"] = apiObject.GetNumShards() tfMap["id"] = apiObject.GetId() if tfMapObject != nil { - object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, *convertRegionConfigSliceToLatest(apiObject.RegionConfigs), tfMapObject["region_configs"].([]any), d, connV2) + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, *convertRegionConfigSliceToLatest(apiObject.RegionConfigs, rootDiskSizeGB), tfMapObject["region_configs"].([]any), d, connV2) if err != nil { return nil, err } tfMap["region_configs"] = object tfMap["container_id"] = containerIDs } else { - object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, *convertRegionConfigSliceToLatest(apiObject.RegionConfigs), nil, d, connV2) + object, containerIDs, err := flattenAdvancedReplicationSpecRegionConfigs(ctx, *convertRegionConfigSliceToLatest(apiObject.RegionConfigs, rootDiskSizeGB), nil, d, connV2) if err != nil { return nil, err } @@ -623,6 +627,7 @@ func hwSpecToDedicatedHwSpec(apiObject *admin.HardwareSpec) *admin.DedicatedHard DiskIOPS: apiObject.DiskIOPS, EbsVolumeType: apiObject.EbsVolumeType, InstanceSize: apiObject.InstanceSize, + DiskSizeGB: apiObject.DiskSizeGB, } } diff --git a/internal/service/advancedcluster/model_sdk_version_conversion.go b/internal/service/advancedcluster/model_sdk_version_conversion.go index b158f6f5cf..a396d8b124 100644 --- a/internal/service/advancedcluster/model_sdk_version_conversion.go +++ b/internal/service/advancedcluster/model_sdk_version_conversion.go @@ -198,7 +198,7 @@ func convertDedicatedHardwareSpecToOldSDK(spec *admin.DedicatedHardwareSpec) *ad } } -func convertDedicatedHwSpecToLatest(spec *admin20231115.DedicatedHardwareSpec) *admin.DedicatedHardwareSpec { +func convertDedicatedHwSpecToLatest(spec *admin20231115.DedicatedHardwareSpec, rootDiskSizeGB *float64) *admin.DedicatedHardwareSpec { if spec == nil { return nil } @@ -207,6 +207,7 @@ func convertDedicatedHwSpecToLatest(spec *admin20231115.DedicatedHardwareSpec) * DiskIOPS: spec.DiskIOPS, EbsVolumeType: spec.EbsVolumeType, InstanceSize: spec.InstanceSize, + DiskSizeGB: rootDiskSizeGB, } } @@ -241,7 +242,7 @@ func convertDiskGBAutoScalingToLatest(settings *admin20231115.DiskGBAutoScaling) } } -func convertHardwareSpecToLatest(hwspec *admin20231115.HardwareSpec) *admin.HardwareSpec { +func convertHardwareSpecToLatest(hwspec *admin20231115.HardwareSpec, rootDiskSizeGB *float64) *admin.HardwareSpec { if hwspec == nil { return nil } @@ -250,10 +251,11 @@ func convertHardwareSpecToLatest(hwspec *admin20231115.HardwareSpec) *admin.Hard EbsVolumeType: hwspec.EbsVolumeType, InstanceSize: hwspec.InstanceSize, NodeCount: hwspec.NodeCount, + DiskSizeGB: rootDiskSizeGB, } } -func convertRegionConfigSliceToLatest(slice *[]admin20231115.CloudRegionConfig) *[]admin.CloudRegionConfig { +func convertRegionConfigSliceToLatest(slice *[]admin20231115.CloudRegionConfig, rootDiskSizeGB *float64) *[]admin.CloudRegionConfig { if slice == nil { return nil } @@ -262,14 +264,14 @@ func convertRegionConfigSliceToLatest(slice *[]admin20231115.CloudRegionConfig) for i := range len(cloudRegionSlice) { cloudRegion := cloudRegionSlice[i] results[i] = admin.CloudRegionConfig{ - ElectableSpecs: convertHardwareSpecToLatest(cloudRegion.ElectableSpecs), + ElectableSpecs: convertHardwareSpecToLatest(cloudRegion.ElectableSpecs, rootDiskSizeGB), Priority: cloudRegion.Priority, ProviderName: cloudRegion.ProviderName, RegionName: cloudRegion.RegionName, AnalyticsAutoScaling: convertAdvancedAutoScalingSettingsToLatest(cloudRegion.AnalyticsAutoScaling), - AnalyticsSpecs: convertDedicatedHwSpecToLatest(cloudRegion.AnalyticsSpecs), + AnalyticsSpecs: convertDedicatedHwSpecToLatest(cloudRegion.AnalyticsSpecs, rootDiskSizeGB), AutoScaling: convertAdvancedAutoScalingSettingsToLatest(cloudRegion.AutoScaling), - ReadOnlySpecs: convertDedicatedHwSpecToLatest(cloudRegion.ReadOnlySpecs), + ReadOnlySpecs: convertDedicatedHwSpecToLatest(cloudRegion.ReadOnlySpecs, rootDiskSizeGB), BackingProviderName: cloudRegion.BackingProviderName, } } From d3f23cd6aad20539dd6326b770762ef60bd64192 Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Fri, 28 Jun 2024 11:24:00 +0100 Subject: [PATCH 7/8] minor --- .../advancedcluster/data_source_advanced_cluster.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/advancedcluster/data_source_advanced_cluster.go b/internal/service/advancedcluster/data_source_advanced_cluster.go index 746c25787d..00622da2ff 100644 --- a/internal/service/advancedcluster/data_source_advanced_cluster.go +++ b/internal/service/advancedcluster/data_source_advanced_cluster.go @@ -278,6 +278,10 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. clusterID = clusterDescOld.GetId() + if err := d.Set("disk_size_gb", clusterDescOld.GetDiskSizeGB()); err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err)) + } + replicationSpecs, err = FlattenAdvancedReplicationSpecsOldSDK(ctx, clusterDescOld.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connLatest) if err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) @@ -287,10 +291,6 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. if diags.HasError() { return diags } - - if err := d.Set("disk_size_gb", clusterDescOld.GetDiskSizeGB()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err)) - } } else { clusterDescLatest, resp, err := connLatest.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute() if err != nil { From 99d3b11aea4aca4e0d807d2b25d49435dcc668c5 Mon Sep 17 00:00:00 2001 From: Aastha Mahendru Date: Fri, 28 Jun 2024 16:21:44 +0100 Subject: [PATCH 8/8] address PR comments --- .../advancedcluster/data_source_advanced_cluster.go | 5 +++-- .../advancedcluster/data_source_advanced_clusters.go | 2 +- .../advancedcluster/model_advanced_cluster.go | 12 ++++-------- .../advancedcluster/model_advanced_cluster_test.go | 2 +- .../advancedcluster/model_sdk_version_conversion.go | 10 +++++----- .../advancedcluster/resource_advanced_cluster.go | 2 +- 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/internal/service/advancedcluster/data_source_advanced_cluster.go b/internal/service/advancedcluster/data_source_advanced_cluster.go index 00622da2ff..ae9b08be3b 100644 --- a/internal/service/advancedcluster/data_source_advanced_cluster.go +++ b/internal/service/advancedcluster/data_source_advanced_cluster.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" + admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" "go.mongodb.org/atlas-sdk/v20240530001/admin" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -269,7 +270,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. if resp.StatusCode == http.StatusNotFound { return nil } - if err.Error() == "ASYMMETRIC_SHARD_UNSUPPORTED" { + if admin20231115.IsErrorCode(err, "ASYMMETRIC_SHARD_UNSUPPORTED") { return diag.FromErr(fmt.Errorf("please add `use_replication_spec_per_shard = true` to your data source configuration to enable asymmetric shard support. Refer to documentation for more details. %s", err)) } } @@ -282,7 +283,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err)) } - replicationSpecs, err = FlattenAdvancedReplicationSpecsOldSDK(ctx, clusterDescOld.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connLatest) + replicationSpecs, err = FlattenAdvancedReplicationSpecsOldSDK(ctx, clusterDescOld.GetReplicationSpecs(), clusterDescOld.GetDiskSizeGB(), d.Get("replication_specs").([]any), d, connLatest) if err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) } diff --git a/internal/service/advancedcluster/data_source_advanced_clusters.go b/internal/service/advancedcluster/data_source_advanced_clusters.go index 64ff559f19..6a33cc3402 100644 --- a/internal/service/advancedcluster/data_source_advanced_clusters.go +++ b/internal/service/advancedcluster/data_source_advanced_clusters.go @@ -275,7 +275,7 @@ func flattenAdvancedClusters(ctx context.Context, connV2 *admin20231115.APIClien if err != nil { log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err) } - replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), nil, d, connLatest) + replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), cluster.GetDiskSizeGB(), nil, d, connLatest) if err != nil { log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err) } diff --git a/internal/service/advancedcluster/model_advanced_cluster.go b/internal/service/advancedcluster/model_advanced_cluster.go index ba14faa4e4..e7f208a1ea 100644 --- a/internal/service/advancedcluster/model_advanced_cluster.go +++ b/internal/service/advancedcluster/model_advanced_cluster.go @@ -449,7 +449,7 @@ func flattenProcessArgs(p *admin20231115.ClusterDescriptionProcessArgs) []map[st } } -func FlattenAdvancedReplicationSpecsOldSDK(ctx context.Context, apiObjects []admin20231115.ReplicationSpec, tfMapObjects []any, +func FlattenAdvancedReplicationSpecsOldSDK(ctx context.Context, apiObjects []admin20231115.ReplicationSpec, rootDiskSizeGB float64, tfMapObjects []any, d *schema.ResourceData, connV2 *admin.APIClient) ([]map[string]any, error) { if len(apiObjects) == 0 { return nil, nil @@ -470,7 +470,7 @@ func FlattenAdvancedReplicationSpecsOldSDK(ctx context.Context, apiObjects []adm continue } - advancedReplicationSpec, err := flattenAdvancedReplicationSpecOldSDK(ctx, &apiObjects[j], tfMapObject, d, connV2) + advancedReplicationSpec, err := flattenAdvancedReplicationSpecOldSDK(ctx, &apiObjects[j], rootDiskSizeGB, tfMapObject, d, connV2) if err != nil { return nil, err @@ -494,7 +494,7 @@ func FlattenAdvancedReplicationSpecsOldSDK(ctx context.Context, apiObjects []adm } j := slices.IndexFunc(wasAPIObjectUsed, func(isUsed bool) bool { return !isUsed }) - advancedReplicationSpec, err := flattenAdvancedReplicationSpecOldSDK(ctx, &apiObjects[j], tfMapObject, d, connV2) + advancedReplicationSpec, err := flattenAdvancedReplicationSpecOldSDK(ctx, &apiObjects[j], rootDiskSizeGB, tfMapObject, d, connV2) if err != nil { return nil, err @@ -511,15 +511,11 @@ func doesAdvancedReplicationSpecMatchAPI(tfObject map[string]any, apiObject *adm return tfObject["id"] == apiObject.GetId() || (tfObject["id"] == nil && tfObject["zone_name"] == apiObject.GetZoneName()) } -func flattenAdvancedReplicationSpecOldSDK(ctx context.Context, apiObject *admin20231115.ReplicationSpec, tfMapObject map[string]any, +func flattenAdvancedReplicationSpecOldSDK(ctx context.Context, apiObject *admin20231115.ReplicationSpec, rootDiskSizeGB float64, tfMapObject map[string]any, d *schema.ResourceData, connV2 *admin.APIClient) (map[string]any, error) { if apiObject == nil { return nil, nil } - var rootDiskSizeGB *float64 - if v, ok := d.GetOk("disk_size_gb"); ok { - rootDiskSizeGB = conversion.Pointer(v.(float64)) - } tfMap := map[string]any{} tfMap["num_shards"] = apiObject.GetNumShards() diff --git a/internal/service/advancedcluster/model_advanced_cluster_test.go b/internal/service/advancedcluster/model_advanced_cluster_test.go index be9a8283f8..556419372e 100644 --- a/internal/service/advancedcluster/model_advanced_cluster_test.go +++ b/internal/service/advancedcluster/model_advanced_cluster_test.go @@ -137,7 +137,7 @@ func TestFlattenReplicationSpecs(t *testing.T) { } resourceData := schema.TestResourceDataRaw(t, testSchema, map[string]any{"project_id": "p1"}) - tfOutputSpecs, err := advancedcluster.FlattenAdvancedReplicationSpecsOldSDK(context.Background(), tc.adminSpecs, tc.tfInputSpecs, resourceData, client) + tfOutputSpecs, err := advancedcluster.FlattenAdvancedReplicationSpecsOldSDK(context.Background(), tc.adminSpecs, 0, tc.tfInputSpecs, resourceData, client) require.NoError(t, err) assert.Len(t, tfOutputSpecs, tc.expectedLen) diff --git a/internal/service/advancedcluster/model_sdk_version_conversion.go b/internal/service/advancedcluster/model_sdk_version_conversion.go index a396d8b124..e6cc345d38 100644 --- a/internal/service/advancedcluster/model_sdk_version_conversion.go +++ b/internal/service/advancedcluster/model_sdk_version_conversion.go @@ -198,7 +198,7 @@ func convertDedicatedHardwareSpecToOldSDK(spec *admin.DedicatedHardwareSpec) *ad } } -func convertDedicatedHwSpecToLatest(spec *admin20231115.DedicatedHardwareSpec, rootDiskSizeGB *float64) *admin.DedicatedHardwareSpec { +func convertDedicatedHwSpecToLatest(spec *admin20231115.DedicatedHardwareSpec, rootDiskSizeGB float64) *admin.DedicatedHardwareSpec { if spec == nil { return nil } @@ -207,7 +207,7 @@ func convertDedicatedHwSpecToLatest(spec *admin20231115.DedicatedHardwareSpec, r DiskIOPS: spec.DiskIOPS, EbsVolumeType: spec.EbsVolumeType, InstanceSize: spec.InstanceSize, - DiskSizeGB: rootDiskSizeGB, + DiskSizeGB: &rootDiskSizeGB, } } @@ -242,7 +242,7 @@ func convertDiskGBAutoScalingToLatest(settings *admin20231115.DiskGBAutoScaling) } } -func convertHardwareSpecToLatest(hwspec *admin20231115.HardwareSpec, rootDiskSizeGB *float64) *admin.HardwareSpec { +func convertHardwareSpecToLatest(hwspec *admin20231115.HardwareSpec, rootDiskSizeGB float64) *admin.HardwareSpec { if hwspec == nil { return nil } @@ -251,11 +251,11 @@ func convertHardwareSpecToLatest(hwspec *admin20231115.HardwareSpec, rootDiskSiz EbsVolumeType: hwspec.EbsVolumeType, InstanceSize: hwspec.InstanceSize, NodeCount: hwspec.NodeCount, - DiskSizeGB: rootDiskSizeGB, + DiskSizeGB: &rootDiskSizeGB, } } -func convertRegionConfigSliceToLatest(slice *[]admin20231115.CloudRegionConfig, rootDiskSizeGB *float64) *[]admin.CloudRegionConfig { +func convertRegionConfigSliceToLatest(slice *[]admin20231115.CloudRegionConfig, rootDiskSizeGB float64) *[]admin.CloudRegionConfig { if slice == nil { return nil } diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index f12a7f54fc..c342b41065 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -565,7 +565,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err)) } - replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connPreview) + replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), cluster.GetDiskSizeGB(), d.Get("replication_specs").([]any), d, connPreview) if err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) }