Skip to content

Commit

Permalink
INTMDB-252: Added two parameters for cluster advanced configuration (#…
Browse files Browse the repository at this point in the history
…558)

* chore: updated vendor

* pass nil in another parameter for create a project

* feat: added two parameters for cluster advance configuration proccess args

* updated docs

* moved to another func to fix the linter

* align a code

* align code

* format tf

* rollback to getok exists because it fails the test for some reason

Co-authored-by: Edgar Lopez <edgarlopez@pop-os.localdomain>
  • Loading branch information
coderGo93 and Edgar Lopez committed Sep 15, 2021
1 parent 051fb72 commit 2d5ff92
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 47 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ require (
github.com/mwielbut/pointy v1.1.0
github.com/spf13/cast v1.4.1
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20210625132053-af2d5c0ad54f
go.mongodb.org/atlas v0.12.0
go.mongodb.org/atlas v0.12.1-0.20210908180847-a8f9b16316e3
go.mongodb.org/realm v0.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,8 @@ go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mI
go.etcd.io/etcd v0.0.0-20200513171258-e048e166ab9c/go.mod h1:xCI7ZzBfRuGgBXyXO6yfWfDmlWd35khcWpUa4L0xI/k=
go.mongodb.org/atlas v0.12.0 h1:/vnHX3rh8jdPrP8mRznuU/2VrGH+cCdz8/Esrzpvaus=
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/atlas v0.12.1-0.20210908180847-a8f9b16316e3 h1:cD6BlSgXT7sPYdp3ZUARdJ96JyKYueMgGicX7ZjgwsQ=
go.mongodb.org/atlas v0.12.1-0.20210908180847-a8f9b16316e3/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.mozilla.org/mozlog v0.0.0-20170222151521-4bb13139d403/go.mod h1:jHoPAGnDrCy6kaI2tAze5Prf0Nr0w/oNkROt2lw3n3o=
Expand Down
114 changes: 69 additions & 45 deletions mongodbatlas/resource_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,51 +311,7 @@ func resourceMongoDBAtlasCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"advanced_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"fail_index_key_too_long": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"javascript_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"minimum_enabled_tls_protocol": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"no_table_scan": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"oplog_size_mb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"sample_size_bi_connector": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"sample_refresh_interval_bi_connector": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
},
},
"advanced_configuration": clusterAdvancedConfigurationSchema(),
"labels": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -1304,6 +1260,14 @@ func flattenRegionsConfig(regionsConfig map[string]matlas.RegionsConfig) []map[s
func expandProcessArgs(d *schema.ResourceData, p map[string]interface{}) *matlas.ProcessArgs {
res := &matlas.ProcessArgs{}

if _, ok := d.GetOkExists("advanced_configuration.0.default_read_concern"); ok {
res.DefaultReadConcern = cast.ToString(p["default_read_concern"])
}

if _, ok := d.GetOkExists("advanced_configuration.0.default_write_concern"); ok {
res.DefaultWriteConcern = cast.ToString(p["default_write_concern"])
}

if _, ok := d.GetOkExists("advanced_configuration.0.fail_index_key_too_long"); ok {
res.FailIndexKeyTooLong = pointy.Bool(cast.ToBool(p["fail_index_key_too_long"]))
}
Expand Down Expand Up @@ -1342,6 +1306,8 @@ func expandProcessArgs(d *schema.ResourceData, p map[string]interface{}) *matlas
func flattenProcessArgs(p *matlas.ProcessArgs) []interface{} {
return []interface{}{
map[string]interface{}{
"default_read_concern": p.DefaultReadConcern,
"default_write_concern": p.DefaultWriteConcern,
"fail_index_key_too_long": cast.ToBool(p.FailIndexKeyTooLong),
"javascript_enabled": cast.ToBool(p.JavascriptEnabled),
"minimum_enabled_tls_protocol": p.MinimumEnabledTLSProtocol,
Expand Down Expand Up @@ -1550,3 +1516,61 @@ func isEqualProviderAutoScalingMaxInstanceSize(k, old, newStr string, d *schema.
}
return true
}

func clusterAdvancedConfigurationSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_read_concern": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"default_write_concern": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"fail_index_key_too_long": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"javascript_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"minimum_enabled_tls_protocol": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"no_table_scan": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"oplog_size_mb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"sample_size_bi_connector": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"sample_refresh_interval_bi_connector": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
},
}
}
134 changes: 134 additions & 0 deletions mongodbatlas/resource_mongodbatlas_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,65 @@ func TestAccResourceMongoDBAtlasCluster_basic_Partial_AdvancedConf(t *testing.T)
})
}

