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_redis_cache - Check if redis_configuration.aof_backup_enabled configured #22774

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions internal/services/redis/redis_cache_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ func expandRedisConfiguration(d *pluginsdk.ResourceData) (*redis.RedisCommonProp
return output, nil
}
raw := input[0].(map[string]interface{})
skuName := d.Get("sku_name").(string)

if v := raw["maxclients"].(int); v > 0 {
output.Maxclients = utils.String(strconv.Itoa(v))
Expand Down Expand Up @@ -825,8 +826,7 @@ func expandRedisConfiguration(d *pluginsdk.ResourceData) (*redis.RedisCommonProp
// nolint : staticcheck
v, valExists := d.GetOkExists("redis_configuration.0.rdb_backup_enabled")
if valExists {
skuName := d.Get("sku_name").(string)
rdbBackupEnabled := raw["rdb_backup_enabled"].(bool)
rdbBackupEnabled := v.(bool)

// rdb_backup_enabled is available when SKU is Premium
if strings.EqualFold(skuName, string(redis.SkuNamePremium)) {
Expand Down Expand Up @@ -861,7 +861,10 @@ func expandRedisConfiguration(d *pluginsdk.ResourceData) (*redis.RedisCommonProp
// nolint : staticcheck
v, valExists = d.GetOkExists("redis_configuration.0.aof_backup_enabled")
if valExists {
output.AofBackupEnabled = utils.String(strconv.FormatBool(v.(bool)))
// aof_backup_enabled is available when SKU is Premium
if strings.EqualFold(skuName, string(redis.SkuNamePremium)) {
output.AofBackupEnabled = utils.String(strconv.FormatBool(v.(bool)))
}
}

if v := raw["aof_storage_connection_string_0"].(string); v != "" {
Expand Down
75 changes: 75 additions & 0 deletions internal/services/redis/redis_cache_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,27 @@ func TestAccRedisCache_AOFBackupEnabledDisabled(t *testing.T) {
})
}

// ignore `aof_backup_enabled` if SKU is not `Premium`
func TestAccRedisCache_IgnoreAOFBackupWhenSKUNotPremium(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_redis_cache", "test")
r := RedisCacheResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.ignoreAOFBackupEnableWhenSKUNotPremium(data, "volatile-lru"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
{
Config: r.ignoreAOFBackupEnableWhenSKUNotPremium(data, "allkeys-lru"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
})
}

func TestAccRedisCache_PatchSchedule(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_redis_cache", "test")
r := RedisCacheResource{}
Expand Down Expand Up @@ -921,6 +942,60 @@ resource "azurerm_redis_cache" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger)
}

func (RedisCacheResource) ignoreAOFBackupEnableWhenSKUNotPremium(data acceptance.TestData, maxMemoryPolicy string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
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_redis_cache" "test" {
name = "acctestRedis-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
capacity = 1
family = "C"
sku_name = "Basic"
enable_non_ssl_port = false

redis_configuration {
aof_backup_enabled = false
maxmemory_reserved = 125
maxmemory_delta = 125
maxmemory_policy = "%s"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, maxMemoryPolicy)
}

func (RedisCacheResource) patchSchedule(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/redis_cache.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ 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. Defaults to `false`.

~> **NOTE:** `aof_backup_enabled` can only be set when SKU is `Premium`.

* `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 Down
Loading