From b09da68005846af2c25ecf67db170bfaafaf2e12 Mon Sep 17 00:00:00 2001 From: Jerome Quere Date: Mon, 13 Jul 2020 15:49:32 +0200 Subject: [PATCH 1/2] feat(instance): improve human output for volume-type list --- internal/namespaces/instance/v1/custom.go | 5 +++ .../instance/v1/custom_volume_type.go | 45 +++++++++++++++++++ .../instance/v1/custom_volume_type_test.go | 15 +++++++ ...e-type-list-volume-type-list.cassette.yaml | 44 ++++++++++++++++++ ...t-volume-type-list-volume-type-list.golden | 30 +++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 internal/namespaces/instance/v1/custom_volume_type.go create mode 100644 internal/namespaces/instance/v1/custom_volume_type_test.go create mode 100644 internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.cassette.yaml create mode 100644 internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.golden diff --git a/internal/namespaces/instance/v1/custom.go b/internal/namespaces/instance/v1/custom.go index 4a0be083c2..a8f45a683c 100644 --- a/internal/namespaces/instance/v1/custom.go +++ b/internal/namespaces/instance/v1/custom.go @@ -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 // diff --git a/internal/namespaces/instance/v1/custom_volume_type.go b/internal/namespaces/instance/v1/custom_volume_type.go new file mode 100644 index 0000000000..03568b6f7a --- /dev/null +++ b/internal/namespaces/instance/v1/custom_volume_type.go @@ -0,0 +1,45 @@ +package instance + +import ( + "context" + + "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, + }) + } + 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 +} diff --git a/internal/namespaces/instance/v1/custom_volume_type_test.go b/internal/namespaces/instance/v1/custom_volume_type_test.go new file mode 100644 index 0000000000..b4b2dd05b9 --- /dev/null +++ b/internal/namespaces/instance/v1/custom_volume_type_test.go @@ -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(), + })) +} diff --git a/internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.cassette.yaml b/internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.cassette.yaml new file mode 100644 index 0000000000..37b805578f --- /dev/null +++ b/internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.cassette.yaml @@ -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: + - ; 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: "" diff --git a/internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.golden b/internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.golden new file mode 100644 index 0000000000..2bf5ae6304 --- /dev/null +++ b/internal/namespaces/instance/v1/testdata/test-volume-type-list-volume-type-list.golden @@ -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 + } + } +] From a1624275306056aa0437a3e07277994e6eeb6ff5 Mon Sep 17 00:00:00 2001 From: Jerome Quere Date: Mon, 13 Jul 2020 15:59:42 +0200 Subject: [PATCH 2/2] fix --- internal/namespaces/instance/v1/custom_volume_type.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/namespaces/instance/v1/custom_volume_type.go b/internal/namespaces/instance/v1/custom_volume_type.go index 03568b6f7a..1c9c52a248 100644 --- a/internal/namespaces/instance/v1/custom_volume_type.go +++ b/internal/namespaces/instance/v1/custom_volume_type.go @@ -2,6 +2,7 @@ package instance import ( "context" + "sort" "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" @@ -26,6 +27,12 @@ func volumeTypeListBuilder(cmd *core.Command) *core.Command { 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 })