From fc0f3c4041acd205968102833ba66cb4f97037f1 Mon Sep 17 00:00:00 2001 From: Kyriakos Oikonomakos Date: Mon, 18 Oct 2021 14:06:00 +0100 Subject: [PATCH] force mongoenabled for mongo34 (#13757) When we enable capability `MongoDBv3.4` azure also sets `EnableMongo`. As a result next time terraform runs it sees both capabilities enabled, and since only `MongoDBv3.4` is enabled in the config it will try to change the capabiltiies to match what it has in the config. In this case it means destroying the database and building it again, since the `capabilities` setting has `ForceNew` set to true. Co-authored-by: kt --- .../cosmos/cosmosdb_account_resource.go | 20 +++++++++++++++++++ .../cosmos/cosmosdb_account_resource_test.go | 12 +++++++++++ website/docs/r/cosmosdb_account.html.markdown | 8 +++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 5e06d230e279..98674ac11d26 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -47,6 +47,26 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { Read: resourceCosmosDbAccountRead, Update: resourceCosmosDbAccountUpdate, Delete: resourceCosmosDbAccountDelete, + CustomizeDiff: pluginsdk.CustomizeDiffShim(func(ctx context.Context, diff *pluginsdk.ResourceDiff, v interface{}) error { + caps := diff.Get("capabilities") + mongo34found := false + enableMongo := false + for _, cap := range caps.(*pluginsdk.Set).List() { + m := cap.(map[string]interface{}) + if v, ok := m["name"].(string); ok { + if v == "MongoDBv3.4" { + mongo34found = true + } else if v == "EnableMongo" { + enableMongo = true + } + } + } + + if mongo34found && !enableMongo { + return fmt.Errorf("capability EnableMongo must be enabled if MongoDBv3.4 is also enabled") + } + return nil + }), // TODO: replace this with an importer which validates the ID during import Importer: pluginsdk.DefaultImporter(), diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index 79e6b0a1d15d..5d9849e39510 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -3,6 +3,7 @@ package cosmos_test import ( "context" "fmt" + "regexp" "strconv" "testing" @@ -469,6 +470,17 @@ func TestAccCosmosDBAccount_capabilities_MongoDBv34(t *testing.T) { testAccCosmosDBAccount_capabilitiesWith(t, documentdb.DatabaseAccountKindMongoDB, []string{"EnableMongo", "MongoDBv3.4"}) } +func TestAccCosmosDBAccount_capabilities_MongoDBv34_NoEnableMongo(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test") + r := CosmosDBAccountResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.capabilities(data, documentdb.DatabaseAccountKindMongoDB, []string{"MongoDBv3.4"}), + ExpectError: regexp.MustCompile("capability EnableMongo must be enabled if MongoDBv3.4 is also enabled"), + }, + }) +} + func TestAccCosmosDBAccount_capabilities_mongoEnableDocLevelTTL(t *testing.T) { testAccCosmosDBAccount_capabilitiesWith(t, documentdb.DatabaseAccountKindMongoDB, []string{"EnableMongo", "mongoEnableDocLevelTTL"}) } diff --git a/website/docs/r/cosmosdb_account.html.markdown b/website/docs/r/cosmosdb_account.html.markdown index c41ba67827d9..c0f409f0a615 100644 --- a/website/docs/r/cosmosdb_account.html.markdown +++ b/website/docs/r/cosmosdb_account.html.markdown @@ -44,6 +44,10 @@ resource "azurerm_cosmosdb_account" "db" { name = "MongoDBv3.4" } + capabilities { + name = "EnableMongo" + } + consistency_policy { consistency_level = "BoundedStaleness" max_interval_in_seconds = 10 @@ -145,7 +149,9 @@ The following arguments are supported: `capabilities` Configures the capabilities to enable for this Cosmos DB account: -* `name` - (Required) The capability to enable - Possible values are `AllowSelfServeUpgradeToMongo36`, `DisableRateLimitingResponses`, `EnableAggregationPipeline`, `EnableCassandra`, `EnableGremlin`, `EnableMongo`, `EnableTable`, `EnableServerless`, `MongoDBv3.4` and `mongoEnableDocLevelTTL`. +* `name` - (Required) The capability to enable - Possible values are `AllowSelfServeUpgradeToMongo36`, `DisableRateLimitingResponses`, `EnableAggregationPipeline`, `EnableCassandra`, `EnableGremlin`, `EnableMongo`, `EnableTable`, `EnableServerless`, `MongoDBv3.4` and `mongoEnableDocLevelTTL`. + +**NOTE:** Setting `MongoDBv3.4` also requires setting `EnableMongo`. **NOTE:** The `prefix` and `failover_priority` fields of a location cannot be changed for the location with a failover priority of `0`.