Skip to content

chore: Updates mongodbatlas_cluster to consume selected processArgs fields from the createCluster/updateCluster APIs #3250

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7379bfc
chore: Bump crazy-max/ghaction-import-gpg from 6.2.0 to 6.3.0 (#3241)
dependabot[bot] Apr 2, 2025
aa35158
doc: Updates Terraform Compatibility Matrix documentation (#3233)
svc-apix-Bot Apr 2, 2025
b8db910
chore: Updates Atlas Go SDK (#3235)
svc-apix-Bot Apr 4, 2025
fee6bd0
add advanced configuration changes to cluster resource
maastha Apr 4, 2025
98796df
minor
maastha Apr 4, 2025
584f6fb
feat: Support configuring BYOK encryption on search nodes (#3199)
oarbusi Apr 4, 2025
b202323
chore: Updates CHANGELOG.md for #3199
svc-apix-Bot Apr 4, 2025
d785e08
cleanup try
maastha Apr 4, 2025
3d29506
min
maastha Apr 4, 2025
ea31f36
min
maastha Apr 4, 2025
372d5a1
cleanup
maastha Apr 4, 2025
76c06b1
min
maastha Apr 4, 2025
29ceb62
min
maastha Apr 4, 2025
cc0368b
min
maastha Apr 4, 2025
c3d7b13
min
maastha Apr 4, 2025
47d02e8
tmp
maastha Apr 4, 2025
d6508be
tmp
maastha Apr 4, 2025
e95c70f
doc: Update guidelines for external contributors to squash commits (#…
maastha Apr 4, 2025
77f0974
chore: Updates organization.md `skip_default_alerts_settings` note fo…
maastha Apr 4, 2025
e8d2ce4
chore: Updates org_clean_test.go to delete stream instances & private…
maastha Apr 7, 2025
8767c03
handle tenant
maastha Apr 7, 2025
320634a
cleanup
maastha Apr 7, 2025
4db5d98
Merge branch 'master' into CLOUDP-296222-cluster-resource
maastha Apr 7, 2025
0bd6530
Revert "Merge branch 'master' into CLOUDP-296222-cluster-resource"
maastha Apr 7, 2025
0bc99c0
nit
maastha Apr 7, 2025
9c2fd80
Reorder imports in org_clean_test.go
maastha Apr 7, 2025
fed811c
Add missing newline at end of file
maastha Apr 7, 2025
fe32f6b
fmt
maastha Apr 7, 2025
a231193
Merge branch 'CLOUDP-296222-cluster-resource' of github.com:mongodb/t…
maastha Apr 7, 2025
07e8518
min
maastha Apr 7, 2025
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 @@ -31,7 +31,7 @@ require (
github.com/stretchr/testify v1.10.0
github.com/wI2L/jsondiff v0.6.1
github.com/zclconf/go-cty v1.16.2
go.mongodb.org/atlas v0.37.0
go.mongodb.org/atlas v0.37.1-0.20250402194754-dd09936138bc
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will be replaced with new release version before merge

Copy link
Member

Choose a reason for hiding this comment

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

recommend to add a comment so we don't forget

go.mongodb.org/atlas-sdk/v20240530005 v20240530005.0.0
go.mongodb.org/atlas-sdk/v20240805005 v20240805005.0.1-0.20250402112219-2468c5354718 // uses api-bot-update-v20240805-backport-cluster to support AdvancedConfiguration in create/updateCluster APIs
go.mongodb.org/atlas-sdk/v20241113005 v20241113005.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ github.com/zclconf/go-cty-yaml v1.0.2/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgK
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
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 v0.37.1-0.20250402194754-dd09936138bc h1:tiJOadNekKrLNouqFW9gcH8Ywgy15Bw2Xhs24ddWafE=
go.mongodb.org/atlas v0.37.1-0.20250402194754-dd09936138bc/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.1-0.20250402112219-2468c5354718 h1:M2mNSBdTkP+paQ1qZ6FliiPdTEbDR9m9qvv4vsWoJAw=
Expand Down
7 changes: 7 additions & 0 deletions internal/common/conversion/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ func StringPtr(v string) *string {
}
return nil
}

func SliceFromPtr[T any](slicePtr *[]T) []T {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we have this need elsewhere and we could use there this func as well, or we're already using another approach/func and we can use that instead of creating this func

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I couldn't find anything similar already present. I think that's because usually we use SDK Get methods that does this for us. But since cluster resource still uses manual SDK, I need to handle with this new function

if slicePtr == nil {
return []T{}
}
return *slicePtr
}
8 changes: 7 additions & 1 deletion internal/service/cluster/data_source_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,13 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
return diag.FromErr(fmt.Errorf(advancedcluster.ErrorAdvancedConfRead, "", clusterName, err))
}

if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs20240530, processArgs)); err != nil {
p := &ProcessArgs{
argsDefault: processArgs,
argsLegacy: processArgs20240530,
clusterAdvancedConfig: cluster.AdvancedConfiguration,
}

if err := d.Set("advanced_configuration", flattenProcessArgs(p)); err != nil {
return diag.FromErr(fmt.Errorf(advancedcluster.ErrorClusterSetting, "advanced_configuration", clusterName, err))
}

Expand Down
7 changes: 6 additions & 1 deletion internal/service/cluster/data_source_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,13 @@ func flattenClusters(ctx context.Context, d *schema.ResourceData, conn *matlas.C

containerID = getContainerID(containers, &clusters[i])
}
p := &ProcessArgs{
argsDefault: processArgs,
argsLegacy: processArgs20240530,
clusterAdvancedConfig: clusters[i].AdvancedConfiguration,
}
result := map[string]any{
"advanced_configuration": flattenProcessArgs(processArgs20240530, processArgs),
"advanced_configuration": flattenProcessArgs(p),
"auto_scaling_compute_enabled": clusters[i].AutoScaling.Compute.Enabled,
"auto_scaling_compute_scale_down_enabled": clusters[i].AutoScaling.Compute.ScaleDownEnabled,
"auto_scaling_disk_gb_enabled": clusters[i].AutoScaling.DiskGBEnabled,
Expand Down
80 changes: 51 additions & 29 deletions internal/service/cluster/model_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
)

type ProcessArgs struct {
argsDefault *admin.ClusterDescriptionProcessArgs20240805
clusterAdvancedConfig *matlas.AdvancedConfiguration
argsLegacy *admin20240530.ClusterDescriptionProcessArgs
}

func flattenCloudProviderSnapshotBackupPolicy(ctx context.Context, d *schema.ResourceData, conn *matlas.Client, projectID, clusterName string) ([]map[string]any, error) {
backupPolicy, res, err := conn.CloudProviderSnapshotBackupPolicies.Get(ctx, projectID, clusterName)
if err != nil {
Expand Down Expand Up @@ -73,32 +79,38 @@ func flattenPolicyItems(items []matlas.PolicyItem) []map[string]any {
return policyItems
}

func flattenProcessArgs(p20240530 *admin20240530.ClusterDescriptionProcessArgs, p *admin.ClusterDescriptionProcessArgs20240805) []map[string]any {
func flattenProcessArgs(p *ProcessArgs) []map[string]any {
flattenedProcessArgs := []map[string]any{
{
// default_read_concern and fail_index_key_too_long have been deprecated, hence using the older SDK
"default_read_concern": p20240530.DefaultReadConcern,
"fail_index_key_too_long": cast.ToBool(p20240530.FailIndexKeyTooLong),
"default_write_concern": p.DefaultWriteConcern,
"javascript_enabled": cast.ToBool(p.JavascriptEnabled),
"minimum_enabled_tls_protocol": p.MinimumEnabledTlsProtocol,
"no_table_scan": cast.ToBool(p.NoTableScan),
"oplog_size_mb": p.OplogSizeMB,
"oplog_min_retention_hours": p.OplogMinRetentionHours,
"sample_size_bi_connector": p.SampleSizeBIConnector,
"sample_refresh_interval_bi_connector": p.SampleRefreshIntervalBIConnector,
"transaction_lifetime_limit_seconds": p.TransactionLifetimeLimitSeconds,
"tls_cipher_config_mode": p.TlsCipherConfigMode,
"custom_openssl_cipher_config_tls12": p.GetCustomOpensslCipherConfigTls12(),
"default_read_concern": p.argsLegacy.DefaultReadConcern,
"fail_index_key_too_long": cast.ToBool(p.argsLegacy.FailIndexKeyTooLong),
"default_write_concern": p.argsDefault.DefaultWriteConcern,
"javascript_enabled": cast.ToBool(p.argsDefault.JavascriptEnabled),
"no_table_scan": cast.ToBool(p.argsDefault.NoTableScan),
"oplog_size_mb": p.argsDefault.OplogSizeMB,
"oplog_min_retention_hours": p.argsDefault.OplogMinRetentionHours,
"sample_size_bi_connector": p.argsDefault.SampleSizeBIConnector,
"sample_refresh_interval_bi_connector": p.argsDefault.SampleRefreshIntervalBIConnector,
"transaction_lifetime_limit_seconds": p.argsDefault.TransactionLifetimeLimitSeconds,
"minimum_enabled_tls_protocol": p.argsDefault.MinimumEnabledTlsProtocol,
"tls_cipher_config_mode": p.argsDefault.TlsCipherConfigMode,
"custom_openssl_cipher_config_tls12": conversion.SliceFromPtr(p.argsDefault.CustomOpensslCipherConfigTls12),
},
}

if p.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds != nil {
flattenedProcessArgs[0]["change_stream_options_pre_and_post_images_expire_after_seconds"] = p.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds
if p.argsDefault.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds != nil {
flattenedProcessArgs[0]["change_stream_options_pre_and_post_images_expire_after_seconds"] = p.argsDefault.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds
} else {
flattenedProcessArgs[0]["change_stream_options_pre_and_post_images_expire_after_seconds"] = -1 // default in schema, otherwise user gets drift detection
}

if p.clusterAdvancedConfig != nil { // For TENANT cluster type, advancedConfiguration field may not be returned from cluster APIs
flattenedProcessArgs[0]["minimum_enabled_tls_protocol"] = p.clusterAdvancedConfig.MinimumEnabledTLSProtocol
flattenedProcessArgs[0]["tls_cipher_config_mode"] = p.clusterAdvancedConfig.TLSCipherConfigMode
flattenedProcessArgs[0]["custom_openssl_cipher_config_tls12"] = conversion.SliceFromPtr(p.clusterAdvancedConfig.CustomOpensslCipherConfigTLS12)
}

return flattenedProcessArgs
}

Expand Down Expand Up @@ -228,6 +240,29 @@ func expandTagSliceFromSetSchema(d *schema.ResourceData) []*matlas.Tag {
return res
}

func expandClusterAdvancedConfiguration(d *schema.ResourceData) *matlas.AdvancedConfiguration {
ac := d.Get("advanced_configuration")
if aclist, ok1 := ac.([]any); ok1 && len(aclist) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

nit: just need check len(aclist) as it won't have elements if there is an error, so you wouldn't need ok1

p := aclist[0].(map[string]any)
res := matlas.AdvancedConfiguration{}

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

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 @@ -248,10 +283,6 @@ func expandProcessArgs(d *schema.ResourceData, p map[string]any, mongodbMajorVer
res.JavascriptEnabled = conversion.Pointer(cast.ToBool(p["javascript_enabled"]))
}

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

if _, ok := d.GetOkExists("advanced_configuration.0.no_table_scan"); ok {
res.NoTableScan = conversion.Pointer(cast.ToBool(p["no_table_scan"]))
}
Expand Down Expand Up @@ -292,15 +323,6 @@ func expandProcessArgs(d *schema.ResourceData, p map[string]any, mongodbMajorVer
res.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds = conversion.Pointer(cast.ToInt(p["change_stream_options_pre_and_post_images_expire_after_seconds"]))
}

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
13 changes: 12 additions & 1 deletion internal/service/cluster/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
AutoScaling: autoScaling,
ProviderSettings: providerSettings,
ReplicationSpecs: replicationSpecs,
Copy link
Member

Choose a reason for hiding this comment

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

there are not test changes, can we add/update tests what would fail before this change but will pass now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

since these are backend changes (i.e. we're only fetching from a different API, no API behavior changes) I'm not sure how we can test the before/after of this, I think as long as expected attributes are set correctly that should suffice. We already have a decent coverage for advanced_configuration for this.
Let me know if you can think of specific scenarios we can capture

AdvancedConfiguration: expandClusterAdvancedConfiguration(d),
}
if v, ok := d.GetOk("cloud_backup"); ok {
clusterRequest.ProviderBackupEnabled = conversion.Pointer(v.(bool))
Expand Down Expand Up @@ -798,7 +799,13 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
return diag.FromErr(fmt.Errorf(advancedcluster.ErrorAdvancedConfRead, "", clusterName, err))
}

if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs20240530, processArgs)); err != nil {
p := &ProcessArgs{
argsDefault: processArgs,
argsLegacy: processArgs20240530,
clusterAdvancedConfig: cluster.AdvancedConfiguration,
}

if err := d.Set("advanced_configuration", flattenProcessArgs(p)); err != nil {
return diag.FromErr(fmt.Errorf(advancedcluster.ErrorClusterSetting, "advanced_configuration", clusterName, err))
}

Expand Down Expand Up @@ -1009,6 +1016,10 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
return diag.FromErr(fmt.Errorf(errorAdvancedConfUpdate, "", clusterName, err))
}
}
clusterAdvConfig := expandClusterAdvancedConfiguration(d)
if !reflect.DeepEqual(cluster.AdvancedConfiguration, matlas.AdvancedConfiguration{}) {
cluster.AdvancedConfiguration = clusterAdvConfig
}
}
}

Expand Down
Loading