44 "context"
55 "fmt"
66 "github.com/RedisLabs/terraform-provider-rediscloud/provider/utils"
7+ "log"
78 "regexp"
89 "strconv"
910 "strings"
@@ -240,6 +241,7 @@ func resourceRedisCloudProDatabase() *schema.Resource {
240241 Description : "Defines the Redis database version. If omitted, the Redis version will be set to the default version" ,
241242 Type : schema .TypeString ,
242243 Optional : true ,
244+ Computed : true ,
243245 },
244246 "modules" : {
245247 Description : "Modules to be provisioned in the database" ,
@@ -538,6 +540,10 @@ func resourceRedisCloudProDatabaseRead(ctx context.Context, d *schema.ResourceDa
538540 return diag .FromErr (err )
539541 }
540542
543+ if err := d .Set ("redis_version" , redis .StringValue (db .RedisVersion )); err != nil {
544+ return diag .FromErr (err )
545+ }
546+
541547 if err := d .Set ("modules" , flattenModules (db .Modules )); err != nil {
542548 return diag .FromErr (err )
543549 }
@@ -683,19 +689,20 @@ func resourceRedisCloudProDatabaseUpdate(ctx context.Context, d *schema.Resource
683689 }
684690
685691 update := databases.UpdateDatabase {
686- Name : redis . String ( d . Get ( "name" ).( string ) ),
687- SupportOSSClusterAPI : redis . Bool ( d . Get ( "support_oss_cluster_api" ).( bool ) ),
688- Replication : redis . Bool ( d . Get ( "replication" ).( bool ) ),
692+ Name : utils . GetString ( d , "name" ),
693+ SupportOSSClusterAPI : utils . GetBool ( d , "support_oss_cluster_api" ),
694+ Replication : utils . GetBool ( d , "replication" ),
689695 ThroughputMeasurement : & databases.UpdateThroughputMeasurement {
690- By : redis . String ( d . Get ( "throughput_measurement_by" ).( string ) ),
691- Value : redis . Int ( d . Get ( "throughput_measurement_value" ).( int ) ),
696+ By : utils . GetString ( d , "throughput_measurement_by" ),
697+ Value : utils . GetInt ( d , "throughput_measurement_value" ),
692698 },
693- DataPersistence : redis .String (d .Get ("data_persistence" ).(string )),
694- DataEvictionPolicy : redis .String (d .Get ("data_eviction" ).(string )),
699+
700+ DataPersistence : utils .GetString (d , "data_persistence" ),
701+ DataEvictionPolicy : utils .GetString (d , "data_eviction" ),
695702 SourceIP : setToStringSlice (d .Get ("source_ips" ).(* schema.Set )),
696703 Alerts : & alerts ,
697704 RemoteBackup : buildBackupPlan (d .Get ("remote_backup" ).([]interface {}), d .Get ("periodic_backup_path" )),
698- EnableDefaultUser : redis . Bool ( d . Get ( "enable_default_user" ).( bool ) ),
705+ EnableDefaultUser : utils . GetBool ( d , "enable_default_user" ),
699706 }
700707
701708 // One of the following fields must be set, validation is handled in the schema (ExactlyOneOf)
@@ -779,6 +786,25 @@ func resourceRedisCloudProDatabaseUpdate(ctx context.Context, d *schema.Resource
779786 return diag .FromErr (err )
780787 }
781788
789+ // if redis_version has changed, then upgrade first
790+ if d .HasChange ("redis_version" ) {
791+ // if we have just created the database, it will detect an upgrade unnecessarily
792+ originalVersion , newVersion := d .GetChange ("redis_version" )
793+
794+ // if either version is blank, it could attempt to upgrade unnecessarily.
795+ // only upgrade when a known version goes to another known version
796+ if originalVersion .(string ) != "" && newVersion .(string ) != "" {
797+ if diags , unlocked := upgradeRedisVersion (ctx , api , subId , dbId , newVersion .(string )); diags != nil {
798+ if ! unlocked {
799+ subscriptionMutex .Unlock (subId )
800+ }
801+ return diags
802+ }
803+ }
804+ }
805+
806+ // Confirm db + sub active status
807+
782808 if err := api .client .Database .Update (ctx , subId , dbId , update ); err != nil {
783809 subscriptionMutex .Unlock (subId )
784810 return diag .FromErr (err )
@@ -803,6 +829,29 @@ func resourceRedisCloudProDatabaseUpdate(ctx context.Context, d *schema.Resource
803829 return resourceRedisCloudProDatabaseRead (ctx , d , meta )
804830}
805831
832+ func upgradeRedisVersion (ctx context.Context , api * apiClient , subId int , dbId int , newVersion string ) (diag.Diagnostics , bool ) {
833+ log .Printf ("[INFO] Requesting Redis version change to %s..." , newVersion )
834+
835+ upgrade := databases.UpgradeRedisVersion {
836+ TargetRedisVersion : redis .String (newVersion ),
837+ }
838+
839+ if err := api .client .Database .UpgradeRedisVersion (ctx , subId , dbId , upgrade ); err != nil {
840+ subscriptionMutex .Unlock (subId )
841+ return diag .Errorf ("failed to change Redis version to %s: %v" , newVersion , err ), true
842+ }
843+
844+ log .Printf ("[INFO] Redis version change request to %s accepted by API" , newVersion )
845+
846+ // wait for upgrade
847+ if err := waitForDatabaseToBeActive (ctx , subId , dbId , api ); err != nil {
848+ subscriptionMutex .Unlock (subId )
849+ return diag .FromErr (err ), true
850+ }
851+
852+ return nil , false
853+ }
854+
806855func buildBackupPlan (data interface {}, periodicBackupPath interface {}) * databases.DatabaseBackupConfig {
807856 var d map [string ]interface {}
808857
0 commit comments