Skip to content

chore: Updates mongodbatlas_advanced_cluster (SDKv2 & TPF) to consume selected processArgs fields from the createCluster/updateCluster APIs #3231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/zclconf/go-cty v1.16.2
go.mongodb.org/atlas v0.37.0
go.mongodb.org/atlas-sdk/v20240530005 v20240530005.0.0
go.mongodb.org/atlas-sdk/v20240805005 v20240805005.0.0
go.mongodb.org/atlas-sdk/v20240805005 v20240805005.0.1-0.20250402112219-2468c5354718
go.mongodb.org/atlas-sdk/v20241113005 v20241113005.0.0
go.mongodb.org/atlas-sdk/v20250312001 v20250312001.1.0
go.mongodb.org/realm v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ go.mongodb.org/atlas v0.37.0 h1:zQnO1o5+bVP9IotpAYpres4UjMD2F4nwNEFTZhNL4ck=
go.mongodb.org/atlas v0.37.0/go.mod h1:DJYtM+vsEpPEMSkQzJnFHrT0sP7ev6cseZc/GGjJYG8=
go.mongodb.org/atlas-sdk/v20240530005 v20240530005.0.0 h1:d/gbYJ+obR0EM/3DZf7+ZMi2QWISegm3mid7Or708cc=
go.mongodb.org/atlas-sdk/v20240530005 v20240530005.0.0/go.mod h1:O47ZrMMfcWb31wznNIq2PQkkdoFoK0ea2GlmRqGJC2s=
go.mongodb.org/atlas-sdk/v20240805005 v20240805005.0.0 h1:EGTT54tKbDbkhhK+jH5AqINFQbHdvaOSpI0oeI5Tl1s=
go.mongodb.org/atlas-sdk/v20240805005 v20240805005.0.0/go.mod h1:UTNpAyiKm/7utu+Nl0FafgjgvS+ONNGEoxBT5g/40WM=
go.mongodb.org/atlas-sdk/v20240805005 v20240805005.0.1-0.20250402112219-2468c5354718 h1:M2mNSBdTkP+paQ1qZ6FliiPdTEbDR9m9qvv4vsWoJAw=
go.mongodb.org/atlas-sdk/v20240805005 v20240805005.0.1-0.20250402112219-2468c5354718/go.mod h1:PeByRxdvzfvz7xhG5vDn60j836EoduWqTqs76okUc9c=
go.mongodb.org/atlas-sdk/v20241113005 v20241113005.0.0 h1:aaU2E4rtzYXuEDxv9MoSON2gOEAA9M2gsDf2CqjcGj8=
go.mongodb.org/atlas-sdk/v20241113005 v20241113005.0.0/go.mod h1:eV9REWR36iVMrpZUAMZ5qPbXEatoVfmzwT+Ue8yqU+U=
go.mongodb.org/atlas-sdk/v20250312001 v20250312001.1.0 h1:yJeENElweq4Ba+bGeUVVA+wC+B7XsVW4+nEpTgjdkLE=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
return diag.FromErr(fmt.Errorf(ErrorAdvancedConfRead, "", clusterName, err))
}

if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs20240530, processArgs)); err != nil {
if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs20240530, processArgs, clusterDesc.AdvancedConfiguration)); err != nil {
Copy link
Collaborator Author

@maastha maastha Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although not an absolute requirement for Read, but still fetching these from cluster response (instead of /processArgs) for consistency.

return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "advanced_configuration", clusterName, err))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func flattenAdvancedClusters(ctx context.Context, connV220240530 *admin20240530.
}

