@@ -9,8 +9,10 @@ import (
99 block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
1010 "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1111 "github.com/scaleway/scaleway-sdk-go/scw"
12+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1213 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1314 "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
15+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance/instancehelpers"
1416)
1517
1618const (
@@ -53,3 +55,42 @@ func customDiffCannotShrink(key string) schema.CustomizeDiffFunc {
5355 return oldValue < newValue
5456 })
5557}
58+
59+ func migrateInstanceToBlockVolume (ctx context.Context , api * instancehelpers.BlockAndInstanceAPI , zone scw.Zone , volumeID string , timeout time.Duration ) (* block.Volume , error ) {
60+ instanceVolumeResp , err := api .GetVolume (& instance.GetVolumeRequest {
61+ Zone : zone ,
62+ VolumeID : volumeID ,
63+ })
64+ if err != nil {
65+ return nil , err
66+ }
67+
68+ plan , err := api .PlanBlockMigration (& instance.PlanBlockMigrationRequest {
69+ Zone : instanceVolumeResp .Volume .Zone ,
70+ VolumeID : & instanceVolumeResp .Volume .ID ,
71+ })
72+ if err != nil {
73+ return nil , err
74+ }
75+
76+ err = api .ApplyBlockMigration (& instance.ApplyBlockMigrationRequest {
77+ Zone : instanceVolumeResp .Volume .Zone ,
78+ VolumeID : & instanceVolumeResp .Volume .ID ,
79+ ValidationKey : plan .ValidationKey ,
80+ })
81+ if err != nil {
82+ return nil , err
83+ }
84+
85+ _ , err = instancehelpers .WaitForVolume (ctx , api .API , zone , volumeID , timeout )
86+ if err != nil && ! httperrors .Is404 (err ) {
87+ return nil , err
88+ }
89+
90+ blockVolume , err := waitForBlockVolume (ctx , api .BlockAPI , zone , volumeID , timeout )
91+ if err != nil {
92+ return nil , err
93+ }
94+
95+ return blockVolume , nil
96+ }
0 commit comments