@@ -816,36 +816,11 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m
816816 updateRequest .DynamicIPRequired = scw .BoolPtr (d .Get ("enable_dynamic_ip" ).(bool ))
817817 }
818818
819- volumes := map [string ]* instanceSDK.VolumeServerTemplate {}
820-
821- if raw , hasAdditionalVolumes := d .GetOk ("additional_volume_ids" ); d .HasChanges ("additional_volume_ids" , "root_volume" ) {
822- volumes ["0" ] = & instanceSDK.VolumeServerTemplate {
823- ID : scw .StringPtr (zonal .ExpandID (d .Get ("root_volume.0.volume_id" )).ID ),
824- Name : scw .StringPtr (types .NewRandomName ("vol" )), // name is ignored by the API, any name will work here
825- Boot : types .ExpandBoolPtr (d .Get ("root_volume.0.boot" )),
826- }
827-
828- if ! hasAdditionalVolumes {
829- raw = []interface {}{} // Set an empty list if not volumes exist
830- }
831-
832- for i , volumeID := range raw .([]interface {}) {
833- volumeHasChange := d .HasChange ("additional_volume_ids." + strconv .Itoa (i ))
834- volume , err := api .GetUnknownVolume (& GetUnknownVolumeRequest {
835- VolumeID : zonal .ExpandID (volumeID ).ID ,
836- Zone : zone ,
837- }, scw .WithContext (ctx ))
838- if err != nil {
839- return diag .FromErr (fmt .Errorf ("failed to get updated volume: %w" , err ))
840- }
841-
842- // local volumes can only be added when the server is stopped
843- if volumeHasChange && ! isStopped && volume .IsLocal () && volume .IsAttached () {
844- return diag .FromErr (errors .New ("instanceSDK must be stopped to change local volumes" ))
845- }
846- volumes [strconv .Itoa (i + 1 )] = volume .VolumeTemplate ()
819+ if d .HasChanges ("additional_volume_ids" , "root_volume" ) {
820+ volumes , err := instanceServerVolumesTemplatesUpdate (ctx , d , api , zone , isStopped )
821+ if err != nil {
822+ return diag .FromErr (err )
847823 }
848-
849824 serverShouldUpdate = true
850825 updateRequest .Volumes = & volumes
851826 }
@@ -1420,3 +1395,39 @@ func ResourceInstanceServerUpdateRootVolumeIOPS(ctx context.Context, api *BlockA
14201395
14211396 return nil
14221397}
1398+
1399+ // instanceServerVolumesTemplatesUpdate returns the list of volumes templates that should be updated for the server.
1400+ // It uses root_volume and additional_volume_ids to build the volumes templates.
1401+ func instanceServerVolumesTemplatesUpdate (ctx context.Context , d * schema.ResourceData , api * BlockAndInstanceAPI , zone scw.Zone , serverIsStopped bool ) (map [string ]* instanceSDK.VolumeServerTemplate , error ) {
1402+ volumes := map [string ]* instanceSDK.VolumeServerTemplate {}
1403+ raw , hasAdditionalVolumes := d .GetOk ("additional_volume_ids" )
1404+
1405+ volumes ["0" ] = & instanceSDK.VolumeServerTemplate {
1406+ ID : scw .StringPtr (zonal .ExpandID (d .Get ("root_volume.0.volume_id" )).ID ),
1407+ Name : scw .StringPtr (types .NewRandomName ("vol" )), // name is ignored by the API, any name will work here
1408+ Boot : types .ExpandBoolPtr (d .Get ("root_volume.0.boot" )),
1409+ }
1410+
1411+ if ! hasAdditionalVolumes {
1412+ raw = []interface {}{} // Set an empty list if not volumes exist
1413+ }
1414+
1415+ for i , volumeID := range raw .([]interface {}) {
1416+ volumeHasChange := d .HasChange ("additional_volume_ids." + strconv .Itoa (i ))
1417+ volume , err := api .GetUnknownVolume (& GetUnknownVolumeRequest {
1418+ VolumeID : zonal .ExpandID (volumeID ).ID ,
1419+ Zone : zone ,
1420+ }, scw .WithContext (ctx ))
1421+ if err != nil {
1422+ return nil , fmt .Errorf ("failed to get updated volume: %w" , err )
1423+ }
1424+
1425+ // local volumes can only be added when the server is stopped
1426+ if volumeHasChange && ! serverIsStopped && volume .IsLocal () && volume .IsAttached () {
1427+ return nil , errors .New ("instanceSDK must be stopped to change local volumes" )
1428+ }
1429+ volumes [strconv .Itoa (i + 1 )] = volume .VolumeTemplate ()
1430+ }
1431+
1432+ return volumes , nil
1433+ }
0 commit comments