Skip to content

Commit

Permalink
INTMDB-155: Fixes a bug related to bi_connector cluster by deprecating (
Browse files Browse the repository at this point in the history
#423)

* refactor: deprecated bi_connector and added parameter bi_connector_config because of issues typemap related in tf 14

* test: added to test to validate if it works or not

* docs: updated docs for deprecated and added parameter bi_connector_config

* fix: fixes linter code

Co-authored-by: Edgar López <[email protected]>
  • Loading branch information
coderGo93 and Edgar López authored Mar 29, 2021
1 parent 9319bbf commit d3a25a5
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 18 deletions.
26 changes: 24 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func dataSourceMongoDBAtlasCluster() *schema.Resource {
Computed: true,
},
"bi_connector": {
Type: schema.TypeMap,
Computed: true,
Type: schema.TypeMap,
Computed: true,
Deprecated: "use bi_connector_config instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Expand All @@ -54,6 +55,23 @@ func dataSourceMongoDBAtlasCluster() *schema.Resource {
},
},
},
"bi_connector_config": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"read_preference": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"cluster_type": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -391,6 +409,10 @@ func dataSourceMongoDBAtlasClusterRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf(errorClusterSetting, "bi_connector", clusterName, err)
}

if err := d.Set("bi_connector_config", flattenBiConnectorConfig(cluster.BiConnector)); err != nil {
return fmt.Errorf(errorClusterSetting, "bi_connector_config", clusterName, err)
}

if cluster.ProviderSettings != nil {
flattenProviderSettings(d, cluster.ProviderSettings, clusterName)
}
Expand Down
23 changes: 21 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func dataSourceMongoDBAtlasClusters() *schema.Resource {
Computed: true,
},
"bi_connector": {
Type: schema.TypeMap,
Computed: true,
Type: schema.TypeMap,
Computed: true,
Deprecated: "use bi_connector_config instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Expand All @@ -61,6 +62,23 @@ func dataSourceMongoDBAtlasClusters() *schema.Resource {
},
},
},
"bi_connector_config": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"read_preference": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"cluster_type": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -370,6 +388,7 @@ func flattenClusters(d *schema.ResourceData, conn *matlas.Client, clusters []mat
"provider_name": clusters[i].ProviderSettings.ProviderName,
"provider_region_name": clusters[i].ProviderSettings.RegionName,
"bi_connector": flattenBiConnector(clusters[i].BiConnector),
"bi_connector_config": flattenBiConnectorConfig(clusters[i].BiConnector),
"replication_specs": flattenReplicationSpecs(clusters[i].ReplicationSpecs),
"labels": flattenLabels(clusters[i].Labels),
"snapshot_backup_policy": snapshotBackupPolicy,
Expand Down
93 changes: 82 additions & 11 deletions mongodbatlas/resource_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,34 @@ func resourceMongoDBAtlasCluster() *schema.Resource {
Default: false,
},
"bi_connector": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Type: schema.TypeMap,
Optional: true,
Deprecated: "use bi_connector_config instead",
ConflictsWith: []string{"bi_connector_config"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeString,
Optional: true,
},
"read_preference": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"bi_connector_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
ConflictsWith: []string{"bi_connector"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"read_preference": {
Expand Down Expand Up @@ -467,11 +487,6 @@ func resourceMongoDBAtlasClusterCreate(d *schema.ResourceData, meta interface{})
}
}

biConnector, err := expandBiConnector(d)
if err != nil {
return fmt.Errorf(errorClusterCreate, err)
}

providerSettings, err := expandProviderSetting(d)
if err != nil {
return fmt.Errorf(errorClusterCreate, err)
Expand All @@ -490,11 +505,26 @@ func resourceMongoDBAtlasClusterCreate(d *schema.ResourceData, meta interface{})
ProviderBackupEnabled: pointy.Bool(d.Get("provider_backup_enabled").(bool)),
PitEnabled: pointy.Bool(d.Get("pit_enabled").(bool)),
AutoScaling: autoScaling,
BiConnector: biConnector,
ProviderSettings: providerSettings,
ReplicationSpecs: replicationSpecs,
}

if _, ok := d.GetOk("bi_connector"); ok {
biConnector, err := expandBiConnector(d)
if err != nil {
return fmt.Errorf(errorClusterCreate, err)
}
clusterRequest.BiConnector = biConnector
}

if _, ok := d.GetOk("bi_connector_config"); ok {
biConnector, err := expandBiConnector(d)
if err != nil {
return fmt.Errorf(errorClusterCreate, err)
}
clusterRequest.BiConnector = biConnector
}

if containsLabelOrKey(expandLabelSliceFromSetSchema(d), defaultLabel) {
return fmt.Errorf("you should not set `Infrastructure Tool` label, it is used for internal purposes")
}
Expand Down Expand Up @@ -676,8 +706,14 @@ func resourceMongoDBAtlasClusterRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf(errorClusterSetting, "state_name", clusterName, err)
}

