Skip to content

Commit 3fb02e0

Browse files
feat(instance): add stocks in server-type list (#827)
1 parent db6073d commit 3fb02e0

16 files changed

Lines changed: 206 additions & 138 deletions

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/mattn/go-colorable v0.1.4
2121
github.com/mattn/go-isatty v0.0.11
2222
github.com/pkg/errors v0.9.1 // indirect
23-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403105108-eb943ac1f1dc
23+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403173805-25a10631420d
2424
github.com/sergi/go-diff v1.0.0 // indirect
2525
github.com/spf13/cobra v0.0.5
2626
github.com/spf13/pflag v1.0.5 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200331160105-1181c3dc1bcd
6363
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200331160105-1181c3dc1bcd/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
6464
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403105108-eb943ac1f1dc h1:YJloAPPmGOEF+nufGL4a9Ppj6jnIMs8FjIorEM3ZBoE=
6565
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403105108-eb943ac1f1dc/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
66+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403173805-25a10631420d h1:oAG5xtIkUri9hwveoJPv0K3JTSREckKG3MfmNmnXAEQ=
67+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200403173805-25a10631420d/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
6668
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
6769
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
6870
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=

internal/human/marshal_func.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,28 @@ func isMarshalable(t reflect.Type) bool {
102102
(t.Kind() == reflect.Ptr && isMarshalable(t.Elem()))
103103
}
104104

105-
// BindAttributesMarshalFunc will apply the Attributes bindings to the value i
106-
func BindAttributesMarshalFunc(attributes Attributes) MarshalerFunc {
105+
// EnumMarshalSpec contains specs used by EnumMarshalFunc.
106+
type EnumMarshalSpec struct {
107+
// Attribute (mainly colors) to use.
108+
Attribute color.Attribute
109+
110+
// Value is the value that will be printed for the given value.
111+
Value string
112+
}
113+
114+
type EnumMarshalSpecs map[interface{}]*EnumMarshalSpec
115+
116+
// EnumMarshalFunc returns a marshal func to marshal an enum.
117+
func EnumMarshalFunc(specs EnumMarshalSpecs) MarshalerFunc {
107118
return func(i interface{}, opt *MarshalOpt) (s string, e error) {
108-
s, _ = defaultMarshalerFunc(i, opt)
109-
attribute, exist := attributes[i]
119+
value, _ := defaultMarshalerFunc(i, opt)
120+
spec, exist := specs[i]
110121
if exist {
111-
s = terminal.Style(s, attribute)
122+
if spec.Value != "" {
123+
value = spec.Value
124+
}
125+
value = terminal.Style(value, spec.Attribute)
112126
}
113-
return s, nil
127+
return value, nil
114128
}
115129
}
116-
117-
// Attributes makes the binding between a value and a color.Attribute
118-
type Attributes map[interface{}]color.Attribute

internal/namespaces/instance/v1/custom.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func GetCommands() *core.Commands {
2323
// Server
2424
//
2525
human.RegisterMarshalerFunc(instance.CreateServerResponse{}, marshallNestedField("Server"))
26-
human.RegisterMarshalerFunc(instance.ServerState(0), serverStateMarshalerFunc)
26+
human.RegisterMarshalerFunc(instance.ServerState(0), human.EnumMarshalFunc(serverStateMarshalSpecs))
2727
human.RegisterMarshalerFunc(instance.ServerLocation{}, serverLocationMarshalerFunc)
2828
human.RegisterMarshalerFunc([]*instance.Server{}, serversMarshalerFunc)
2929
human.RegisterMarshalerFunc(instance.GetServerResponse{}, getServerResponseMarshalerFunc)
@@ -46,6 +46,8 @@ func GetCommands() *core.Commands {
4646
//
4747
// Server-Type
4848
//
49+
human.RegisterMarshalerFunc(instance.ServerTypesAvailability(""), human.EnumMarshalFunc(serverTypesAvailabilityMarshalSpecs))
50+
4951
cmds.MustFind("instance", "server-type", "list").Override(serverTypeListBuilder)
5052

5153
//
@@ -70,15 +72,15 @@ func GetCommands() *core.Commands {
7072
// Volume
7173
//
7274
human.RegisterMarshalerFunc(instance.CreateVolumeResponse{}, marshallNestedField("Volume"))
73-
human.RegisterMarshalerFunc(instance.VolumeState(0), human.BindAttributesMarshalFunc(volumeStateAttributes))
75+
human.RegisterMarshalerFunc(instance.VolumeState(0), human.EnumMarshalFunc(volumeStateMarshalSpecs))
7476
human.RegisterMarshalerFunc(instance.VolumeSummary{}, volumeSummaryMarshalerFunc)
7577
human.RegisterMarshalerFunc(map[string]*instance.Volume{}, volumeMapMarshalerFunc)
7678

7779
//
7880
// Security Group
7981
//
8082
human.RegisterMarshalerFunc(instance.CreateSecurityGroupResponse{}, marshallNestedField("SecurityGroup"))
81-
human.RegisterMarshalerFunc(instance.SecurityGroupPolicy(0), human.BindAttributesMarshalFunc(securityGroupPolicyAttribute))
83+
human.RegisterMarshalerFunc(instance.SecurityGroupPolicy(0), human.EnumMarshalFunc(securityGroupPolicyMarshalSpecs))
8284

8385
cmds.MustFind("instance", "security-group", "get").Override(securityGroupGetBuilder)
8486
cmds.MustFind("instance", "security-group", "delete").Override(securityGroupDeleteBuilder)
@@ -92,7 +94,7 @@ func GetCommands() *core.Commands {
9294
// Security Group Rule
9395
//
9496
human.RegisterMarshalerFunc(instance.CreateSecurityGroupRuleResponse{}, marshallNestedField("Rule"))
95-
human.RegisterMarshalerFunc(instance.SecurityGroupRuleAction(0), human.BindAttributesMarshalFunc(securityGroupRuleActionAttribute))
97+
human.RegisterMarshalerFunc(instance.SecurityGroupRuleAction(0), human.EnumMarshalFunc(securityGroupRuleActionMarshalSpecs))
9698

9799
//
98100
// Placement Group

internal/namespaces/instance/v1/custom_security_group.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import (
2424
//
2525

2626
var (
27-
securityGroupPolicyAttribute = human.Attributes{
28-
instance.SecurityGroupPolicyDrop: color.FgRed,
29-
instance.SecurityGroupPolicyAccept: color.FgGreen,
27+
securityGroupPolicyMarshalSpecs = human.EnumMarshalSpecs{
28+
instance.SecurityGroupPolicyDrop: &human.EnumMarshalSpec{Attribute: color.FgRed},
29+
instance.SecurityGroupPolicyAccept: &human.EnumMarshalSpec{Attribute: color.FgGreen},
3030
}
3131

32-
securityGroupRuleActionAttribute = human.Attributes{
33-
instance.SecurityGroupRuleActionDrop: color.FgRed,
34-
instance.SecurityGroupRuleActionAccept: color.FgGreen,
32+
securityGroupRuleActionMarshalSpecs = human.EnumMarshalSpecs{
33+
instance.SecurityGroupRuleActionDrop: &human.EnumMarshalSpec{Attribute: color.FgRed},
34+
instance.SecurityGroupRuleActionAccept: &human.EnumMarshalSpec{Attribute: color.FgGreen},
3535
}
3636
)
3737

internal/namespaces/instance/v1/custom_security_group_rule.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

internal/namespaces/instance/v1/custom_server.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"time"
1111

1212
"github.com/fatih/color"
13+
1314
"github.com/hashicorp/go-multierror"
1415
"github.com/scaleway/scaleway-cli/internal/core"
1516
"github.com/scaleway/scaleway-cli/internal/human"
1617
"github.com/scaleway/scaleway-cli/internal/interactive"
17-
"github.com/scaleway/scaleway-cli/internal/terminal"
1818
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1919
"github.com/scaleway/scaleway-sdk-go/logger"
2020
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -28,14 +28,17 @@ const (
2828
// Marshalers
2929
//
3030

31-
// serverStateMarshalerFunc marshals a instance.ServerState.
32-
func serverStateMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
33-
// The Scaleway console shows "archived" for a stopped server.
34-
if i.(instance.ServerState) == instance.ServerStateStopped {
35-
return terminal.Style("archived", color.Faint), nil
31+
// serverStateMarshalSpecs allows to override the displayed instance.ServerState.
32+
var (
33+
serverStateMarshalSpecs = human.EnumMarshalSpecs{
34+
instance.ServerStateRunning: &human.EnumMarshalSpec{Attribute: color.FgGreen},
35+
instance.ServerStateStopped: &human.EnumMarshalSpec{Attribute: color.Faint, Value: "archived"},
36+
instance.ServerStateStoppedInPlace: &human.EnumMarshalSpec{Attribute: color.Faint},
37+
instance.ServerStateStarting: &human.EnumMarshalSpec{Attribute: color.FgBlue},
38+
instance.ServerStateStopping: &human.EnumMarshalSpec{Attribute: color.FgBlue},
39+
instance.ServerStateLocked: &human.EnumMarshalSpec{Attribute: color.FgRed},
3640
}
37-
return human.BindAttributesMarshalFunc(serverStateAttributes)(i, opt)
38-
}
41+
)
3942

4043
// serverLocationMarshalerFunc marshals a instance.ServerLocation.
4144
func serverLocationMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {

internal/namespaces/instance/v1/custom_server_type.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,25 @@ import (
55
"sort"
66
"strings"
77

8+
"github.com/fatih/color"
89
"github.com/scaleway/scaleway-cli/internal/core"
10+
"github.com/scaleway/scaleway-cli/internal/human"
911
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1012
"github.com/scaleway/scaleway-sdk-go/scw"
1113
)
1214

15+
//
16+
// Marshalers
17+
//
18+
19+
var (
20+
serverTypesAvailabilityMarshalSpecs = human.EnumMarshalSpecs{
21+
instance.ServerTypesAvailabilityAvailable: &human.EnumMarshalSpec{Attribute: color.FgGreen},
22+
instance.ServerTypesAvailabilityScarce: &human.EnumMarshalSpec{Attribute: color.FgYellow, Value: "low stock"},
23+
instance.ServerTypesAvailabilityShortage: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "out of stock"},
24+
}
25+
)
26+
1327
//
1428
// Builders
1529
//
@@ -33,30 +47,36 @@ func serverTypeListBuilder(c *core.Command) *core.Command {
3347
"X64-60GB": {},
3448
}
3549

36-
originalRun := c.Run
37-
3850
c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
3951
type customServerType struct {
40-
Name string `json:"name"`
41-
MonthlyPrice *scw.Money `json:"monthly_price"`
42-
HourlyPrice *scw.Money `json:"hourly_price"`
43-
LocalVolumeSize scw.Size `json:"local_volume_size"`
44-
CPU uint32 `json:"cpu"`
45-
GPU *uint64 `json:"gpu"`
46-
RAM scw.Size `json:"ram"`
47-
Arch instance.Arch `json:"arch"`
52+
Name string `json:"name"`
53+
MonthlyPrice *scw.Money `json:"monthly_price"`
54+
HourlyPrice *scw.Money `json:"hourly_price"`
55+
LocalVolumeSize scw.Size `json:"local_volume_size"`
56+
CPU uint32 `json:"cpu"`
57+
GPU *uint64 `json:"gpu"`
58+
RAM scw.Size `json:"ram"`
59+
Arch instance.Arch `json:"arch"`
60+
Availability instance.ServerTypesAvailability `json:"availability"`
4861
}
4962

50-
originalRes, err := originalRun(ctx, argsI)
63+
api := instance.NewAPI(core.ExtractClient(ctx))
64+
65+
// Get server types.
66+
request := argsI.(*instance.ListServersTypesRequest)
67+
listServersTypesResponse, err := api.ListServersTypes(request)
5168
if err != nil {
5269
return nil, err
5370
}
54-
55-
listServersTypesResponse := originalRes.(*instance.ListServersTypesResponse)
5671
serverTypes := []*customServerType(nil)
5772

58-
for name, serverType := range listServersTypesResponse.Servers {
73+
// Get server availabilities.
74+
availabilitiesResponse, err := api.GetServerTypesAvailability(&instance.GetServerTypesAvailabilityRequest{})
75+
if err != nil {
76+
return nil, err
77+
}
5978

79+
for name, serverType := range listServersTypesResponse.Servers {
6080
_, isDeprecated := deprecatedNames[name]
6181
if isDeprecated {
6282
continue
@@ -71,6 +91,7 @@ func serverTypeListBuilder(c *core.Command) *core.Command {
7191
GPU: serverType.Gpu,
7292
RAM: scw.Size(serverType.RAM),
7393
Arch: serverType.Arch,
94+
Availability: availabilitiesResponse.Servers[name].Availability,
7495
})
7596
}
7697

internal/namespaces/instance/v1/custom_server_type_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/scaleway/scaleway-cli/internal/core"
77
)
88

9-
func Test_serverTypeListBuilder(t *testing.T) {
9+
func Test_ServerTypeList(t *testing.T) {
1010
t.Run("server-type list", core.Test(&core.TestConfig{
1111
Commands: GetCommands(),
1212
Cmd: "scw instance server-type list",

internal/namespaces/instance/v1/custom_volume.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
//
1414

1515
var (
16-
volumeStateAttributes = human.Attributes{
17-
instance.VolumeStateError: color.FgRed,
18-
instance.VolumeStateAvailable: color.FgGreen,
16+
volumeStateMarshalSpecs = human.EnumMarshalSpecs{
17+
instance.VolumeStateError: &human.EnumMarshalSpec{Attribute: color.FgRed},
18+
instance.VolumeStateAvailable: &human.EnumMarshalSpec{Attribute: color.FgGreen},
1919
}
2020
)
2121

0 commit comments

Comments
 (0)