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 - support new property backup.tier #24595

Merged
merged 11 commits into from Feb 20, 2024
27 changes: 26 additions & 1 deletion internal/services/cosmos/cosmosdb_account_resource.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand All @@ -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

Expand Down
16 changes: 12 additions & 4 deletions internal/services/cosmos/cosmosdb_account_resource_test.go
Expand Up @@ -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),
),
Expand All @@ -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),
),
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cosmosdb_account.html.markdown
Expand Up @@ -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`.
Expand Down