Skip to content

Commit

Permalink
force mongoenabled for mongo34 (#13757)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
koikonom and katbyte authored Oct 18, 2021
1 parent 5648d4a commit fc0f3c4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
20 changes: 20 additions & 0 deletions internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
12 changes: 12 additions & 0 deletions internal/services/cosmos/cosmosdb_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cosmos_test
import (
"context"
"fmt"
"regexp"
"strconv"
"testing"

Expand Down Expand Up @@ -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"})
}
Expand Down
8 changes: 7 additions & 1 deletion website/docs/r/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.

Expand Down

0 comments on commit fc0f3c4

Please sign in to comment.