@@ -3,6 +3,7 @@ package provider
33import (
44 "context"
55 "fmt"
6+ "regexp"
67 "strconv"
78 "strings"
89 "time"
@@ -49,7 +50,7 @@ func resourceRedisCloudProDatabase() *schema.Resource {
4950 Delete : schema .DefaultTimeout (10 * time .Minute ),
5051 },
5152
52- CustomizeDiff : remoteBackupIntervalSetCorrectly ( "remote_backup" ),
53+ CustomizeDiff : customizeDiff ( ),
5354
5455 Schema : map [string ]* schema.Schema {
5556 "subscription_id" : {
@@ -215,6 +216,25 @@ func resourceRedisCloudProDatabase() *schema.Resource {
215216 },
216217 },
217218 },
219+ "query_performance_factor" : {
220+ Description : "Query performance factor for this specific database" ,
221+ Type : schema .TypeString ,
222+ Optional : true ,
223+ Computed : true ,
224+ ForceNew : true ,
225+ ValidateFunc : func (val interface {}, key string ) (warns []string , errs []error ) {
226+ v := val .(string )
227+ matched , err := regexp .MatchString (`^([2468])x$` , v )
228+ if err != nil {
229+ errs = append (errs , fmt .Errorf ("regex match failed: %s" , err ))
230+ return
231+ }
232+ if ! matched {
233+ errs = append (errs , fmt .Errorf ("%q must be an even value between 2x and 8x (inclusive), got: %s" , key , v ))
234+ }
235+ return
236+ },
237+ },
218238 "modules" : {
219239 Description : "Modules to be provisioned in the database" ,
220240 Type : schema .TypeSet ,
@@ -341,6 +361,7 @@ func resourceRedisCloudProDatabaseCreate(ctx context.Context, d *schema.Resource
341361 throughputMeasurementBy := d .Get ("throughput_measurement_by" ).(string )
342362 throughputMeasurementValue := d .Get ("throughput_measurement_value" ).(int )
343363 averageItemSizeInBytes := d .Get ("average_item_size_in_bytes" ).(int )
364+ queryPerformanceFactor := d .Get ("query_performance_factor" ).(string )
344365
345366 createModules := make ([]* databases.Module , 0 )
346367 modules := d .Get ("modules" ).(* schema.Set )
@@ -388,6 +409,10 @@ func resourceRedisCloudProDatabaseCreate(ctx context.Context, d *schema.Resource
388409 RemoteBackup : buildBackupPlan (d .Get ("remote_backup" ).([]interface {}), d .Get ("periodic_backup_path" )),
389410 }
390411
412+ if queryPerformanceFactor != "" {
413+ createDatabase .QueryPerformanceFactor = redis .String (queryPerformanceFactor )
414+ }
415+
391416 if password != "" {
392417 createDatabase .Password = redis .String (password )
393418 }
@@ -513,6 +538,10 @@ func resourceRedisCloudProDatabaseRead(ctx context.Context, d *schema.ResourceDa
513538 return diag .FromErr (err )
514539 }
515540
541+ if err := d .Set ("query_performance_factor" , redis .StringValue (db .QueryPerformanceFactor )); err != nil {
542+ return diag .FromErr (err )
543+ }
544+
516545 if err := d .Set ("modules" , flattenModules (db .Modules )); err != nil {
517546 return diag .FromErr (err )
518547 }
@@ -585,6 +614,12 @@ func resourceRedisCloudProDatabaseRead(ctx context.Context, d *schema.ResourceDa
585614 return diag .FromErr (err )
586615 }
587616
617+ if db .QueryPerformanceFactor != nil {
618+ if err := d .Set ("query_performance_factor" , redis .String (* db .QueryPerformanceFactor )); err != nil {
619+ return diag .FromErr (err )
620+ }
621+ }
622+
588623 if err := readTags (ctx , api , subId , dbId , d ); err != nil {
589624 return diag .FromErr (err )
590625 }
@@ -681,6 +716,11 @@ func resourceRedisCloudProDatabaseUpdate(ctx context.Context, d *schema.Resource
681716 update .SourceIP = []* string {redis .String ("0.0.0.0/0" )}
682717 }
683718
719+ queryPerformanceFactor := d .Get ("query_performance_factor" ).(string )
720+ if queryPerformanceFactor != "" {
721+ update .QueryPerformanceFactor = redis .String (queryPerformanceFactor )
722+ }
723+
684724 if d .Get ("password" ).(string ) != "" {
685725 update .Password = redis .String (d .Get ("password" ).(string ))
686726 }
@@ -873,6 +913,61 @@ func skipDiffIfIntervalIs12And12HourTimeDiff(k, oldValue, newValue string, d *sc
873913 return oldTime .Minute () == newTime .Minute () && oldTime .Add (12 * time .Hour ).Hour () == newTime .Hour ()
874914}
875915
916+ func customizeDiff () schema.CustomizeDiffFunc {
917+ return func (ctx context.Context , diff * schema.ResourceDiff , meta interface {}) error {
918+ if err := validateQueryPerformanceFactor ()(ctx , diff , meta ); err != nil {
919+ return err
920+ }
921+ if err := remoteBackupIntervalSetCorrectly ("remote_backup" )(ctx , diff , meta ); err != nil {
922+ return err
923+ }
924+ return nil
925+ }
926+ }
927+
928+ func validateQueryPerformanceFactor () schema.CustomizeDiffFunc {
929+ return func (ctx context.Context , diff * schema.ResourceDiff , meta interface {}) error {
930+ // Check if "query_performance_factor" is set
931+ qpf , qpfExists := diff .GetOk ("query_performance_factor" )
932+
933+ // Ensure "modules" is explicitly defined in the HCL
934+ _ , modulesExists := diff .GetOkExists ("modules" )
935+
936+ if qpfExists && qpf .(string ) != "" {
937+ if ! modulesExists {
938+ return fmt .Errorf (`"query_performance_factor" requires the "modules" key to be explicitly defined in HCL` )
939+ }
940+
941+ // Retrieve modules as a slice of interfaces
942+ rawModules := diff .Get ("modules" ).(* schema.Set ).List ()
943+
944+ // Convert modules to []map[string]interface{}
945+ var modules []map [string ]interface {}
946+ for _ , rawModule := range rawModules {
947+ if moduleMap , ok := rawModule .(map [string ]interface {}); ok {
948+ modules = append (modules , moduleMap )
949+ }
950+ }
951+
952+ // Check if "RediSearch" exists
953+ if ! containsDBModule (modules , "RediSearch" ) {
954+ return fmt .Errorf (`"query_performance_factor" requires the "modules" list to contain "RediSearch"` )
955+ }
956+ }
957+ return nil
958+ }
959+ }
960+
961+ // Helper function to check if a module exists
962+ func containsDBModule (modules []map [string ]interface {}, moduleName string ) bool {
963+ for _ , module := range modules {
964+ if name , ok := module ["name" ].(string ); ok && name == moduleName {
965+ return true
966+ }
967+ }
968+ return false
969+ }
970+
876971func remoteBackupIntervalSetCorrectly (key string ) schema.CustomizeDiffFunc {
877972 // Validate multiple attributes - https://github.com/hashicorp/terraform-plugin-sdk/issues/233
878973
0 commit comments