Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ARGS:

FLAGS:
-h, --help help for create
-w, --wait wait until the server is ready

GLOBAL FLAGS:
-c, --config string The path to the config file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ARGS:

FLAGS:
-h, --help help for delete
-w, --wait wait until the server is ready

GLOBAL FLAGS:
-c, --config string The path to the config file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ARGS:

FLAGS:
-h, --help help for reboot
-w, --wait wait until the server is ready

GLOBAL FLAGS:
-c, --config string The path to the config file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ AVAILABLE COMMANDS:
reboot Reboot a server
reinstall Reinstall a server
update Update a server
wait Wait for a server to reach a stable state

FLAGS:
-h, --help help for server
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
Wait for server to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the server.

USAGE:
scw apple-silicon server wait <server-id ...> [arg=value ...]

EXAMPLES:
Wait for a server to reach a stable state
scw apple-silicon server wait 11111111-1111-1111-1111-111111111111

ARGS:
server-id ID of the server.
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config

FLAGS:
-h, --help help for wait

GLOBAL FLAGS:
-c, --config string The path to the config file
-D, --debug Enable debug mode
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
-p, --profile string The config profile to use
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/mattn/go-colorable v0.1.7
github.com/mattn/go-isatty v0.0.12
github.com/pkg/errors v0.9.1 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210202173413-337fb17121a1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210204115440-da015cdd15e3
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210202173413-337fb17121a1 h1:vytHInJpH5I4FheLyFW2wu5LmtcQOJhetm3+jaVGp74=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210202173413-337fb17121a1/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210204115440-da015cdd15e3 h1:BBfPhRUJX2N0ftf8WqqgwI/Ixaj792XTmQApPK6zEEQ=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210204115440-da015cdd15e3/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down
10 changes: 10 additions & 0 deletions internal/namespaces/applesilicon/v1alpha1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ import (
func GetCommands() *core.Commands {
cmds := GetGeneratedCommands()

cmds.Merge(
core.NewCommands(
serverWaitCommand(),
),
)

human.RegisterMarshalerFunc(applesilicon.ServerStatus(""), human.EnumMarshalFunc(serverStatusMarshalSpecs))

cmds.MustFind("apple-silicon", "server", "create").Override(serverCreateBuilder)
cmds.MustFind("apple-silicon", "server", "reboot").Override(serverRebootBuilder)
cmds.MustFind("apple-silicon", "server", "delete").Override(serverDeleteBuilder)

return cmds
}
108 changes: 108 additions & 0 deletions internal/namespaces/applesilicon/v1alpha1/custom_server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package applesilicon

import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"time"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/human"
applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
serverActionTimeout = 60 * time.Minute
)

const (
serverActionCreate = iota
serverActionDelete
serverActionReboot
)

var (
Expand All @@ -15,3 +34,92 @@ var (
applesilicon.ServerStatusUpdating: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "updating"},
}
)

func serverCreateBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForServerFunc(serverActionCreate)
return c
}

func serverDeleteBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForServerFunc(serverActionDelete)
return c
}

func serverRebootBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForServerFunc(serverActionReboot)
return c
}

func waitForServerFunc(action int) core.WaitFunc {
return func(ctx context.Context, _, respI interface{}) (interface{}, error) {
server, err := applesilicon.NewAPI(core.ExtractClient(ctx)).WaitForServer(&applesilicon.WaitForServerRequest{
Zone: respI.(*applesilicon.Server).Zone,
ServerID: respI.(*applesilicon.Server).ID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})

switch action {
case serverActionCreate:
return server, err
case serverActionReboot:
return server, err
case serverActionDelete:
if err != nil {
// if we get a 404 here, it means the resource was successfully deleted
notFoundError := &scw.ResourceNotFoundError{}
responseError := &scw.ResponseError{}
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, &notFoundError) {
return fmt.Sprintf("Server %s successfully deleted.", respI.(*applesilicon.Server).ID), nil
}
}
}
return nil, err
}
}

func serverWaitCommand() *core.Command {
type customServerWaitArgs struct {
applesilicon.WaitForServerRequest
}

return &core.Command{
Short: `Wait for a server to reach a stable state`,
Long: `Wait for server to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the server.`,
Namespace: "apple-silicon",
Resource: "server",
Verb: "wait",
ArgsType: reflect.TypeOf(customServerWaitArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
args := argsI.(*customServerWaitArgs)

api := applesilicon.NewAPI(core.ExtractClient(ctx))
cluster, err := api.WaitForServer(&applesilicon.WaitForServerRequest{
Zone: args.Zone,
ServerID: args.ServerID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
return nil, err
}

return cluster, nil
},
ArgSpecs: core.ArgSpecs{
{
Name: "server-id",
Short: `ID of the server.`,
Required: true,
Positional: true,
},
core.ZoneArgSpec(),
},
Examples: []*core.Example{
{
Short: "Wait for a server to reach a stable state",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}