Skip to content

Commit 3f67fba

Browse files
coderGo93Edgar Lopez
andauthored
INTMDB-15: Added parameter advanced conf for cluster datasource (#646)
* added advanced configuration in datasource and docs of cluster(s) * fixes typo * fixes test * fixes test * fix tests * fix tests Co-authored-by: Edgar Lopez <[email protected]>
1 parent a125b02 commit 3f67fba

6 files changed

+282
-0
lines changed

mongodbatlas/data_source_mongodbatlas_cluster.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func dataSourceMongoDBAtlasCluster() *schema.Resource {
2121
Type: schema.TypeString,
2222
Required: true,
2323
},
24+
"advanced_configuration": clusterAdvancedConfigurationSchemaComputed(),
2425
"auto_scaling_disk_gb_enabled": {
2526
Type: schema.TypeBool,
2627
Computed: true,
@@ -444,6 +445,18 @@ func dataSourceMongoDBAtlasClusterRead(ctx context.Context, d *schema.ResourceDa
444445
return diag.FromErr(fmt.Errorf(errorClusterSetting, "version_release_system", clusterName, err))
445446
}
446447

448+
/*
449+
Get the advaced configuration options and set up to the terraform state
450+
*/
451+
processArgs, _, err := conn.Clusters.GetProcessArgs(ctx, projectID, clusterName)
452+
if err != nil {
453+
return diag.FromErr(fmt.Errorf(errorAdvancedConfRead, clusterName, err))
454+
}
455+
456+
if err := d.Set("advanced_configuration", flattenProcessArgs(processArgs)); err != nil {
457+
return diag.FromErr(fmt.Errorf(errorClusterSetting, "advanced_configuration", clusterName, err))
458+
}
459+
447460
// Get the snapshot policy and set the data
448461
snapshotBackupPolicy, err := flattenCloudProviderSnapshotBackupPolicy(ctx, d, conn, projectID, clusterName)
449462
if err != nil {
@@ -458,3 +471,50 @@ func dataSourceMongoDBAtlasClusterRead(ctx context.Context, d *schema.ResourceDa
458471

459472
return nil
460473
}
474+
475+
func clusterAdvancedConfigurationSchemaComputed() *schema.Schema {
476+
return &schema.Schema{
477+
Type: schema.TypeList,
478+
Computed: true,
479+
Elem: &schema.Resource{
480+
Schema: map[string]*schema.Schema{
481+
"default_read_concern": {
482+
Type: schema.TypeString,
483+
Computed: true,
484+
},
485+
"default_write_concern": {
486+
Type: schema.TypeString,
487+
Computed: true,
488+
},
489+
"fail_index_key_too_long": {
490+
Type: schema.TypeBool,
491+
Computed: true,
492+
},
493+
"javascript_enabled": {
494+
Type: schema.TypeBool,
495+
Computed: true,
496+
},
497+
"minimum_enabled_tls_protocol": {
498+
Type: schema.TypeString,
499+
Computed: true,
500+
},
501+
"no_table_scan": {
502+
Type: schema.TypeBool,
503+
Computed: true,
504+
},
505+
"oplog_size_mb": {
506+
Type: schema.TypeInt,
507+
Computed: true,
508+
},
509+
"sample_size_bi_connector": {
510+
Type: schema.TypeInt,
511+
Computed: true,
512+
},
513+
"sample_refresh_interval_bi_connector": {
514+
Type: schema.TypeInt,
515+
Computed: true,
516+
},
517+
},
518+
},
519+
}
520+
}

mongodbatlas/data_source_mongodbatlas_cluster_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/mwielbut/pointy"
1011
matlas "go.mongodb.org/atlas/mongodbatlas"
1112
)
1213

@@ -57,6 +58,52 @@ func TestAccDataSourceMongoDBAtlasCluster_basic(t *testing.T) {
5758
})
5859
}
5960

