Skip to content

Commit 68d5977

Browse files
committed
fix(instance): display sbs volumes' size in instance image list details
1 parent 7438cf9 commit 68d5977

6 files changed

+4934
-0
lines changed

internal/namespaces/instance/v1/custom_image.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func imageListBuilder(c *core.Command) *core.Command {
191191
req.Public = scw.BoolPtr(false)
192192
client := core.ExtractClient(ctx)
193193
api := instance.NewAPI(client)
194+
blockAPI := block.NewAPI(client)
194195

195196
opts := []scw.RequestOption{scw.WithAllPages()}
196197
if req.Zone == scw.Zone(core.AllLocalities) {
@@ -210,6 +211,33 @@ func imageListBuilder(c *core.Command) *core.Command {
210211
newCustomImage := &imageListItem{
211212
Image: image,
212213
}
214+
215+
if image.RootVolume.VolumeType == instance.VolumeVolumeTypeSbsSnapshot {
216+
blockVolume, err := blockAPI.GetSnapshot(&block.GetSnapshotRequest{
217+
SnapshotID: image.RootVolume.ID,
218+
Zone: image.Zone,
219+
}, scw.WithContext(ctx))
220+
if err != nil {
221+
return nil, err
222+
}
223+
224+
newCustomImage.Image.RootVolume.Size = blockVolume.Size
225+
}
226+
227+
for index, volume := range image.ExtraVolumes {
228+
if volume.VolumeType == instance.VolumeVolumeTypeSbsSnapshot {
229+
blockVolume, err := blockAPI.GetSnapshot(&block.GetSnapshotRequest{
230+
SnapshotID: volume.ID,
231+
Zone: volume.Zone,
232+
}, scw.WithContext(ctx))
233+
if err != nil {
234+
return nil, err
235+
}
236+
237+
newCustomImage.Image.ExtraVolumes[index].Size = blockVolume.Size
238+
}
239+
}
240+
213241
customImages = append(customImages, newCustomImage)
214242

215243
if image.FromServer == "" {

internal/namespaces/instance/v1/custom_image_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/scaleway/scaleway-cli/v2/core"
7+
block "github.com/scaleway/scaleway-cli/v2/internal/namespaces/block/v1alpha1"
78
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/instance/v1"
89
"github.com/scaleway/scaleway-cli/v2/internal/testhelpers"
910
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
@@ -111,6 +112,50 @@ func createImage(metaKey string) core.BeforeFunc {
111112
)
112113
}
113114

115+
func createImageWithSBSRootVolume(metaKey string) core.BeforeFunc {
116+
return core.BeforeFuncCombine(
117+
core.ExecStoreBeforeCmd(
118+
"Server",
119+
testServerCommand("stopped=true image=ubuntu-jammy root-volume=sbs:20G:5000 --wait"),
120+
),
121+
core.ExecStoreBeforeCmd(
122+
"Snapshot",
123+
`scw block snapshot create volume-id={{ (index .Server.Volumes "0").ID }} --wait`,
124+
),
125+
core.ExecStoreBeforeCmd(
126+
metaKey,
127+
`scw instance image create snapshot-id={{ .Snapshot.ID }} arch=x86_64`,
128+
),
129+
)
130+
}
131+
132+
func createImageWithSBSAdditionalVolumes(metaKey string) core.BeforeFunc {
133+
return core.BeforeFuncCombine(
134+
core.ExecStoreBeforeCmd(
135+
"Server",
136+
testServerCommand(
137+
"stopped=true image=ubuntu-jammy root-volume=local:20G additional-volumes.0=sbs:10GB:5000 additional-volumes.1=sbs:15GB:15000 --wait",
138+
),
139+
),
140+
core.ExecStoreBeforeCmd(
141+
"SnapshotRoot",
142+
`scw instance snapshot create volume-id={{ (index .Server.Volumes "0").ID }} --wait`,
143+
),
144+
core.ExecStoreBeforeCmd(
145+
"SnapshotAdditional1",
146+
`scw block snapshot create volume-id={{ (index .Server.Volumes "1").ID }} --wait`,
147+
),
148+
core.ExecStoreBeforeCmd(
149+
"SnapshotAdditional2",
150+
`scw block snapshot create volume-id={{ (index .Server.Volumes "2").ID }} --wait`,
151+
),
152+
core.ExecStoreBeforeCmd(
153+
metaKey,
154+
`scw instance image create snapshot-id={{ .SnapshotRoot.ID }} additional-volumes.0.id={{ .SnapshotAdditional1.ID }} additional-volumes.1.id={{ .SnapshotAdditional2.ID }} arch=x86_64`,
155+
),
156+
)
157+
}
158+
114159
func deleteImage(metaKey string) core.AfterFunc {
115160
return core.ExecAfterCmd(
116161
`scw instance image delete {{ .` + metaKey + `.Image.ID }} with-snapshots=true`,
@@ -128,6 +173,34 @@ func Test_ImageList(t *testing.T) {
128173
),
129174
AfterFunc: deleteImage("Image"),
130175
}))
176+
177+
t.Run("With SBS root volume", core.Test(&core.TestConfig{
178+
BeforeFunc: createImageWithSBSRootVolume("ImageSBSRoot"),
179+
Commands: core.NewCommandsMerge(
180+
instance.GetCommands(),
181+
block.GetCommands(),
182+
),
183+
Cmd: "scw instance image list",
184+
Check: core.TestCheckCombine(
185+
core.TestCheckGolden(),
186+
core.TestCheckExitCode(0),
187+
),
188+
AfterFunc: deleteImage("ImageSBSRoot"),
189+
}))
190+
191+
t.Run("With SBS additional volumes", core.Test(&core.TestConfig{
192+
BeforeFunc: createImageWithSBSAdditionalVolumes("ImageSBSAdditional"),
193+
Commands: core.NewCommandsMerge(
194+
instance.GetCommands(),
195+
block.GetCommands(),
196+
),
197+
Cmd: "scw instance image list",
198+
Check: core.TestCheckCombine(
199+
core.TestCheckGolden(),
200+
core.TestCheckExitCode(0),
201+
),
202+
AfterFunc: deleteImage("ImageSBSAdditional"),
203+
}))
131204
}
132205

133206
func Test_ImageUpdate(t *testing.T) {

0 commit comments

Comments
 (0)