Skip to content

Commit

Permalink
feat(iot): change device create output (#2593)
Browse files Browse the repository at this point in the history
Co-authored-by: Rémy Léone <rleone@scaleway.com>
  • Loading branch information
yfodil and remyleone authored Nov 18, 2022
1 parent 1953a69 commit 121b725
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/namespaces/iot/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func GetCommands() *core.Commands {
human.RegisterMarshalerFunc(iot.DeviceMessageFiltersRulePolicy(""), human.EnumMarshalFunc(deviceMessageFiltersRulePolicyMarshalSpecs))
human.RegisterMarshalerFunc(iot.DeviceStatus(""), human.EnumMarshalFunc(deviceStatusMarshalSpecs))
human.RegisterMarshalerFunc(iot.CreateNetworkResponse{}, iotNetworkCreateResponsedMarshalerFunc)
human.RegisterMarshalerFunc(iot.CreateDeviceResponse{}, iotDeviceCreateResponsedMarshalerFunc)

cmds.MustFind("iot", "hub", "create").Override(hubCreateBuilder)

Expand Down
35 changes: 35 additions & 0 deletions internal/namespaces/iot/v1/custom_device.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package iot

import (
"strings"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/human"
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
"github.com/scaleway/scaleway-sdk-go/api/iot/v1"
)

Expand All @@ -18,3 +21,35 @@ var (
iot.DeviceStatusError: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "error"},
}
)

func iotDeviceCreateResponsedMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp iot.CreateDeviceResponse
deviceCreateResponse := tmp(i.(iot.CreateDeviceResponse))

deviceContent, err := human.Marshal(deviceCreateResponse.Device, opt)
if err != nil {
return "", err
}
deviceView := terminal.Style("Device:\n", color.Bold) + deviceContent

certificateContent, err := human.Marshal(deviceCreateResponse.Certificate.Crt, opt)
if err != nil {
return "", err
}
certificateView := terminal.Style("Certificate:\n", color.Bold) + certificateContent

privateKeyContent, err := human.Marshal(deviceCreateResponse.Certificate.Key, opt)
if err != nil {
return "", err
}
privateKeyView := terminal.Style("Private Key:\n", color.Bold) + privateKeyContent

warningKeys := "WARNING: The keys below are displayed only once. Make sure to store them securely"

return strings.Join([]string{
deviceView,
warningKeys,
certificateView,
privateKeyView,
}, "\n\n"), nil
}
20 changes: 20 additions & 0 deletions internal/namespaces/iot/v1/custom_device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package iot

