-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(baremetal): add create server with type (#768)
Co-authored-by: Quentin Brosse <quentin.brosse@icloud.com> Co-authored-by: Loïc Bourgois <loic@bourgois.pro>
- Loading branch information
1 parent
c58750e
commit 0c5827c
Showing
25 changed files
with
3,905 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
cmd/scw/testdata/test-all-usage-baremetal-server-wait-usage.stderr.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Wait for a 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 baremetal server wait [arg=value ...] | ||
|
||
EXAMPLES: | ||
Wait for a server to reach a stable state | ||
scw baremetal server wait server-id=11111111-1111-1111-1111-111111111111 | ||
|
||
ARGS: | ||
server-id ID of the server affected by the action. | ||
[zone] Zone to target. If none is passed will use default zone from the config | ||
|
||
FLAGS: | ||
-h, --help help for wait | ||
|
||
GLOBAL FLAGS: | ||
-D, --debug Enable debug mode | ||
-o, --output string Output format: json or human | ||
-p, --profile string The config profile to use |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package baremetal | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/scaleway/scaleway-cli/internal/core" | ||
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
const ( | ||
serverActionTimeout = 20 * time.Minute | ||
) | ||
|
||
func serverWaitCommand() *core.Command { | ||
type serverWaitRequest struct { | ||
ServerID string | ||
Zone scw.Zone | ||
} | ||
|
||
return &core.Command{ | ||
Short: `Wait for a server to reach a stable state`, | ||
Long: `Wait for a 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: "baremetal", | ||
Resource: "server", | ||
Verb: "wait", | ||
ArgsType: reflect.TypeOf(serverWaitRequest{}), | ||
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) { | ||
api := baremetal.NewAPI(core.ExtractClient(ctx)) | ||
return api.WaitForServer(&baremetal.WaitForServerRequest{ | ||
ServerID: argsI.(*serverWaitRequest).ServerID, | ||
Zone: argsI.(*serverWaitRequest).Zone, | ||
Timeout: serverActionTimeout, | ||
}) | ||
}, | ||
ArgSpecs: core.ArgSpecs{ | ||
{ | ||
Name: "server-id", | ||
Short: `ID of the server affected by the action.`, | ||
Required: true, | ||
}, | ||
core.ZoneArgSpec(), | ||
}, | ||
Examples: []*core.Example{ | ||
{ | ||
Short: "Wait for a server to reach a stable state", | ||
Request: `{"server_id": "11111111-1111-1111-1111-111111111111"}`, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// serverStartBuilder overrides the baremetalServerStart command | ||
func serverStartBuilder(c *core.Command) *core.Command { | ||
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { | ||
api := baremetal.NewAPI(core.ExtractClient(ctx)) | ||
return api.WaitForServer(&baremetal.WaitForServerRequest{ | ||
Zone: argsI.(*baremetal.StartServerRequest).Zone, | ||
ServerID: respI.(*baremetal.StartServerRequest).ServerID, | ||
Timeout: serverActionTimeout, | ||
}) | ||
} | ||
|
||
return c | ||
} | ||
|
||
// serverStopBuilder overrides the baremetalServerStop command | ||
func serverStopBuilder(c *core.Command) *core.Command { | ||
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { | ||
api := baremetal.NewAPI(core.ExtractClient(ctx)) | ||
return api.WaitForServer(&baremetal.WaitForServerRequest{ | ||
Zone: argsI.(*baremetal.StopServerRequest).Zone, | ||
ServerID: respI.(*baremetal.StopServerRequest).ServerID, | ||
Timeout: serverActionTimeout, | ||
}) | ||
} | ||
|
||
return c | ||
} | ||
|
||
// serverRebootBuilder overrides the baremetalServerReboot command | ||
func serverRebootBuilder(c *core.Command) *core.Command { | ||
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { | ||
api := baremetal.NewAPI(core.ExtractClient(ctx)) | ||
return api.WaitForServer(&baremetal.WaitForServerRequest{ | ||
Zone: argsI.(*baremetal.RebootServerRequest).Zone, | ||
ServerID: respI.(*baremetal.RebootServerRequest).ServerID, | ||
Timeout: serverActionTimeout, | ||
}) | ||
} | ||
|
||
return c | ||
} |
119 changes: 119 additions & 0 deletions
119
internal/namespaces/baremetal/v1alpha1/custom_server_create.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package baremetal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/scaleway/scaleway-cli/internal/core" | ||
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
func serverCreateBuilder(c *core.Command) *core.Command { | ||
type baremetalCreateServerRequestCustom struct { | ||
Zone scw.Zone `json:"-"` | ||
// OrganizationID with which the server will be created | ||
OrganizationID string `json:"organization_id"` | ||
// Name of the server (≠hostname) | ||
Name string `json:"name"` | ||
// Description associated to the server, max 255 characters | ||
Description string `json:"description"` | ||
// Tags associated with the server | ||
Tags []string `json:"tags"` | ||
// Type of the server | ||
Type string | ||
} | ||
|
||
c.ArgsType = reflect.TypeOf(baremetalCreateServerRequestCustom{}) | ||
|
||
c.ArgSpecs.DeleteByName("offer-id") | ||
|
||
c.ArgSpecs.GetByName("name").Default = core.RandomValueGenerator("bm") | ||
c.ArgSpecs.GetByName("description").Required = false | ||
|
||
c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{ | ||
Name: "type", | ||
Short: "Server commercial type", | ||
Default: core.DefaultValueSetter("GP-BM1-S"), | ||
|
||
EnumValues: []string{ | ||
// General Purpose offers | ||
"GP-BM1-L", | ||
"GP-BM1-M", | ||
"GP-BM1-S", | ||
|
||
// High-computing offers | ||
"HC-BM1-L", | ||
"HC-BM1-S", | ||
|
||
// High-Memory offers | ||
"HM-BM1-XL", | ||
"HM-BM1-M", | ||
}, | ||
}) | ||
|
||
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) { | ||
client := core.ExtractClient(ctx) | ||
api := baremetal.NewAPI(client) | ||
|
||
tmpRequest := argsI.(*baremetalCreateServerRequestCustom) | ||
request := &baremetal.CreateServerRequest{ | ||
Zone: tmpRequest.Zone, | ||
OrganizationID: tmpRequest.OrganizationID, | ||
Name: tmpRequest.Name, | ||
Description: tmpRequest.Description, | ||
Tags: tmpRequest.Tags, | ||
} | ||
|
||
// We need to find the offer ID. | ||
// While baremetal does not have list offer name filter we are forced to iterate | ||
// on the list of offers provided. | ||
offer, err := api.GetOfferFromName(&baremetal.GetOfferFromOfferNameRequest{ | ||
OfferName: tmpRequest.Type, | ||
Zone: tmpRequest.Zone, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if offer == nil { | ||
return nil, fmt.Errorf("could not match an offer with the type: %s", tmpRequest.Type) | ||
} | ||
request.OfferID = offer.ID | ||
|
||
return api.CreateServer(request) | ||
} | ||
|
||
c.SeeAlsos = []*core.SeeAlso{ | ||
{ | ||
Short: "List os", | ||
Command: "scw baremetal os list", | ||
}, | ||
{ | ||
Short: "Install an OS on your server", | ||
Command: "scw baremetal server install", | ||
}, | ||
} | ||
|
||
c.Examples = []*core.Example{ | ||
{ | ||
Short: "Create instance", | ||
Request: `{}`, | ||
}, | ||
{ | ||
Short: "Create a GP-BM1-M instance, give it a name and add tags", | ||
Request: `{"type":"GP-BM1-M","name":"foo","tags":["prod","blue"]}`, | ||
}, | ||
} | ||
|
||
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { | ||
api := baremetal.NewAPI(core.ExtractClient(ctx)) | ||
return api.WaitForServer(&baremetal.WaitForServerRequest{ | ||
Zone: argsI.(*baremetalCreateServerRequestCustom).Zone, | ||
ServerID: respI.(*baremetal.Server).ID, | ||
Timeout: serverActionTimeout, | ||
}) | ||
} | ||
|
||
return c | ||
} |
Oops, something went wrong.