|
1 | 1 | package applesilicon
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "reflect" |
| 9 | + "time" |
| 10 | + |
4 | 11 | "github.com/fatih/color"
|
| 12 | + "github.com/scaleway/scaleway-cli/internal/core" |
5 | 13 | "github.com/scaleway/scaleway-cli/internal/human"
|
6 | 14 | 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 |
7 | 26 | )
|
8 | 27 |
|
9 | 28 | var (
|
|
15 | 34 | applesilicon.ServerStatusUpdating: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "updating"},
|
16 | 35 | }
|
17 | 36 | )
|
| 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, ¬FoundError) { |
| 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