Skip to content

Commit c9423ba

Browse files
authored
feat(as): add support for wait command and wait flag (#1745)
1 parent ee2e9bc commit c9423ba

9 files changed

+148
-3
lines changed

cmd/scw/testdata/test-all-usage-apple-silicon-server-create-usage.golden

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ARGS:
1212

1313
FLAGS:
1414
-h, --help help for create
15+
-w, --wait wait until the server is ready
1516

1617
GLOBAL FLAGS:
1718
-c, --config string The path to the config file

cmd/scw/testdata/test-all-usage-apple-silicon-server-delete-usage.golden

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ARGS:
1010

1111
FLAGS:
1212
-h, --help help for delete
13+
-w, --wait wait until the server is ready
1314

1415
GLOBAL FLAGS:
1516
-c, --config string The path to the config file

cmd/scw/testdata/test-all-usage-apple-silicon-server-reboot-usage.golden

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ARGS:
1010

1111
FLAGS:
1212
-h, --help help for reboot
13+
-w, --wait wait until the server is ready
1314

1415
GLOBAL FLAGS:
1516
-c, --config string The path to the config file

cmd/scw/testdata/test-all-usage-apple-silicon-server-usage.golden

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ AVAILABLE COMMANDS:
1313
reboot Reboot a server
1414
reinstall Reinstall a server
1515
update Update a server
16+
wait Wait for a server to reach a stable state
1617

1718
FLAGS:
1819
-h, --help help for server
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
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.
4+
5+
USAGE:
6+
scw apple-silicon server wait <server-id ...> [arg=value ...]
7+
8+
EXAMPLES:
9+
Wait for a server to reach a stable state
10+
scw apple-silicon server wait 11111111-1111-1111-1111-111111111111
11+
12+
ARGS:
13+
server-id ID of the server.
14+
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config
15+
16+
FLAGS:
17+
-h, --help help for wait
18+
19+
GLOBAL FLAGS:
20+
-c, --config string The path to the config file
21+
-D, --debug Enable debug mode
22+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
23+
-p, --profile string The config profile to use

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/mattn/go-colorable v0.1.7
2323
github.com/mattn/go-isatty v0.0.12
2424
github.com/pkg/errors v0.9.1 // indirect
25-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210202173413-337fb17121a1
25+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210204115440-da015cdd15e3
2626
github.com/sergi/go-diff v1.1.0 // indirect
2727
github.com/spf13/cobra v1.0.0
2828
github.com/spf13/pflag v1.0.5

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
118118
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
119119
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
120120
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
121-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210202173413-337fb17121a1 h1:vytHInJpH5I4FheLyFW2wu5LmtcQOJhetm3+jaVGp74=
122-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210202173413-337fb17121a1/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
121+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210204115440-da015cdd15e3 h1:BBfPhRUJX2N0ftf8WqqgwI/Ixaj792XTmQApPK6zEEQ=
122+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210204115440-da015cdd15e3/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
123123
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
124124
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
125125
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=

internal/namespaces/applesilicon/v1alpha1/custom.go

+10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ import (
99
func GetCommands() *core.Commands {
1010
cmds := GetGeneratedCommands()
1111

12+
cmds.Merge(
13+
core.NewCommands(
14+
serverWaitCommand(),
15+
),
16+
)
17+
1218
human.RegisterMarshalerFunc(applesilicon.ServerStatus(""), human.EnumMarshalFunc(serverStatusMarshalSpecs))
1319

20+
cmds.MustFind("apple-silicon", "server", "create").Override(serverCreateBuilder)
21+
cmds.MustFind("apple-silicon", "server", "reboot").Override(serverRebootBuilder)
22+
cmds.MustFind("apple-silicon", "server", "delete").Override(serverDeleteBuilder)
23+
1424
return cmds
1525
}

internal/namespaces/applesilicon/v1alpha1/custom_server.go

+108
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
package applesilicon
22

33
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
"reflect"
9+
"time"
10+
411
"github.com/fatih/color"
12+
"github.com/scaleway/scaleway-cli/internal/core"
513
"github.com/scaleway/scaleway-cli/internal/human"
614
applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
15+
"github.com/scaleway/scaleway-sdk-go/scw"
16+
)
17+
18+
const (
19+
serverActionTimeout = 60 * time.Minute
20+
)
21+
22+
const (
23+
serverActionCreate = iota
24+
serverActionDelete
25+
serverActionReboot
726
)
827

928
var (
@@ -15,3 +34,92 @@ var (
1534
applesilicon.ServerStatusUpdating: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "updating"},
1635
}
1736
)
37+
38+
func serverCreateBuilder(c *core.Command) *core.Command {
39+
c.WaitFunc = waitForServerFunc(serverActionCreate)
40+
return c
41+
}
42+
43+
func serverDeleteBuilder(c *core.Command) *core.Command {
44+
c.WaitFunc = waitForServerFunc(serverActionDelete)
45+
return c
46+
}
47+
48+
func serverRebootBuilder(c *core.Command) *core.Command {
49+
c.WaitFunc = waitForServerFunc(serverActionReboot)
50+
return c
51+
}
52+
53+
func waitForServerFunc(action int) core.WaitFunc {
54+
return func(ctx context.Context, _, respI interface{}) (interface{}, error) {
55+
server, err := applesilicon.NewAPI(core.ExtractClient(ctx)).WaitForServer(&applesilicon.WaitForServerRequest{
56+
Zone: respI.(*applesilicon.Server).Zone,
57+
ServerID: respI.(*applesilicon.Server).ID,
58+
Timeout: scw.TimeDurationPtr(serverActionTimeout),
59+
RetryInterval: core.DefaultRetryInterval,
60+
})
61+
62+
switch action {
63+
case serverActionCreate:
64+
return server, err
65+
case serverActionReboot:
66+
return server, err
67+
case serverActionDelete:
68+
if err != nil {
69+
// if we get a 404 here, it means the resource was successfully deleted
70+
notFoundError := &scw.ResourceNotFoundError{}
71+
responseError := &scw.ResponseError{}
72+
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, &notFoundError) {
73+
return fmt.Sprintf("Server %s successfully deleted.", respI.(*applesilicon.Server).ID), nil
74+
}
75+
}
76+
}
77+
return nil, err
78+
}
79+
}
80+
81+
func serverWaitCommand() *core.Command {
82+
type customServerWaitArgs struct {
83+
applesilicon.WaitForServerRequest
84+
}
85+
86+
return &core.Command{
87+
Short: `Wait for a server to reach a stable state`,
88+
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.`,
89+
Namespace: "apple-silicon",
90+
Resource: "server",
91+
Verb: "wait",
92+
ArgsType: reflect.TypeOf(customServerWaitArgs{}),
93+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
94+
args := argsI.(*customServerWaitArgs)
95+
96+
api := applesilicon.NewAPI(core.ExtractClient(ctx))
97+
cluster, err := api.WaitForServer(&applesilicon.WaitForServerRequest{
98+
Zone: args.Zone,
99+
ServerID: args.ServerID,
100+
Timeout: scw.TimeDurationPtr(serverActionTimeout),
101+
RetryInterval: core.DefaultRetryInterval,
102+
})
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
return cluster, nil
108+
},
109+
ArgSpecs: core.ArgSpecs{
110+
{
111+
Name: "server-id",
112+
Short: `ID of the server.`,
113+
Required: true,
114+
Positional: true,
115+
},
116+
core.ZoneArgSpec(),
117+
},
118+
Examples: []*core.Example{
119+
{
120+
Short: "Wait for a server to reach a stable state",
121+
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
122+
},
123+
},
124+
}
125+
}

0 commit comments

Comments
 (0)