From 4f16229b41212aeae49cfffd3cf812db36bfe7cf Mon Sep 17 00:00:00 2001 From: lmasson Date: Fri, 26 Jul 2024 16:36:51 +0200 Subject: [PATCH 1/2] feat(dedibox): apply default partition if not specify in install --- internal/namespaces/dedibox/v1/custom.go | 2 + .../namespaces/dedibox/v1/custom_server.go | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 internal/namespaces/dedibox/v1/custom_server.go diff --git a/internal/namespaces/dedibox/v1/custom.go b/internal/namespaces/dedibox/v1/custom.go index 001030eea4..4a530aadde 100644 --- a/internal/namespaces/dedibox/v1/custom.go +++ b/internal/namespaces/dedibox/v1/custom.go @@ -9,6 +9,8 @@ import ( func GetCommands() *core.Commands { cmds := GetGeneratedCommands() + cmds.MustFind("dedibox", "server", "install").Override(serverInstallBuilder) + for _, commandPath := range [][]string{ {"dedibox", "server", "list"}, {"dedibox", "service", "list"}, diff --git a/internal/namespaces/dedibox/v1/custom_server.go b/internal/namespaces/dedibox/v1/custom_server.go new file mode 100644 index 0000000000..c1c356c435 --- /dev/null +++ b/internal/namespaces/dedibox/v1/custom_server.go @@ -0,0 +1,42 @@ +package dedibox + +import ( + "context" + "github.com/scaleway/scaleway-cli/v2/internal/core" + "github.com/scaleway/scaleway-sdk-go/api/dedibox/v1" +) + +func serverInstallBuilder(c *core.Command) *core.Command { + c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { + args := argsI.(*dedibox.InstallServerRequest) + if args.Partitions == nil { + client := core.ExtractClient(ctx) + api := dedibox.NewAPI(client) + + getDefaultPartition, err := api.GetServerDefaultPartitioning(&dedibox.GetServerDefaultPartitioningRequest{ + Zone: args.Zone, + ServerID: args.ServerID, + OsID: args.OsID, + }) + if err != nil { + return nil, err + } + for _, partitions := range getDefaultPartition.Partitions { + InstallPartition := dedibox.InstallPartition{ + FileSystem: partitions.FileSystem, + MountPoint: partitions.MountPoint, + RaidLevel: partitions.RaidLevel, + Capacity: partitions.Capacity, + Connectors: partitions.Connectors, + } + args.Partitions = append(args.Partitions, &InstallPartition) + } + } else { + for _, partitions := range args.Partitions { + partitions.Capacity = partitions.Capacity / 1000000 + } + } + return runner(ctx, args) + }) + return c +} From 2ede8a2804c14510c2afa0b45b8fb230d8f8847a Mon Sep 17 00:00:00 2001 From: lmasson Date: Tue, 30 Jul 2024 09:47:42 +0200 Subject: [PATCH 2/2] tests --- .../dedibox/v1/custom_server_test.go | 28 +++++++++++++++++++ internal/namespaces/dedibox/v1/helper_test.go | 27 ++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 internal/namespaces/dedibox/v1/custom_server_test.go create mode 100644 internal/namespaces/dedibox/v1/helper_test.go diff --git a/internal/namespaces/dedibox/v1/custom_server_test.go b/internal/namespaces/dedibox/v1/custom_server_test.go new file mode 100644 index 0000000000..d69ad65677 --- /dev/null +++ b/internal/namespaces/dedibox/v1/custom_server_test.go @@ -0,0 +1,28 @@ +package dedibox_test + +import ( + "github.com/scaleway/scaleway-cli/v2/internal/core" + "github.com/scaleway/scaleway-cli/v2/internal/namespaces/dedibox/v1" + "testing" +) + +func Test_InstallServer(t *testing.T) { + t.Run("Install server without partitions setup", core.Test(&core.TestConfig{ + Commands: dedibox.GetCommands(), + BeforeFunc: createServer(), + Cmd: "", + Args: nil, + Check: nil, + AfterFunc: nil, + DisableParallel: false, + BuildInfo: nil, + TmpHomeDir: false, + OverrideEnv: nil, + OverrideExec: nil, + Client: nil, + Ctx: nil, + PromptResponseMocks: nil, + Stdin: nil, + EnableAliases: false, + })) +} diff --git a/internal/namespaces/dedibox/v1/helper_test.go b/internal/namespaces/dedibox/v1/helper_test.go new file mode 100644 index 0000000000..79dcf8c7d0 --- /dev/null +++ b/internal/namespaces/dedibox/v1/helper_test.go @@ -0,0 +1,27 @@ +package dedibox_test + +import ( + "github.com/scaleway/scaleway-cli/v2/internal/core" + "github.com/scaleway/scaleway-sdk-go/api/dedibox/v1" +) + +func createServer(metaKey string) core.BeforeFunc { + return core.ExecStoreBeforeCmd(metaKey, "scw dedibox server create offer-id=28483 zone=fr-par-2") +} + +func stopInstall(metaKey string) core.AfterFunc { + return core.ExecAfterCmd("scw dedibox server cancel-install server-id={{ ." + metaKey + ".ID }}") +} + +func deleteServer(metaKey string) core.AfterFunc { + return func(ctx *core.AfterFuncCtx) error { + server := ctx.Meta[metaKey].(*dedibox.Server) + if server.Status == dedibox.ServerStatusReady { + err := core.ExecAfterCmd("scw dedibox server stop server-id={{ ." + metaKey + ".ID }}")(ctx) + if err != nil { + return err + } + } + return core.ExecAfterCmd("scw dedibox server delete server-id={{ ." + metaKey + ".ID }}")(ctx) + } +}