61+
func TestAccDataSourceMongoDBAtlasCluster_advancedConf(t *testing.T) {
62+
var (
63+
cluster matlas.Cluster
64+
resourceName = "mongodbatlas_cluster.test"
65+
dataSourceName = "data.mongodbatlas_cluster.test"
66+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
67+
name = acctest.RandomWithPrefix("test-acc")
68+
)
69+
70+
resource.ParallelTest(t, resource.TestCase{
71+
PreCheck: func() { testAccPreCheck(t) },
72+
ProviderFactories: testAccProviderFactories,
73+
CheckDestroy: testAccCheckMongoDBAtlasClusterDestroy,
74+
Steps: []resource.TestStep{
75+
{
76+
Config: testAccDataSourceMongoDBAtlasClusterConfigAdvancedConf(projectID, name, false, &matlas.ProcessArgs{
77+
FailIndexKeyTooLong: pointy.Bool(true),
78+
JavascriptEnabled: pointy.Bool(true),
79+
MinimumEnabledTLSProtocol: "TLS1_1",
80+
NoTableScan: pointy.Bool(false),
81+
OplogSizeMB: pointy.Int64(1000),
82+
SampleRefreshIntervalBIConnector: pointy.Int64(310),
83+
SampleSizeBIConnector: pointy.Int64(110),
84+
}),
85+
Check: resource.ComposeTestCheckFunc(
86+
testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster),
87+
testAccCheckMongoDBAtlasClusterAttributes(&cluster, name),
88+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
89+
resource.TestCheckResourceAttr(resourceName, "name", name),
90+
resource.TestCheckResourceAttr(resourceName, "disk_size_gb", "10"),
91+
resource.TestCheckResourceAttrSet(resourceName, "mongo_uri"),
92+
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"),
93+
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.regions_config.#"),
94+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
95+
resource.TestCheckResourceAttr(dataSourceName, "name", name),
96+
resource.TestCheckResourceAttr(dataSourceName, "disk_size_gb", "10"),
97+
resource.TestCheckResourceAttr(dataSourceName, "pit_enabled", "false"),
98+
resource.TestCheckResourceAttrSet(dataSourceName, "mongo_uri"),
99+
resource.TestCheckResourceAttrSet(dataSourceName, "replication_specs.#"),
100+
resource.TestCheckResourceAttr(dataSourceName, "version_release_system", "LTS"),
101+
),
102+
},
103+
},
104+
})
105+
}
106+
60107
func testAccDataSourceMongoDBAtlasClusterConfig(projectID, name, backupEnabled, autoScalingEnabled, scaleDownEnabled, minSizeName, maxSizeName string) string {
61108
return fmt.Sprintf(`
62109
resource "mongodbatlas_cluster" "basic_ds" {
@@ -102,3 +149,48 @@ func testAccDataSourceMongoDBAtlasClusterConfig(projectID, name, backupEnabled,
102149
}
103150
`, projectID, name, backupEnabled, autoScalingEnabled, scaleDownEnabled, minSizeName, maxSizeName)
104151
}
152+
153+
func testAccDataSourceMongoDBAtlasClusterConfigAdvancedConf(projectID, name string, autoscalingEnabled bool, p *matlas.ProcessArgs) string {
154+
return fmt.Sprintf(`
155+
resource "mongodbatlas_cluster" "test" {
156+
project_id = %[1]q
157+
name = %[2]q
158+
disk_size_gb = 10
159+
160+
cluster_type = "REPLICASET"
161+
replication_specs {
162+
num_shards = 1
163+
regions_config {
164+
region_name = "US_EAST_2"
165+
electable_nodes = 3
166+
priority = 7
167+
read_only_nodes = 0
168+
}
169+
}
170+
171+
provider_name = "AWS"
172+
provider_instance_size_name = "M10"
173+
174+
backup_enabled = false
175+
auto_scaling_disk_gb_enabled = %[3]t
176+
mongo_db_major_version = "4.0"
177+
178+
advanced_configuration {
179+
fail_index_key_too_long = %[4]t
180+
javascript_enabled = %[5]t
181+
minimum_enabled_tls_protocol = %[6]q
182+
no_table_scan = %[7]t
183+
oplog_size_mb = %[8]d
184+
sample_size_bi_connector = %[9]d
185+
sample_refresh_interval_bi_connector = %[10]d
186+
}
187+
}
188+
189+
data "mongodbatlas_cluster" "test" {
190+
project_id = mongodbatlas_cluster.test.project_id
191+
name = mongodbatlas_cluster.test.name
192+
}
193+
`, projectID, name, autoscalingEnabled,
194+
*p.FailIndexKeyTooLong, *p.JavascriptEnabled, p.MinimumEnabledTLSProtocol, *p.NoTableScan,
195+
*p.OplogSizeMB, *p.SampleSizeBIConnector, *p.SampleRefreshIntervalBIConnector)
196+
}