if err := d.Set("bi_connector", flattenBiConnector(cluster.BiConnector)); err != nil {
return fmt.Errorf(errorClusterSetting, "bi_connector", clusterName, err)
if _, ok := d.GetOk("bi_connector"); ok {
if err = d.Set("bi_connector", flattenBiConnector(cluster.BiConnector)); err != nil {
return fmt.Errorf(errorClusterSetting, "bi_connector", clusterName, err)
}
}

if err := d.Set("bi_connector_config", flattenBiConnectorConfig(cluster.BiConnector)); err != nil {
return fmt.Errorf(errorClusterSetting, "bi_connector_config", clusterName, err)
}

if cluster.ProviderSettings != nil {
Expand Down Expand Up @@ -746,6 +782,10 @@ func resourceMongoDBAtlasClusterUpdate(d *schema.ResourceData, meta interface{})
cluster.BiConnector, _ = expandBiConnector(d)
}

if d.HasChange("bi_connector_config") {
cluster.BiConnector, _ = expandBiConnectorConfig(d)
}

// If at least one of the provider settings argument has changed, expand all provider settings
if d.HasChange("provider_disk_iops") ||
d.HasChange("backing_provider_name") ||
Expand Down Expand Up @@ -956,6 +996,7 @@ func splitSClusterImportID(id string) (projectID, clusterName *string, err error
return
}

// Deprecated: will be deleted later
func expandBiConnector(d *schema.ResourceData) (*matlas.BiConnector, error) {
var biConnector matlas.BiConnector

Expand All @@ -973,6 +1014,27 @@ func expandBiConnector(d *schema.ResourceData) (*matlas.BiConnector, error) {
return &biConnector, nil
}

func expandBiConnectorConfig(d *schema.ResourceData) (*matlas.BiConnector, error) {
var biConnector matlas.BiConnector

if v, ok := d.GetOk("bi_connector_config"); ok {
biConn := v.([]interface{})
if len(biConn) > 0 {
biConnMap := biConn[0].(map[string]interface{})

enabled := cast.ToBool(biConnMap["enabled"])

biConnector = matlas.BiConnector{
Enabled: &enabled,
ReadPreference: cast.ToString(biConnMap["read_preference"]),
}
}
}

return &biConnector, nil
}

// Deprecated: will be deleted later
func flattenBiConnector(biConnector *matlas.BiConnector) map[string]interface{} {
biConnectorMap := make(map[string]interface{})

Expand All @@ -987,6 +1049,15 @@ func flattenBiConnector(biConnector *matlas.BiConnector) map[string]interface{}
return biConnectorMap
}

func flattenBiConnectorConfig(biConnector *matlas.BiConnector) []interface{} {
return []interface{}{
map[string]interface{}{
"enabled": *biConnector.Enabled,
"read_preference": biConnector.ReadPreference,
},
}
}

func getInstanceSizeToInt(instanceSize string) int {
regex := regexp.MustCompile(`\d+`)
num := regex.FindString(instanceSize)
Expand Down
69 changes: 69 additions & 0 deletions mongodbatlas/resource_mongodbatlas_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,51 @@ func TestAccResourceMongoDBAtlasCluster_basicGCP(t *testing.T) {
})
}

func TestAccResourceMongoDBAtlasCluster_WithBiConnectorGCP(t *testing.T) {
var (
cluster matlas.Cluster
resourceName = "mongodbatlas_cluster.basic_gcp"
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) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasClusterConfigGCPWithBiConnector(projectID, name, "true", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasClusterAttributes(&cluster, name),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "disk_size_gb", "40"),
resource.TestCheckResourceAttrSet(resourceName, "mongo_uri"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.regions_config.#"),
resource.TestCheckResourceAttr(resourceName, "bi_connector_config.0.enabled", "false"),
),
},
{
Config: testAccMongoDBAtlasClusterConfigGCPWithBiConnector(projectID, name, "false", true),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasClusterAttributes(&cluster, name),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "disk_size_gb", "40"),
resource.TestCheckResourceAttrSet(resourceName, "mongo_uri"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"),
resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.regions_config.#"),
resource.TestCheckResourceAttr(resourceName, "bi_connector_config.0.enabled", "true"),
),
},
},
})
}

