Skip to content

Commit

Permalink
azurerm_cosmosdb_account : Fix issue about enabling analytical stor…
Browse files Browse the repository at this point in the history
…age for CosmosDB is forcing re-creation (#19659)

* fix issue 19630

* update code

* update code

* update code
  • Loading branch information
sinbai authored Dec 14, 2022
1 parent e466e4d commit d9491c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
6 changes: 5 additions & 1 deletion internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func resourceCosmosDbAccount() *pluginsdk.Resource {
return old.(string) == string(documentdb.TypeContinuous) && new.(string) == string(documentdb.TypePeriodic)
}),

pluginsdk.ForceNewIfChange("analytical_storage_enabled", func(ctx context.Context, old, new, _ interface{}) bool {
// analytical_storage_enabled can not be changed after being set to true
return old.(bool) && !new.(bool)
}),

pluginsdk.CustomizeDiffShim(func(ctx context.Context, diff *pluginsdk.ResourceDiff, v interface{}) error {
caps := diff.Get("capabilities")
mongo34found := false
Expand Down Expand Up @@ -231,7 +236,6 @@ func resourceCosmosDbAccount() *pluginsdk.Resource {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},

"public_network_access_enabled": {
Expand Down
18 changes: 13 additions & 5 deletions internal/services/cosmos/cosmosdb_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,17 @@ func TestAccCosmosDBAccount_analyticalStorage(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.analyticalStorage(data, "GlobalDocumentDB", documentdb.DefaultConsistencyLevelEventual),
Config: r.analyticalStorage(data, "MongoDB", documentdb.DefaultConsistencyLevelStrong, false),
Check: acceptance.ComposeAggregateTestCheckFunc(
checkAccCosmosDBAccount_basic(data, documentdb.DefaultConsistencyLevelEventual, 1),
checkAccCosmosDBAccount_basic(data, documentdb.DefaultConsistencyLevelStrong, 1),
check.That(data.ResourceName).Key("analytical_storage_enabled").HasValue("false"),
),
},
data.ImportStep(),
{
Config: r.analyticalStorage(data, "MongoDB", documentdb.DefaultConsistencyLevelStrong, true),
Check: acceptance.ComposeAggregateTestCheckFunc(
checkAccCosmosDBAccount_basic(data, documentdb.DefaultConsistencyLevelStrong, 1),
check.That(data.ResourceName).Key("analytical_storage_enabled").HasValue("true"),
),
},
Expand Down Expand Up @@ -2068,7 +2076,7 @@ resource "azurerm_cosmosdb_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency))
}

func (CosmosDBAccountResource) analyticalStorage(data acceptance.TestData, kind documentdb.DatabaseAccountKind, consistency documentdb.DefaultConsistencyLevel) string {
func (CosmosDBAccountResource) analyticalStorage(data acceptance.TestData, kind documentdb.DatabaseAccountKind, consistency documentdb.DefaultConsistencyLevel, enableAnalyticalStorage bool) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand All @@ -2086,7 +2094,7 @@ resource "azurerm_cosmosdb_account" "test" {
offer_type = "Standard"
kind = "%s"
analytical_storage_enabled = true
analytical_storage_enabled = %t
consistency_policy {
consistency_level = "%s"
Expand All @@ -2097,7 +2105,7 @@ resource "azurerm_cosmosdb_account" "test" {
failover_priority = 0
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency))
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), enableAnalyticalStorage, string(consistency))
}

func (CosmosDBAccountResource) mongoAnalyticalStorage(data acceptance.TestData, consistency documentdb.DefaultConsistencyLevel) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ resource "azurerm_cosmosdb_sql_container" "test" {
partition_key_path = "/definition/id"
analytical_storage_ttl = 600
}
`, CosmosDBAccountResource{}.analyticalStorage(data, "GlobalDocumentDB", documentdb.DefaultConsistencyLevelEventual), data.RandomInteger, data.RandomInteger)
`, CosmosDBAccountResource{}.analyticalStorage(data, "GlobalDocumentDB", documentdb.DefaultConsistencyLevelEventual, true), data.RandomInteger, data.RandomInteger)
}

func (CosmosSqlContainerResource) analyticalStorageTTL_removed(data acceptance.TestData) string {
Expand All @@ -356,7 +356,7 @@ resource "azurerm_cosmosdb_sql_container" "test" {
database_name = azurerm_cosmosdb_sql_database.test.name
partition_key_path = "/definition/id"
}
`, CosmosDBAccountResource{}.analyticalStorage(data, "GlobalDocumentDB", documentdb.DefaultConsistencyLevelEventual), data.RandomInteger, data.RandomInteger)
`, CosmosDBAccountResource{}.analyticalStorage(data, "GlobalDocumentDB", documentdb.DefaultConsistencyLevelEventual, true), data.RandomInteger, data.RandomInteger)
}

func (CosmosSqlContainerResource) update(data acceptance.TestData) string {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ The following arguments are supported:

* `enable_free_tier` - (Optional) Enable 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`. 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 fail over for this Cosmos DB account.

Expand Down

0 comments on commit d9491c8

Please sign in to comment.