-
Notifications
You must be signed in to change notification settings - Fork 151
/
custom_server.go
125 lines (111 loc) · 3.75 KB
/
custom_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package applesilicon
import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"time"
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/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 (
serverStatusMarshalSpecs = human.EnumMarshalSpecs{
applesilicon.ServerStatusError: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "error"},
applesilicon.ServerStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen, Value: "ready"},
applesilicon.ServerStatusRebooting: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "rebooting"},
applesilicon.ServerStatusStarting: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "starting"},
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, ¬FoundError) {
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"}`,
},
},
}
}