Skip to content

Commit aed1ed4

Browse files
authored
feat(instance): improve human output for volume-type list (#1213)
1 parent dcd012a commit aed1ed4

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

internal/namespaces/instance/v1/custom.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ func GetCommands() *core.Commands {
116116
cmds.MustFind("instance", "volume", "create").Override(volumeCreateBuilder)
117117
cmds.MustFind("instance", "volume", "list").Override(volumeListBuilder)
118118

119+
//
120+
// Volume-Type
121+
//
122+
cmds.MustFind("instance", "volume-type", "list").Override(volumeTypeListBuilder)
123+
119124
//
120125
// Security Group
121126
//
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package instance
2+
3+
import (
4+
"context"
5+
"sort"
6+
7+
"github.com/scaleway/scaleway-cli/internal/core"
8+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
9+
)
10+
11+
func volumeTypeListBuilder(cmd *core.Command) *core.Command {
12+
type customVolumeType struct {
13+
Type string `json:"type"`
14+
instance.VolumeType
15+
}
16+
17+
cmd.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
18+
res, err := runner(ctx, argsI)
19+
if err != nil {
20+
return res, err
21+
}
22+
23+
volumeTypes := []*customVolumeType(nil)
24+
for typeName, volumeType := range res.(*instance.ListVolumesTypesResponse).Volumes {
25+
volumeTypes = append(volumeTypes, &customVolumeType{
26+
Type: typeName,
27+
VolumeType: *volumeType,
28+
})
29+
}
30+
31+
// sort for consistent order output
32+
sort.Slice(volumeTypes, func(i, j int) bool {
33+
return volumeTypes[i].Type < volumeTypes[j].Type
34+
})
35+
36+
return volumeTypes, nil
37+
})
38+
39+
cmd.AllowAnonymousClient = true
40+
41+
cmd.View = &core.View{
42+
Fields: []*core.ViewField{
43+
{FieldName: "Type", Label: "Type"},
44+
{FieldName: "DisplayName", Label: "Name"},
45+
{FieldName: "Capabilities.Snapshot", Label: "Snapshot"},
46+
{FieldName: "Constraints.Min", Label: "Min"},
47+
{FieldName: "Constraints.Max", Label: "Max"},
48+
},
49+
}
50+
51+
return cmd
52+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package instance
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/internal/core"
7+
)
8+
9+
func Test_VolumeTypeList(t *testing.T) {
10+
t.Run("volume-type list", core.Test(&core.TestConfig{
11+
Commands: GetCommands(),
12+
Cmd: "scw instance volume-type list",
13+
Check: core.TestCheckGolden(),
14+
}))
15+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: ""
6+
form: {}
7+
headers:
8+
User-Agent:
9+
- scaleway-sdk-go/v1.0.0-beta.6+dev (go1.14.4; darwin; amd64) cli-e2e-test
10+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/products/volumes
11+
method: GET
12+
response:
13+
body: '{"volumes": {"b_ssd": {"capabilities": {"snapshot": true}, "display_name":
14+
"Block Storage SSD", "constraints": {"min": 1000000000, "max": 10000000000000}},
15+
"l_ssd": {"capabilities": {"snapshot": true}, "display_name": "Local SSD", "constraints":
16+
{"min": 1000000000, "max": 800000000000}}}}'
17+
headers:
18+
Cache-Control:
19+
- no-cache
20+
Content-Length:
21+
- "289"
22+
Content-Security-Policy:
23+
- default-src 'none'; frame-ancestors 'none'
24+
Content-Type:
25+
- application/json
26+
Date:
27+
- Mon, 13 Jul 2020 13:47:48 GMT
28+
Link:
29+
- </products/volumes?page=1&per_page=50&>; rel="last"
30+
Server:
31+
- agw_listener_public_vip
32+
Strict-Transport-Security:
33+
- max-age=63072000
34+
X-Content-Type-Options:
35+
- nosniff
36+
X-Frame-Options:
37+
- DENY
38+
X-Request-Id:
39+
- 5147d971-7070-449a-8f0b-0c2d691eb7ec
40+
X-Total-Count:
41+
- "2"
42+
status: 200 OK
43+
code: 200
44+
duration: ""
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
Type Name Snapshot Min Max
4+
b_ssd Block Storage SSD true 1.0 GB 10 TB
5+
l_ssd Local SSD true 1.0 GB 800 GB
6+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
7+
[
8+
{
9+
"type": "b_ssd",
10+
"display_name": "Block Storage SSD",
11+
"capabilities": {
12+
"snapshot": true
13+
},
14+
"constraints": {
15+
"min": 1000000000,
16+
"max": 10000000000000
17+
}
18+
},
19+
{
20+
"type": "l_ssd",
21+
"display_name": "Local SSD",
22+
"capabilities": {
23+
"snapshot": true
24+
},
25+
"constraints": {
26+
"min": 1000000000,
27+
"max": 800000000000
28+
}
29+
}
30+
]

0 commit comments

Comments
 (0)