mongodbatlas/data_source_mongodbatlas_clusters.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func dataSourceMongoDBAtlasClusters() *schema.Resource {
2929
Type: schema.TypeString,
3030
Computed: true,
3131
},
32+
"advanced_configuration": clusterAdvancedConfigurationSchemaComputed(),
3233
"auto_scaling_disk_gb_enabled": {
3334
Type: schema.TypeBool,
3435
Computed: true,
@@ -352,7 +353,11 @@ func flattenClusters(ctx context.Context, d *schema.ResourceData, conn *matlas.C
352353
log.Printf("[WARN] Error setting `snapshot_backup_policy` for the cluster(%s): %s", clusters[i].ID, err)
353354
}
354355

356+
processArgs, _, err := conn.Clusters.GetProcessArgs(ctx, clusters[i].GroupID, clusters[i].Name)
357+
log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", clusters[i].ID, err)
358+
355359
result := map[string]interface{}{
360+
"advanced_configuration": flattenProcessArgs(processArgs),
356361
"auto_scaling_compute_enabled": clusters[i].AutoScaling.Compute.Enabled,
357362
"auto_scaling_compute_scale_down_enabled": clusters[i].AutoScaling.Compute.ScaleDownEnabled,
358363
"auto_scaling_disk_gb_enabled": clusters[i].BackupEnabled,

mongodbatlas/data_source_mongodbatlas_clusters_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/mwielbut/pointy"
1011
matlas "go.mongodb.org/atlas/mongodbatlas"
1112
)
1213

@@ -51,6 +52,50 @@ func TestAccDataSourceMongoDBAtlasClusters_basic(t *testing.T) {
5152
})
5253
}
5354

