From 58b3e9d46d484f0067d1fb6c1154534a56e7b8de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20L=C3=B3pez?= Date: Tue, 23 Mar 2021 18:37:17 -0700 Subject: [PATCH 1/4] refactor: deprecated bi_connector and added parameter bi_connector_config because of issues typemap related in tf 14 --- .../data_source_mongodbatlas_cluster.go | 26 ++++++- .../data_source_mongodbatlas_clusters.go | 23 +++++- mongodbatlas/resource_mongodbatlas_cluster.go | 76 +++++++++++++++++-- 3 files changed, 116 insertions(+), 9 deletions(-) diff --git a/mongodbatlas/data_source_mongodbatlas_cluster.go b/mongodbatlas/data_source_mongodbatlas_cluster.go index 89afc89771..d1d510b1d7 100644 --- a/mongodbatlas/data_source_mongodbatlas_cluster.go +++ b/mongodbatlas/data_source_mongodbatlas_cluster.go @@ -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": { @@ -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, @@ -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) } diff --git a/mongodbatlas/data_source_mongodbatlas_clusters.go b/mongodbatlas/data_source_mongodbatlas_clusters.go index 8f7bb77e48..39e96dbd2c 100644 --- a/mongodbatlas/data_source_mongodbatlas_clusters.go +++ b/mongodbatlas/data_source_mongodbatlas_clusters.go @@ -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": { @@ -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, @@ -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, diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 532cad5cb0..01489de254 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -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": { @@ -472,6 +492,11 @@ func resourceMongoDBAtlasClusterCreate(d *schema.ResourceData, meta interface{}) return fmt.Errorf(errorClusterCreate, err) } + biConnector, err = expandBiConnectorConfig(d) + if err != nil { + return fmt.Errorf(errorClusterCreate, err) + } + providerSettings, err := expandProviderSetting(d) if err != nil { return fmt.Errorf(errorClusterCreate, err) @@ -676,8 +701,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 { @@ -746,6 +777,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") || @@ -956,6 +991,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 @@ -973,6 +1009,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{}) @@ -987,6 +1044,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) From ef7d28ee4d80c4e691a361bb77decdb1a03973d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20L=C3=B3pez?= Date: Tue, 23 Mar 2021 18:37:36 -0700 Subject: [PATCH 2/4] test: added to test to validate if it works or not --- .../resource_mongodbatlas_cluster_test.go | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/mongodbatlas/resource_mongodbatlas_cluster_test.go b/mongodbatlas/resource_mongodbatlas_cluster_test.go index f2e60f14d7..9079063fca 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster_test.go +++ b/mongodbatlas/resource_mongodbatlas_cluster_test.go @@ -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 @@ -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" { From 4182418d245d6ba6fdcc7cdcc3971b0487cbf932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20L=C3=B3pez?= Date: Tue, 23 Mar 2021 18:37:58 -0700 Subject: [PATCH 3/4] docs: updated docs for deprecated and added parameter bi_connector_config --- website/docs/d/cluster.html.markdown | 3 ++- website/docs/d/clusters.html.markdown | 3 ++- website/docs/r/cluster.html.markdown | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/website/docs/d/cluster.html.markdown b/website/docs/d/cluster.html.markdown index 2152aaf671..2b4ab66469 100644 --- a/website/docs/d/cluster.html.markdown +++ b/website/docs/d/cluster.html.markdown @@ -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. diff --git a/website/docs/d/clusters.html.markdown b/website/docs/d/clusters.html.markdown index 39cb2345c8..91652b8e51 100644 --- a/website/docs/d/clusters.html.markdown +++ b/website/docs/d/clusters.html.markdown @@ -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. diff --git a/website/docs/r/cluster.html.markdown b/website/docs/r/cluster.html.markdown index 4aa3acaaa5..f0d480b173 100644 --- a/website/docs/r/cluster.html.markdown +++ b/website/docs/r/cluster.html.markdown @@ -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?** From 961910ffd0216a48be74ad329c4ebd504af32286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20L=C3=B3pez?= Date: Tue, 23 Mar 2021 19:09:18 -0700 Subject: [PATCH 4/4] fix: fixes linter code --- mongodbatlas/resource_mongodbatlas_cluster.go | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 01489de254..538f75b3c1 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -487,16 +487,6 @@ func resourceMongoDBAtlasClusterCreate(d *schema.ResourceData, meta interface{}) } } - biConnector, err := expandBiConnector(d) - if err != nil { - return fmt.Errorf(errorClusterCreate, err) - } - - biConnector, err = expandBiConnectorConfig(d) - if err != nil { - return fmt.Errorf(errorClusterCreate, err) - } - providerSettings, err := expandProviderSetting(d) if err != nil { return fmt.Errorf(errorClusterCreate, err) @@ -515,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") } @@ -991,7 +996,7 @@ func splitSClusterImportID(id string) (projectID, clusterName *string, err error return } -// Deprecated, will be deleted later +// Deprecated: will be deleted later func expandBiConnector(d *schema.ResourceData) (*matlas.BiConnector, error) { var biConnector matlas.BiConnector @@ -1029,7 +1034,7 @@ func expandBiConnectorConfig(d *schema.ResourceData) (*matlas.BiConnector, error return &biConnector, nil } -// Deprecated, will be deleted later +// Deprecated: will be deleted later func flattenBiConnector(biConnector *matlas.BiConnector) map[string]interface{} { biConnectorMap := make(map[string]interface{})