From 4925e9ac63cbe5cc8531c2efb0a964105cf200cf Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 23 Jan 2024 13:21:15 +0800 Subject: [PATCH 1/7] azurerm_cosmosdb_account - support new property tier --- .../cosmos/cosmosdb_account_resource.go | 22 +++++++++++- .../cosmos/cosmosdb_account_resource_test.go | 35 +++++++++++++++---- website/docs/r/cosmosdb_account.html.markdown | 2 ++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 7740e3d1e5a1..0d6f83f765b4 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -521,6 +521,12 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { }, false), }, + "tier": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice(cosmosdb.PossibleValuesForContinuousTier(), false), + }, + "interval_in_minutes": { Type: pluginsdk.TypeInt, Optional: true, @@ -1869,13 +1875,26 @@ func expandCosmosdbAccountBackup(input []interface{}, backupHasChange bool, crea if v := attr["storage_redundancy"].(string); v != "" && !backupHasChange { return nil, fmt.Errorf("`storage_redundancy` can not be set when `type` in `backup` is `Continuous`") } - 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` only works when `backup.type` is `Continuous`") } + if v := attr["tier"].(string); v != "" && !backupHasChange { + return nil, fmt.Errorf("`tier` can not be set when `type` in `backup` is `Periodic`") + } + return cosmosdb.PeriodicModeBackupPolicy{ PeriodicModeProperties: &cosmosdb.PeriodicModeProperties{ BackupIntervalInMinutes: utils.Int64(int64(attr["interval_in_minutes"].(int))), @@ -1899,6 +1918,7 @@ func flattenCosmosdbAccountBackup(input cosmosdb.BackupPolicy) ([]interface{}, e return []interface{}{ map[string]interface{}{ "type": string(cosmosdb.BackupPolicyTypeContinuous), + "tier": string(pointer.From(backupPolicy.ContinuousModeProperties.Tier)), }, }, nil diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index fb4f08e5727f..020e471633d9 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -1039,7 +1039,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), ), @@ -1054,7 +1054,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), ), @@ -3239,7 +3246,7 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "MongoDB" - default_identity_type = join("=", ["UserAssignedIdentity", %[4]s]) + default_identity_type = join("=", ["UserAssignedIdentity", "%[4]s"]) capabilities { name = "EnableMongo" @@ -3258,7 +3265,7 @@ resource "azurerm_cosmosdb_account" "test" { identity { type = "UserAssigned" - identity_ids = [%[4]s] + identity_ids = ["%[4]s"] } } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, identityResource) @@ -3386,7 +3393,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 {} @@ -3415,9 +3422,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 { @@ -3452,6 +3460,10 @@ resource "azurerm_cosmosdb_account" "test" { backup { type = "Continuous" } + + lifecycle { + ignore_changes = [backup.0.tier] + } } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency)) } @@ -4007,6 +4019,10 @@ resource "azurerm_cosmosdb_account" "test" { location = azurerm_resource_group.test.location failover_priority = 0 } + + lifecycle { + ignore_changes = [backup.0.tier] + } } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency)) } @@ -4045,6 +4061,10 @@ resource "azurerm_cosmosdb_account" "test1" { backup { type = "Continuous" } + + lifecycle { + ignore_changes = [backup.0.tier] + } } resource "azurerm_cosmosdb_mongo_database" "test" { @@ -4109,7 +4129,8 @@ resource "azurerm_cosmosdb_account" "test" { // As "restore_timestamp_in_utc" is retrieved dynamically, so it would cause diff when tf plan. So we have to ignore it here. lifecycle { ignore_changes = [ - restore.0.restore_timestamp_in_utc + restore.0.restore_timestamp_in_utc, + backup.0.tier ] } } diff --git a/website/docs/r/cosmosdb_account.html.markdown b/website/docs/r/cosmosdb_account.html.markdown index 3c30ec3a9563..e03bd81963d6 100644 --- a/website/docs/r/cosmosdb_account.html.markdown +++ b/website/docs/r/cosmosdb_account.html.markdown @@ -241,6 +241,8 @@ A `backup` block supports the following: * `type` - (Required) The type of the `backup`. Possible values are `Continuous` and `Periodic`. 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. This is configurable only when `type` is `Periodic`. Possible values are between 60 and 1440. * `retention_in_hours` - (Optional) The time in hours that each backup is retained. This is configurable only when `type` is `Periodic`. Possible values are between 8 and 720. From 8bc1118d229ed4b4995dfa7c94906e691f1ea190 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 23 Jan 2024 13:37:43 +0800 Subject: [PATCH 2/7] update code --- internal/services/cosmos/cosmosdb_account_resource.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 0d6f83f765b4..7f75fb1b8060 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -1915,10 +1915,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(pointer.From(backupPolicy.ContinuousModeProperties.Tier)), + "tier": string(tier), }, }, nil From dc8b0a8c5b00c8833619636223482864761832ac Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 23 Jan 2024 17:26:35 +0800 Subject: [PATCH 3/7] update code --- internal/services/cosmos/cosmosdb_account_resource_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index 020e471633d9..1eb24f57d960 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -3246,7 +3246,7 @@ resource "azurerm_cosmosdb_account" "test" { offer_type = "Standard" kind = "MongoDB" - default_identity_type = join("=", ["UserAssignedIdentity", "%[4]s"]) + default_identity_type = join("=", ["UserAssignedIdentity", %[4]s]) capabilities { name = "EnableMongo" @@ -3265,7 +3265,7 @@ resource "azurerm_cosmosdb_account" "test" { identity { type = "UserAssigned" - identity_ids = ["%[4]s"] + identity_ids = [%[4]s] } } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, identityResource) From 415e02f3da8addfa946f7c4db8d5c8014eb36a4b Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 1 Feb 2024 10:57:57 +0800 Subject: [PATCH 4/7] update code --- internal/services/cosmos/cosmosdb_account_resource.go | 1 + internal/services/cosmos/cosmosdb_account_resource_test.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 95ed647e8734..366befba4f25 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -527,6 +527,7 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { }, false), }, + // Though `tier` has the default value `Continuous7Days` 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, diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index 60dc90aac05b..c4f9700b7ee4 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -3492,6 +3492,7 @@ resource "azurerm_cosmosdb_account" "test" { type = "Continuous" } + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [backup.0.tier] } @@ -4051,6 +4052,7 @@ resource "azurerm_cosmosdb_account" "test" { failover_priority = 0 } + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [backup.0.tier] } @@ -4093,6 +4095,7 @@ resource "azurerm_cosmosdb_account" "test1" { type = "Continuous" } + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [backup.0.tier] } @@ -4158,6 +4161,7 @@ resource "azurerm_cosmosdb_account" "test" { } // As "restore_timestamp_in_utc" is retrieved dynamically, so it would cause diff when tf plan. So we have to ignore it here. + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [ restore.0.restore_timestamp_in_utc, From 8eb6b72b62462b839876d5c599decf0095a5e59a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 1 Feb 2024 13:42:45 +0800 Subject: [PATCH 5/7] update code --- .../cosmos/cosmosdb_account_resource_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index c4f9700b7ee4..60990c7ea863 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -4206,6 +4206,11 @@ resource "azurerm_cosmosdb_account" "test1" { backup { type = "Continuous" } + + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. + lifecycle { + ignore_changes = [backup.0.tier] + } } resource "azurerm_cosmosdb_table" "test" { @@ -4262,9 +4267,11 @@ resource "azurerm_cosmosdb_account" "test" { } // As "restore_timestamp_in_utc" is retrieved dynamically, so it would cause diff when tf plan. So we have to ignore it here. + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [ - restore.0.restore_timestamp_in_utc + restore.0.restore_timestamp_in_utc, + backup.0.tier ] } } @@ -4305,6 +4312,11 @@ resource "azurerm_cosmosdb_account" "test1" { backup { type = "Continuous" } + + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. + lifecycle { + ignore_changes = [backup.0.tier] + } } resource "azurerm_cosmosdb_gremlin_database" "test" { @@ -4377,9 +4389,11 @@ resource "azurerm_cosmosdb_account" "test" { } // As "restore_timestamp_in_utc" is retrieved dynamically, so it would cause diff when tf plan. So we have to ignore it here. + // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [ - restore.0.restore_timestamp_in_utc + restore.0.restore_timestamp_in_utc, + backup.0.tier ] } } From 9dd4808496cb263e1a884f3a38fec5560fb96de7 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 20 Feb 2024 10:35:11 +0800 Subject: [PATCH 6/7] update code --- .../cosmos/cosmosdb_account_resource.go | 1 + .../cosmos/cosmosdb_account_resource_test.go | 37 ++----------------- 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 26812defa3d8..8d43c88fdf02 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -533,6 +533,7 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { "tier": { Type: pluginsdk.TypeString, Optional: true, + Computed: true, ValidateFunc: validation.StringInSlice(cosmosdb.PossibleValuesForContinuousTier(), false), }, diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index 6d5db93acb82..d0e73d01f0e7 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -3557,11 +3557,6 @@ resource "azurerm_cosmosdb_account" "test" { backup { type = "Continuous" } - - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. - lifecycle { - ignore_changes = [backup.0.tier] - } } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency)) } @@ -4117,11 +4112,6 @@ resource "azurerm_cosmosdb_account" "test" { location = azurerm_resource_group.test.location failover_priority = 0 } - - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. - lifecycle { - ignore_changes = [backup.0.tier] - } } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, string(kind), string(consistency)) } @@ -4160,11 +4150,6 @@ resource "azurerm_cosmosdb_account" "test1" { backup { type = "Continuous" } - - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. - lifecycle { - ignore_changes = [backup.0.tier] - } } resource "azurerm_cosmosdb_mongo_database" "test" { @@ -4227,11 +4212,9 @@ resource "azurerm_cosmosdb_account" "test" { } // As "restore_timestamp_in_utc" is retrieved dynamically, so it would cause diff when tf plan. So we have to ignore it here. - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [ - restore.0.restore_timestamp_in_utc, - backup.0.tier + restore.0.restore_timestamp_in_utc ] } } @@ -4272,11 +4255,6 @@ resource "azurerm_cosmosdb_account" "test1" { backup { type = "Continuous" } - - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. - lifecycle { - ignore_changes = [backup.0.tier] - } } resource "azurerm_cosmosdb_table" "test" { @@ -4333,11 +4311,9 @@ resource "azurerm_cosmosdb_account" "test" { } // As "restore_timestamp_in_utc" is retrieved dynamically, so it would cause diff when tf plan. So we have to ignore it here. - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [ - restore.0.restore_timestamp_in_utc, - backup.0.tier + restore.0.restore_timestamp_in_utc ] } } @@ -4378,11 +4354,6 @@ resource "azurerm_cosmosdb_account" "test1" { backup { type = "Continuous" } - - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. - lifecycle { - ignore_changes = [backup.0.tier] - } } resource "azurerm_cosmosdb_gremlin_database" "test" { @@ -4455,11 +4426,9 @@ resource "azurerm_cosmosdb_account" "test" { } // As "restore_timestamp_in_utc" is retrieved dynamically, so it would cause diff when tf plan. So we have to ignore it here. - // API would return the default value "Continuous7Days" when backup.type is "Continuous" and "tier" isn't set. At this time, it would cause diff. So here ignore_changes for "tier" is added. lifecycle { ignore_changes = [ - restore.0.restore_timestamp_in_utc, - backup.0.tier + restore.0.restore_timestamp_in_utc ] } } From e5a606223f4a97e87eea7f1701e5fe789ddafc7f Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 20 Feb 2024 16:58:11 +0800 Subject: [PATCH 7/7] update code --- internal/services/cosmos/cosmosdb_account_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 8d43c88fdf02..f2dae067718e 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -529,7 +529,7 @@ func resourceCosmosDbAccount() *pluginsdk.Resource { }, false), }, - // Though `tier` has the default value `Continuous7Days` but `tier` is only for the backup type `Continuous`. So the default value isn't added in the property schema. + // 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,