Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(as): add server type list format #1748

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/namespaces/applesilicon/v1alpha1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ func GetCommands() *core.Commands {
),
)

human.RegisterMarshalerFunc(applesilicon.ServerTypeCPU{}, cpuMarshalerFunc)
human.RegisterMarshalerFunc(applesilicon.ServerTypeDisk{}, diskMarshalerFunc)
human.RegisterMarshalerFunc(applesilicon.ServerTypeMemory{}, memoryMarshalerFunc)

human.RegisterMarshalerFunc(applesilicon.ServerStatus(""), human.EnumMarshalFunc(serverStatusMarshalSpecs))
human.RegisterMarshalerFunc(applesilicon.ServerTypeStock(""), human.EnumMarshalFunc(serverTypeStockMarshalSpecs))

cmds.MustFind("apple-silicon", "server", "create").Override(serverCreateBuilder)
cmds.MustFind("apple-silicon", "server", "reboot").Override(serverRebootBuilder)
cmds.MustFind("apple-silicon", "server", "delete").Override(serverDeleteBuilder)

cmds.MustFind("apple-silicon", "server-type", "list").Override(serverTypeBuilder)

return cmds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package applesilicon

import (
"context"
"fmt"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/human"
applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
)

var (
serverTypeStockMarshalSpecs = human.EnumMarshalSpecs{
applesilicon.ServerTypeStockLowStock: &human.EnumMarshalSpec{Attribute: color.FgYellow, Value: "low stock"},
applesilicon.ServerTypeStockNoStock: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "no stock"},
applesilicon.ServerTypeStockHighStock: &human.EnumMarshalSpec{Attribute: color.FgGreen, Value: "high stock"},
}
)

func cpuMarshalerFunc(i interface{}, _ *human.MarshalOpt) (string, error) {
cpu := i.(applesilicon.ServerTypeCPU)
return fmt.Sprintf("%s (%d cores)", cpu.Name, cpu.CoreCount), nil
}

func diskMarshalerFunc(i interface{}, _ *human.MarshalOpt) (string, error) {
disk := i.(applesilicon.ServerTypeDisk)
capacityStr, err := human.Marshal(disk.Capacity, nil)
if err != nil {
return "", err
}
return capacityStr, nil
}

func memoryMarshalerFunc(i interface{}, _ *human.MarshalOpt) (string, error) {
memory := i.(applesilicon.ServerTypeMemory)
capacityStr, err := human.Marshal(memory.Capacity, nil)
if err != nil {
return "", err
}
return capacityStr, nil
}

func serverTypeBuilder(c *core.Command) *core.Command {
c.View = &core.View{
Fields: []*core.ViewField{
{
Label: "Name",
FieldName: "Name",
},
{
Label: "CPU",
FieldName: "CPU",
},
{
Label: "Memory",
FieldName: "Memory",
},
{
Label: "Disk",
FieldName: "Disk",
},
{
Label: "Stock",
FieldName: "Stock",
},
{
Label: "Minimum Lease Duration",
FieldName: "MinimumLeaseDuration",
},
},
}

c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
originalRes, err := runner(ctx, argsI)
if err != nil {
return nil, err
}

versionsResponse := originalRes.(*applesilicon.ListServerTypesResponse)
return versionsResponse.ServerTypes, nil
})

return c
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package applesilicon

import (
"testing"

"github.com/scaleway/scaleway-cli/internal/core"
)

func Test_ServerTypeList(t *testing.T) {
t.Run("base", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw apple-silicon server-type list",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
}))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/apple-silicon/v1alpha1/zones/fr-par-1/server-types
method: GET
response:
body: '{"server_types":[{"name":"M1-M","cpu":{"name":"Apple M1","core_count":8},"disk":{"capacity":256000000000,"type":"SSD"},"memory":{"capacity":8000000000,"type":"LPDDR4"},"stock":"low_stock","minimum_lease_duration":"86400s"}]}'
headers:
Content-Length:
- "224"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 05 Feb 2021 18:04:16 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- f68db5c3-b1c7-4cb4-98dd-67d6cd32c41a
status: 200 OK
code: 200
duration: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
Name CPU Memory Disk Stock Minimum Lease Duration
M1-M Apple M1 (8 cores) 8.0 GB 256 GB low stock 1 days
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
[
{
"cpu": {
"name": "Apple M1",
"core_count": 8
},
"disk": {
"capacity": 256000000000,
"type": "SSD"
},
"name": "M1-M",
"memory": {
"capacity": 8000000000,
"type": "LPDDR4"
},
"stock": "low_stock",
"minimum_lease_duration": "86400.000000000s"
}
]