Skip to content

Commit

Permalink
feat(instance): support sbs volumes in server update (#4467)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Jan 29, 2025
1 parent 05bfd00 commit 89df998
Show file tree
Hide file tree
Showing 3 changed files with 512 additions and 249 deletions.
24 changes: 21 additions & 3 deletions internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-cli/v2/core/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/api/vpc/v2"
"github.com/scaleway/scaleway-sdk-go/logger"
Expand Down Expand Up @@ -291,9 +292,17 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
volumes := make(map[string]*instance.VolumeServerTemplate)
for i, volumeID := range *customRequest.VolumeIDs {
index := strconv.Itoa(i)
volumes[index] = &instance.VolumeServerTemplate{
ID: scw.StringPtr(volumeID),
Name: scw.StringPtr(getServerResponse.Server.Name + "-" + index),

if volumeIsFromSBS(block.NewAPI(client), customRequest.Zone, volumeID) {
volumes[index] = &instance.VolumeServerTemplate{
ID: scw.StringPtr(volumeID),
VolumeType: instance.VolumeVolumeTypeSbsVolume,
}
} else {
volumes[index] = &instance.VolumeServerTemplate{
ID: scw.StringPtr(volumeID),
Name: scw.StringPtr(getServerResponse.Server.Name + "-" + index),
}
}
}
customRequest.Volumes = &volumes
Expand Down Expand Up @@ -323,6 +332,15 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
return c
}

func volumeIsFromSBS(api *block.API, zone scw.Zone, volumeID string) bool {
_, err := api.GetVolume(&block.GetVolumeRequest{
Zone: zone,
VolumeID: volumeID,
})

return err == nil
}

func serverGetBuilder(c *core.Command) *core.Command {
// This method is here as a proof of concept before we find the correct way to implement it at larger scale
c.ArgSpecs.GetPositionalArg().AutoCompleteFunc = func(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
Expand Down
27 changes: 17 additions & 10 deletions internal/namespaces/instance/v1/custom_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,27 @@ func Test_ServerUpdateCustom(t *testing.T) {
// Volumes cases.
t.Run("Volumes", func(t *testing.T) {
t.Run("valid simple block volume", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Commands: core.NewCommandsMerge(
block.GetCommands(),
instance.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
createServerBionic("Server"),
createVolume("Volume", 10, instanceSDK.VolumeVolumeTypeBSSD),
createSbsVolume("VolumeSBS", 10),
),
Cmd: `scw instance server update {{ .Server.ID }} volume-ids.0={{ (index .Server.Volumes "0").ID }} volume-ids.1={{ .Volume.ID }} volume-ids.2={{ .VolumeSBS.ID }}`,
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
size0 := ctx.Result.(*instanceSDK.UpdateServerResponse).Server.Volumes["0"].Size
size1 := ctx.Result.(*instanceSDK.UpdateServerResponse).Server.Volumes["1"].Size
assert.Equal(t, 20*scw.GB, instance.SizeValue(size0), "Size of volume should be 20 GB")
assert.Equal(t, 10*scw.GB, instance.SizeValue(size1), "Size of volume should be 10 GB")
assert.Equal(t, instanceSDK.VolumeServerVolumeTypeSbsVolume, ctx.Result.(*instanceSDK.UpdateServerResponse).Server.Volumes["2"].VolumeType)
},
),
Cmd: `scw instance server update {{ .Server.ID }} volume-ids.0={{ (index .Server.Volumes "0").ID }} volume-ids.1={{ .Volume.ID }}`,
Check: func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
size0 := ctx.Result.(*instanceSDK.UpdateServerResponse).Server.Volumes["0"].Size
size1 := ctx.Result.(*instanceSDK.UpdateServerResponse).Server.Volumes["1"].Size
assert.Equal(t, 20*scw.GB, instance.SizeValue(size0), "Size of volume should be 20 GB")
assert.Equal(t, 10*scw.GB, instance.SizeValue(size1), "Size of volume should be 10 GB")
},
AfterFunc: deleteServer("Server"),
}))

Expand Down
Loading

0 comments on commit 89df998

Please # to comment.