func TestAccResourceMongoDBAtlasCluster_basic_DefaultWriteRead_AdvancedConf(t *testing.T) {
var (
cluster matlas.Cluster
resourceName = "mongodbatlas_cluster.advance_conf"
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
name = fmt.Sprintf("test-acc-%s", acctest.RandString(10))
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasClusterConfigAdvancedConfDefaultWriteRead(projectID, name, "false", &matlas.ProcessArgs{
DefaultReadConcern: "available",
DefaultWriteConcern: "1",
FailIndexKeyTooLong: pointy.Bool(true),
JavascriptEnabled: pointy.Bool(true),
MinimumEnabledTLSProtocol: "TLS1_1",
NoTableScan: pointy.Bool(false),
OplogSizeMB: pointy.Int64(1000),
SampleRefreshIntervalBIConnector: pointy.Int64(310),
SampleSizeBIConnector: pointy.Int64(110),
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasClusterAttributes(&cluster, name),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.default_read_concern", "available"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.default_write_concern", "1"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.javascript_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.minimum_enabled_tls_protocol", "TLS1_1"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.no_table_scan", "false"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.oplog_size_mb", "1000"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.sample_refresh_interval_bi_connector", "310"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.sample_size_bi_connector", "110"),
),
},
{
Config: testAccMongoDBAtlasClusterConfigAdvancedConfPartialDefault(projectID, name, "false", &matlas.ProcessArgs{
MinimumEnabledTLSProtocol: "TLS1_2",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasClusterAttributes(&cluster, name),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.default_read_concern", "available"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.default_write_concern", "1"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.javascript_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.minimum_enabled_tls_protocol", "TLS1_2"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.no_table_scan", "false"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.oplog_size_mb", "1000"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.sample_refresh_interval_bi_connector", "310"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.sample_size_bi_connector", "110"),
),
},
},
})
}

func TestAccResourceMongoDBAtlasCluster_emptyAdvancedConf(t *testing.T) {
var (
resourceName = "mongodbatlas_cluster.advance_conf"
Expand Down Expand Up @@ -1199,6 +1258,47 @@ func testAccMongoDBAtlasClusterConfigAdvancedConf(projectID, name, autoscalingEn
*p.OplogSizeMB, *p.SampleSizeBIConnector, *p.SampleRefreshIntervalBIConnector)
}

func testAccMongoDBAtlasClusterConfigAdvancedConfDefaultWriteRead(projectID, name, autoscalingEnabled string, p *matlas.ProcessArgs) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "advance_conf" {
project_id = %[1]q
name = %[2]q
disk_size_gb = 10
cluster_type = "REPLICASET"
replication_specs {
num_shards = 1
regions_config {
region_name = "EU_CENTRAL_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}
}
backup_enabled = false
auto_scaling_disk_gb_enabled = %[3]s
mongo_db_major_version = "4.4"
// Provider Settings "block"
provider_name = "AWS"
provider_instance_size_name = "M10"
advanced_configuration {
default_read_concern = %[10]q
default_write_concern = %[11]q
javascript_enabled = %[4]t
minimum_enabled_tls_protocol = %[5]q
no_table_scan = %[6]t
oplog_size_mb = %[7]d
sample_size_bi_connector = %[8]d
sample_refresh_interval_bi_connector = %[9]d
}
}
`, projectID, name, autoscalingEnabled,
*p.JavascriptEnabled, p.MinimumEnabledTLSProtocol, *p.NoTableScan,
*p.OplogSizeMB, *p.SampleSizeBIConnector, *p.SampleRefreshIntervalBIConnector, p.DefaultReadConcern, p.DefaultWriteConcern)
}

func testAccMongoDBAtlasClusterConfigAdvancedConfPartial(projectID, name, autoscalingEnabled string, p *matlas.ProcessArgs) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "advance_conf" {
Expand Down Expand Up @@ -1233,6 +1333,40 @@ func testAccMongoDBAtlasClusterConfigAdvancedConfPartial(projectID, name, autosc
`, projectID, name, autoscalingEnabled, p.MinimumEnabledTLSProtocol)
}

func testAccMongoDBAtlasClusterConfigAdvancedConfPartialDefault(projectID, name, autoscalingEnabled string, p *matlas.ProcessArgs) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "advance_conf" {
project_id = "%s"
name = "%s"
disk_size_gb = 10
cluster_type = "REPLICASET"
replication_specs {
num_shards = 1
regions_config {
region_name = "EU_CENTRAL_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}
}
backup_enabled = false
auto_scaling_disk_gb_enabled = %s
mongo_db_major_version = "4.4"
// Provider Settings "block"
provider_name = "AWS"
provider_instance_size_name = "M10"
provider_region_name = "EU_CENTRAL_1"
advanced_configuration {
minimum_enabled_tls_protocol = "%s"
}
}
`, projectID, name, autoscalingEnabled, p.MinimumEnabledTLSProtocol)
}

func testAccMongoDBAtlasClusterConfigAzure(projectID, name, backupEnabled string) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "basic_azure" {
Expand Down
2 changes: 1 addition & 1 deletion mongodbatlas/resource_mongodbatlas_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func resourceMongoDBAtlasProjectCreate(ctx context.Context, d *schema.ResourceDa
Name: d.Get("name").(string),
}

project, _, err := conn.Projects.Create(ctx, projectReq)
project, _, err := conn.Projects.Create(ctx, projectReq, nil)
if err != nil {
return diag.FromErr(fmt.Errorf(errorProjectCreate, err))
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ Include **desired options** within advanced_configuration:
}
```

* `default_read_concern` - (Optional) [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/).
* `default_write_concern` - (Optional) [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/).
* `fail_index_key_too_long` - (Optional) 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.
* `javascript_enabled` - (Optional) When true, the cluster allows execution of operations that perform server-side executions of JavaScript. When false, the cluster disables execution of those operations.
* `minimum_enabled_tls_protocol` - (Optional) Sets the minimum Transport Layer Security (TLS) version the cluster accepts for incoming connections.Valid values are:
Expand Down

0 comments on commit 2d5ff92

Please sign in to comment.