From e2d6745a49220d71404f42bce6f285e0e79a9e8c Mon Sep 17 00:00:00 2001 From: Steph Date: Wed, 3 Apr 2024 16:19:28 +0200 Subject: [PATCH 1/7] deprecate connection_strings and rename enable_* properties to *_enabled in data source --- .../cosmos/cosmosdb_account_data_source.go | 70 +++++++++++++------ .../cosmos/cosmosdb_account_resource.go | 31 ++++---- 2 files changed, 67 insertions(+), 34 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_data_source.go b/internal/services/cosmos/cosmosdb_account_data_source.go index 52dca60e7ad6..c430dcbd5f97 100644 --- a/internal/services/cosmos/cosmosdb_account_data_source.go +++ b/internal/services/cosmos/cosmosdb_account_data_source.go @@ -5,6 +5,7 @@ package cosmos import ( "fmt" + "github.com/hashicorp/terraform-provider-azurerm/internal/features" "log" "sort" "time" @@ -22,7 +23,7 @@ import ( ) func dataSourceCosmosDbAccount() *pluginsdk.Resource { - return &pluginsdk.Resource{ + dataSource := &pluginsdk.Resource{ Read: dataSourceCosmosDbAccountRead, Timeouts: &pluginsdk.ResourceTimeout{ @@ -56,14 +57,12 @@ func dataSourceCosmosDbAccount() *pluginsdk.Resource { Computed: true, }, - // TODO 4.0: change this from enable_* to *_enabled - "enable_free_tier": { + "free_tier_enabled": { Type: pluginsdk.TypeBool, Computed: true, }, - // TODO 4.0: change this from enable_* to *_enabled - "enable_automatic_failover": { + "automatic_failover_enabled": { Type: pluginsdk.TypeBool, Computed: true, }, @@ -150,8 +149,7 @@ func dataSourceCosmosDbAccount() *pluginsdk.Resource { Computed: true, }, - // TODO 4.0: change this from enable_* to *_enabled - "enable_multiple_write_locations": { + "multiple_write_locations_enabled": { Type: pluginsdk.TypeBool, Computed: true, }, @@ -201,16 +199,6 @@ func dataSourceCosmosDbAccount() *pluginsdk.Resource { Sensitive: true, }, - "connection_strings": { - Type: pluginsdk.TypeList, - Computed: true, - Sensitive: true, - Elem: &pluginsdk.Schema{ - Type: pluginsdk.TypeString, - Sensitive: true, - }, - }, - "primary_sql_connection_string": { Type: pluginsdk.TypeString, Computed: true, @@ -260,6 +248,37 @@ func dataSourceCosmosDbAccount() *pluginsdk.Resource { }, }, } + + if !features.FourPointOhBeta() { + dataSource.Schema["connection_strings"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeList, + Computed: true, + Sensitive: true, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + Sensitive: true, + }, + Deprecated: "This property has been superseded by the primary and secondary connection strings for sql, mongodb and readonly and will be removed in v4.0 of the AzureRM provider", + } + dataSource.Schema["enable_free_tier"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeBool, + Computed: true, + Deprecated: "This property has been renamed to `free_tier_enabled` and will be removed in v4.0 of the AzureRM provider", + } + dataSource.Schema["enable_automatic_failover"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeBool, + Computed: true, + Deprecated: "This property has been renamed to `automatic_failover_enabled` and will be removed in v4.0 of the AzureRM provider", + } + dataSource.Schema["enable_multiple_write_locations"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeBool, + Computed: true, + Deprecated: "This property has been renamed to `multiple_write_locations_enabled` and will be removed in v4.0 of the AzureRM provider", + } + + } + + return dataSource } func dataSourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) error { @@ -293,8 +312,15 @@ func dataSourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) d.Set("ip_range_filter", common.CosmosDBIpRulesToIpRangeFilterThreePointOh(props.IPRules)) d.Set("endpoint", props.DocumentEndpoint) d.Set("is_virtual_network_filter_enabled", props.IsVirtualNetworkFilterEnabled) - d.Set("enable_free_tier", props.EnableFreeTier) - d.Set("enable_automatic_failover", props.EnableAutomaticFailover) + if !features.FourPointOhBeta() { + d.Set("enable_free_tier", props.EnableFreeTier) + d.Set("enable_automatic_failover", props.EnableAutomaticFailover) + d.Set("enable_multiple_write_locations", props.EnableMultipleWriteLocations) + } + + d.Set("free_tier_enabled", props.EnableFreeTier) + d.Set("automatic_failover_enabled", props.EnableAutomaticFailover) + d.Set("multiple_write_locations_enabled", props.EnableMultipleWriteLocations) if v := props.KeyVaultKeyUri; v != nil { d.Set("key_vault_key_id", v) @@ -368,8 +394,6 @@ func dataSourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) if err := d.Set("write_endpoints", writeEndpoints); err != nil { return fmt.Errorf("setting `write_endpoints`: %s", err) } - - d.Set("enable_multiple_write_locations", props.EnableMultipleWriteLocations) } if err := tags.FlattenAndSet(d, model.Tags); err != nil { @@ -417,7 +441,9 @@ func dataSourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) } } - d.Set("connection_strings", connStrings) + if !features.FourPointOhBeta() { + d.Set("connection_strings", connStrings) + } } } return nil diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 4001cad6415e..ee592b81fd9d 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -124,7 +124,7 @@ func suppressConsistencyPolicyStalenessConfiguration(_, _, _ string, d *pluginsd } func resourceCosmosDbAccount() *pluginsdk.Resource { - return &pluginsdk.Resource{ + resource := &pluginsdk.Resource{ Create: resourceCosmosDbAccountCreate, Read: resourceCosmosDbAccountRead, Update: resourceCosmosDbAccountUpdate, @@ -709,16 +709,6 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { Sensitive: true, }, - "connection_strings": { - Type: pluginsdk.TypeList, - Computed: true, - Sensitive: true, - Elem: &pluginsdk.Schema{ - Type: pluginsdk.TypeString, - Sensitive: true, - }, - }, - "primary_sql_connection_string": { Type: pluginsdk.TypeString, Computed: true, @@ -770,6 +760,21 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { "tags": commonschema.Tags(), }, } + + if !features.FourPointOhBeta() { + resource.Schema["connection_strings"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeList, + Computed: true, + Sensitive: true, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + Sensitive: true, + }, + Deprecated: "This property has been superseded by the primary and secondary connection strings for sql, mongodb and readonly and will be removed in v4.0 of the provider", + } + } + + return resource } func resourceCosmosDbAccountCreate(d *pluginsdk.ResourceData, meta interface{}) error { @@ -1550,7 +1555,9 @@ func resourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) er } } - d.Set("connection_strings", connStrings) + if !features.FourPointOhBeta() { + d.Set("connection_strings", connStrings) + } return tags.FlattenAndSet(d, existing.Model.Tags) } From 071ad4173be026563501e4a7b8d31715e2db88fa Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 4 Apr 2024 08:16:52 +0200 Subject: [PATCH 2/7] rename enable_ to _enabled --- .../cosmos/cosmosdb_account_resource.go | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index ee592b81fd9d..35b63c1f100f 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -242,12 +242,17 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { }, }, - // TODO: 4.0 - set the default to Tls12 // per Microsoft's documentation, as of April 1 2023 the default minimal TLS version for all new accounts is 1.2 "minimal_tls_version": { - Type: pluginsdk.TypeString, - Optional: true, - Computed: true, + Type: pluginsdk.TypeString, + Optional: true, + Computed: !features.FourPointOhBeta(), + Default: func() interface{} { + if !features.FourPointOhBeta() { + return nil + } + return string(cosmosdb.MinimalTlsVersionTlsOneTwo) + }(), ValidateFunc: validation.StringInSlice(cosmosdb.PossibleValuesForMinimalTlsVersion(), false), }, @@ -471,13 +476,6 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { Set: resourceAzureRMCosmosDBAccountVirtualNetworkRuleHash, }, - // TODO 4.0: change this from enable_* to *_enabled - "enable_multiple_write_locations": { - Type: pluginsdk.TypeBool, - Optional: true, - Default: false, - }, - "access_key_metadata_writes_enabled": { Type: pluginsdk.TypeBool, Optional: true, @@ -770,7 +768,27 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { Type: pluginsdk.TypeString, Sensitive: true, }, - Deprecated: "This property has been superseded by the primary and secondary connection strings for sql, mongodb and readonly and will be removed in v4.0 of the provider", + Deprecated: "This property has been superseded by the primary and secondary connection strings for sql, mongodb and readonly and will be removed in v4.0 of the AzureRM provider", + } + resource.Schema["enable_multiple_write_locations"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeBool, + Optional: true, + Default: false, + Deprecated: "This property will be renamed to `multiple_write_locations_enabled` in v4.0 of the AzureRM Provider", + } + resource.Schema["enable_free_tier"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeBool, + Optional: true, + Default: false, + Deprecated: "This property will be renamed to `multiple_write_locations_enabled` in v4.0 of the AzureRM Provider", + } + } + + if features.FourPointOh() { + resource.Schema["multiple_write_locations_enabled"] = &pluginsdk.Schema{ + Type: pluginsdk.TypeBool, + Optional: true, + Default: false, } } From aa2a5881c1bb8de40bce63d445952cc1c5eab9d8 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 4 Apr 2024 09:43:22 +0200 Subject: [PATCH 3/7] update create, update and read for new properties --- .../cosmos/cosmosdb_account_resource.go | 172 ++++++++++++++---- 1 file changed, 138 insertions(+), 34 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 35b63c1f100f..dfe71ab0e1d8 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -315,12 +315,23 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { } }(), - // TODO 4.0: change this from enable_* to *_enabled - "enable_free_tier": { + "free_tier_enabled": { Type: pluginsdk.TypeBool, Optional: true, - Default: false, - ForceNew: true, + Default: func() interface{} { + if !features.FourPointOhBeta() { + return nil + } + return false + }(), + ForceNew: features.FourPointOhBeta(), + Computed: !features.FourPointOhBeta(), + ConflictsWith: func() []string { + if !features.FourPointOhBeta() { + return []string{"enable_free_tier"} + } + return []string{} + }(), }, "analytical_storage_enabled": { @@ -335,11 +346,22 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { Default: true, }, - // TODO 4.0: change this from enable_* to *_enabled - "enable_automatic_failover": { + "automatic_failover_enabled": { Type: pluginsdk.TypeBool, Optional: true, - Default: false, + Default: func() interface{} { + if !features.FourPointOhBeta() { + return nil + } + return false + }(), + Computed: !features.FourPointOhBeta(), + ConflictsWith: func() []string { + if !features.FourPointOhBeta() { + return []string{"enable_automatic_failover"} + } + return []string{} + }(), }, "key_vault_key_id": { @@ -500,6 +522,24 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { }, false), }, + "multiple_write_locations_enabled": { + Type: pluginsdk.TypeBool, + Optional: true, + Default: func() interface{} { + if !features.FourPointOhBeta() { + return nil + } + return false + }(), + Computed: !features.FourPointOhBeta(), + ConflictsWith: func() []string { + if !features.FourPointOhBeta() { + return []string{"enable_multiple_write_locations"} + } + return []string{} + }(), + }, + "network_acl_bypass_for_azure_services": { Type: pluginsdk.TypeBool, Optional: true, @@ -771,24 +811,40 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { Deprecated: "This property has been superseded by the primary and secondary connection strings for sql, mongodb and readonly and will be removed in v4.0 of the AzureRM provider", } resource.Schema["enable_multiple_write_locations"] = &pluginsdk.Schema{ - Type: pluginsdk.TypeBool, - Optional: true, - Default: false, - Deprecated: "This property will be renamed to `multiple_write_locations_enabled` in v4.0 of the AzureRM Provider", + Type: pluginsdk.TypeBool, + Optional: true, + Computed: true, + ConflictsWith: func() []string { + if !features.FourPointOhBeta() { + return []string{"multiple_write_locations_enabled"} + } + return []string{} + }(), + Deprecated: "This property has been superseded by `multiple_write_locations_enabled` and will be removed in v4.0 of the AzureRM Provider", } resource.Schema["enable_free_tier"] = &pluginsdk.Schema{ - Type: pluginsdk.TypeBool, - Optional: true, - Default: false, - Deprecated: "This property will be renamed to `multiple_write_locations_enabled` in v4.0 of the AzureRM Provider", + Type: pluginsdk.TypeBool, + Optional: true, + Computed: true, + ConflictsWith: func() []string { + if !features.FourPointOhBeta() { + return []string{"free_tier_enabled"} + } + return []string{} + }(), + Deprecated: "This property has been superseded by `free_tier_enabled` and will be removed in v4.0 of the AzureRM Provider", } - } - - if features.FourPointOh() { - resource.Schema["multiple_write_locations_enabled"] = &pluginsdk.Schema{ + resource.Schema["enable_automatic_failover"] = &pluginsdk.Schema{ Type: pluginsdk.TypeBool, Optional: true, - Default: false, + Computed: true, + ConflictsWith: func() []string { + if !features.FourPointOhBeta() { + return []string{"automatic_failover_enabled"} + } + return []string{} + }(), + Deprecated: "This property has been superseded by `automatic_failover_enabled` and will be removed in v4.0 of the AzureRM Provider", } } @@ -818,7 +874,7 @@ func resourceCosmosDbAccountCreate(d *pluginsdk.ResourceData, meta interface{}) } } - location := azure.NormalizeLocation(d.Get("location").(string)) + location := location.Normalize(d.Get("location").(string)) t := d.Get("tags").(map[string]interface{}) kind := d.Get("kind").(string) offerType := d.Get("offer_type").(string) @@ -831,9 +887,23 @@ func resourceCosmosDbAccountCreate(d *pluginsdk.ResourceData, meta interface{}) } isVirtualNetworkFilterEnabled := d.Get("is_virtual_network_filter_enabled").(bool) - enableFreeTier := d.Get("enable_free_tier").(bool) - enableAutomaticFailover := d.Get("enable_automatic_failover").(bool) - enableMultipleWriteLocations := d.Get("enable_multiple_write_locations").(bool) + + enableFreeTier := d.Get("free_tier_enabled").(bool) + enableAutomaticFailover := d.Get("automatic_failover_enabled").(bool) + enableMultipleWriteLocations := d.Get("multiple_write_locations_enabled").(bool) + + if !features.FourPointOhBeta() { + if v, ok := d.GetOkExists("enable_automatic_failover"); ok { + enableAutomaticFailover = v.(bool) + } + if v, ok := d.GetOkExists("multiple_write_locations_enabled"); ok { + enableMultipleWriteLocations = v.(bool) + } + if v, ok := d.GetOkExists("enable_free_tier"); ok { + enableFreeTier = v.(bool) + } + } + partitionMergeEnabled := d.Get("partition_merge_enabled").(bool) enableAnalyticalStorage := d.Get("analytical_storage_enabled").(bool) disableLocalAuthentication := d.Get("local_authentication_disabled").(bool) @@ -1057,15 +1127,19 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) updateRequired := false if props := existing.Model.Properties; props != nil { - location := azure.NormalizeLocation(pointer.From(existing.Model.Location)) + location := location.Normalize(pointer.From(existing.Model.Location)) offerType := d.Get("offer_type").(string) t := tags.Expand(d.Get("tags").(map[string]interface{})) kind := cosmosdb.DatabaseAccountKind(d.Get("kind").(string)) isVirtualNetworkFilterEnabled := pointer.To(d.Get("is_virtual_network_filter_enabled").(bool)) - enableFreeTier := pointer.To(d.Get("enable_free_tier").(bool)) - enableAutomaticFailover := pointer.To(d.Get("enable_automatic_failover").(bool)) enableAnalyticalStorage := pointer.To(d.Get("analytical_storage_enabled").(bool)) disableLocalAuthentication := pointer.To(d.Get("local_authentication_disabled").(bool)) + enableAutomaticFailover := pointer.To(d.Get("automatic_failover_enabled").(bool)) + if !features.FourPointOhBeta() { + if v, ok := d.GetOkExists("enable_automatic_failover"); ok { + enableAutomaticFailover = pointer.To(v.(bool)) + } + } networkByPass := cosmosdb.NetworkAclBypassNone if d.Get("network_acl_bypass_for_azure_services").(bool) { @@ -1088,11 +1162,12 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) // 'DatabaseAccountCreateUpdateParameters' below or // are included in the 'DatabaseAccountCreateUpdateParameters' // later, however we need to know if they changed or not... + // TODO Post 4.0 remove `enable_automatic_failover` from this list if d.HasChanges("consistency_policy", "virtual_network_rule", "cors_rule", "access_key_metadata_writes_enabled", "network_acl_bypass_for_azure_services", "network_acl_bypass_ids", "analytical_storage", "capacity", "create_mode", "restore", "key_vault_key_id", "mongo_server_version", "public_network_access_enabled", "ip_range_filter", "offer_type", "is_virtual_network_filter_enabled", - "kind", "tags", "enable_free_tier", "enable_automatic_failover", "analytical_storage_enabled", + "kind", "tags", "enable_automatic_failover", "automatic_failover_enabled", "analytical_storage_enabled", "local_authentication_disabled", "partition_merge_enabled", "minimal_tls_version") { updateRequired = true } @@ -1125,7 +1200,7 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) DatabaseAccountOfferType: cosmosdb.DatabaseAccountOfferType(offerType), IPRules: ipRangeFilter, IsVirtualNetworkFilterEnabled: isVirtualNetworkFilterEnabled, - EnableFreeTier: enableFreeTier, + EnableFreeTier: existing.Model.Properties.EnableFreeTier, EnableAutomaticFailover: enableAutomaticFailover, MinimalTlsVersion: pointer.To(cosmosdb.MinimalTlsVersion(d.Get("minimal_tls_version").(string))), Capabilities: capabilities, @@ -1206,10 +1281,28 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) } // Update the following properties independently after the initial CreateOrUpdate... - if d.HasChange("enable_multiple_write_locations") { + if !features.FourPointOhBeta() { + if d.HasChange("enable_multiple_write_locations") { + log.Printf("[INFO] Updating AzureRM Cosmos DB Account: Updating 'EnableMultipleWriteLocations'") + + enableMultipleWriteLocations := pointer.To(d.Get("enable_multiple_write_locations").(bool)) + if props.EnableMultipleWriteLocations != enableMultipleWriteLocations { + account.Properties.EnableMultipleWriteLocations = enableMultipleWriteLocations + + // Update the database... + if err = resourceCosmosDbAccountApiCreateOrUpdate(client, ctx, *id, account, d); err != nil { + return fmt.Errorf("updating %q EnableMultipleWriteLocations: %+v", id, err) + } + } + } else { + log.Printf("[INFO] [SKIP] AzureRM Cosmos DB Account: Updating 'EnableMultipleWriteLocations' [NO CHANGE]") + } + } + + if d.HasChange("multiple_write_locations_enabled") { log.Printf("[INFO] Updating AzureRM Cosmos DB Account: Updating 'EnableMultipleWriteLocations'") - enableMultipleWriteLocations := pointer.To(d.Get("enable_multiple_write_locations").(bool)) + enableMultipleWriteLocations := pointer.To(d.Get("multiple_write_locations_enabled").(bool)) if props.EnableMultipleWriteLocations != enableMultipleWriteLocations { account.Properties.EnableMultipleWriteLocations = enableMultipleWriteLocations @@ -1420,7 +1513,18 @@ func resourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) er } d.Set("endpoint", props.DocumentEndpoint) - d.Set("enable_free_tier", props.EnableFreeTier) + + if !features.FourPointOhBeta() { + d.Set("enable_free_tier", props.EnableFreeTier) + if v := existing.Model.Properties.EnableMultipleWriteLocations; v != nil { + d.Set("enable_multiple_write_locations", props.EnableMultipleWriteLocations) + } + if v := existing.Model.Properties.EnableAutomaticFailover; v != nil { + d.Set("enable_automatic_failover", props.EnableAutomaticFailover) + } + } + + d.Set("free_tier_enabled", props.EnableFreeTier) d.Set("analytical_storage_enabled", props.EnableAnalyticalStorage) d.Set("public_network_access_enabled", pointer.From(props.PublicNetworkAccess) == cosmosdb.PublicNetworkAccessEnabled) d.Set("default_identity_type", props.DefaultIdentity) @@ -1433,7 +1537,7 @@ func resourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) er } if v := existing.Model.Properties.EnableAutomaticFailover; v != nil { - d.Set("enable_automatic_failover", props.EnableAutomaticFailover) + d.Set("automatic_failover_enabled", props.EnableAutomaticFailover) } if v := existing.Model.Properties.KeyVaultKeyUri; v != nil { @@ -1441,7 +1545,7 @@ func resourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) er } if v := existing.Model.Properties.EnableMultipleWriteLocations; v != nil { - d.Set("enable_multiple_write_locations", props.EnableMultipleWriteLocations) + d.Set("multiple_write_locations_enabled", props.EnableMultipleWriteLocations) } if err := d.Set("analytical_storage", flattenCosmosDBAccountAnalyticalStorageConfiguration(props.AnalyticalStorageConfiguration)); err != nil { From ba83b79266ee7841055d2d362e58375343a27bc3 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 4 Apr 2024 09:56:25 +0200 Subject: [PATCH 4/7] imports and lint --- internal/services/cosmos/cosmosdb_account_data_source.go | 2 +- internal/services/cosmos/cosmosdb_account_resource.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/services/cosmos/cosmosdb_account_data_source.go b/internal/services/cosmos/cosmosdb_account_data_source.go index c430dcbd5f97..6bb4d9353418 100644 --- a/internal/services/cosmos/cosmosdb_account_data_source.go +++ b/internal/services/cosmos/cosmosdb_account_data_source.go @@ -5,7 +5,6 @@ package cosmos import ( "fmt" - "github.com/hashicorp/terraform-provider-azurerm/internal/features" "log" "sort" "time" @@ -17,6 +16,7 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/tags" "github.com/hashicorp/go-azure-sdk/resource-manager/cosmosdb/2023-04-15/cosmosdb" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/features" "github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/common" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index dfe71ab0e1d8..8d63232b025a 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -893,12 +893,15 @@ func resourceCosmosDbAccountCreate(d *pluginsdk.ResourceData, meta interface{}) enableMultipleWriteLocations := d.Get("multiple_write_locations_enabled").(bool) if !features.FourPointOhBeta() { + // nolint : staticcheck if v, ok := d.GetOkExists("enable_automatic_failover"); ok { enableAutomaticFailover = v.(bool) } + // nolint : staticcheck if v, ok := d.GetOkExists("multiple_write_locations_enabled"); ok { enableMultipleWriteLocations = v.(bool) } + // nolint : staticcheck if v, ok := d.GetOkExists("enable_free_tier"); ok { enableFreeTier = v.(bool) } @@ -1136,6 +1139,7 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) disableLocalAuthentication := pointer.To(d.Get("local_authentication_disabled").(bool)) enableAutomaticFailover := pointer.To(d.Get("automatic_failover_enabled").(bool)) if !features.FourPointOhBeta() { + // nolint : staticcheck if v, ok := d.GetOkExists("enable_automatic_failover"); ok { enableAutomaticFailover = pointer.To(v.(bool)) } From 40065498c23db261e258ef3d168a671ddc75def8 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 4 Apr 2024 11:05:46 +0200 Subject: [PATCH 5/7] fix property name in create for enable_multiple_write_locations --- internal/services/cosmos/cosmosdb_account_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 8d63232b025a..986c6dec43cf 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -898,7 +898,7 @@ func resourceCosmosDbAccountCreate(d *pluginsdk.ResourceData, meta interface{}) enableAutomaticFailover = v.(bool) } // nolint : staticcheck - if v, ok := d.GetOkExists("multiple_write_locations_enabled"); ok { + if v, ok := d.GetOkExists("enable_multiple_write_locations"); ok { enableMultipleWriteLocations = v.(bool) } // nolint : staticcheck From c00763236e6e859e37268153f2aafc1b98e2d5c5 Mon Sep 17 00:00:00 2001 From: Steph Date: Fri, 5 Apr 2024 08:22:35 +0200 Subject: [PATCH 6/7] update tests and docs --- .../cosmos/cosmosdb_account_resource_test.go | 198 +++++++++++++++--- website/docs/d/cosmosdb_account.html.markdown | 8 +- website/docs/r/cosmosdb_account.html.markdown | 8 +- 3 files changed, 176 insertions(+), 38 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index 03fd73f82c39..6310352043c7 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -837,7 +837,7 @@ func TestAccCosmosDBAccount_freeTier(t *testing.T) { Config: r.freeTier(data, "GlobalDocumentDB", cosmosdb.DefaultConsistencyLevelEventual), Check: acceptance.ComposeAggregateTestCheckFunc( checkAccCosmosDBAccount_basic(data, cosmosdb.DefaultConsistencyLevelEventual, 1), - check.That(data.ResourceName).Key("enable_free_tier").HasValue("true"), + check.That(data.ResourceName).Key("free_tier_enabled").HasValue("true"), ), }, data.ImportStep(), @@ -960,7 +960,7 @@ func TestAccCosmosDBAccount_vNetFilters(t *testing.T) { }) } -// todo remove for 4.0 +// TODO 4.0 remove post 4.0 func TestAccCosmosDBAccount_vNetFiltersThreePointOh(t *testing.T) { if features.FourPointOhBeta() { t.Skip("this test requires 3.0 mode") @@ -1354,7 +1354,7 @@ func TestAccCosmosDBAccount_gremlinDatabasesToRestore(t *testing.T) { }) } -// todo remove for 4.0 +// TODO 4.0 remove post 4.0 func TestAccCosmosDBAccount_ipRangeFiltersThreePointOh(t *testing.T) { if features.FourPointOhBeta() { t.Skip("this test requires 3.0 mode") @@ -1387,6 +1387,55 @@ func TestAccCosmosDBAccount_ipRangeFiltersThreePointOh(t *testing.T) { }) } +func TestAccCosmosDBAccount_supersededProperties(t *testing.T) { + if features.FourPointOhBeta() { + t.Skip("this test contains deprecated properties and can be removed post 4.0") + } + + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test") + // Limited regional availability + data.Locations.Primary = "westeurope" + data.Locations.Secondary = "northeurope" + r := CosmosDBAccountResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basicMongoDB(data, cosmosdb.DefaultConsistencyLevelEventual), + Check: acceptance.ComposeAggregateTestCheckFunc( + checkAccCosmosDBAccount_basic(data, cosmosdb.DefaultConsistencyLevelEventual, 1), + ), + }, + data.ImportStep(), + { + Config: r.supersededProperties(data, cosmosdb.DefaultConsistencyLevelEventual), + Check: acceptance.ComposeAggregateTestCheckFunc( + checkAccCosmosDBAccount_basic(data, cosmosdb.DefaultConsistencyLevelEventual, 2), + ), + }, + data.ImportStep(), + }) +} + +func TestAccCosmosDBAccount_supersededFreeTier(t *testing.T) { + if features.FourPointOhBeta() { + t.Skip("this test contains deprecated properties and can be removed post 4.0") + } + + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test") + r := CosmosDBAccountResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.supersededFreeTier(data, "GlobalDocumentDB", cosmosdb.DefaultConsistencyLevelEventual), + Check: acceptance.ComposeAggregateTestCheckFunc( + checkAccCosmosDBAccount_basic(data, cosmosdb.DefaultConsistencyLevelEventual, 1), + check.That(data.ResourceName).Key("enable_free_tier").HasValue("true"), + ), + }, + data.ImportStep(), + }) +} + func TestAccCosmosDBAccount_ipRangeFilters(t *testing.T) { if !features.FourPointOhBeta() { t.Skip("this test requires 4.0 mode") @@ -1695,7 +1744,7 @@ resource "azurerm_cosmosdb_account" "test" { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location @@ -1753,7 +1802,7 @@ resource "azurerm_cosmosdb_account" "test" { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location @@ -1818,7 +1867,7 @@ resource "azurerm_cosmosdb_account" "test" { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location @@ -1867,7 +1916,7 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "%s" - enable_multiple_write_locations = true + multiple_write_locations_enabled = true consistency_policy { consistency_level = "BoundedStaleness" @@ -1911,7 +1960,7 @@ resource "azurerm_cosmosdb_account" "test" { name = "EnableMongo" } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true consistency_policy { consistency_level = "BoundedStaleness" @@ -1972,7 +2021,7 @@ resource "azurerm_cosmosdb_account" "test" { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location @@ -2037,7 +2086,7 @@ resource "azurerm_cosmosdb_account" "test" { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location @@ -2114,7 +2163,7 @@ resource "azurerm_cosmosdb_account" "test" { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location @@ -2186,7 +2235,7 @@ resource "azurerm_cosmosdb_account" "test" { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location @@ -2407,8 +2456,8 @@ resource "azurerm_cosmosdb_account" "test" { name = "EnableMongo" } - enable_multiple_write_locations = true - enable_automatic_failover = true + multiple_write_locations_enabled = true + automatic_failover_enabled = true consistency_policy { consistency_level = "%s" @@ -2477,8 +2526,8 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "GlobalDocumentDB" - enable_multiple_write_locations = false - enable_automatic_failover = false + multiple_write_locations_enabled = false + automatic_failover_enabled = false consistency_policy { consistency_level = "Eventual" @@ -2518,8 +2567,8 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "GlobalDocumentDB" - enable_multiple_write_locations = false - enable_automatic_failover = false + multiple_write_locations_enabled = false + automatic_failover_enabled = false consistency_policy { consistency_level = "Eventual" @@ -2566,7 +2615,7 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "%s" - enable_free_tier = true + free_tier_enabled = true consistency_policy { consistency_level = "%s" @@ -4509,8 +4558,8 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "GlobalDocumentDB" - enable_multiple_write_locations = false - enable_automatic_failover = false + multiple_write_locations_enabled = false + automatic_failover_enabled = false consistency_policy { consistency_level = "Eventual" @@ -4550,8 +4599,8 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "GlobalDocumentDB" - enable_multiple_write_locations = false - enable_automatic_failover = false + multiple_write_locations_enabled = false + automatic_failover_enabled = false consistency_policy { consistency_level = "Eventual" @@ -4591,8 +4640,8 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "GlobalDocumentDB" - enable_multiple_write_locations = false - enable_automatic_failover = false + multiple_write_locations_enabled = false + automatic_failover_enabled = false consistency_policy { consistency_level = "Eventual" @@ -4632,8 +4681,8 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "GlobalDocumentDB" - enable_multiple_write_locations = false - enable_automatic_failover = false + multiple_write_locations_enabled = false + automatic_failover_enabled = false consistency_policy { consistency_level = "Eventual" @@ -4733,7 +4782,7 @@ resource "azurerm_cosmosdb_account" "test" { virtual_network_rule { id = azurerm_subnet.subnet2.id } - enable_multiple_write_locations = true + multiple_write_locations_enabled = true geo_location { location = azurerm_resource_group.test.location failover_priority = 0 @@ -4757,3 +4806,96 @@ resource "azurerm_cosmosdb_account" "test" { } `, r.completePreReqs(data), data.RandomInteger, string(kind), string(consistency), data.Locations.Secondary, data.Locations.Ternary) } + +func (CosmosDBAccountResource) supersededProperties(data acceptance.TestData, consistency cosmosdb.DefaultConsistencyLevel) string { + return fmt.Sprintf(` +variable "geo_location" { + type = list(object({ + location = string + failover_priority = string + zone_redundant = bool + })) + default = [ + { + location = "%s" + failover_priority = 0 + zone_redundant = false + }, + { + location = "%s" + failover_priority = 1 + zone_redundant = true + } + ] +} + +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-cosmos-%d" + location = "%s" +} + +resource "azurerm_cosmosdb_account" "test" { + name = "acctest-ca-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + offer_type = "Standard" + kind = "MongoDB" + + capabilities { + name = "EnableMongo" + } + + enable_multiple_write_locations = true + enable_automatic_failover = true + + consistency_policy { + consistency_level = "%s" + } + + dynamic "geo_location" { + for_each = var.geo_location + content { + location = geo_location.value.location + failover_priority = geo_location.value.failover_priority + zone_redundant = geo_location.value.zone_redundant + } + } +} +`, data.Locations.Primary, data.Locations.Secondary, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(consistency)) +} + +func (CosmosDBAccountResource) supersededFreeTier(data acceptance.TestData, kind cosmosdb.DatabaseAccountKind, consistency cosmosdb.DefaultConsistencyLevel) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-cosmos-%d" + location = "%s" +} + +resource "azurerm_cosmosdb_account" "test" { + name = "acctest-ca-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + offer_type = "Standard" + kind = "%s" + + enable_free_tier = true + + consistency_policy { + consistency_level = "%s" + } + + geo_location { + location = azurerm_resource_group.test.location + failover_priority = 0 + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency)) +} diff --git a/website/docs/d/cosmosdb_account.html.markdown b/website/docs/d/cosmosdb_account.html.markdown index 4f28baea31b0..28bc57215930 100644 --- a/website/docs/d/cosmosdb_account.html.markdown +++ b/website/docs/d/cosmosdb_account.html.markdown @@ -51,9 +51,9 @@ The following attributes are exported: * `ip_range_filter` - The current IP Filter for this CosmosDB account -* `enable_free_tier` - If Free Tier pricing option is enabled for this CosmosDB Account. You can have up to one free tier Azure Cosmos DB account per Azure subscription. +* `free_tier_enabled` - If Free Tier pricing option is enabled for this CosmosDB Account. You can have up to one free tier Azure Cosmos DB account per Azure subscription. -* `enable_automatic_failover` - If automatic failover is enabled for this CosmosDB Account. +* `automatic_failover_enabled` - If automatic failover is enabled for this CosmosDB Account. * `capabilities` - Capabilities enabled on this Cosmos DB account. @@ -61,7 +61,7 @@ The following attributes are exported: * `virtual_network_rule` - Subnets that are allowed to access this CosmosDB account. -* `enable_multiple_write_locations` - If multiple write locations are enabled for this Cosmos DB account. +* `multiple_write_locations_enabled` - If multiple write locations are enabled for this Cosmos DB account. `consistency_policy` The current consistency Settings for this CosmosDB account with the following properties: @@ -93,8 +93,6 @@ The following attributes are exported: * `secondary_readonly_key` - The secondary read-only key for the CosmosDB account. -* `connection_strings` - A list of connection strings available for this CosmosDB account. - * `primary_sql_connection_string` - The primary SQL connection string for the CosmosDB Account. * `secondary_sql_connection_string` - The secondary SQL connection string for the CosmosDB Account. diff --git a/website/docs/r/cosmosdb_account.html.markdown b/website/docs/r/cosmosdb_account.html.markdown index 0970ddbe7e9b..e15f6badcf26 100644 --- a/website/docs/r/cosmosdb_account.html.markdown +++ b/website/docs/r/cosmosdb_account.html.markdown @@ -142,11 +142,11 @@ The following arguments are supported: ~> **Note:** To enable the "Accept connections from within public Azure datacenters" behavior, you should add `0.0.0.0` to the list, see the [documentation](https://docs.microsoft.com/azure/cosmos-db/how-to-configure-firewall#allow-requests-from-global-azure-datacenters-or-other-sources-within-azure) for more details. -* `enable_free_tier` - (Optional) Enable the Free Tier pricing option for this Cosmos DB account. Defaults to `false`. Changing this forces a new resource to be created. +* `free_tier_enabled` - (Optional) Enable the Free Tier pricing option for this Cosmos DB account. Defaults to `false`. Changing this forces a new resource to be created. * `analytical_storage_enabled` - (Optional) Enable Analytical Storage option for this Cosmos DB account. Defaults to `false`. Enabling and then disabling analytical storage forces a new resource to be created. -* `enable_automatic_failover` - (Optional) Enable automatic failover for this Cosmos DB account. +* `automatic_failover_enabled` - (Optional) Enable automatic failover for this Cosmos DB account. * `partition_merge_enabled` - (Optional) Is partition merge on the Cosmos DB account enabled? Defaults to `false`. @@ -164,7 +164,7 @@ The following arguments are supported: * `virtual_network_rule` - (Optional) Specifies a `virtual_network_rule` block as defined below, used to define which subnets are allowed to access this CosmosDB account. -* `enable_multiple_write_locations` - (Optional) Enable multiple write locations for this Cosmos DB account. +* `multiple_write_locations_enabled` - (Optional) Enable multiple write locations for this Cosmos DB account. * `access_key_metadata_writes_enabled` - (Optional) Is write operations on metadata resources (databases, containers, throughput) via account keys enabled? Defaults to `true`. @@ -331,8 +331,6 @@ In addition to the Arguments listed above - the following Attributes are exporte * `secondary_readonly_key` - The Secondary read-only key for the CosmosDB Account. -* `connection_strings` - A list of connection strings available for this CosmosDB account. - * `primary_sql_connection_string` - Primary SQL connection string for the CosmosDB Account. * `secondary_sql_connection_string` - Secondary SQL connection string for the CosmosDB Account. From 104950f037c68d9a164f114ab2b9452149a5ac60 Mon Sep 17 00:00:00 2001 From: Steph Date: Fri, 5 Apr 2024 11:18:51 +0200 Subject: [PATCH 7/7] update deprecated property enable_automatic_failover only when set to true --- internal/services/cosmos/cosmosdb_account_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 986c6dec43cf..bdbf31ad0554 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -1140,7 +1140,7 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) enableAutomaticFailover := pointer.To(d.Get("automatic_failover_enabled").(bool)) if !features.FourPointOhBeta() { // nolint : staticcheck - if v, ok := d.GetOkExists("enable_automatic_failover"); ok { + if v, ok := d.GetOkExists("enable_automatic_failover"); ok && v.(bool) { enableAutomaticFailover = pointer.To(v.(bool)) } }