Skip to content

Commit 7d55f06

Browse files
trentrosenbaumburythehammer
authored andcommitted
Updates pro sub and DB with QPF CustomDiff validation
1 parent 5380ea2 commit 7d55f06

File tree

3 files changed

+121
-20
lines changed

3 files changed

+121
-20
lines changed

provider/resource_rediscloud_pro_database.go

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func resourceRedisCloudProDatabase() *schema.Resource {
4949
Delete: schema.DefaultTimeout(10 * time.Minute),
5050
},
5151

52-
CustomizeDiff: remoteBackupIntervalSetCorrectly("remote_backup"),
52+
CustomizeDiff: customizeDiff(),
5353

5454
Schema: map[string]*schema.Schema{
5555
"subscription_id": {
@@ -91,12 +91,6 @@ func resourceRedisCloudProDatabase() *schema.Resource {
9191
Computed: true,
9292
ExactlyOneOf: []string{"memory_limit_in_gb", "dataset_size_in_gb"},
9393
},
94-
"query_performance_factor": {
95-
Description: "Query performance factor for this specific database",
96-
Type: schema.TypeString,
97-
Optional: true,
98-
Computed: true,
99-
},
10094
"support_oss_cluster_api": {
10195
Description: "Support Redis open-source (OSS) Cluster API",
10296
Type: schema.TypeBool,
@@ -221,6 +215,13 @@ func resourceRedisCloudProDatabase() *schema.Resource {
221215
},
222216
},
223217
},
218+
"query_performance_factor": {
219+
Description: "Query performance factor for this specific database",
220+
Type: schema.TypeString,
221+
Optional: true,
222+
Computed: true,
223+
ForceNew: true,
224+
},
224225
"modules": {
225226
Description: "Modules to be provisioned in the database",
226227
Type: schema.TypeSet,
@@ -521,6 +522,10 @@ func resourceRedisCloudProDatabaseRead(ctx context.Context, d *schema.ResourceDa
521522
return diag.FromErr(err)
522523
}
523524

525+
if err := d.Set("query_performance_factor", redis.StringValue(db.QueryPerformanceFactor)); err != nil {
526+
return diag.FromErr(err)
527+
}
528+
524529
if err := d.Set("modules", flattenModules(db.Modules)); err != nil {
525530
return diag.FromErr(err)
526531
}
@@ -593,6 +598,11 @@ func resourceRedisCloudProDatabaseRead(ctx context.Context, d *schema.ResourceDa
593598
return diag.FromErr(err)
594599
}
595600

601+
////query_performance_factor
602+
//if err := d.Set("query_performance_factor", redis.String(*db.)); err != nil {
603+
// return diag.FromErr(err)
604+
//}
605+
596606
if err := readTags(ctx, api, subId, dbId, d); err != nil {
597607
return diag.FromErr(err)
598608
}
@@ -882,6 +892,61 @@ func skipDiffIfIntervalIs12And12HourTimeDiff(k, oldValue, newValue string, d *sc
882892
return oldTime.Minute() == newTime.Minute() && oldTime.Add(12*time.Hour).Hour() == newTime.Hour()
883893
}
884894

895+
func customizeDiff() schema.CustomizeDiffFunc {
896+
return func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
897+
if err := validateQueryPerformanceFactor()(ctx, diff, meta); err != nil {
898+
return err
899+
}
900+
if err := remoteBackupIntervalSetCorrectly("remote_backup")(ctx, diff, meta); err != nil {
901+
return err
902+
}
903+
return nil
904+
}
905+
}
906+
907+
func validateQueryPerformanceFactor() schema.CustomizeDiffFunc {
908+
return func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
909+
// Check if "query_performance_factor" is set
910+
qpf, qpfExists := diff.GetOk("query_performance_factor")
911+
912+
// Ensure "modules" is explicitly defined in the HCL
913+
_, modulesExists := diff.GetOkExists("modules")
914+
915+
if qpfExists && qpf.(string) != "" {
916+
if !modulesExists {
917+
return fmt.Errorf(`"query_performance_factor" requires the "modules" key to be explicitly defined in HCL`)
918+
}
919+
920+
// Retrieve modules as a slice of interfaces
921+
rawModules := diff.Get("modules").(*schema.Set).List()
922+
923+
// Convert modules to []map[string]interface{}
924+
var modules []map[string]interface{}
925+
for _, rawModule := range rawModules {
926+
if moduleMap, ok := rawModule.(map[string]interface{}); ok {
927+
modules = append(modules, moduleMap)
928+
}
929+
}
930+
931+
// Check if "RediSearch" exists
932+
if !containsDBModule(modules, "RediSearch") {
933+
return fmt.Errorf(`"query_performance_factor" requires the "modules" list to contain "RediSearch"`)
934+
}
935+
}
936+
return nil
937+
}
938+
}
939+
940+
// Helper function to check if a module exists
941+
func containsDBModule(modules []map[string]interface{}, moduleName string) bool {
942+
for _, module := range modules {
943+
if name, ok := module["name"].(string); ok && name == moduleName {
944+
return true
945+
}
946+
}
947+
return false
948+
}
949+
885950
func remoteBackupIntervalSetCorrectly(key string) schema.CustomizeDiffFunc {
886951
// Validate multiple attributes - https://github.com/hashicorp/terraform-plugin-sdk/issues/233
887952

provider/resource_rediscloud_pro_database_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ resource "rediscloud_subscription" "example" {
310310
quantity = 1
311311
replication=false
312312
support_oss_cluster_api=false
313-
modules = []
313+
modules = ["RediSearch"]
314314
}
315315
}
316316
`

provider/resource_rediscloud_pro_subscription.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,60 @@ import (
2121
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2222
)
2323

24+
func containsModule(modules []interface{}, requiredModule string) bool {
25+
for _, m := range modules {
26+
if mod, ok := m.(string); ok && mod == requiredModule {
27+
return true
28+
}
29+
}
30+
return false
31+
}
32+
2433
func resourceRedisCloudProSubscription() *schema.Resource {
2534
return &schema.Resource{
26-
Description: "Creates a Pro Subscription within your Redis Enterprise Cloud Account.",
27-
CreateContext: resourceRedisCloudProSubscriptionCreate,
28-
ReadContext: resourceRedisCloudProSubscriptionRead,
29-
UpdateContext: resourceRedisCloudProSubscriptionUpdate,
30-
DeleteContext: resourceRedisCloudProSubscriptionDelete,
31-
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
32-
_, cPlanExists := diff.GetOk("creation_plan")
33-
if cPlanExists {
35+
36+
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
37+
38+
// Ensure the "creation_plan" block exists
39+
_, creationPlanExists := diff.GetOk("creation_plan")
40+
if !creationPlanExists {
41+
if diff.Id() == "" {
42+
return fmt.Errorf(`the "creation_plan" block is required`)
43+
}
3444
return nil
3545
}
3646

37-
// The resource hasn't been created yet, but the creation plan is missing.
38-
if diff.Id() == "" {
39-
return fmt.Errorf(`the "creation_plan" block is required`)
47+
// Validate "query_performance_factor" dependency on "modules"
48+
creationPlan := diff.Get("creation_plan").([]interface{})
49+
if len(creationPlan) > 0 {
50+
plan := creationPlan[0].(map[string]interface{})
51+
52+
qpf, qpfExists := plan["query_performance_factor"].(string)
53+
54+
// Ensure "modules" key is explicitly defined in HCL
55+
_, modulesExists := diff.GetOkExists("creation_plan.0.modules")
56+
57+
if qpfExists && qpf != "" {
58+
if !modulesExists {
59+
return fmt.Errorf(`"query_performance_factor" requires the "modules" key to be explicitly defined in HCL`)
60+
}
61+
62+
modules, _ := plan["modules"].([]interface{})
63+
if !containsModule(modules, "RediSearch") {
64+
return fmt.Errorf(`"query_performance_factor" requires the "modules" list to contain "RediSearch"`)
65+
}
66+
}
4067
}
68+
4169
return nil
4270
},
4371

72+
Description: "Creates a Pro Subscription within your Redis Enterprise Cloud Account.",
73+
CreateContext: resourceRedisCloudProSubscriptionCreate,
74+
ReadContext: resourceRedisCloudProSubscriptionRead,
75+
UpdateContext: resourceRedisCloudProSubscriptionUpdate,
76+
DeleteContext: resourceRedisCloudProSubscriptionDelete,
77+
4478
Importer: &schema.ResourceImporter{
4579
// Let the READ operation do the heavy lifting for importing values from the API.
4680
StateContext: schema.ImportStatePassthroughContext,
@@ -259,6 +293,8 @@ func resourceRedisCloudProSubscription() *schema.Resource {
259293
Description: "Query performance factor for this specific database",
260294
Type: schema.TypeString,
261295
Optional: true,
296+
Computed: true,
297+
ForceNew: true,
262298
},
263299
"throughput_measurement_by": {
264300
Description: "Throughput measurement method, (either ‘number-of-shards’ or ‘operations-per-second’)",
@@ -804,7 +840,7 @@ func buildSubscriptionCreatePlanDatabases(memoryStorage string, planMap map[stri
804840
}
805841

806842
queryPerformanceFactor := ""
807-
if v, ok := planMap["queryPerformanceFactor"]; ok && v != nil {
843+
if v, ok := planMap["query_performance_factor"]; ok && v != nil {
808844
queryPerformanceFactor = v.(string)
809845
}
810846

0 commit comments

Comments
 (0)