Skip to content

Commit 1dd4bdc

Browse files
committed
refactor: cleaning up how fields are fetched
1 parent a7c40d7 commit 1dd4bdc

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

provider/resource_rediscloud_pro_database.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"github.com/RedisLabs/terraform-provider-rediscloud/provider/utils"
67
"regexp"
78
"strconv"
89
"strings"
@@ -351,23 +352,9 @@ func resourceRedisCloudProDatabase() *schema.Resource {
351352
func resourceRedisCloudProDatabaseCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
352353
api := meta.(*apiClient)
353354

354-
subId := d.Get("subscription_id").(int)
355-
355+
subId := *utils.GetInt(d, "subscription_id")
356356
subscriptionMutex.Lock(subId)
357357

358-
name := d.Get("name").(string)
359-
protocol := d.Get("protocol").(string)
360-
supportOSSClusterAPI := d.Get("support_oss_cluster_api").(bool)
361-
respVersion := d.Get("resp_version").(string)
362-
dataPersistence := d.Get("data_persistence").(string)
363-
dataEviction := d.Get("data_eviction").(string)
364-
password := d.Get("password").(string)
365-
replication := d.Get("replication").(bool)
366-
throughputMeasurementBy := d.Get("throughput_measurement_by").(string)
367-
throughputMeasurementValue := d.Get("throughput_measurement_value").(int)
368-
averageItemSizeInBytes := d.Get("average_item_size_in_bytes").(int)
369-
queryPerformanceFactor := d.Get("query_performance_factor").(string)
370-
371358
createModules := make([]*databases.Module, 0)
372359
modules := d.Get("modules").(*schema.Set)
373360
for _, module := range modules.List() {
@@ -399,35 +386,37 @@ func resourceRedisCloudProDatabaseCreate(ctx context.Context, d *schema.Resource
399386
}
400387

401388
createDatabase := databases.CreateDatabase{
402-
Name: redis.String(name),
403-
Protocol: redis.String(protocol),
404-
SupportOSSClusterAPI: redis.Bool(supportOSSClusterAPI),
405-
DataPersistence: redis.String(dataPersistence),
406-
DataEvictionPolicy: redis.String(dataEviction),
407-
Replication: redis.Bool(replication),
389+
Name: utils.GetString(d, "name"),
390+
Protocol: utils.GetString(d, "protocol"),
391+
SupportOSSClusterAPI: utils.GetBool(d, "support_oss_cluster_api"),
392+
DataPersistence: utils.GetString(d, "data_persistence"),
393+
DataEvictionPolicy: utils.GetString(d, "data_eviction"),
394+
Replication: utils.GetBool(d, "replication"),
408395
ThroughputMeasurement: &databases.CreateThroughputMeasurement{
409-
By: redis.String(throughputMeasurementBy),
410-
Value: redis.Int(throughputMeasurementValue),
396+
By: utils.GetString(d, "throughput_measurement_by"),
397+
Value: utils.GetInt(d, "throughput_measurement_value"),
411398
},
412399
Modules: createModules,
413400
Alerts: createAlerts,
414401
RemoteBackup: buildBackupPlan(d.Get("remote_backup").([]interface{}), d.Get("periodic_backup_path")),
415402
}
416403

417-
if queryPerformanceFactor != "" {
418-
createDatabase.QueryPerformanceFactor = redis.String(queryPerformanceFactor)
404+
if v, ok := d.GetOk("query_performance_factor"); ok {
405+
createDatabase.QueryPerformanceFactor = redis.String(v.(string))
419406
}
420407

421408
if v, ok := d.GetOk("redis_version"); ok {
422409
createDatabase.RedisVersion = redis.String(v.(string))
423410
}
424411

425-
if password != "" {
426-
createDatabase.Password = redis.String(password)
412+
if v, ok := d.GetOk("password"); ok {
413+
createDatabase.Password = redis.String(v.(string))
427414
}
428415

429-
if averageItemSizeInBytes > 0 {
430-
createDatabase.AverageItemSizeInBytes = &averageItemSizeInBytes
416+
if v, ok := d.GetOk("average_item_size_in_bytes"); ok {
417+
if size, valid := v.(int); valid && size > 0 {
418+
createDatabase.AverageItemSizeInBytes = redis.Int(size)
419+
}
431420
}
432421

433422
if v, ok := d.GetOk("dataset_size_in_gb"); ok {
@@ -442,8 +431,10 @@ func resourceRedisCloudProDatabaseCreate(ctx context.Context, d *schema.Resource
442431
createDatabase.PortNumber = redis.Int(v.(int))
443432
}
444433

445-
if respVersion != "" {
446-
createDatabase.RespVersion = redis.String(respVersion)
434+
if v, ok := d.GetOk("resp_version"); ok {
435+
if s, valid := v.(string); valid && s != "" {
436+
createDatabase.RespVersion = redis.String(s)
437+
}
447438
}
448439

449440
// Confirm sub is ready to accept a db request

0 commit comments

Comments
 (0)