55+
func TestAccDataSourceMongoDBAtlasClusters_advancedConf(t *testing.T) {
56+
var (
57+
cluster matlas.Cluster
58+
resourceName = "mongodbatlas_cluster.test"
59+
dataSourceName = "data.mongodbatlas_clusters.test"
60+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
61+
name = fmt.Sprintf("test-acc-%s", acctest.RandString(10))
62+
)
63+
64+
resource.ParallelTest(t, resource.TestCase{
65+
PreCheck: func() { testAccPreCheck(t) },
66+
ProviderFactories: testAccProviderFactories,
67+
CheckDestroy: testAccCheckMongoDBAtlasClusterDestroy,
68+
Steps: []resource.TestStep{
69+
{
70+
Config: testAccDataSourceMongoDBAtlasClustersConfigAdvancedConf(projectID, name, false, &matlas.ProcessArgs{
71+
FailIndexKeyTooLong: pointy.Bool(true),
72+
JavascriptEnabled: pointy.Bool(true),
73+
MinimumEnabledTLSProtocol: "TLS1_1",
74+
NoTableScan: pointy.Bool(false),
75+
OplogSizeMB: pointy.Int64(1000),
76+
SampleRefreshIntervalBIConnector: pointy.Int64(310),
77+
SampleSizeBIConnector: pointy.Int64(110),
78+
}),
79+
Check: resource.ComposeTestCheckFunc(
80+
testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster),
81+
testAccCheckMongoDBAtlasClusterAttributes(&cluster, name),
82+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
83+
resource.TestCheckResourceAttr(resourceName, "name", name),
84+
resource.TestCheckResourceAttr(resourceName, "disk_size_gb", "10"),
85+
resource.TestCheckResourceAttrSet(resourceName, "mongo_uri"),
86+
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"),
87+
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.regions_config.#"),
88+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
89+
resource.TestCheckResourceAttrSet(dataSourceName, "results.#"),
90+
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.replication_specs.#"),
91+
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.name"),
92+
resource.TestCheckResourceAttr(dataSourceName, "results.0.version_release_system", "LTS"),
93+
),
94+
},
95+
},
96+
})
97+
}
98+
5499
func testAccDataSourceMongoDBAtlasClustersConfig(projectID, name, backupEnabled, autoScalingEnabled, scaleDownEnabled, minSizeName, maxSizeName string) string {
55100
return fmt.Sprintf(`
56101
resource "mongodbatlas_cluster" "basic_ds" {
@@ -96,3 +141,47 @@ func testAccDataSourceMongoDBAtlasClustersConfig(projectID, name, backupEnabled,
96141
}
97142
`, projectID, name, backupEnabled, autoScalingEnabled, scaleDownEnabled, minSizeName, maxSizeName)
98143
}
144+
145+
func testAccDataSourceMongoDBAtlasClustersConfigAdvancedConf(projectID, name string, autoscalingEnabled bool, p *matlas.ProcessArgs) string {
146+
return fmt.Sprintf(`
147+
resource "mongodbatlas_cluster" "test" {
148+
project_id = %[1]q
149+
name = %[2]q
150+
disk_size_gb = 10
151+
152+
cluster_type = "REPLICASET"
153+
replication_specs {
154+
num_shards = 1
155+
regions_config {
156+
region_name = "US_EAST_2"
157+
electable_nodes = 3
158+
priority = 7
159+
read_only_nodes = 0
160+
}
161+
}
162+
163+
provider_name = "AWS"
164+
provider_instance_size_name = "M10"
165+
166+
backup_enabled = false
167+
auto_scaling_disk_gb_enabled = %[3]t
168+
mongo_db_major_version = "4.0"
169+
170+
advanced_configuration {
171+
fail_index_key_too_long = %[4]t
172+
javascript_enabled = %[5]t
173+
minimum_enabled_tls_protocol = %[6]q
174+
no_table_scan = %[7]t
175+
oplog_size_mb = %[8]d
176+
sample_size_bi_connector = %[9]d
177+
sample_refresh_interval_bi_connector = %[10]d
178+
}
179+
}
180+
181+
data "mongodbatlas_clusters" "test" {
182+
project_id = mongodbatlas_cluster.test.project_id
183+
}
184+
`, projectID, name, autoscalingEnabled,
185+
*p.FailIndexKeyTooLong, *p.JavascriptEnabled, p.MinimumEnabledTLSProtocol, *p.NoTableScan,
186+
*p.OplogSizeMB, *p.SampleSizeBIConnector, *p.SampleRefreshIntervalBIConnector)
187+
}

website/docs/d/cluster.html.markdown

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ In addition to all arguments above, the following attributes are exported:
122122

123123
* `version_release_system` - Release cadence that Atlas uses for this cluster.
124124

