Skip to content

Commit

Permalink
Replace Redis-named ACL fields with generalized versions (#533)
Browse files Browse the repository at this point in the history
* Deprecate Redis-specific fields for generic versions

* Fix a couple of linting issues
  • Loading branch information
christhemorse authored Nov 22, 2024
1 parent 84e63ac commit da906ce
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 62 deletions.
20 changes: 10 additions & 10 deletions vultr/data_source_vultr_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func dataSourceVultrDatabase() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"redis_eviction_policy": {
"eviction_policy": {
Type: schema.TypeString,
Computed: true,
},
Expand Down Expand Up @@ -212,7 +212,7 @@ func dataSourceVultrDatabaseRead(ctx context.Context, d *schema.ResourceData, me
return diag.Errorf("unable to set resource database `plan` read value: %v", err)
}

if databaseList[0].DatabaseEngine != "redis" {
if databaseList[0].DatabaseEngine != "redis" && databaseList[0].DatabaseEngine != "valkey" {
if err := d.Set("plan_disk", databaseList[0].PlanDisk); err != nil {
return diag.Errorf("unable to set resource database `plan_disk` read value: %v", err)
}
Expand Down Expand Up @@ -346,13 +346,13 @@ func dataSourceVultrDatabaseRead(ctx context.Context, d *schema.ResourceData, me
}
}

if databaseList[0].DatabaseEngine == "redis" {
if err := d.Set("redis_eviction_policy", databaseList[0].RedisEvictionPolicy); err != nil {
return diag.Errorf("unable to set resource database `redis_eviction_policy` read value: %v", err)
if databaseList[0].DatabaseEngine == "redis" || databaseList[0].DatabaseEngine == "valkey" {
if err := d.Set("eviction_policy", databaseList[0].EvictionPolicy); err != nil {
return diag.Errorf("unable to set resource database `eviction_policy` read value: %v", err)
}
}

if databaseList[0].DatabaseEngine != "redis" {
if databaseList[0].DatabaseEngine != "redis" && databaseList[0].DatabaseEngine != "valkey" {
if err := d.Set("cluster_time_zone", databaseList[0].ClusterTimeZone); err != nil {
return diag.Errorf("unable to set resource database `cluster_time_zone` read value: %v", err)
}
Expand Down Expand Up @@ -414,7 +414,7 @@ func flattenReplicas(db *govultr.Database) []map[string]interface{} {
"mysql_require_primary_key": db.ReadReplicas[v].MySQLRequirePrimaryKey,
"mysql_slow_query_log": db.ReadReplicas[v].MySQLSlowQueryLog,
"mysql_long_query_time": db.ReadReplicas[v].MySQLLongQueryTime,
"redis_eviction_policy": db.ReadReplicas[v].RedisEvictionPolicy,
"eviction_policy": db.ReadReplicas[v].EvictionPolicy,
"cluster_time_zone": db.ReadReplicas[v].ClusterTimeZone,
}

Expand All @@ -433,11 +433,11 @@ func flattenReplicas(db *govultr.Database) []map[string]interface{} {
delete(r, "mysql_sql_long_query_time")
}

if db.DatabaseEngine != "redis" {
delete(r, "redis_eviction_policy")
if db.DatabaseEngine != "redis" && db.DatabaseEngine != "valkey" {
delete(r, "eviction_policy")
}

if db.DatabaseEngine == "redis" {
if db.DatabaseEngine == "redis" || db.DatabaseEngine == "valkey" {
delete(r, "cluster_time_zone")
}

Expand Down
22 changes: 11 additions & 11 deletions vultr/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func readReplicaSchema(isReadReplica bool) map[string]*schema.Schema {
Computed: true,
Optional: true,
},
"redis_eviction_policy": {
"eviction_policy": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Expand All @@ -169,24 +169,24 @@ func readReplicaSchema(isReadReplica bool) map[string]*schema.Schema {
return s
}

func redisACLSchema() map[string]*schema.Schema {
func userACLSchema() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"redis_acl_categories": {
"acl_categories": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"redis_acl_channels": {
"acl_channels": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"redis_acl_commands": {
"acl_commands": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"redis_acl_keys": {
"acl_keys": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -196,13 +196,13 @@ func redisACLSchema() map[string]*schema.Schema {
return s
}

func flattenRedisACL(dbUser *govultr.DatabaseUser) []map[string]interface{} {
func flattenUserACL(dbUser *govultr.DatabaseUser) []map[string]interface{} {
f := []map[string]interface{}{
{
"redis_acl_categories": dbUser.AccessControl.RedisACLCategories,
"redis_acl_channels": dbUser.AccessControl.RedisACLChannels,
"redis_acl_commands": dbUser.AccessControl.RedisACLCommands,
"redis_acl_keys": dbUser.AccessControl.RedisACLKeys,
"acl_categories": dbUser.AccessControl.ACLCategories,
"acl_channels": dbUser.AccessControl.ACLChannels,
"acl_commands": dbUser.AccessControl.ACLCommands,
"acl_keys": dbUser.AccessControl.ACLKeys,
},
}
return f
Expand Down
28 changes: 14 additions & 14 deletions vultr/resource_vultr_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func resourceVultrDatabase() *schema.Resource {
Computed: true,
Optional: true,
},
"redis_eviction_policy": {
"eviction_policy": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Expand Down Expand Up @@ -219,7 +219,7 @@ func resourceVultrDatabaseCreate(ctx context.Context, d *schema.ResourceData, me
MySQLRequirePrimaryKey: govultr.BoolToBoolPtr(true),
MySQLSlowQueryLog: govultr.BoolToBoolPtr(false),
MySQLLongQueryTime: d.Get("mysql_long_query_time").(int),
RedisEvictionPolicy: d.Get("redis_eviction_policy").(string),
EvictionPolicy: d.Get("eviction_policy").(string),
}

if trustedIPs, trustedIPsOK := d.GetOk("trusted_ips"); trustedIPsOK {
Expand Down Expand Up @@ -269,7 +269,7 @@ func resourceVultrDatabaseCreate(ctx context.Context, d *schema.ResourceData, me
}

// Default user (vultradmin) password can only be changed after creation
if password, passwordOK := d.GetOk("password"); passwordOK && d.Get("database_engine").(string) != "redis" {
if password, passwordOK := d.GetOk("password"); passwordOK && d.Get("database_engine").(string) != "redis" && d.Get("database_engine").(string) != "valkey" { //nolint:lll
req3 := &govultr.DatabaseUserUpdateReq{
Password: password.(string),
}
Expand Down Expand Up @@ -304,7 +304,7 @@ func resourceVultrDatabaseRead(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf("unable to set resource database `plan` read value: %v", err)
}

if database.DatabaseEngine != "redis" {
if database.DatabaseEngine != "redis" && database.DatabaseEngine != "valkey" {
if err := d.Set("plan_disk", database.PlanDisk); err != nil {
return diag.Errorf("unable to set resource database `plan_disk` read value: %v", err)
}
Expand Down Expand Up @@ -438,13 +438,13 @@ func resourceVultrDatabaseRead(ctx context.Context, d *schema.ResourceData, meta
}
}

if database.DatabaseEngine == "redis" {
if err := d.Set("redis_eviction_policy", database.RedisEvictionPolicy); err != nil {
return diag.Errorf("unable to set resource database `redis_eviction_policy` read value: %v", err)
if database.DatabaseEngine == "redis" || database.DatabaseEngine == "valkey" {
if err := d.Set("eviction_policy", database.EvictionPolicy); err != nil {
return diag.Errorf("unable to set resource database `eviction_policy` read value: %v", err)
}
}

if database.DatabaseEngine != "redis" {
if database.DatabaseEngine != "redis" && database.DatabaseEngine != "valkey" {
if err := d.Set("cluster_time_zone", database.ClusterTimeZone); err != nil {
return diag.Errorf("unable to set resource database `cluster_time_zone` read value: %v", err)
}
Expand Down Expand Up @@ -557,11 +557,11 @@ func resourceVultrDatabaseUpdate(ctx context.Context, d *schema.ResourceData, me
req.MySQLLongQueryTime = mysqlLongQueryTime
}

if d.HasChange("redis_eviction_policy") {
log.Printf("[INFO] Updating Redis Eviction Policy")
_, newVal := d.GetChange("redis_eviction_policy")
redisEvictionPolicy := newVal.(string)
req.RedisEvictionPolicy = redisEvictionPolicy
if d.HasChange("eviction_policy") {
log.Printf("[INFO] Updating Eviction Policy")
_, newVal := d.GetChange("eviction_policy")
evictionPolicy := newVal.(string)
req.EvictionPolicy = evictionPolicy
}

if _, _, err := client.Database.Update(ctx, d.Id(), req); err != nil {
Expand All @@ -581,7 +581,7 @@ func resourceVultrDatabaseUpdate(ctx context.Context, d *schema.ResourceData, me
}

// Updating the default user password requires a separate API call
if d.HasChange("password") && d.Get("database_engine").(string) != "redis" {
if d.HasChange("password") && d.Get("database_engine").(string) != "redis" && d.Get("database_engine").(string) != "valkey" { //nolint:lll
_, newVal := d.GetChange("password")
password := newVal.(string)
reqP := &govultr.DatabaseUserUpdateReq{
Expand Down
10 changes: 5 additions & 5 deletions vultr/resource_vultr_database_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func resourceVultrDatabaseReplicaRead(ctx context.Context, d *schema.ResourceDat
return diag.Errorf("unable to set resource database read replica `plan` read value: %v", err)
}

if database.DatabaseEngine != "redis" {
if database.DatabaseEngine != "redis" && database.DatabaseEngine != "valkey" {
if err := d.Set("plan_disk", database.PlanDisk); err != nil {
return diag.Errorf("unable to set resource database read replica `plan_disk` read value: %v", err)
}
Expand Down Expand Up @@ -229,16 +229,16 @@ func resourceVultrDatabaseReplicaRead(ctx context.Context, d *schema.ResourceDat
}
}

if database.DatabaseEngine == "redis" {
if err := d.Set("redis_eviction_policy", database.RedisEvictionPolicy); err != nil {
if database.DatabaseEngine == "redis" || database.DatabaseEngine == "valkey" {
if err := d.Set("eviction_policy", database.EvictionPolicy); err != nil {
return diag.Errorf(
"unable to set resource database read replica `redis_eviction_policy` read value: %v",
"unable to set resource database read replica `eviction_policy` read value: %v",
err,
)
}
}

if database.DatabaseEngine != "redis" {
if database.DatabaseEngine != "redis" && database.DatabaseEngine != "valkey" {
if err := d.Set("cluster_time_zone", database.ClusterTimeZone); err != nil {
return diag.Errorf("unable to set resource database read replica `cluster_time_zone` read value: %v", err)
}
Expand Down
28 changes: 14 additions & 14 deletions vultr/resource_vultr_database_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func resourceVultrDatabaseUser() *schema.Resource {
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: redisACLSchema(),
Schema: userACLSchema(),
},
},
// Computed
Expand Down Expand Up @@ -89,9 +89,9 @@ func resourceVultrDatabaseUserCreate(ctx context.Context, d *schema.ResourceData

d.SetId(databaseUser.Username)

// Redis user access control can only be updated after creation
// Redis/Valkey user access control can only be updated after creation
if accessControl, accessControlOK := d.GetOk("access_control"); accessControlOK {
if err := updateRedisACL(ctx, client, databaseID, d, accessControl); err != nil {
if err := updateUserACL(ctx, client, databaseID, d, accessControl); err != nil {
return diag.Errorf("error updating user access control: %v", err)
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func resourceVultrDatabaseUserRead(ctx context.Context, d *schema.ResourceData,
}

if databaseUser.AccessControl != nil {
if err := d.Set("access_control", flattenRedisACL(databaseUser)); err != nil {
if err := d.Set("access_control", flattenUserACL(databaseUser)); err != nil {
return diag.Errorf("unable to set resource database user `access_control` read value: %v", err)
}
}
Expand All @@ -170,7 +170,7 @@ func resourceVultrDatabaseUserUpdate(ctx context.Context, d *schema.ResourceData

if d.HasChange("access_control") {
_, accessControl := d.GetChange("access_control")
if err := updateRedisACL(ctx, client, databaseID, d, accessControl); err != nil {
if err := updateUserACL(ctx, client, databaseID, d, accessControl); err != nil {
return diag.Errorf("error updating user access control: %v", err)
}
}
Expand Down Expand Up @@ -203,32 +203,32 @@ func resourceVultrDatabaseUserDelete(ctx context.Context, d *schema.ResourceData
return nil
}

func updateRedisACL(ctx context.Context, client *govultr.Client, databaseID string, d *schema.ResourceData, accessControl interface{}) error { //nolint:lll
func updateUserACL(ctx context.Context, client *govultr.Client, databaseID string, d *schema.ResourceData, accessControl interface{}) error { //nolint:lll
// This should only loop once due to MaxItems: 1 in the resource definition
for _, v := range accessControl.(*schema.Set).List() {
var req = &govultr.DatabaseUserACLReq{}
var aclCategories, aclChannels, aclCommands, aclKeys []string
obj := v.(map[string]interface{})

for _, r := range obj["redis_acl_categories"].(*schema.Set).List() {
for _, r := range obj["acl_categories"].(*schema.Set).List() {
aclCategories = append(aclCategories, r.(string))
}
req.RedisACLCategories = &aclCategories
req.ACLCategories = &aclCategories

for _, r := range obj["redis_acl_channels"].(*schema.Set).List() {
for _, r := range obj["acl_channels"].(*schema.Set).List() {
aclChannels = append(aclChannels, r.(string))
}
req.RedisACLChannels = &aclChannels
req.ACLChannels = &aclChannels

for _, r := range obj["redis_acl_commands"].(*schema.Set).List() {
for _, r := range obj["acl_commands"].(*schema.Set).List() {
aclCommands = append(aclCommands, r.(string))
}
req.RedisACLCommands = &aclCommands
req.ACLCommands = &aclCommands

for _, r := range obj["redis_acl_keys"].(*schema.Set).List() {
for _, r := range obj["acl_keys"].(*schema.Set).List() {
aclKeys = append(aclKeys, r.(string))
}
req.RedisACLKeys = &aclKeys
req.ACLKeys = &aclKeys

log.Printf("[INFO] Updating user access control")
if _, _, err := client.Database.UpdateUserACL(ctx, databaseID, d.Id(), req); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The following attributes are exported:
* `mysql_require_primary_key` - The configuration value for whether primary keys are required on the managed database (MySQL engine types only).
* `mysql_slow_query_log` - The configuration value for slow query logging on the managed database (MySQL engine types only).
* `mysql_long_query_time` - The configuration value for the long query time (in seconds) on the managed database (MySQL engine types only).
* `redis_eviction_policy` - The configuration value for the data eviction policy on the managed database (Redis engine types only).
* `redis_eviction_policy` - (Deprecated: use `eviction_policy` instead) The configuration value for the data eviction policy on the managed database (Redis engine types only).
* `eviction_policy` - The configuration value for the data eviction policy on the managed database (Redis engine types only).
* `cluster_time_zone` - The configured time zone for the Managed Database in TZ database format.
* `read_replicas` - A list of read replicas attached to the managed database.
6 changes: 4 additions & 2 deletions website/docs/r/database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ The following arguments are supported:
* `mysql_require_primary_key` - (Optional) The configuration value for whether primary keys are required on the managed database (MySQL engine types only).
* `mysql_slow_query_log` - (Optional) The configuration value for slow query logging on the managed database (MySQL engine types only).
* `mysql_long_query_time` - (Optional) The configuration value for the long query time (in seconds) on the managed database (MySQL engine types only).
* `redis_eviction_policy` - (Optional) The configuration value for the data eviction policy on the managed database (Redis engine types only - `noeviction`, `allkeys-lru`, `volatile-lru`, `allkeys-random`, `volatile-random`, `volatile-ttl`, `volatile-lfu`, `allkeys-lfu`).
* `redis_eviction_policy` - (Deprecated: use `eviction_policy` instead) The configuration value for the data eviction policy on the managed database (Redis engine types only).
* `eviction_policy` - (Optional) The configuration value for the data eviction policy on the managed database (Redis engine types only - `noeviction`, `allkeys-lru`, `volatile-lru`, `allkeys-random`, `volatile-random`, `volatile-ttl`, `volatile-lfu`, `allkeys-lfu`).

## Attributes Reference

Expand Down Expand Up @@ -103,7 +104,8 @@ The following attributes are exported:
* `mysql_require_primary_key` - The configuration value for whether primary keys are required on the managed database (MySQL engine types only).
* `mysql_slow_query_log` - The configuration value for slow query logging on the managed database (MySQL engine types only).
* `mysql_long_query_time` - The configuration value for the long query time (in seconds) on the managed database (MySQL engine types only).
* `redis_eviction_policy` - The configuration value for the data eviction policy on the managed database (Redis engine types only).
* `redis_eviction_policy` - (Deprecated: use `eviction_policy` instead) The configuration value for the data eviction policy on the managed database (Redis engine types only).
* `eviction_policy` - The configuration value for the data eviction policy on the managed database (Redis engine types only).
* `cluster_time_zone` - The configured time zone for the Managed Database in TZ database format.
* `read_replicas` - A list of read replicas attached to the managed database.

Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/database_replica.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ The following attributes are exported:
* `mysql_require_primary_key` - The configuration value for whether primary keys are required on the managed database read replica (MySQL engine types only).
* `mysql_slow_query_log` - The configuration value for slow query logging on the managed database read replica (MySQL engine types only).
* `mysql_long_query_time` - The configuration value for the long query time (in seconds) on the managed database read replica (MySQL engine types only).
* `redis_eviction_policy` - The configuration value for the data eviction policy on the managed database read replica (Redis engine types only).
* `redis_eviction_policy` - (Deprecated: use `eviction_policy` instead) The configuration value for the data eviction policy on the managed database read replica (Redis engine types only).
* `eviction_policy` - The configuration value for the data eviction policy on the managed database read replica (Redis engine types only).
* `cluster_time_zone` - The configured time zone for the managed database read replica in TZ database format.
12 changes: 8 additions & 4 deletions website/docs/r/database_user.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ The following attributes are exported:

`access_control`

* `redis_acl_categories` - List of command category rules for this managed database user (Redis engine types only).
* `redis_acl_channels` - List of publish/subscribe channel patterns for this managed database user (Redis engine types only).
* `redis_acl_commands` - List of individual command rules for this managed database user (Redis engine types only).
* `redis_acl_keys` - List of access rules for this managed database user (Redis engine types only).
* `redis_acl_categories` - (Deprecated: use `acl_categories` instead) List of command category rules for this managed database user (Redis engine types only).
* `redis_acl_channels` - (Deprecated: use `acl_channels` instead) List of publish/subscribe channel patterns for this managed database user (Redis engine types only).
* `redis_acl_commands` - (Deprecated: use `acl_commands` instead) List of individual command rules for this managed database user (Redis engine types only).
* `redis_acl_keys` - (Deprecated: use `acl_keys` instead) List of access rules for this managed database user (Redis engine types only).
* `acl_categories` - List of command category rules for this managed database user (Redis engine types only).
* `acl_channels` - List of publish/subscribe channel patterns for this managed database user (Redis engine types only).
* `acl_commands` - List of individual command rules for this managed database user (Redis engine types only).
* `acl_keys` - List of access rules for this managed database user (Redis engine types only).

0 comments on commit da906ce

Please sign in to comment.