-
Notifications
You must be signed in to change notification settings - Fork 189
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
Changes from all commits
7379bfc
aa35158
b8db910
fee6bd0
98796df
584f6fb
b202323
d785e08
3d29506
ea31f36
372d5a1
76c06b1
29ceb62
cc0368b
c3d7b13
47d02e8
d6508be
e95c70f
77f0974
e8d2ce4
8767c03
320634a
4db5d98
0bd6530
0bc99c0
9c2fd80
fed811c
fe32f6b
a231193
07e8518
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,10 @@ func StringPtr(v string) *string { | |
} | ||
return nil | ||
} | ||
|
||
func SliceFromPtr[T any](slicePtr *[]T) []T { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{} | ||
|
@@ -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"])) | ||
} | ||
|
@@ -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 | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -508,6 +508,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag. | |
AutoScaling: autoScaling, | ||
ProviderSettings: providerSettings, | ||
ReplicationSpecs: replicationSpecs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
AdvancedConfiguration: expandClusterAdvancedConfiguration(d), | ||
} | ||
if v, ok := d.GetOk("cloud_backup"); ok { | ||
clusterRequest.ProviderBackupEnabled = conversion.Pointer(v.(bool)) | ||
|
@@ -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)) | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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