125+
* `advanced_configuration` - Get the advanced configuration options. See [Advanced Configuration](#advanced-configuration) below for more details.
126+
125127
### BI Connector
126128

127129
Indicates BI Connector for Atlas configuration.
@@ -194,6 +196,22 @@ Contains a key-value pair that tags that the cluster was created by a Terraform
194196
* `snapshot_backup_policy.#.policies.#.policy_item.#.retention_unit` - The unit of time in which snapshot retention is measured (days, weeks, months).
195197
* `snapshot_backup_policy.#.policies.#.policy_item.#.retention_value` - The number of days, weeks, or months the snapshot is retained.
196198

199+
#### Advanced Configuration
200+
201+
* `default_read_concern` - [Default level of acknowledgment requested from MongoDB for read operations](https://docs.mongodb.com/manual/reference/read-concern/) set for this cluster. MongoDB 4.4 clusters default to [available](https://docs.mongodb.com/manual/reference/read-concern-available/).
202+
* `default_write_concern` - [Default level of acknowledgment requested from MongoDB for write operations](https://docs.mongodb.com/manual/reference/write-concern/) set for this cluster. MongoDB 4.4 clusters default to [1](https://docs.mongodb.com/manual/reference/write-concern/).
203+
* `fail_index_key_too_long` - When true, documents can only be updated or inserted if, for all indexed fields on the target collection, the corresponding index entries do not exceed 1024 bytes. When false, mongod writes documents that exceed the limit but does not index them.
204+
* `javascript_enabled` - When true, the cluster allows execution of operations that perform server-side executions of JavaScript. When false, the cluster disables execution of those operations.
205+
* `minimum_enabled_tls_protocol` - Sets the minimum Transport Layer Security (TLS) version the cluster accepts for incoming connections.Valid values are:
206+
207+
- TLS1_0
208+
- TLS1_1
209+
- TLS1_2
210+
211+
* `no_table_scan` - When true, the cluster disables the execution of any query that requires a collection scan to return results. When false, the cluster allows the execution of those operations.
212+
* `oplog_size_mb` - The custom oplog size of the cluster. Without a value that indicates that the cluster uses the default oplog size calculated by Atlas.
213+
* `sample_size_bi_connector` - Number of documents per database to sample when gathering schema information. Defaults to 100. Available only for Atlas deployments in which BI Connector for Atlas is enabled.
214+
* `sample_refresh_interval_bi_connector` - Interval in seconds at which the mongosqld process re-samples data to create its relational schema. The default value is 300. The specified value must be a positive integer. Available only for Atlas deployments in which BI Connector for Atlas is enabled.
197215

198216

199217
See detailed information for arguments and attributes: [MongoDB API Clusters](https://docs.atlas.mongodb.com/reference/api/clusters-create-one/)

website/docs/d/clusters.html.markdown

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ In addition to all arguments above, the following attributes are exported:
123123

124124
* `version_release_system` - Release cadence that Atlas uses for this cluster.
125125

126+
* `advanced_configuration` - Get the advanced configuration options. See [Advanced Configuration](#advanced-configuration) below for more details.
126127

127128
### BI Connector
128129

@@ -195,5 +196,22 @@ Contains a key-value pair that tags that the cluster was created by a Terraform
195196
* `snapshot_backup_policy.#.policies.#.policy_item.#.retention_unit` - The unit of time in which snapshot retention is measured (days, weeks, months).
196197
* `snapshot_backup_policy.#.policies.#.policy_item.#.retention_value` - The number of days, weeks, or months the snapshot is retained.
197198

199+
#### Advanced Configuration
200+
201+
* `default_read_concern` - [Default level of acknowledgment requested from MongoDB for read operations](https://docs.mongodb.com/manual/reference/read-concern/) set for this cluster. MongoDB 4.4 clusters default to [available](https://docs.mongodb.com/manual/reference/read-concern-available/).
202+
* `default_write_concern` - [Default level of acknowledgment requested from MongoDB for write operations](https://docs.mongodb.com/manual/reference/write-concern/) set for this cluster. MongoDB 4.4 clusters default to [1](https://docs.mongodb.com/manual/reference/write-concern/).
203+
* `fail_index_key_too_long` - When true, documents can only be updated or inserted if, for all indexed fields on the target collection, the corresponding index entries do not exceed 1024 bytes. When false, mongod writes documents that exceed the limit but does not index them.
204+
* `javascript_enabled` - When true, the cluster allows execution of operations that perform server-side executions of JavaScript. When false, the cluster disables execution of those operations.
205+
* `minimum_enabled_tls_protocol` - Sets the minimum Transport Layer Security (TLS) version the cluster accepts for incoming connections.Valid values are:
206+
207+
- TLS1_0
208+
- TLS1_1
209+
- TLS1_2
210+
211+
* `no_table_scan` - When true, the cluster disables the execution of any query that requires a collection scan to return results. When false, the cluster allows the execution of those operations.
212+
* `oplog_size_mb` - The custom oplog size of the cluster. Without a value that indicates that the cluster uses the default oplog size calculated by Atlas.
213+
* `sample_size_bi_connector` - Number of documents per database to sample when gathering schema information. Defaults to 100. Available only for Atlas deployments in which BI Connector for Atlas is enabled.
214+
* `sample_refresh_interval_bi_connector` - Interval in seconds at which the mongosqld process re-samples data to create its relational schema. The default value is 300. The specified value must be a positive integer. Available only for Atlas deployments in which BI Connector for Atlas is enabled.
215+
198216

199217
See detailed information for arguments and attributes: [MongoDB API Clusters](https://docs.atlas.mongodb.com/reference/api/clusters-create-one/)

0 commit comments

Comments
 (0)