@@ -782,36 +782,11 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m
782
782
updateRequest .DynamicIPRequired = scw .BoolPtr (d .Get ("enable_dynamic_ip" ).(bool ))
783
783
}
784
784
785
- volumes := map [string ]* instanceSDK.VolumeServerTemplate {}
786
-
787
- if raw , hasAdditionalVolumes := d .GetOk ("additional_volume_ids" ); d .HasChanges ("additional_volume_ids" , "root_volume" ) {
788
- volumes ["0" ] = & instanceSDK.VolumeServerTemplate {
789
- ID : scw .StringPtr (zonal .ExpandID (d .Get ("root_volume.0.volume_id" )).ID ),
790
- Name : scw .StringPtr (types .NewRandomName ("vol" )), // name is ignored by the API, any name will work here
791
- Boot : types .ExpandBoolPtr (d .Get ("root_volume.0.boot" )),
792
- }
793
-
794
- if ! hasAdditionalVolumes {
795
- raw = []interface {}{} // Set an empty list if not volumes exist
796
- }
797
-
798
- for i , volumeID := range raw .([]interface {}) {
799
- volumeHasChange := d .HasChange ("additional_volume_ids." + strconv .Itoa (i ))
800
- volume , err := api .GetUnknownVolume (& GetUnknownVolumeRequest {
801
- VolumeID : zonal .ExpandID (volumeID ).ID ,
802
- Zone : zone ,
803
- }, scw .WithContext (ctx ))
804
- if err != nil {
805
- return diag .FromErr (fmt .Errorf ("failed to get updated volume: %w" , err ))
806
- }
807
-
808
- // local volumes can only be added when the server is stopped
809
- if volumeHasChange && ! isStopped && volume .IsLocal () && volume .IsAttached () {
810
- return diag .FromErr (errors .New ("instanceSDK must be stopped to change local volumes" ))
811
- }
812
- volumes [strconv .Itoa (i + 1 )] = volume .VolumeTemplate ()
785
+ if d .HasChanges ("additional_volume_ids" , "root_volume" ) {
786
+ volumes , err := instanceServerVolumesTemplatesUpdate (ctx , d , api , zone , isStopped )
787
+ if err != nil {
788
+ return diag .FromErr (err )
813
789
}
814
-
815
790
serverShouldUpdate = true
816
791
updateRequest .Volumes = & volumes
817
792
}
@@ -1346,3 +1321,39 @@ func ResourceInstanceServerUpdateIPs(ctx context.Context, d *schema.ResourceData
1346
1321
1347
1322
return nil
1348
1323
}
1324
+
1325
+ // instanceServerVolumesTemplatesUpdate returns the list of volumes templates that should be updated for the server.
1326
+ // It uses root_volume and additional_volume_ids to build the volumes templates.
1327
+ func instanceServerVolumesTemplatesUpdate (ctx context.Context , d * schema.ResourceData , api * BlockAndInstanceAPI , zone scw.Zone , serverIsStopped bool ) (map [string ]* instanceSDK.VolumeServerTemplate , error ) {
1328
+ volumes := map [string ]* instanceSDK.VolumeServerTemplate {}
1329
+ raw , hasAdditionalVolumes := d .GetOk ("additional_volume_ids" )
1330
+
1331
+ volumes ["0" ] = & instanceSDK.VolumeServerTemplate {
1332
+ ID : scw .StringPtr (zonal .ExpandID (d .Get ("root_volume.0.volume_id" )).ID ),
1333
+ Name : scw .StringPtr (types .NewRandomName ("vol" )), // name is ignored by the API, any name will work here
1334
+ Boot : types .ExpandBoolPtr (d .Get ("root_volume.0.boot" )),
1335
+ }
1336
+
1337
+ if ! hasAdditionalVolumes {
1338
+ raw = []interface {}{} // Set an empty list if not volumes exist
1339
+ }
1340
+
1341
+ for i , volumeID := range raw .([]interface {}) {
1342
+ volumeHasChange := d .HasChange ("additional_volume_ids." + strconv .Itoa (i ))
1343
+ volume , err := api .GetUnknownVolume (& GetUnknownVolumeRequest {
1344
+ VolumeID : zonal .ExpandID (volumeID ).ID ,
1345
+ Zone : zone ,
1346
+ }, scw .WithContext (ctx ))
1347
+ if err != nil {
1348
+ return nil , fmt .Errorf ("failed to get updated volume: %w" , err )
1349
+ }
1350
+
1351
+ // local volumes can only be added when the server is stopped
1352
+ if volumeHasChange && ! serverIsStopped && volume .IsLocal () && volume .IsAttached () {
1353
+ return nil , errors .New ("instanceSDK must be stopped to change local volumes" )
1354
+ }
1355
+ volumes [strconv .Itoa (i + 1 )] = volume .VolumeTemplate ()
1356
+ }
1357
+
1358
+ return volumes , nil
1359
+ }
0 commit comments