Skip to content

Commit

Permalink
azurerm_cosmosdb_account - support new property backup.tier (hash…
Browse files Browse the repository at this point in the history
…icorp#24595)

* azurerm_cosmosdb_account - support new property tier

* update code

* update code

* update code

* update code

* update code

* update code
  • Loading branch information
neil-yechenwei authored and rizkybiz committed Feb 21, 2024
1 parent 381fe4f commit fe0c063
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
27 changes: 26 additions & 1 deletion internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,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 @@ -1945,13 +1953,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 @@ -1978,9 +1998,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
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,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 @@ -1099,7 +1099,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 @@ -3530,7 +3537,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 @@ -3559,9 +3566,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
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,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

0 comments on commit fe0c063

Please sign in to comment.