Skip to content

fix(instance): display sbs volumes' size in instance image list details #4766

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

Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions internal/namespaces/instance/v1/custom_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func imageListBuilder(c *core.Command) *core.Command {
req.Public = scw.BoolPtr(false)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
blockAPI := block.NewAPI(client)

opts := []scw.RequestOption{scw.WithAllPages()}
if req.Zone == scw.Zone(core.AllLocalities) {
Expand All @@ -210,6 +211,33 @@ func imageListBuilder(c *core.Command) *core.Command {
newCustomImage := &imageListItem{
Image: image,
}

if image.RootVolume.VolumeType == instance.VolumeVolumeTypeSbsSnapshot {
blockVolume, err := blockAPI.GetSnapshot(&block.GetSnapshotRequest{
SnapshotID: image.RootVolume.ID,
Zone: image.Zone,
}, scw.WithContext(ctx))
if err != nil {
return nil, err
}

newCustomImage.Image.RootVolume.Size = blockVolume.Size
}

for index, volume := range image.ExtraVolumes {
if volume.VolumeType == instance.VolumeVolumeTypeSbsSnapshot {
blockVolume, err := blockAPI.GetSnapshot(&block.GetSnapshotRequest{
SnapshotID: volume.ID,
Zone: volume.Zone,
}, scw.WithContext(ctx))
if err != nil {
return nil, err
}

newCustomImage.Image.ExtraVolumes[index].Size = blockVolume.Size
}
}

customImages = append(customImages, newCustomImage)

if image.FromServer == "" {
Expand Down
73 changes: 73 additions & 0 deletions internal/namespaces/instance/v1/custom_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/scaleway/scaleway-cli/v2/core"
block "github.com/scaleway/scaleway-cli/v2/internal/namespaces/block/v1alpha1"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/instance/v1"
"github.com/scaleway/scaleway-cli/v2/internal/testhelpers"
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
Expand Down Expand Up @@ -111,6 +112,50 @@ func createImage(metaKey string) core.BeforeFunc {
)
}

func createImageWithSBSRootVolume(metaKey string) core.BeforeFunc {
return core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true image=ubuntu-jammy root-volume=sbs:20G:5000 --wait"),
),
core.ExecStoreBeforeCmd(
"Snapshot",
`scw block snapshot create volume-id={{ (index .Server.Volumes "0").ID }} --wait`,
),
core.ExecStoreBeforeCmd(
metaKey,
`scw instance image create snapshot-id={{ .Snapshot.ID }} arch=x86_64`,
),
)
}

func createImageWithSBSAdditionalVolumes(metaKey string) core.BeforeFunc {
return core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"Server",
testServerCommand(
"stopped=true image=ubuntu-jammy root-volume=local:20G additional-volumes.0=sbs:10GB:5000 additional-volumes.1=sbs:15GB:15000 --wait",
),
),
core.ExecStoreBeforeCmd(
"SnapshotRoot",
`scw instance snapshot create volume-id={{ (index .Server.Volumes "0").ID }} --wait`,
),
core.ExecStoreBeforeCmd(
"SnapshotAdditional1",
`scw block snapshot create volume-id={{ (index .Server.Volumes "1").ID }} --wait`,
),
core.ExecStoreBeforeCmd(
"SnapshotAdditional2",
`scw block snapshot create volume-id={{ (index .Server.Volumes "2").ID }} --wait`,
),
core.ExecStoreBeforeCmd(
metaKey,
`scw instance image create snapshot-id={{ .SnapshotRoot.ID }} additional-volumes.0.id={{ .SnapshotAdditional1.ID }} additional-volumes.1.id={{ .SnapshotAdditional2.ID }} arch=x86_64`,
),
)
}

func deleteImage(metaKey string) core.AfterFunc {
return core.ExecAfterCmd(
`scw instance image delete {{ .` + metaKey + `.Image.ID }} with-snapshots=true`,
Expand All @@ -128,6 +173,34 @@ func Test_ImageList(t *testing.T) {
),
AfterFunc: deleteImage("Image"),
}))

t.Run("With SBS root volume", core.Test(&core.TestConfig{
BeforeFunc: createImageWithSBSRootVolume("ImageSBSRoot"),
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
Cmd: "scw instance image list",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: deleteImage("ImageSBSRoot"),
}))

t.Run("With SBS additional volumes", core.Test(&core.TestConfig{
BeforeFunc: createImageWithSBSAdditionalVolumes("ImageSBSAdditional"),
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
Cmd: "scw instance image list",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: deleteImage("ImageSBSAdditional"),
}))
}

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