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(instance): improve human output for volume-type list #1213

Merged
merged 4 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions internal/namespaces/instance/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func GetCommands() *core.Commands {
cmds.MustFind("instance", "volume", "create").Override(volumeCreateBuilder)
cmds.MustFind("instance", "volume", "list").Override(volumeListBuilder)

//
// Volume-Type
//
cmds.MustFind("instance", "volume-type", "list").Override(volumeTypeListBuilder)

//
// Security Group
//
Expand Down
52 changes: 52 additions & 0 deletions internal/namespaces/instance/v1/custom_volume_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package instance

import (
"context"
"sort"

"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
)

func volumeTypeListBuilder(cmd *core.Command) *core.Command {
type customVolumeType struct {
Type string `json:"type"`
instance.VolumeType
}

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

volumeTypes := []*customVolumeType(nil)
for typeName, volumeType := range res.(*instance.ListVolumesTypesResponse).Volumes {
volumeTypes = append(volumeTypes, &customVolumeType{
Type: typeName,
VolumeType: *volumeType,
})
}

// sort for consistent order output
sort.Slice(volumeTypes, func(i, j int) bool {
return volumeTypes[i].Type < volumeTypes[j].Type
})

return volumeTypes, nil
})

cmd.AllowAnonymousClient = true

cmd.View = &core.View{
Fields: []*core.ViewField{
{FieldName: "Type", Label: "Type"},
{FieldName: "DisplayName", Label: "Name"},
{FieldName: "Capabilities.Snapshot", Label: "Snapshot"},
{FieldName: "Constraints.Min", Label: "Min"},
{FieldName: "Constraints.Max", Label: "Max"},
},
}

return cmd
}
15 changes: 15 additions & 0 deletions internal/namespaces/instance/v1/custom_volume_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package instance

import (
"testing"

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

func Test_VolumeTypeList(t *testing.T) {
t.Run("volume-type list", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw instance volume-type list",
Check: core.TestCheckGolden(),
}))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.6+dev (go1.14.4; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/products/volumes
method: GET
response:
body: '{"volumes": {"b_ssd": {"capabilities": {"snapshot": true}, "display_name":
"Block Storage SSD", "constraints": {"min": 1000000000, "max": 10000000000000}},
"l_ssd": {"capabilities": {"snapshot": true}, "display_name": "Local SSD", "constraints":
{"min": 1000000000, "max": 800000000000}}}}'
headers:
Cache-Control:
- no-cache
Content-Length:
- "289"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Mon, 13 Jul 2020 13:47:48 GMT
Link:
- </products/volumes?page=1&per_page=50&>; rel="last"
Server:
- agw_listener_public_vip
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 5147d971-7070-449a-8f0b-0c2d691eb7ec
X-Total-Count:
- "2"
status: 200 OK
code: 200
duration: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
Type Name Snapshot Min Max
b_ssd Block Storage SSD true 1.0 GB 10 TB
l_ssd Local SSD true 1.0 GB 800 GB
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
[
{
"type": "b_ssd",
"display_name": "Block Storage SSD",
"capabilities": {
"snapshot": true
},
"constraints": {
"min": 1000000000,
"max": 10000000000000
}
},
{
"type": "l_ssd",
"display_name": "Local SSD",
"capabilities": {
"snapshot": true
},
"constraints": {
"min": 1000000000,
"max": 800000000000
}
}
]