func TestAccResourceMongoDBAtlasCluster_MultiRegion(t *testing.T) {
var (
cluster matlas.Cluster
Expand Down Expand Up @@ -1065,6 +1110,30 @@ func testAccMongoDBAtlasClusterConfigGCP(projectID, name, backupEnabled string)
`, projectID, name, backupEnabled)
}

func testAccMongoDBAtlasClusterConfigGCPWithBiConnector(projectID, name, backupEnabled string, biConnectorEnabled bool) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "basic_gcp" {
project_id = "%s"
name = "%s"
disk_size_gb = 40
num_shards = 1
replication_factor = 3
provider_backup_enabled = %s
auto_scaling_disk_gb_enabled = true
mongo_db_major_version = "4.0"
// Provider Settings "block"
provider_name = "GCP"
provider_instance_size_name = "M30"
provider_region_name = "US_EAST_4"
bi_connector_config {
enabled = %t
}
}
`, projectID, name, backupEnabled, biConnectorEnabled)
}

func testAccMongoDBAtlasClusterConfigMultiRegion(projectID, name, backupEnabled string) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "multi_region" {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ In addition to all arguments above, the following attributes are exported:
* `auto_scaling_compute_scale_down_enabled` - (Optional) Set to `true` to enable the cluster tier to scale down.

* `backup_enabled` - Legacy Option, Indicates whether Atlas continuous backups are enabled for the cluster.
* `bi_connector` - Indicates BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
* `bi_connector` - Indicates BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details. **DEPRECATED** Use `bi_connector_config` instead.
* `bi_connector_config` - Indicates BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
* `cluster_type` - Indicates the type of the cluster that you want to modify. You cannot convert a sharded cluster deployment to a replica set deployment.
* `connection_strings` - Set of connection strings that your applications use to connect to this cluster. More info in [Connection-strings](https://docs.mongodb.com/manual/reference/connection-string/). Use the parameters in this object to connect your applications to this cluster. To learn more about the formats of connection strings, see [Connection String Options](https://docs.atlas.mongodb.com/reference/faq/connection-changes/). NOTE: Atlas returns the contents of this object after the cluster is operational, not while it builds the cluster.
- `connection_strings.standard` - Public mongodb:// connection string for this cluster.
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/clusters.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ In addition to all arguments above, the following attributes are exported:
* `auto_scaling_compute_enabled` - (Optional) Specifies whether cluster tier auto-scaling is enabled. The default is false.
* `auto_scaling_compute_scale_down_enabled` - (Optional) Set to `true` to enable the cluster tier to scale down.
* `backup_enabled` - Legacy Option, Indicates whether Atlas continuous backups are enabled for the cluster.
* `bi_connector` - Indicates BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
* `bi_connector` - Indicates BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details. **DEPRECATED** Use `bi_connector_config` instead.
* `bi_connector_config` - Indicates BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
* `cluster_type` - Indicates the type of the cluster that you want to modify. You cannot convert a sharded cluster deployment to a replica set deployment.
* `connection_strings` - Set of connection strings that your applications use to connect to this cluster. More info in [Connection-strings](https://docs.mongodb.com/manual/reference/connection-string/). Use the parameters in this object to connect your applications to this cluster. To learn more about the formats of connection strings, see [Connection String Options](https://docs.atlas.mongodb.com/reference/faq/connection-changes/). NOTE: Atlas returns the contents of this object after the cluster is operational, not while it builds the cluster.
- `connection_strings.standard` - Public mongodb:// connection string for this cluster.
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ But in order to explicitly change `provider_instance_size_name` comment the `lif
```
* The default value is false. M10 and above only.

* `bi_connector` - (Optional) Specifies BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
* `bi_connector` - (Optional) Specifies BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details. **DEPRECATED** Use `bi_connector_config` instead.
* `bi_connector_config` - (Optional) Specifies BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
* `cluster_type` - (Required) Specifies the type of the cluster that you want to modify. You cannot convert a sharded cluster deployment to a replica set deployment.

-> **WHEN SHOULD YOU USE CLUSTERTYPE?**
Expand Down

0 comments on commit d3a25a5

Please sign in to comment.