Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
sinbai committed Apr 14, 2023
1 parent f9f073f commit 27b63fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 43 deletions.
50 changes: 9 additions & 41 deletions internal/services/redis/redis_cache_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/zones"
"github.com/hashicorp/go-azure-sdk/resource-manager/redis/2021-06-01/patchschedules"
"github.com/hashicorp/go-azure-sdk/resource-manager/redis/2021-06-01/redis"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/go-cty/cty/gocty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
azValidate "github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
Expand Down Expand Up @@ -820,28 +818,14 @@ func expandRedisConfiguration(d *pluginsdk.ResourceData) (*redis.RedisCommonProp
}

// RDB Backup
// the "output.RdbBackupEnabled" should be set value when the rdb_backup_enabled is set to false in config since not setting false means it could not be disabled using patch to update the Redis Cache.
// d.GetOk cannot identify if user sets the property that is bool type and `rdb_backup_enabled` is set as `false`. So it has to identify it using `d.GetRawConfig()`
rc := make(map[string]cty.Value, 0)
if err := gocty.FromCtyValue(d.GetRawConfig(), &rc); err != nil {
// RDB Backup
// the "output.RdbBackupEnabled" should be set value when the rdb_backup_enabled is set to false in config since not setting false means it could not be disabled using patch to update the Redis Cache.
// d.GetOk cannot identify if user sets the property that is bool type and `rdb_backup_enabled` is set as `false`. So it has to identify it using `d.GetRawConfig()`
if c := d.GetRawConfig().AsValueMap(); len(c) > 0 {
rc := cty.Value{}
if err := gocty.FromCtyValue(c["redis_configuration"], &rc); err != nil {
if v := c["redis_configuration"].AsValueSlice(); len(v) > 0 && !v[0].AsValueMap()["rdb_backup_enabled"].IsNull() {
if v[0].AsValueMap()["rdb_backup_enabled"].True() {
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("true")
} else {
output.RdbBackupEnabled = utils.String("false")
}
}
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.(bool)))
}

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

// AOF Backup
// the "output.AdditionalProperties["aof-backup-enabled"]" should be set value when the aof_backup_enabled is set to false in config since not setting false means it could not be disabled using patch to update the Redis Cache.
// d.GetOk cannot identify if user sets the property that is bool type and `aof_backup_enabled` is set as `false`. So it has to identify it using `d.GetRawConfig()`
rc = make(map[string]cty.Value, 0)
if err := gocty.FromCtyValue(d.GetRawConfig(), &rc); err != nil {
// AOF Backup
// the "output.AdditionalProperties["aof-backup-enabled"]" should be set value when the aof_backup_enabled is set to false in config since not setting false means it could not be disabled using patch to update the Redis Cache.
// d.GetOk cannot identify if user sets the property that is bool type and `aof_backup_enabled` is set as `false`. So it has to identify it using `d.GetRawConfig()`
if c := d.GetRawConfig().AsValueMap(); len(c) > 0 {
rc := cty.Value{}
if err := gocty.FromCtyValue(c["redis_configuration"], &rc); err != nil {
if v := d.GetRawConfig().AsValueMap()["redis_configuration"].AsValueSlice(); len(v) > 0 && !v[0].AsValueMap()["aof_backup_enabled"].IsNull() {
if v[0].AsValueMap()["aof_backup_enabled"].True() {
output.AofBackupEnabled = utils.String("true")
} else {
output.AofBackupEnabled = utils.String("false")
}
}
}
}
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 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 @@ -105,7 +105,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 @@ -129,7 +129,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 27b63fe

Please sign in to comment.