diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 4665d07650f0..f2dae067718e 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -529,6 +529,14 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { }, false), }, + // Though `tier` has the default value `Continuous30Days` but `tier` is only for the backup type `Continuous`. So the default value isn't added in the property schema. + "tier": { + Type: pluginsdk.TypeString, + Optional: true, + Computed: true, + ValidateFunc: validation.StringInSlice(cosmosdb.PossibleValuesForContinuousTier(), false), + }, + "interval_in_minutes": { Type: pluginsdk.TypeInt, Optional: true, @@ -1920,13 +1928,25 @@ func expandCosmosdbAccountBackup(input []interface{}, backupHasChange bool, crea return nil, fmt.Errorf("`storage_redundancy` cannot be defined when the `backup.type` is set to %q", cosmosdb.BackupPolicyTypeContinuous) } - return cosmosdb.ContinuousModeBackupPolicy{}, nil + result := cosmosdb.ContinuousModeBackupPolicy{} + + if v := attr["tier"].(string); v != "" { + result.ContinuousModeProperties = &cosmosdb.ContinuousModeProperties{ + Tier: pointer.To(cosmosdb.ContinuousTier(v)), + } + } + + return result, nil case string(cosmosdb.BackupPolicyTypePeriodic): if createMode != "" { return nil, fmt.Errorf("`create_mode` can only be defined when the `backup.type` is set to %q, got %q", cosmosdb.BackupPolicyTypeContinuous, cosmosdb.BackupPolicyTypePeriodic) } + if v := attr["tier"].(string); v != "" && !backupHasChange { + return nil, fmt.Errorf("`tier` can not be set when `type` in `backup` is `Periodic`") + } + // Mirror the behavior of the old SDK... periodicModeBackupPolicy := cosmosdb.PeriodicModeBackupPolicy{ PeriodicModeProperties: &cosmosdb.PeriodicModeProperties{ @@ -1953,9 +1973,14 @@ func flattenCosmosdbAccountBackup(input cosmosdb.BackupPolicy) ([]interface{}, e switch backupPolicy := input.(type) { case cosmosdb.ContinuousModeBackupPolicy: + var tier cosmosdb.ContinuousTier + if v := backupPolicy.ContinuousModeProperties; v != nil { + tier = pointer.From(v.Tier) + } return []interface{}{ map[string]interface{}{ "type": string(cosmosdb.BackupPolicyTypeContinuous), + "tier": string(tier), }, }, nil diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index 07778117d1c4..d0e73d01f0e7 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -1069,7 +1069,7 @@ func TestAccCosmosDBAccount_backupPeriodicToContinuous(t *testing.T) { }, data.ImportStep(), { - Config: r.basicWithBackupContinuous(data, cosmosdb.DatabaseAccountKindGlobalDocumentDB, cosmosdb.DefaultConsistencyLevelEventual), + Config: r.basicWithBackupContinuous(data, cosmosdb.DatabaseAccountKindGlobalDocumentDB, cosmosdb.DefaultConsistencyLevelEventual, cosmosdb.ContinuousTierContinuousSevenDays), Check: acceptance.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -1084,7 +1084,14 @@ func TestAccCosmosDBAccount_backupContinuous(t *testing.T) { data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.basicWithBackupContinuous(data, cosmosdb.DatabaseAccountKindGlobalDocumentDB, cosmosdb.DefaultConsistencyLevelEventual), + Config: r.basicWithBackupContinuous(data, cosmosdb.DatabaseAccountKindGlobalDocumentDB, cosmosdb.DefaultConsistencyLevelEventual, cosmosdb.ContinuousTierContinuousSevenDays), + Check: acceptance.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.basicWithBackupContinuous(data, cosmosdb.DatabaseAccountKindGlobalDocumentDB, cosmosdb.DefaultConsistencyLevelEventual, cosmosdb.ContinuousTierContinuousThreeZeroDays), Check: acceptance.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -3483,7 +3490,7 @@ resource "azurerm_cosmosdb_account" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency)) } -func (CosmosDBAccountResource) basicWithBackupContinuous(data acceptance.TestData, kind cosmosdb.DatabaseAccountKind, consistency cosmosdb.DefaultConsistencyLevel) string { +func (CosmosDBAccountResource) basicWithBackupContinuous(data acceptance.TestData, kind cosmosdb.DatabaseAccountKind, consistency cosmosdb.DefaultConsistencyLevel, tier cosmosdb.ContinuousTier) string { return fmt.Sprintf(` provider "azurerm" { features {} @@ -3512,9 +3519,10 @@ resource "azurerm_cosmosdb_account" "test" { backup { type = "Continuous" + tier = "%s" } } -`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency)) +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency), string(tier)) } func (CosmosDBAccountResource) basicWithBackupContinuousUpdate(data acceptance.TestData, kind cosmosdb.DatabaseAccountKind, consistency cosmosdb.DefaultConsistencyLevel) string { diff --git a/website/docs/r/cosmosdb_account.html.markdown b/website/docs/r/cosmosdb_account.html.markdown index 40ec813ffc8f..3c6c18afa427 100644 --- a/website/docs/r/cosmosdb_account.html.markdown +++ b/website/docs/r/cosmosdb_account.html.markdown @@ -245,6 +245,8 @@ A `backup` block supports the following: ~> **Note:** Migration of `Periodic` to `Continuous` is one-way, changing `Continuous` to `Periodic` forces a new resource to be created. +* `tier` - (Optional) The continuous backup tier. Possible values are `Continuous7Days` and `Continuous30Days`. + * `interval_in_minutes` - (Optional) The interval in minutes between two backups. Possible values are between 60 and 1440. Defaults to `240`. * `retention_in_hours` - (Optional) The time in hours that each backup is retained. Possible values are between 8 and 720. Defaults to `8`.