Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instance): handle sbs volumes in server backup #4248

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions internal/namespaces/instance/v1/custom_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"reflect"
"sort"
"strconv"
"strings"
"time"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-cli/v2/internal/human"
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"
)
Expand Down Expand Up @@ -246,6 +248,7 @@ func imageDeleteBuilder(c *core.Command) *core.Command {
args := argsI.(*customDeleteImageRequest)

api := instance.NewAPI(core.ExtractClient(ctx))
blockAPI := block.NewAPI(core.ExtractClient(ctx))

// If we want to delete snapshot we must GET image before we delete it
image := (*instance.Image)(nil)
Expand All @@ -266,21 +269,49 @@ func imageDeleteBuilder(c *core.Command) *core.Command {
return nil, err
}

type UnknownSnapshot struct {
ID string
Type instance.VolumeVolumeType
}

// Once the image is deleted we can delete snapshots.
if args.WithSnapshots {
snapshotIDs := []string{
image.RootVolume.ID,
snapshots := []UnknownSnapshot{
{
ID: image.RootVolume.ID,
Type: image.RootVolume.VolumeType,
},
}
for _, snapshot := range image.ExtraVolumes {
snapshotIDs = append(snapshotIDs, snapshot.ID)
}
for _, snapshotID := range snapshotIDs {
err := api.DeleteSnapshot(&instance.DeleteSnapshotRequest{
Zone: args.Zone,
SnapshotID: snapshotID,
for _, extraVolume := range image.ExtraVolumes {
snapshots = append(snapshots, UnknownSnapshot{
ID: extraVolume.ID,
Type: extraVolume.VolumeType,
})
if err != nil {
return nil, err
}
for _, snapshot := range snapshots {
if strings.HasPrefix(string(snapshot.Type), "sbs") {
Codelax marked this conversation as resolved.
Show resolved Hide resolved
_, err := blockAPI.WaitForSnapshot(&block.WaitForSnapshotRequest{
SnapshotID: snapshot.ID,
Zone: args.Zone,
})
if err != nil {
return nil, err
}
err = blockAPI.DeleteSnapshot(&block.DeleteSnapshotRequest{
Zone: args.Zone,
SnapshotID: snapshot.ID,
})
if err != nil {
return nil, err
}
} else {
err := api.DeleteSnapshot(&instance.DeleteSnapshotRequest{
Zone: args.Zone,
SnapshotID: snapshot.ID,
})
if err != nil {
return nil, err
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions internal/namespaces/instance/v1/custom_server_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,14 @@ Once your image is ready you will be able to create a new server based on this i
VolumeType: instance.SnapshotVolumeTypeUnified,
}
} else {
template = &instance.ServerActionRequestVolumeBackupTemplate{
VolumeType: instance.SnapshotVolumeType(v.VolumeType),
if v.VolumeType == instance.VolumeServerVolumeTypeSbsVolume {
template = &instance.ServerActionRequestVolumeBackupTemplate{
VolumeType: instance.SnapshotVolumeType("sbs_snapshot"),
}
} else {
template = &instance.ServerActionRequestVolumeBackupTemplate{
VolumeType: instance.SnapshotVolumeType(v.VolumeType),
}
}
}
req.Volumes[v.ID] = template
Expand Down
14 changes: 14 additions & 0 deletions internal/namespaces/instance/v1/custom_server_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ func Test_ServerBackup(t *testing.T) {
core.ExecAfterCmd("scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=local"),
),
}))

t.Run("With SBS volumes", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd("Server", testServerCommand("root-volume=sbs:20G stopped=true image=ubuntu-jammy")),
Cmd: `scw instance server backup {{ .Server.ID }} name=backup`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd("scw instance image delete {{ .CmdResult.Image.ID }} with-snapshots=true"),
core.ExecAfterCmd("scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=local"),
),
}))
}

func Test_ServerAction(t *testing.T) {
Expand Down
Loading
Loading