import (
"testing"

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

func Test_CreateDevice(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: createHub(),
Cmd: "scw iot device create hub-id={{ .Hub.ID }} name=foo",
Check: core.TestCheckGolden(),
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd("scw iot device delete {{ .CmdResult.Device.ID }}"),
deleteHub(),
),
}))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
---
version: 1
interactions:
- request:
body: '{"name":"cli-hub-goofy-faraday","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","product_plan":"plan_shared","disable_events":null,"events_topic_prefix":null}'
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs
method: POST
response:
body: '{"region":"fr-par","id":"2658f811-fbb5-4c50-b52a-3947fbbdfa34","organization_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","name":"cli-hub-goofy-faraday","status":"enabling","product_plan":"plan_shared","endpoint":"iot.fr-par.scw.cloud","created_at":"2022-11-16T14:45:01.496Z","updated_at":"2022-11-16T14:45:01.496Z","enabled":true,"device_count":0,"connected_device_count":0,"disable_events":false,"events_topic_prefix":"$SCW/events","enable_device_auto_provisioning":false,"has_custom_ca":false}'
headers:
Content-Length:
- "546"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 16 Nov 2022 14:45:02 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 736705b2-5184-4d05-83af-4053b19bc954
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs/2658f811-fbb5-4c50-b52a-3947fbbdfa34
method: GET
response:
body: '{"region":"fr-par","id":"2658f811-fbb5-4c50-b52a-3947fbbdfa34","organization_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","name":"cli-hub-goofy-faraday","status":"enabling","product_plan":"plan_shared","endpoint":"iot.fr-par.scw.cloud","created_at":"2022-11-16T14:45:01.496Z","updated_at":"2022-11-16T14:45:01.496Z","enabled":true,"device_count":0,"connected_device_count":0,"disable_events":false,"events_topic_prefix":"$SCW/events","enable_device_auto_provisioning":false,"has_custom_ca":false}'
headers:
Content-Length:
- "546"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 16 Nov 2022 14:45:02 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 7fc9705c-5aea-4fe8-84f2-37403be7692c
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs/2658f811-fbb5-4c50-b52a-3947fbbdfa34
method: GET
response:
body: '{"region":"fr-par","id":"2658f811-fbb5-4c50-b52a-3947fbbdfa34","organization_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","name":"cli-hub-goofy-faraday","status":"enabling","product_plan":"plan_shared","endpoint":"iot.fr-par.scw.cloud","created_at":"2022-11-16T14:45:01.496Z","updated_at":"2022-11-16T14:45:01.496Z","enabled":true,"device_count":0,"connected_device_count":0,"disable_events":false,"events_topic_prefix":"$SCW/events","enable_device_auto_provisioning":false,"has_custom_ca":false}'
headers:
Content-Length:
- "546"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 16 Nov 2022 14:45:07 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 75aa2532-db88-4024-8af2-ce553be1d1f6
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs/2658f811-fbb5-4c50-b52a-3947fbbdfa34
method: GET
response:
body: '{"region":"fr-par","id":"2658f811-fbb5-4c50-b52a-3947fbbdfa34","organization_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","name":"cli-hub-goofy-faraday","status":"ready","product_plan":"plan_shared","endpoint":"iot.fr-par.scw.cloud","created_at":"2022-11-16T14:45:01.496Z","updated_at":"2022-11-16T14:45:01.496Z","enabled":true,"device_count":0,"connected_device_count":0,"disable_events":false,"events_topic_prefix":"$SCW/events","enable_device_auto_provisioning":false,"has_custom_ca":false}'
headers:
Content-Length:
- "543"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 16 Nov 2022 14:45:12 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 1901021e-ff77-40c8-a25f-8096bdbdb640
status: 200 OK
code: 200
duration: ""
- request:
body: '{"name":"foo","hub_id":"2658f811-fbb5-4c50-b52a-3947fbbdfa34","allow_insecure":false,"allow_multiple_connections":false,"message_filters":null,"description":null}'
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/iot/v1/regions/fr-par/devices
method: POST
response:
body: '{"device":{"id":"87dc3ddb-dc41-414c-8527-d987dfeadb9b","name":"foo","status":"enabled","hub_id":"2658f811-fbb5-4c50-b52a-3947fbbdfa34","created_at":"2022-11-16T14:45:12.488Z","updated_at":"2022-11-16T14:45:12.488Z","allow_insecure":false,"last_activity_at":"1970-01-01T00:00:00Z","is_connected":false,"message_filters":{"publish":{"policy":"reject","topics":[]},"subscribe":{"policy":"reject","topics":[]}},"allow_multiple_connections":false,"description":"","has_custom_certificate":false},"certificate":{"crt":"-----BEGIN
CERTIFICATE-----\nMIIETjCCAzagAwIBAgIQfXLKLFLHcMNdN7okvo/VtDANBgkqhkiG9w0BAQsFADCB\nzjELMAkGA1UEBhMCRlIxDzANBgNVBAgTBkZyYW5jZTEOMAwGA1UEBxMFUGFyaXMx\nEjAQBgNVBAkTCUxhIE1haXNvbjEJMAcGA1UEERMAMRgwFgYDVQQKEw9TY2FsZXdh\neS9PbmxpbmUxEDAOBgNVBAsTB0xhYi9Jb1QxITAfBgNVBAMTGFNjYWxld2F5IElv\nVCBDZXJ0aWZpY2F0ZTEwMC4GA1UEBRMnMjE1MzE0NTA3MTE4NDY5Njc2OTQ1Njk5\nOTYwNjA4NzA5MjYxNTcwMB4XDTIyMTExNTE0NDUxMloXDTMyMTExNTE0NDUxM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iLxERH7jnMtAdv/OKDffsLi1\nRyPoaloalkF0HoaxkxG2M7npeliRzJ3dpJ59zc2mdF/R0BxIwVLSA/Ucj3NYZTc0\nvLhoNR0aApxOEoo3D1vBdoRdDcabysyHYwt4fppjAoGAaYFYdmLbMFLZ55NIc91H\nUVvX3KVWREXTg6xPpnSJiy1zhYQ8L7NOnSioXYYQ0H0gXza483MNvZb+SlSOV890\nKCcKN0F6xiJ+Fop7zeAO5zepLCKY1i46NXJB3nmiZCE26tylhrjQPYxJdqG4jBH8\nKweuBFagxsB4PX6pmzoE1dkCgYEA/PyVW4w91LNnTDjTLp70VX+9qS3qiOAsyEC5\nkrjNqXNd0ZG9DptXIURQG5uf/1J48KombmTNstBy0rG/dq3RTAGH8WzJhQBge25Y\nyFxbb7nnb8sI5Nw5Uu7KAJ7BYGbwbPgQzkoarfBGwk8dXkb8RFrfFgiQlE7rzuzH\nxs/MfDcCgYEAu7p+1KZ+zmJCUPt3x81CVSAH/ntJL6j++MqB7qeb9eCjzQuNKK46\nq2FkMIva1cQqIkQTyoofsDxDblNDdlwS3htF0bbs79tI+AJfn+WIV2kuepi9vmyy\na9pRpSCncvC3TaNfRzNBMGbjnghnqGXz89/GUYXoXK9fnbMhFNHUU6w=\n-----END
RSA PRIVATE KEY-----\n"}}'
headers:
Content-Length:
- "3811"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 16 Nov 2022 14:45:13 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- f5854fea-a79a-4365-882b-750a1ba4427a
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/iot/v1/regions/fr-par/devices/87dc3ddb-dc41-414c-8527-d987dfeadb9b
method: DELETE
response:
body: ""
headers:
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 16 Nov 2022 14:45:13 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 9b6f8134-d4e8-4cfc-bd82-a1040fe2defe
status: 204 No Content
code: 204
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs/2658f811-fbb5-4c50-b52a-3947fbbdfa34
method: DELETE
response:
body: ""
headers:
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 16 Nov 2022 14:45:13 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 31154b0f-6f73-490b-adc1-a4baa367b8dc
status: 204 No Content
code: 204
duration: ""
Loading

0 comments on commit 121b725

Please sign in to comment.