result := map[string]any{
"advanced_configuration": flattenProcessArgs(processArgs20240530, processArgs),
"advanced_configuration": flattenProcessArgs(processArgs20240530, processArgs, cluster.AdvancedConfiguration),
"backup_enabled": cluster.GetBackupEnabled(),
"bi_connector_config": flattenBiConnectorConfig(cluster.BiConnector),
"cluster_type": cluster.GetClusterType(),
Expand Down
52 changes: 35 additions & 17 deletions internal/service/advancedcluster/model_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ func expandBiConnectorConfig(d *schema.ResourceData) *admin.BiConnector {
return nil
}

func flattenProcessArgs(p20240530 *admin20240530.ClusterDescriptionProcessArgs, p *admin.ClusterDescriptionProcessArgs20240805) []map[string]any {
func flattenProcessArgs(p20240530 *admin20240530.ClusterDescriptionProcessArgs,
p *admin.ClusterDescriptionProcessArgs20240805,
clusterAdvConfig *admin.ApiAtlasClusterAdvancedConfiguration) []map[string]any {
if p20240530 == nil {
return nil
}
Expand All @@ -540,7 +542,6 @@ func flattenProcessArgs(p20240530 *admin20240530.ClusterDescriptionProcessArgs,
"default_write_concern": p20240530.GetDefaultWriteConcern(),
"fail_index_key_too_long": p20240530.GetFailIndexKeyTooLong(),
"javascript_enabled": p20240530.GetJavascriptEnabled(),
"minimum_enabled_tls_protocol": p20240530.GetMinimumEnabledTlsProtocol(),
"no_table_scan": p20240530.GetNoTableScan(),
"oplog_size_mb": p20240530.GetOplogSizeMB(),
"oplog_min_retention_hours": p20240530.GetOplogMinRetentionHours(),
Expand All @@ -559,8 +560,12 @@ func flattenProcessArgs(p20240530 *admin20240530.ClusterDescriptionProcessArgs,
if v := p.DefaultMaxTimeMS; v != nil {
flattenedProcessArgs[0]["default_max_time_ms"] = p.GetDefaultMaxTimeMS()
}
flattenedProcessArgs[0]["tls_cipher_config_mode"] = p.GetTlsCipherConfigMode()
flattenedProcessArgs[0]["custom_openssl_cipher_config_tls12"] = p.GetCustomOpensslCipherConfigTls12()
}

if clusterAdvConfig != nil {
flattenedProcessArgs[0]["tls_cipher_config_mode"] = clusterAdvConfig.GetTlsCipherConfigMode()
flattenedProcessArgs[0]["custom_openssl_cipher_config_tls12"] = clusterAdvConfig.GetCustomOpensslCipherConfigTls12()
flattenedProcessArgs[0]["minimum_enabled_tls_protocol"] = clusterAdvConfig.GetMinimumEnabledTlsProtocol()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it happen that minimum_enabled_tls_protocol exists in p20240530 but not in clusterAdvConfig? in case it makes sense to keep reading it from p20240530, and override if present at clusterAdvConfig

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

}

return flattenedProcessArgs
Expand Down Expand Up @@ -843,6 +848,32 @@ func flattenAdvancedReplicationSpecAutoScaling(apiObject *admin.AdvancedAutoScal
return tfList
}

func expandClusterAdvancedConfiguration(d *schema.ResourceData) *admin.ApiAtlasClusterAdvancedConfiguration {
res := admin.ApiAtlasClusterAdvancedConfiguration{}

if ac, ok := d.GetOk("advanced_configuration"); ok {
if aclist, ok := ac.([]any); ok && len(aclist) > 0 {
p := aclist[0].(map[string]any)

if _, ok := d.GetOkExists("advanced_configuration.0.minimum_enabled_tls_protocol"); ok {
res.MinimumEnabledTlsProtocol = conversion.StringPtr(cast.ToString(p["minimum_enabled_tls_protocol"]))
Copy link
Collaborator

@oarbusi oarbusi Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: cast p["minimum_enabled_tls_protocol"].(string) instead of using the current way

}

if _, ok := d.GetOkExists("advanced_configuration.0.tls_cipher_config_mode"); ok {
res.TlsCipherConfigMode = conversion.StringPtr(cast.ToString(p["tls_cipher_config_mode"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.custom_openssl_cipher_config_tls12"); ok {
tmp := conversion.ExpandStringListFromSetSchema(d.Get("advanced_configuration.0.custom_openssl_cipher_config_tls12").(*schema.Set))
res.CustomOpensslCipherConfigTls12 = &tmp
}

return &res
}
}
return nil
}

func expandProcessArgs(d *schema.ResourceData, p map[string]any, mongodbMajorVersion *string) (admin20240530.ClusterDescriptionProcessArgs, admin.ClusterDescriptionProcessArgs20240805) {
res20240530 := admin20240530.ClusterDescriptionProcessArgs{}
res := admin.ClusterDescriptionProcessArgs20240805{}
Expand All @@ -863,10 +894,6 @@ func expandProcessArgs(d *schema.ResourceData, p map[string]any, mongodbMajorVer
res20240530.JavascriptEnabled = conversion.Pointer(cast.ToBool(p["javascript_enabled"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.minimum_enabled_tls_protocol"); ok {
res20240530.MinimumEnabledTlsProtocol = conversion.StringPtr(cast.ToString(p["minimum_enabled_tls_protocol"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.no_table_scan"); ok {
res20240530.NoTableScan = conversion.Pointer(cast.ToBool(p["no_table_scan"]))
}
Expand Down Expand Up @@ -917,15 +944,6 @@ func expandProcessArgs(d *schema.ResourceData, p map[string]any, mongodbMajorVer
log.Print(ErrorDefaultMaxTimeMinVersion)
}
}

if _, ok := d.GetOkExists("advanced_configuration.0.tls_cipher_config_mode"); ok {
res.TlsCipherConfigMode = conversion.StringPtr(cast.ToString(p["tls_cipher_config_mode"]))
}

if _, ok := d.GetOkExists("advanced_configuration.0.custom_openssl_cipher_config_tls12"); ok {
tmp := conversion.ExpandStringListFromSetSchema(d.Get("advanced_configuration.0.custom_openssl_cipher_config_tls12").(*schema.Set))
res.CustomOpensslCipherConfigTls12 = &tmp
}
return res20240530, res
}

Expand Down
27 changes: 22 additions & 5 deletions internal/service/advancedcluster/resource_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,10 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
}

params := &admin.ClusterDescription20240805{
Name: conversion.StringPtr(cast.ToString(d.Get("name"))),
ClusterType: conversion.StringPtr(cast.ToString(d.Get("cluster_type"))),
ReplicationSpecs: replicationSpecs,
Name: conversion.StringPtr(cast.ToString(d.Get("name"))),
ClusterType: conversion.StringPtr(cast.ToString(d.Get("cluster_type"))),
ReplicationSpecs: replicationSpecs,
AdvancedConfiguration: expandClusterAdvancedConfiguration(d),
}

if v, ok := d.GetOk("backup_enabled"); ok {
Expand Down Expand Up @@ -688,7 +689,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
return diag.FromErr(fmt.Errorf(errorConfigRead, clusterName, err))
}

if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs20240530, processArgs)); err != nil {
if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs20240530, processArgs, cluster.AdvancedConfiguration)); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "advanced_configuration", clusterName, err))
}

Expand Down Expand Up @@ -819,6 +820,11 @@ func setRootFields(d *schema.ResourceData, cluster *admin.ClusterDescription2024
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pinned_fcv", clusterName, err))
}

// TODO: set adv_config
// if err := d.Set("pinned_fcv", FlattenPinnedFCV(cluster)); err != nil {
// return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pinned_fcv", clusterName, err))
// }

return nil
}

Expand Down Expand Up @@ -939,7 +945,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") {
if d.HasChange("replica_set_scaling_strategy") || d.HasChange("redact_client_log_data") || d.HasChange("config_server_management_mode") || d.HasChange("advanced_configuration") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AgustinBettati can you confirm if this should be okay or we should update this in the above call (connV220240530.ClustersApi.UpdateCluster #L943)? In that case, will need the SDK for that to be updated as well to support the new AdvancedConfiguration attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a concern here, as we are still respecting what the comment below mentions when calling the latest API version.

can call latest API (2024-10-23 or newer) as replications specs (with nested autoscaling property) is not specified

In this case I understand we only add the advancedConfiguration property but no replicationSpecs, meaning no risk.

request := new(admin.ClusterDescription20240805)
if d.HasChange("replica_set_scaling_strategy") {
request.ReplicaSetScalingStrategy = conversion.Pointer(d.Get("replica_set_scaling_strategy").(string))
Expand All @@ -950,6 +956,12 @@ 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))
}
if d.HasChange("advanced_configuration") {
if aclist, ok := d.Get("advanced_configuration").([]any); ok && len(aclist) > 0 {
request.AdvancedConfiguration = expandClusterAdvancedConfiguration(d)
}
}

// can call latest API (2024-10-23 or newer) as replications specs (with nested 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))
Expand Down Expand Up @@ -1148,6 +1160,11 @@ func updateRequest(ctx context.Context, d *schema.ResourceData, projectID, clust
if d.HasChange("config_server_management_mode") {
cluster.ConfigServerManagementMode = conversion.StringPtr(d.Get("config_server_management_mode").(string))
}
if d.HasChange("advanced_configuration") {
if aclist, ok := d.Get("advanced_configuration").([]any); ok && len(aclist) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that removing advanced_configuration from the config will never update?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the behavior today as well right? since it's O/C, or did you mean something else?

cluster.AdvancedConfiguration = expandClusterAdvancedConfiguration(d)
}
}

return cluster, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ func singleShardedMultiCloudTestCase(t *testing.T, usePreviewProvider bool) reso
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configShardedOldSchemaMultiCloud(t, usePreviewProvider, projectID, clusterName, 1, "M10", nil),
Config: configShardedOldSchemaMultiCloud(t, usePreviewProvider, projectID, clusterName, 2, "M10", nil),
Check: checkShardedOldSchemaMultiCloud(usePreviewProvider, clusterName, 1, "M10", true, nil),
},
{
Config: configShardedOldSchemaMultiCloud(t, usePreviewProvider, projectID, clusterNameUpdated, 1, "M10", nil),
Config: configShardedOldSchemaMultiCloud(t, usePreviewProvider, projectID, clusterNameUpdated, 2, "M10", nil),
Check: checkShardedOldSchemaMultiCloud(usePreviewProvider, clusterNameUpdated, 1, "M10", true, nil),
},
acc.TestStepImportCluster(resourceName),
Expand Down Expand Up @@ -1990,7 +1990,7 @@ func configShardedOldSchemaMultiCloud(t *testing.T, usePreviewProvider bool, pro
project_id = %[1]q
name = %[2]q
cluster_type = "SHARDED"
%[5]s
%[5]s

replication_specs {
num_shards = %[3]d
Expand Down
Loading