Skip to content

Commit

Permalink
feat(block_volume): support migration from instance volume (#2922)
Browse files Browse the repository at this point in the history
* refactor(instance): extract block helpers to a package

* feat(instance_volume): support migration

* feat(block_volume): support migration from instance volume

* add more checks to test

* lint

* update cassette
  • Loading branch information
Codelax authored Feb 17, 2025
1 parent 363cdbb commit 0f02db0
Show file tree
Hide file tree
Showing 20 changed files with 1,966 additions and 456 deletions.
41 changes: 41 additions & 0 deletions internal/services/block/helpers_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance/instancehelpers"
)

const (
Expand Down Expand Up @@ -53,3 +55,42 @@ func customDiffCannotShrink(key string) schema.CustomizeDiffFunc {
return oldValue < newValue
})
}

func migrateInstanceToBlockVolume(ctx context.Context, api *instancehelpers.BlockAndInstanceAPI, zone scw.Zone, volumeID string, timeout time.Duration) (*block.Volume, error) {
instanceVolumeResp, err := api.GetVolume(&instance.GetVolumeRequest{
Zone: zone,
VolumeID: volumeID,
})
if err != nil {
return nil, err
}

plan, err := api.PlanBlockMigration(&instance.PlanBlockMigrationRequest{
Zone: instanceVolumeResp.Volume.Zone,
VolumeID: &instanceVolumeResp.Volume.ID,
})
if err != nil {
return nil, err
}

err = api.ApplyBlockMigration(&instance.ApplyBlockMigrationRequest{
Zone: instanceVolumeResp.Volume.Zone,
VolumeID: &instanceVolumeResp.Volume.ID,
ValidationKey: plan.ValidationKey,
})
if err != nil {
return nil, err
}

_, err = instancehelpers.WaitForVolume(ctx, api.API, zone, volumeID, timeout)
if err != nil && !httperrors.Is404(err) {
return nil, err
}

blockVolume, err := waitForBlockVolume(ctx, api.BlockAPI, zone, volumeID, timeout)
if err != nil {
return nil, err
}

return blockVolume, nil
}
Loading

0 comments on commit 0f02db0

Please sign in to comment.