Skip to content

Commit

Permalink
azurerm_cosmosdb_account - Change the order of updating `capabiliti…
Browse files Browse the repository at this point in the history
…es` (#24029)

* fix issue 23986

* update code
  • Loading branch information
sinbai authored Nov 28, 2023
1 parent 20565f1 commit d0c2da6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 24 deletions.
49 changes: 25 additions & 24 deletions internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,30 +927,6 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{})
var capabilities *[]documentdb.Capability
if existing.DatabaseAccountGetProperties.Capabilities != nil {
capabilities = existing.DatabaseAccountGetProperties.Capabilities
if d.HasChange("capabilities") {
log.Printf("[INFO] Updating AzureRM Cosmos DB Account: Updating 'Capabilities'")

newCapabilities := expandAzureRmCosmosDBAccountCapabilities(d)
updateParameters := documentdb.DatabaseAccountUpdateParameters{
DatabaseAccountUpdateProperties: &documentdb.DatabaseAccountUpdateProperties{
Capabilities: newCapabilities,
},
}

// Update Database 'capabilities'...
future, err := client.Update(ctx, id.ResourceGroup, id.Name, updateParameters)
if err != nil {
return fmt.Errorf("updating CosmosDB Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the CosmosDB Account %q (Resource Group %q) to finish updating: %+v", id.Name, id.ResourceGroup, err)
}

capabilities = newCapabilities
} else {
log.Printf("[INFO] [SKIP] AzureRM Cosmos DB Account: Updating 'Capabilities' [NO CHANGE]")
}
}

// backup must be updated independently
Expand Down Expand Up @@ -1283,6 +1259,31 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{})
}
}

if existing.DatabaseAccountGetProperties.Capabilities != nil {
if d.HasChange("capabilities") {
log.Printf("[INFO] Updating AzureRM Cosmos DB Account: Updating 'Capabilities'")

newCapabilities := expandAzureRmCosmosDBAccountCapabilities(d)
updateParameters := documentdb.DatabaseAccountUpdateParameters{
DatabaseAccountUpdateProperties: &documentdb.DatabaseAccountUpdateProperties{
Capabilities: newCapabilities,
},
}

// Update Database 'capabilities'...
future, err := client.Update(ctx, id.ResourceGroup, id.Name, updateParameters)
if err != nil {
return fmt.Errorf("updating CosmosDB Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the CosmosDB Account %q (Resource Group %q) to finish updating: %+v", id.Name, id.ResourceGroup, err)
}
} else {
log.Printf("[INFO] [SKIP] AzureRM Cosmos DB Account: Updating 'Capabilities' [NO CHANGE]")
}
}

return resourceCosmosDbAccountRead(d, meta)
}

Expand Down
61 changes: 61 additions & 0 deletions internal/services/cosmos/cosmosdb_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ func TestAccCosmosDBAccount_customerManagedKeyWithIdentity(t *testing.T) {
})
}

func TestAccCosmosDBAccount_updateMongoDBVersionCapabilities(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")
r := CosmosDBAccountResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicMongoDB(data, documentdb.DefaultConsistencyLevelStrong),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.updateMongoDBVersionCapabilities(data, documentdb.DefaultConsistencyLevelStrong),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccCosmosDBAccount_keyVaultUriUpdateConsistancy(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")
r := CosmosDBAccountResource{}
Expand Down Expand Up @@ -3561,6 +3583,45 @@ resource "azurerm_cosmosdb_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(consistency))
}

func (CosmosDBAccountResource) updateMongoDBVersionCapabilities(data acceptance.TestData, consistency documentdb.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 = "MongoDB"
mongo_server_version = "4.2"
capabilities {
name = "EnableMongo"
}
consistency_policy {
consistency_level = "%s"
}
geo_location {
location = azurerm_resource_group.test.location
failover_priority = 0
}
capabilities {
name = "EnableMongo16MBDocumentSupport"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(consistency))
}

func (CosmosDBAccountResource) basicMongoDBVersion36(data acceptance.TestData, consistency documentdb.DefaultConsistencyLevel) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down

0 comments on commit d0c2da6

Please sign in to comment.