Skip to content

Commit

Permalink
feat: Adjust mongodbatlas_advanced_cluster update operation to use …
Browse files Browse the repository at this point in the history
…latest sdk (#2814)

* change API call version

* removed temporary 20240805 functions and adjust create operation

* added changelog

* adjusted comments

* Implements review feedback
  • Loading branch information
cveticm authored Nov 22, 2024
1 parent e28ae0b commit 4200385
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 125 deletions.
3 changes: 3 additions & 0 deletions .changelog/2814.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/mongodbatlas_advanced_cluster: Adjusts update operation to support cluster tier auto scaling per shard.
```
14 changes: 0 additions & 14 deletions internal/common/conversion/flatten_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package conversion
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

admin20240805 "go.mongodb.org/atlas-sdk/v20240805005/admin"
"go.mongodb.org/atlas-sdk/v20241023002/admin"
)

Expand All @@ -29,19 +28,6 @@ func FlattenTags(tags []admin.ResourceTag) []map[string]string {
return ret
}

func ExpandTagsFromSetSchemaV220240805(d *schema.ResourceData) *[]admin20240805.ResourceTag {
list := d.Get("tags").(*schema.Set)
ret := make([]admin20240805.ResourceTag, list.Len())
for i, item := range list.List() {
tag := item.(map[string]any)
ret[i] = admin20240805.ResourceTag{
Key: tag["key"].(string),
Value: tag["value"].(string),
}
}
return &ret
}

func ExpandTagsFromSetSchema(d *schema.ResourceData) *[]admin.ResourceTag {
list := d.Get("tags").(*schema.Set)
ret := make([]admin.ResourceTag, list.Len())
Expand Down
45 changes: 22 additions & 23 deletions internal/service/advancedcluster/model_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

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"
Expand Down Expand Up @@ -401,7 +400,7 @@ func flattenTags(tags *[]admin.ResourceTag) []map[string]string {
}

// CheckRegionConfigsPriorityOrder will be deleted in CLOUDP-275825
func CheckRegionConfigsPriorityOrder(regionConfigs []admin20240805.ReplicationSpec20240805) error {
func CheckRegionConfigsPriorityOrder(regionConfigs []admin.ReplicationSpec20240805) error {
for _, spec := range regionConfigs {
configs := spec.GetRegionConfigs()
for i := 0; i < len(configs)-1; i++ {
Expand Down Expand Up @@ -473,11 +472,11 @@ func flattenBiConnectorConfig(biConnector *admin.BiConnector) []map[string]any {
}
}

func expandBiConnectorConfig(d *schema.ResourceData) *admin20240805.BiConnector {
func expandBiConnectorConfig(d *schema.ResourceData) *admin.BiConnector {
if v, ok := d.GetOk("bi_connector_config"); ok {
if biConn := v.([]any); len(biConn) > 0 {
biConnMap := biConn[0].(map[string]any)
return &admin20240805.BiConnector{
return &admin.BiConnector{
Enabled: conversion.Pointer(cast.ToBool(biConnMap["enabled"])),
ReadPreference: conversion.StringPtr(cast.ToString(biConnMap["read_preference"])),
}
Expand Down Expand Up @@ -695,11 +694,11 @@ func hwSpecToDedicatedHwSpec(apiObject *admin.HardwareSpec20240805) *admin.Dedic
}
}

func dedicatedHwSpecToHwSpec(apiObject *admin20240805.DedicatedHardwareSpec20240805) *admin20240805.HardwareSpec20240805 {
func dedicatedHwSpecToHwSpec(apiObject *admin.DedicatedHardwareSpec20240805) *admin.HardwareSpec20240805 {
if apiObject == nil {
return nil
}
return &admin20240805.HardwareSpec20240805{
return &admin.HardwareSpec20240805{
DiskSizeGB: apiObject.DiskSizeGB,
NodeCount: apiObject.NodeCount,
DiskIOPS: apiObject.DiskIOPS,
Expand Down Expand Up @@ -872,25 +871,25 @@ func IsChangeStreamOptionsMinRequiredMajorVersion(input *string) bool {
return value >= minVersionForChangeStreamOptions
}

func expandLabelSliceFromSetSchema(d *schema.ResourceData) ([]admin20240805.ComponentLabel, diag.Diagnostics) {
func expandLabelSliceFromSetSchema(d *schema.ResourceData) ([]admin.ComponentLabel, diag.Diagnostics) {
list := d.Get("labels").(*schema.Set)
res := make([]admin20240805.ComponentLabel, list.Len())
res := make([]admin.ComponentLabel, list.Len())
for i, val := range list.List() {
v := val.(map[string]any)
key := v["key"].(string)
if key == ignoreLabel {
return nil, diag.FromErr(fmt.Errorf("you should not set `Infrastructure Tool` label, it is used for internal purposes"))
}
res[i] = admin20240805.ComponentLabel{
res[i] = admin.ComponentLabel{
Key: conversion.StringPtr(key),
Value: conversion.StringPtr(v["value"].(string)),
}
}
return res, nil
}

func expandAdvancedReplicationSpecs(tfList []any, rootDiskSizeGB *float64) *[]admin20240805.ReplicationSpec20240805 {
var apiObjects []admin20240805.ReplicationSpec20240805
func expandAdvancedReplicationSpecs(tfList []any, rootDiskSizeGB *float64) *[]admin.ReplicationSpec20240805 {
var apiObjects []admin.ReplicationSpec20240805
for _, tfMapRaw := range tfList {
tfMap, ok := tfMapRaw.(map[string]any)
if !ok || tfMap == nil {
Expand Down Expand Up @@ -927,8 +926,8 @@ func expandAdvancedReplicationSpecsOldSDK(tfList []any) *[]admin20240530.Replica
return &apiObjects
}

func expandAdvancedReplicationSpec(tfMap map[string]any, rootDiskSizeGB *float64) *admin20240805.ReplicationSpec20240805 {
apiObject := &admin20240805.ReplicationSpec20240805{
func expandAdvancedReplicationSpec(tfMap map[string]any, rootDiskSizeGB *float64) *admin.ReplicationSpec20240805 {
apiObject := &admin.ReplicationSpec20240805{
ZoneName: conversion.StringPtr(tfMap["zone_name"].(string)),
RegionConfigs: expandRegionConfigs(tfMap["region_configs"].([]any), rootDiskSizeGB),
}
Expand All @@ -950,8 +949,8 @@ func expandAdvancedReplicationSpecOldSDK(tfMap map[string]any) *admin20240530.Re
return apiObject
}

func expandRegionConfigs(tfList []any, rootDiskSizeGB *float64) *[]admin20240805.CloudRegionConfig20240805 {
var apiObjects []admin20240805.CloudRegionConfig20240805
func expandRegionConfigs(tfList []any, rootDiskSizeGB *float64) *[]admin.CloudRegionConfig20240805 {
var apiObjects []admin.CloudRegionConfig20240805
for _, tfMapRaw := range tfList {
tfMap, ok := tfMapRaw.(map[string]any)
if !ok || tfMap == nil {
Expand All @@ -966,9 +965,9 @@ func expandRegionConfigs(tfList []any, rootDiskSizeGB *float64) *[]admin20240805
return &apiObjects
}

func expandRegionConfig(tfMap map[string]any, rootDiskSizeGB *float64) *admin20240805.CloudRegionConfig20240805 {
func expandRegionConfig(tfMap map[string]any, rootDiskSizeGB *float64) *admin.CloudRegionConfig20240805 {
providerName := tfMap["provider_name"].(string)
apiObject := &admin20240805.CloudRegionConfig20240805{
apiObject := &admin.CloudRegionConfig20240805{
Priority: conversion.Pointer(cast.ToInt(tfMap["priority"])),
ProviderName: conversion.StringPtr(providerName),
RegionName: conversion.StringPtr(tfMap["region_name"].(string)),
Expand All @@ -995,9 +994,9 @@ func expandRegionConfig(tfMap map[string]any, rootDiskSizeGB *float64) *admin202
return apiObject
}

func expandRegionConfigSpec(tfList []any, providerName string, rootDiskSizeGB *float64) *admin20240805.DedicatedHardwareSpec20240805 {
func expandRegionConfigSpec(tfList []any, providerName string, rootDiskSizeGB *float64) *admin.DedicatedHardwareSpec20240805 {
tfMap, _ := tfList[0].(map[string]any)
apiObject := new(admin20240805.DedicatedHardwareSpec20240805)
apiObject := new(admin.DedicatedHardwareSpec20240805)
if providerName == constant.AWS || providerName == constant.AZURE {
if v, ok := tfMap["disk_iops"]; ok && v.(int) > 0 {
apiObject.DiskIOPS = conversion.Pointer(v.(int))
Expand Down Expand Up @@ -1027,11 +1026,11 @@ func expandRegionConfigSpec(tfList []any, providerName string, rootDiskSizeGB *f
return apiObject
}

func expandRegionConfigAutoScaling(tfList []any) *admin20240805.AdvancedAutoScalingSettings {
func expandRegionConfigAutoScaling(tfList []any) *admin.AdvancedAutoScalingSettings {
tfMap, _ := tfList[0].(map[string]any)
settings := admin20240805.AdvancedAutoScalingSettings{
DiskGB: new(admin20240805.DiskGBAutoScaling),
Compute: new(admin20240805.AdvancedComputeAutoScaling),
settings := admin.AdvancedAutoScalingSettings{
DiskGB: new(admin.DiskGBAutoScaling),
Compute: new(admin.AdvancedComputeAutoScaling),
}

if v, ok := tfMap["disk_gb_enabled"]; ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -451,13 +450,13 @@ func TestCheckRegionConfigsPriorityOrder(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
configs := make([]admin20240805.CloudRegionConfig20240805, len(tc.priorities))
configs := make([]admin.CloudRegionConfig20240805, len(tc.priorities))
configsOld := make([]admin20240530.CloudRegionConfig, len(tc.priorities))
for i, priority := range tc.priorities {
configs[i].Priority = conversion.IntPtr(priority)
configsOld[i].Priority = conversion.IntPtr(priority)
}
err := advancedcluster.CheckRegionConfigsPriorityOrder([]admin20240805.ReplicationSpec20240805{{RegionConfigs: &configs}})
err := advancedcluster.CheckRegionConfigsPriorityOrder([]admin.ReplicationSpec20240805{{RegionConfigs: &configs}})
assert.Equal(t, tc.errorExpected, err != nil)
err = advancedcluster.CheckRegionConfigsPriorityOrderOld([]admin20240530.ReplicationSpec{{RegionConfigs: &configsOld}})
assert.Equal(t, tc.errorExpected, err != nil)
Expand Down
19 changes: 9 additions & 10 deletions internal/service/advancedcluster/model_sdk_version_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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"
Expand All @@ -20,7 +19,7 @@ func convertTagsPtrToLatest(tags *[]admin20240530.ResourceTag) *[]admin.Resource
return &result
}

func convertTagsPtrToOldSDK(tags *[]admin20240805.ResourceTag) *[]admin20240530.ResourceTag {
func convertTagsPtrToOldSDK(tags *[]admin.ResourceTag) *[]admin20240530.ResourceTag {
if tags == nil {
return nil
}
Expand Down Expand Up @@ -48,7 +47,7 @@ func convertTagsToLatest(tags []admin20240530.ResourceTag) []admin.ResourceTag {
return results
}

func convertBiConnectToOldSDK(biconnector *admin20240805.BiConnector) *admin20240530.BiConnector {
func convertBiConnectToOldSDK(biconnector *admin.BiConnector) *admin20240530.BiConnector {
if biconnector == nil {
return nil
}
Expand Down Expand Up @@ -126,7 +125,7 @@ func convertLabelsToLatest(labels *[]admin20240530.ComponentLabel) *[]admin.Comp
return &results
}

func convertLabelSliceToOldSDK(slice []admin20240805.ComponentLabel, err diag.Diagnostics) ([]admin20240530.ComponentLabel, diag.Diagnostics) {
func convertLabelSliceToOldSDK(slice []admin.ComponentLabel, err diag.Diagnostics) ([]admin20240530.ComponentLabel, diag.Diagnostics) {
if err != nil {
return nil, err
}
Expand All @@ -141,7 +140,7 @@ func convertLabelSliceToOldSDK(slice []admin20240805.ComponentLabel, err diag.Di
return results, nil
}

func convertRegionConfigSliceToOldSDK(slice *[]admin20240805.CloudRegionConfig20240805) *[]admin20240530.CloudRegionConfig {
func convertRegionConfigSliceToOldSDK(slice *[]admin.CloudRegionConfig20240805) *[]admin20240530.CloudRegionConfig {
if slice == nil {
return nil
}
Expand All @@ -164,7 +163,7 @@ func convertRegionConfigSliceToOldSDK(slice *[]admin20240805.CloudRegionConfig20
return &results
}

func convertHardwareSpecToOldSDK(hwspec *admin20240805.HardwareSpec20240805) *admin20240530.HardwareSpec {
func convertHardwareSpecToOldSDK(hwspec *admin.HardwareSpec20240805) *admin20240530.HardwareSpec {
if hwspec == nil {
return nil
}
Expand All @@ -176,7 +175,7 @@ func convertHardwareSpecToOldSDK(hwspec *admin20240805.HardwareSpec20240805) *ad
}
}

func convertAdvancedAutoScalingSettingsToOldSDK(settings *admin20240805.AdvancedAutoScalingSettings) *admin20240530.AdvancedAutoScalingSettings {
func convertAdvancedAutoScalingSettingsToOldSDK(settings *admin.AdvancedAutoScalingSettings) *admin20240530.AdvancedAutoScalingSettings {
if settings == nil {
return nil
}
Expand All @@ -186,7 +185,7 @@ func convertAdvancedAutoScalingSettingsToOldSDK(settings *admin20240805.Advanced
}
}

func convertAdvancedComputeAutoScalingToOldSDK(settings *admin20240805.AdvancedComputeAutoScaling) *admin20240530.AdvancedComputeAutoScaling {
func convertAdvancedComputeAutoScalingToOldSDK(settings *admin.AdvancedComputeAutoScaling) *admin20240530.AdvancedComputeAutoScaling {
if settings == nil {
return nil
}
Expand All @@ -198,7 +197,7 @@ func convertAdvancedComputeAutoScalingToOldSDK(settings *admin20240805.AdvancedC
}
}

func convertDiskGBAutoScalingToOldSDK(settings *admin20240805.DiskGBAutoScaling) *admin20240530.DiskGBAutoScaling {
func convertDiskGBAutoScalingToOldSDK(settings *admin.DiskGBAutoScaling) *admin20240530.DiskGBAutoScaling {
if settings == nil {
return nil
}
Expand All @@ -207,7 +206,7 @@ func convertDiskGBAutoScalingToOldSDK(settings *admin20240805.DiskGBAutoScaling)
}
}

func convertDedicatedHardwareSpecToOldSDK(spec *admin20240805.DedicatedHardwareSpec20240805) *admin20240530.DedicatedHardwareSpec {
func convertDedicatedHardwareSpecToOldSDK(spec *admin.DedicatedHardwareSpec20240805) *admin20240530.DedicatedHardwareSpec {
if spec == nil {
return nil
}
Expand Down
31 changes: 14 additions & 17 deletions internal/service/advancedcluster/resource_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
rootDiskSizeGB = conversion.Pointer(v.(float64))
}

params := &admin20240805.ClusterDescription20240805{
params := &admin.ClusterDescription20240805{
Name: conversion.StringPtr(cast.ToString(d.Get("name"))),
ClusterType: conversion.StringPtr(cast.ToString(d.Get("cluster_type"))),
ReplicationSpecs: expandAdvancedReplicationSpecs(d.Get("replication_specs").([]any), rootDiskSizeGB),
Expand All @@ -442,7 +442,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
}

if _, ok := d.GetOk("tags"); ok {
params.Tags = conversion.ExpandTagsFromSetSchemaV220240805(d)
params.Tags = conversion.ExpandTagsFromSetSchema(d)
}
if v, ok := d.GetOk("mongo_db_major_version"); ok {
params.MongoDBMajorVersion = conversion.StringPtr(FormatMongoDBMajorVersion(v.(string)))
Expand Down Expand Up @@ -482,8 +482,8 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
if err := CheckRegionConfigsPriorityOrder(params.GetReplicationSpecs()); err != nil {
return diag.FromErr(err)
}
// cannot call latest API (2024-10-23 or newer) as it can enable ISS autoscaling
cluster, _, err := connV220240805.ClustersApi.CreateCluster(ctx, projectID, params).Execute()

cluster, _, err := connV2.ClustersApi.CreateCluster(ctx, projectID, params).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf(errorCreate, err))
}
Expand Down Expand Up @@ -821,7 +821,6 @@ func resourceUpgrade(ctx context.Context, upgradeRequest *admin.LegacyAtlasTenan

func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV220240530 := meta.(*config.MongoDBClient).AtlasV220240530
connV220240805 := meta.(*config.MongoDBClient).AtlasV220240805
connV2 := meta.(*config.MongoDBClient).AtlasV2
ids := conversion.DecodeStateID(d.Id())
projectID := ids["project_id"]
Expand Down Expand Up @@ -850,7 +849,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
waitOnUpdate = true
}
if d.HasChange("replica_set_scaling_strategy") || d.HasChange("redact_client_log_data") || d.HasChange("config_server_management_mode") {
request := new(admin20240805.ClusterDescription20240805)
request := new(admin.ClusterDescription20240805)
if d.HasChange("replica_set_scaling_strategy") {
request.ReplicaSetScalingStrategy = conversion.Pointer(d.Get("replica_set_scaling_strategy").(string))
}
Expand All @@ -860,8 +859,8 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
if d.HasChange("config_server_management_mode") {
request.ConfigServerManagementMode = conversion.StringPtr(d.Get("config_server_management_mode").(string))
}
// can call latest API (2024-10-23 or newer) as autoscaling property is not specified, using older version just for caution until iss autoscaling epic is done
if _, _, err := connV220240805.ClustersApi.UpdateCluster(ctx, projectID, clusterName, request).Execute(); err != nil {
// can call latest API (2024-10-23 or newer) as replications specs with autoscaling property is not specified
if _, _, err := connV2.ClustersApi.UpdateCluster(ctx, projectID, clusterName, request).Execute(); err != nil {
return diag.FromErr(fmt.Errorf(errorUpdate, clusterName, err))
}
waitOnUpdate = true
Expand All @@ -881,8 +880,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
if err := CheckRegionConfigsPriorityOrder(req.GetReplicationSpecs()); err != nil {
return diag.FromErr(err)
}
// cannot call latest API (2024-10-23 or newer) as it can enable ISS autoscaling
if _, _, err := connV220240805.ClustersApi.UpdateCluster(ctx, projectID, clusterName, req).Execute(); err != nil {
if _, _, err := connV2.ClustersApi.UpdateCluster(ctx, projectID, clusterName, req).Execute(); err != nil {
return diag.FromErr(fmt.Errorf(errorUpdate, clusterName, err))
}
if err := waitForUpdateToFinish(ctx, connV2, projectID, clusterName, timeout); err != nil {
Expand Down Expand Up @@ -916,11 +914,10 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
}

if d.Get("paused").(bool) {
clusterRequest := &admin20240805.ClusterDescription20240805{
clusterRequest := &admin.ClusterDescription20240805{
Paused: conversion.Pointer(true),
}
// can call latest API (2024-10-23 or newer) as autoscaling property is not specified, using older version just for caution until iss autoscaling epic is done
if _, _, err := connV220240805.ClustersApi.UpdateCluster(ctx, projectID, clusterName, clusterRequest).Execute(); err != nil {
if _, _, err := connV2.ClustersApi.UpdateCluster(ctx, projectID, clusterName, clusterRequest).Execute(); err != nil {
return diag.FromErr(fmt.Errorf(errorUpdate, clusterName, err))
}
if err := waitForUpdateToFinish(ctx, connV2, projectID, clusterName, timeout); err != nil {
Expand All @@ -931,8 +928,8 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
return resourceRead(ctx, d, meta)
}

func updateRequest(ctx context.Context, d *schema.ResourceData, projectID, clusterName string, connV2 *admin.APIClient) (*admin20240805.ClusterDescription20240805, diag.Diagnostics) {
cluster := new(admin20240805.ClusterDescription20240805)
func updateRequest(ctx context.Context, d *schema.ResourceData, projectID, clusterName string, connV2 *admin.APIClient) (*admin.ClusterDescription20240805, diag.Diagnostics) {
cluster := new(admin.ClusterDescription20240805)

if d.HasChange("replication_specs") || d.HasChange("disk_size_gb") {
var updatedDiskSizeGB *float64
Expand Down Expand Up @@ -979,7 +976,7 @@ func updateRequest(ctx context.Context, d *schema.ResourceData, projectID, clust
}

if d.HasChange("tags") {
cluster.Tags = conversion.ExpandTagsFromSetSchemaV220240805(d)
cluster.Tags = conversion.ExpandTagsFromSetSchema(d)
}

if d.HasChange("mongo_db_major_version") {
Expand Down Expand Up @@ -1073,7 +1070,7 @@ func updateRequestOldAPI(d *schema.ResourceData, clusterName string) (*admin2024
}

if d.HasChange("tags") {
cluster.Tags = convertTagsPtrToOldSDK(conversion.ExpandTagsFromSetSchemaV220240805(d))
cluster.Tags = convertTagsPtrToOldSDK(conversion.ExpandTagsFromSetSchema(d))
}

if d.HasChange("mongo_db_major_version") {
Expand Down
Loading

0 comments on commit 4200385

Please sign in to comment.