Skip to content

Commit

Permalink
azurerm_redis_cache - Fix the issue about data persistence could no…
Browse files Browse the repository at this point in the history
…t be switched (hashicorp#20286)
  • Loading branch information
sinbai authored and xiaxyi committed Apr 28, 2023
1 parent 6260b6e commit 8d9638e
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 23 deletions.
19 changes: 12 additions & 7 deletions internal/services/redis/redis_cache_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,15 @@ func expandRedisConfiguration(d *pluginsdk.ResourceData) (*redis.RedisCommonProp
}

// RDB Backup
if v := raw["rdb_backup_enabled"].(bool); v {
if connStr := raw["rdb_storage_connection_string"].(string); connStr == "" {
return nil, fmt.Errorf("The rdb_storage_connection_string property must be set when rdb_backup_enabled is true")
// nolint : staticcheck
v, valExists := d.GetOkExists("redis_configuration.0.rdb_backup_enabled")
if valExists {
if v := raw["rdb_backup_enabled"].(bool); v {
if connStr := raw["rdb_storage_connection_string"].(string); connStr == "" {
return nil, fmt.Errorf("The rdb_storage_connection_string property must be set when rdb_backup_enabled is true")
}
}
output.RdbBackupEnabled = utils.String(strconv.FormatBool(v))
output.RdbBackupEnabled = utils.String(strconv.FormatBool(v.(bool)))
}

if v := raw["rdb_backup_frequency"].(int); v > 0 {
Expand All @@ -842,8 +846,10 @@ func expandRedisConfiguration(d *pluginsdk.ResourceData) (*redis.RedisCommonProp
}

// AOF Backup
if v := raw["aof_backup_enabled"].(bool); v {
output.AofBackupEnabled = utils.String(strconv.FormatBool(v))
// nolint : staticcheck
v, valExists = d.GetOkExists("redis_configuration.0.aof_backup_enabled")
if valExists {
output.AofBackupEnabled = utils.String(strconv.FormatBool(v.(bool)))
}

if v := raw["aof_storage_connection_string_0"].(string); v != "" {
Expand All @@ -853,7 +859,6 @@ func expandRedisConfiguration(d *pluginsdk.ResourceData) (*redis.RedisCommonProp
if v := raw["aof_storage_connection_string_1"].(string); v != "" {
output.AofStorageConnectionString1 = utils.String(v)
}

authEnabled := raw["enable_authentication"].(bool)
// Redis authentication can only be disabled if it is launched inside a VNET.
if _, isPrivate := d.GetOk("subnet_id"); !isPrivate {
Expand Down
130 changes: 116 additions & 14 deletions internal/services/redis/redis_cache_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func TestAccRedisCache_BackupDisabled(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
// `redis_configuration.0.aof_storage_connection_string_0` and `redis_configuration.0.aof_storage_connection_string_1` are returned as:
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf"
// TODO: remove this once the Bug's been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/3037
ExpectNonEmptyPlan: true,
},
})
}
Expand Down Expand Up @@ -178,7 +183,7 @@ func TestAccRedisCache_BackupEnabledDisabled(t *testing.T) {
check.That(data.ResourceName).ExistsInAzure(r),
),
// `redis_configuration.0.rdb_storage_connection_string` is returned as:
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf"
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf..."
// TODO: remove this once the Bug's been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/3037
ExpectNonEmptyPlan: true,
Expand All @@ -188,8 +193,8 @@ func TestAccRedisCache_BackupEnabledDisabled(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
// `redis_configuration.0.rdb_storage_connection_string` is returned as:
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf"
// `redis_configuration.0.aof_storage_connection_string_0` and `redis_configuration.0.aof_storage_connection_string_1` are returned as:
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf..."
// TODO: remove this once the Bug's been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/3037
ExpectNonEmptyPlan: true,
Expand Down Expand Up @@ -224,13 +229,19 @@ func TestAccRedisCache_AOFBackupEnabledDisabled(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
// `redis_configuration.0.aof_storage_connection_string_0` and `aof_storage_connection_string_1` are returned as:
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf..."
// TODO: remove this once the Bug's been fixed:
ExpectNonEmptyPlan: true,
},
{
Config: r.aofBackupDisabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
// `redis_configuration.0.rdb_storage_connection_string` is returned as:
// "...;AccountKey=[key hidden]" rather than "...;AccountKey=fsjfvjnfnf..."
// TODO: remove this once the Bug's been fixed:
ExpectNonEmptyPlan: true,
},
})
Expand Down Expand Up @@ -690,6 +701,42 @@ resource "azurerm_resource_group" "test" {
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
tags = {
environment = "staging"
}
}
resource "azurerm_storage_account" "test2" {
name = "acctestsa2%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
tags = {
environment = "staging"
}
}
resource "azurerm_storage_account" "test3" {
name = "acctestsa3%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
tags = {
environment = "staging"
}
}
resource "azurerm_redis_cache" "test" {
name = "acctestRedis-%d"
location = azurerm_resource_group.test.location
Expand All @@ -700,10 +747,13 @@ resource "azurerm_redis_cache" "test" {
enable_non_ssl_port = false
redis_configuration {
rdb_backup_enabled = false
rdb_backup_enabled = false
aof_backup_enabled = true
aof_storage_connection_string_0 = azurerm_storage_account.test2.primary_connection_string
aof_storage_connection_string_1 = azurerm_storage_account.test3.primary_connection_string
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString, data.RandomInteger)
}

func (RedisCacheResource) backupEnabled(data acceptance.TestData) string {
Expand Down Expand Up @@ -742,7 +792,7 @@ resource "azurerm_redis_cache" "test" {
rdb_backup_enabled = true
rdb_backup_frequency = 60
rdb_backup_max_snapshot_count = 1
rdb_storage_connection_string = "DefaultEndpointsProtocol=https;BlobEndpoint=${azurerm_storage_account.test.primary_blob_endpoint};AccountName=${azurerm_storage_account.test.name};AccountKey=${azurerm_storage_account.test.primary_access_key}"
rdb_storage_connection_string = azurerm_storage_account.test.primary_connection_string
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
Expand All @@ -759,6 +809,42 @@ resource "azurerm_resource_group" "test" {
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
tags = {
environment = "staging"
}
}
resource "azurerm_storage_account" "test2" {
name = "acctestsa2%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
tags = {
environment = "staging"
}
}
resource "azurerm_storage_account" "test3" {
name = "acctestsa3%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
tags = {
environment = "staging"
}
}
resource "azurerm_redis_cache" "test" {
name = "acctestRedis-%d"
location = azurerm_resource_group.test.location
Expand All @@ -769,10 +855,14 @@ resource "azurerm_redis_cache" "test" {
enable_non_ssl_port = false
redis_configuration {
aof_backup_enabled = false
aof_backup_enabled = false
rdb_backup_enabled = true
rdb_backup_frequency = 60
rdb_backup_max_snapshot_count = 1
rdb_storage_connection_string = azurerm_storage_account.test3.primary_connection_string
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString, data.RandomInteger)
}

func (RedisCacheResource) aofBackupEnabled(data acceptance.TestData) string {
Expand All @@ -787,7 +877,19 @@ resource "azurerm_resource_group" "test" {
}
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "GRS"
tags = {
environment = "staging"
}
}
resource "azurerm_storage_account" "test2" {
name = "acctestsa2%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
Expand All @@ -809,11 +911,11 @@ resource "azurerm_redis_cache" "test" {
redis_configuration {
aof_backup_enabled = true
aof_storage_connection_string_0 = "DefaultEndpointsProtocol=https;BlobEndpoint=${azurerm_storage_account.test.primary_blob_endpoint};AccountName=${azurerm_storage_account.test.name};AccountKey=${azurerm_storage_account.test.primary_access_key}"
aof_storage_connection_string_1 = "DefaultEndpointsProtocol=https;BlobEndpoint=${azurerm_storage_account.test.primary_blob_endpoint};AccountName=${azurerm_storage_account.test.name};AccountKey=${azurerm_storage_account.test.secondary_access_key}"
aof_storage_connection_string_0 = azurerm_storage_account.test.primary_connection_string
aof_storage_connection_string_1 = azurerm_storage_account.test2.primary_connection_string
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger)
}

func (RedisCacheResource) patchSchedule(data acceptance.TestData) string {
Expand Down Expand Up @@ -1192,8 +1294,8 @@ resource "azurerm_redis_cache" "test" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
capacity = 2
family = "C"
sku_name = "Basic"
family = "P"
sku_name = "Premium"
enable_non_ssl_port = false
minimum_tls_version = "1.2"
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/redis_cache.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ An `identity` block supports the following:

A `redis_configuration` block supports the following:

* `aof_backup_enabled` - (Optional) Enable or disable AOF persistence for this Redis Cache.
* `aof_backup_enabled` - (Optional) Enable or disable AOF persistence for this Redis Cache. Defaults to `false`.
* `aof_storage_connection_string_0` - (Optional) First Storage Account connection string for AOF persistence.
* `aof_storage_connection_string_1` - (Optional) Second Storage Account connection string for AOF persistence.

Expand All @@ -131,7 +131,7 @@ redis_configuration {

* `maxfragmentationmemory_reserved` - (Optional) Value in megabytes reserved to accommodate for memory fragmentation. Defaults are shown below.

* `rdb_backup_enabled` - (Optional) Is Backup Enabled? Only supported on Premium SKUs.
* `rdb_backup_enabled` - (Optional) Is Backup Enabled? Only supported on Premium SKUs. Defaults to `false`.

-> **NOTE:** If `rdb_backup_enabled` set to `true`, `rdb_storage_connection_string` must also be set.

Expand Down

0 comments on commit 8d9638e

Please sign in to comment.