Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_cosmosdb_account - backup.0.type is updated separately when set to Continuous #22638

Merged
merged 1 commit into from Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 34 additions & 11 deletions internal/services/cosmos/cosmosdb_account_resource.go
Expand Up @@ -925,6 +925,38 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{})
}
}

// backup must be updated independently
var backup documentdb.BasicBackupPolicy
if existing.DatabaseAccountGetProperties.BackupPolicy != nil {
backup = existing.DatabaseAccountGetProperties.BackupPolicy
if d.HasChange("backup") {
if v, ok := d.GetOk("backup"); ok {
newBackup, err := expandCosmosdbAccountBackup(v.([]interface{}), d.HasChange("backup.0.type"), string(existing.DatabaseAccountGetProperties.CreateMode))
if err != nil {
return fmt.Errorf("expanding `backup`: %+v", err)
}
updateParameters := documentdb.DatabaseAccountUpdateParameters{
DatabaseAccountUpdateProperties: &documentdb.DatabaseAccountUpdateProperties{
BackupPolicy: newBackup,
},
}

// Update Database 'backup'...
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)
}
backup = newBackup
} else if string(existing.CreateMode) != "" {
return fmt.Errorf("`create_mode` only works when `backup.type` is `Continuous`")
}
}
}

updateRequired := false
if props := existing.DatabaseAccountGetProperties; props != nil {
location := azure.NormalizeLocation(pointer.From(existing.Location))
Expand Down Expand Up @@ -960,7 +992,7 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{})
// later, however we need to know if they changed or not...
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", "backup",
"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",
"local_authentication_disabled") {
Expand Down Expand Up @@ -1009,6 +1041,7 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{})
NetworkACLBypass: networkByPass,
NetworkACLBypassResourceIds: utils.ExpandStringSlice(d.Get("network_acl_bypass_ids").([]interface{})),
DisableLocalAuth: disableLocalAuthentication,
BackupPolicy: backup,
},
Tags: t,
}
Expand Down Expand Up @@ -1056,16 +1089,6 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{})
}
}

if v, ok := d.GetOk("backup"); ok {
policy, err := expandCosmosdbAccountBackup(v.([]interface{}), d.HasChange("backup.0.type"), createMode)
if err != nil {
return fmt.Errorf("expanding `backup`: %+v", err)
}
accountProps.BackupPolicy = policy
} else if createMode != "" {
return fmt.Errorf("`create_mode` only works when `backup.type` is `Continuous`")
}

// Only do this update if a value has changed above...
if updateRequired {
log.Printf("[INFO] Updating AzureRM Cosmos DB Account: Updating 'DatabaseAccountCreateUpdateParameters'")
Expand Down
58 changes: 58 additions & 0 deletions internal/services/cosmos/cosmosdb_account_resource_test.go
Expand Up @@ -1018,6 +1018,28 @@ func TestAccCosmosDBAccount_backupContinuous(t *testing.T) {
})
}

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicWithBackupPeriodic(data, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelEventual),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.basicWithBackupContinuousUpdate(data, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelEventual),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccCosmosDBAccount_networkBypass(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")
r := CosmosDBAccountResource{}
Expand Down Expand Up @@ -3341,6 +3363,42 @@ resource "azurerm_cosmosdb_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency))
}

func (CosmosDBAccountResource) basicWithBackupContinuousUpdate(data acceptance.TestData, kind documentdb.DatabaseAccountKind, 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 = "%s"

is_virtual_network_filter_enabled = true

consistency_policy {
consistency_level = "%s"
}

geo_location {
location = azurerm_resource_group.test.location
failover_priority = 0
}

backup {
type = "Continuous"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency))
}

func (CosmosDBAccountResource) basicWithNetworkBypassTemplate(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down