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

redis - fix various tests #27557

Merged
merged 4 commits into from
Nov 14, 2024
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
103 changes: 102 additions & 1 deletion internal/services/redis/redis_cache_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package redis

import (
"fmt"
"strconv"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
Expand Down Expand Up @@ -319,7 +320,7 @@ func dataSourceRedisCacheRead(d *pluginsdk.ResourceData, meta interface{}) error
}
d.Set("subnet_id", subnetId)

redisConfiguration, err := flattenRedisConfiguration(props.RedisConfiguration)
redisConfiguration, err := flattenDataSourceRedisConfiguration(props.RedisConfiguration)
if err != nil {
return fmt.Errorf("flattening `redis_configuration`: %+v", err)
}
Expand Down Expand Up @@ -348,3 +349,103 @@ func dataSourceRedisCacheRead(d *pluginsdk.ResourceData, meta interface{}) error

return nil
}

func flattenDataSourceRedisConfiguration(input *redis.RedisCommonPropertiesRedisConfiguration) ([]interface{}, error) {
outputs := make(map[string]interface{})

if input.AadEnabled != nil {
a, err := strconv.ParseBool(*input.AadEnabled)
if err != nil {
return nil, fmt.Errorf("parsing `aad-enabled` %q: %+v", *input.AadEnabled, err)
}
outputs["active_directory_authentication_enabled"] = a
}

if input.Maxclients != nil {
i, err := strconv.Atoi(*input.Maxclients)
if err != nil {
return nil, fmt.Errorf("parsing `maxclients` %q: %+v", *input.Maxclients, err)
}
outputs["maxclients"] = i
}
if input.MaxmemoryDelta != nil {
i, err := strconv.Atoi(*input.MaxmemoryDelta)
if err != nil {
return nil, fmt.Errorf("parsing `maxmemory-delta` %q: %+v", *input.MaxmemoryDelta, err)
}
outputs["maxmemory_delta"] = i
}
if input.MaxmemoryReserved != nil {
i, err := strconv.Atoi(*input.MaxmemoryReserved)
if err != nil {
return nil, fmt.Errorf("parsing `maxmemory-reserved` %q: %+v", *input.MaxmemoryReserved, err)
}
outputs["maxmemory_reserved"] = i
}
if input.MaxmemoryPolicy != nil {
outputs["maxmemory_policy"] = *input.MaxmemoryPolicy
}

if input.PreferredDataPersistenceAuthMethod != nil {
outputs["data_persistence_authentication_method"] = *input.PreferredDataPersistenceAuthMethod
}

if input.MaxfragmentationmemoryReserved != nil {
i, err := strconv.Atoi(*input.MaxfragmentationmemoryReserved)
if err != nil {
return nil, fmt.Errorf("parsing `maxfragmentationmemory-reserved` %q: %+v", *input.MaxfragmentationmemoryReserved, err)
}
outputs["maxfragmentationmemory_reserved"] = i
}

// delta, reserved, enabled, frequency,, count,
if input.RdbBackupEnabled != nil {
b, err := strconv.ParseBool(*input.RdbBackupEnabled)
if err != nil {
return nil, fmt.Errorf("parsing `rdb-backup-enabled` %q: %+v", *input.RdbBackupEnabled, err)
}
outputs["rdb_backup_enabled"] = b
}
if input.RdbBackupFrequency != nil {
i, err := strconv.Atoi(*input.RdbBackupFrequency)
if err != nil {
return nil, fmt.Errorf("parsing `rdb-backup-frequency` %q: %+v", *input.RdbBackupFrequency, err)
}
outputs["rdb_backup_frequency"] = i
}
if input.RdbBackupMaxSnapshotCount != nil {
i, err := strconv.Atoi(*input.RdbBackupMaxSnapshotCount)
if err != nil {
return nil, fmt.Errorf("parsing `rdb-backup-max-snapshot-count` %q: %+v", *input.RdbBackupMaxSnapshotCount, err)
}
outputs["rdb_backup_max_snapshot_count"] = i
}
if input.RdbStorageConnectionString != nil {
outputs["rdb_storage_connection_string"] = *input.RdbStorageConnectionString
}
outputs["notify_keyspace_events"] = pointer.From(input.NotifyKeyspaceEvents)

if v := input.AofBackupEnabled; v != nil {
b, err := strconv.ParseBool(*v)
if err != nil {
return nil, fmt.Errorf("parsing `aof-backup-enabled` %q: %+v", *v, err)
}
outputs["aof_backup_enabled"] = b
}
if input.AofStorageConnectionString0 != nil {
outputs["aof_storage_connection_string_0"] = *input.AofStorageConnectionString0
}
if input.AofStorageConnectionString1 != nil {
outputs["aof_storage_connection_string_1"] = *input.AofStorageConnectionString1
}

// `authnotrequired` is not set for instances launched outside a VNET
outputs["authentication_enabled"] = true
if v := input.Authnotrequired; v != nil {
outputs["authentication_enabled"] = isAuthRequiredAsBool(*v)
}

outputs["storage_account_subscription_id"] = pointer.From(input.StorageSubscriptionId)

return []interface{}{outputs}, nil
}
25 changes: 20 additions & 5 deletions internal/services/redis/redis_cache_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ func resourceRedisCacheRead(d *pluginsdk.ResourceData, meta interface{}) error {
d.Set("redis_version", props.RedisVersion)
d.Set("tenant_settings", flattenTenantSettings(props.TenantSettings))

redisConfiguration, err := flattenRedisConfiguration(props.RedisConfiguration)
redisConfiguration, err := flattenRedisConfiguration(d, props.RedisConfiguration)
if err != nil {
return fmt.Errorf("flattening `redis_configuration`: %+v", err)
}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func flattenTenantSettings(input *map[string]string) map[string]string {
return output
}

func flattenRedisConfiguration(input *redis.RedisCommonPropertiesRedisConfiguration) ([]interface{}, error) {
func flattenRedisConfiguration(d *pluginsdk.ResourceData, input *redis.RedisCommonPropertiesRedisConfiguration) ([]interface{}, error) {
outputs := make(map[string]interface{})

if input.AadEnabled != nil {
Expand Down Expand Up @@ -1074,7 +1074,12 @@ func flattenRedisConfiguration(input *redis.RedisCommonPropertiesRedisConfigurat
outputs["rdb_backup_max_snapshot_count"] = i
}
if input.RdbStorageConnectionString != nil {
outputs["rdb_storage_connection_string"] = *input.RdbStorageConnectionString
// The API returns AccountKey=[key hidden] instead of the value being passed in so we'll just set that value to what Terraform thinks the value is
if len(strings.Split(*input.RdbStorageConnectionString, "AccountKey=[key hidden]")) > 1 {
outputs["rdb_storage_connection_string"] = d.Get("redis_configuration.0.rdb_storage_connection_string")
} else {
outputs["rdb_storage_connection_string"] = *input.RdbStorageConnectionString
}
}
outputs["notify_keyspace_events"] = pointer.From(input.NotifyKeyspaceEvents)

Expand All @@ -1086,10 +1091,20 @@ func flattenRedisConfiguration(input *redis.RedisCommonPropertiesRedisConfigurat
outputs["aof_backup_enabled"] = b
}
if input.AofStorageConnectionString0 != nil {
outputs["aof_storage_connection_string_0"] = *input.AofStorageConnectionString0
// The API returns AccountKey=[key hidden] instead of the value being passed in so we'll just set that value to what Terraform thinks the value is
if len(strings.Split(*input.AofStorageConnectionString0, "AccountKey=[key hidden]")) > 1 {
outputs["aof_storage_connection_string_0"] = d.Get("redis_configuration.0.aof_storage_connection_string_0")
} else {
outputs["aof_storage_connection_string_0"] = *input.AofStorageConnectionString0
}
}
if input.AofStorageConnectionString1 != nil {
outputs["aof_storage_connection_string_1"] = *input.AofStorageConnectionString1
// The API returns AccountKey=[key hidden] instead of the value being passed in so we'll just set that value to what Terraform thinks the value is
if len(strings.Split(*input.AofStorageConnectionString1, "AccountKey=[key hidden]")) > 1 {
outputs["aof_storage_connection_string_1"] = d.Get("redis_configuration.0.aof_storage_connection_string_1")
} else {
outputs["aof_storage_connection_string_1"] = *input.AofStorageConnectionString1
}
}

// `authnotrequired` is not set for instances launched outside a VNET
Expand Down
45 changes: 0 additions & 45 deletions internal/services/redis/redis_cache_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ 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 All @@ -194,11 +189,6 @@ func TestAccRedisCache_BackupEnabled(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"
// TODO: remove this once the Bug's been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/3037
ExpectNonEmptyPlan: true,
},
data.ImportStep("redis_configuration.0.rdb_storage_connection_string"),
})
Expand All @@ -214,11 +204,6 @@ 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..."
// TODO: remove this once the Bug's been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/3037
ExpectNonEmptyPlan: true,
},
{
Config: r.backupDisabled(data),
Expand All @@ -244,7 +229,6 @@ func TestAccRedisCache_AOFBackupEnabled(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
ExpectNonEmptyPlan: true,
},
data.ImportStep("redis_configuration.0.aof_storage_connection_string_0",
"redis_configuration.0.aof_storage_connection_string_1"),
Expand All @@ -261,20 +245,12 @@ 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 @@ -540,27 +516,6 @@ func TestAccRedisCache_identity(t *testing.T) {
})
}

func TestAccRedisCache_SkuDowngrade(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_redis_cache", "test")
r := RedisCacheResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.standard(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.SkuDowngrade(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
})
}

func TestAccRedisCache_AccessKeysAuthenticationEnabledDisabled(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_redis_cache", "test")
r := RedisCacheResource{}
Expand Down
Loading