-
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(as): add server type list format (#1748)
- Loading branch information
Showing
5 changed files
with
169 additions
and
0 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
85 changes: 85 additions & 0 deletions
85
internal/namespaces/applesilicon/v1alpha1/custom_server_types.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,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 | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/namespaces/applesilicon/v1alpha1/custom_server_types_test.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,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), | ||
), | ||
})) | ||
} |
35 changes: 35 additions & 0 deletions
35
internal/namespaces/applesilicon/v1alpha1/testdata/test-server-type-list-base.cassette.yaml
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,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: "" |
24 changes: 24 additions & 0 deletions
24
internal/namespaces/applesilicon/v1alpha1/testdata/test-server-type-list-base.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,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" | ||
} | ||
] |