Skip to content

Commit d19e6ad

Browse files
authored
feat(iot): improve network create output (#2584)
1 parent ebbf6a8 commit d19e6ad

File tree

8 files changed

+254
-0
lines changed

8 files changed

+254
-0
lines changed

cmd/scw/testdata/test-all-usage-iot-hub-create-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ARGS:
1616

1717
FLAGS:
1818
-h, --help help for create
19+
-w, --wait wait until the hub is ready
1920

2021
GLOBAL FLAGS:
2122
-c, --config string The path to the config file

internal/namespaces/iot/v1/custom.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ func GetCommands() *core.Commands {
1212
human.RegisterMarshalerFunc(iot.HubStatus(""), human.EnumMarshalFunc(hubStatusMarshalSpecs))
1313
human.RegisterMarshalerFunc(iot.DeviceMessageFiltersRulePolicy(""), human.EnumMarshalFunc(deviceMessageFiltersRulePolicyMarshalSpecs))
1414
human.RegisterMarshalerFunc(iot.DeviceStatus(""), human.EnumMarshalFunc(deviceStatusMarshalSpecs))
15+
human.RegisterMarshalerFunc(iot.CreateNetworkResponse{}, iotNetworkCreateResponsedMarshalerFunc)
16+
17+
cmds.MustFind("iot", "hub", "create").Override(hubCreateBuilder)
1518

1619
return cmds
1720
}

internal/namespaces/iot/v1/custom_hub.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package iot
22

33
import (
4+
"context"
5+
"time"
6+
47
"github.com/fatih/color"
8+
"github.com/scaleway/scaleway-cli/v2/internal/core"
59
"github.com/scaleway/scaleway-cli/v2/internal/human"
610
"github.com/scaleway/scaleway-sdk-go/api/iot/v1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
12+
)
13+
14+
const (
15+
hubActionTimeout = 5 * time.Minute
716
)
817

918
var (
@@ -15,3 +24,16 @@ var (
1524
iot.HubStatusReady: &human.EnumMarshalSpec{Attribute: color.FgGreen, Value: "ready"},
1625
}
1726
)
27+
28+
func hubCreateBuilder(c *core.Command) *core.Command {
29+
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
30+
api := iot.NewAPI(core.ExtractClient(ctx))
31+
return api.WaitForHub(&iot.WaitForHubRequest{
32+
HubID: respI.(*iot.Hub).ID,
33+
Region: respI.(*iot.Hub).Region,
34+
Timeout: scw.TimeDurationPtr(hubActionTimeout),
35+
RetryInterval: core.DefaultRetryInterval,
36+
})
37+
}
38+
return c
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package iot
2+
3+
import (
4+
"strings"
5+
6+
"github.com/fatih/color"
7+
"github.com/scaleway/scaleway-cli/v2/internal/human"
8+
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
9+
"github.com/scaleway/scaleway-sdk-go/api/iot/v1"
10+
)
11+
12+
func iotNetworkCreateResponsedMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
13+
type tmp iot.CreateNetworkResponse
14+
networkCreateResponse := tmp(i.(iot.CreateNetworkResponse))
15+
16+
networkContent, err := human.Marshal(networkCreateResponse.Network, opt)
17+
if err != nil {
18+
return "", err
19+
}
20+
networkView := terminal.Style("Network:\n", color.Bold) + networkContent
21+
22+
secretContent, err := human.Marshal(networkCreateResponse.Secret, opt)
23+
if err != nil {
24+
return "", err
25+
}
26+
secretView := terminal.Style("Secret: ", color.Bold) + secretContent
27+
28+
warningSecret := "WARNING: The secret below is displayed only once, we do not keep it. Make sure to store it securely"
29+
30+
return strings.Join([]string{
31+
networkView,
32+
warningSecret,
33+
secretView,
34+
}, "\n\n"), nil
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package iot
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/v2/internal/core"
7+
)
8+
9+
func Test_CreateNetwork(t *testing.T) {
10+
t.Run("Simple", core.Test(&core.TestConfig{
11+
Commands: GetCommands(),
12+
BeforeFunc: createHub(),
13+
Cmd: "scw iot network create hub-id={{ .Hub.ID }} type=sigfox topic-prefix=foo",
14+
Check: core.TestCheckGolden(),
15+
AfterFunc: deleteHub(),
16+
}))
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package iot
2+
3+
import (
4+
"github.com/scaleway/scaleway-cli/v2/internal/core"
5+
)
6+
7+
func createHub() core.BeforeFunc {
8+
return core.ExecStoreBeforeCmd(
9+
"Hub",
10+
"scw iot hub create product-plan=plan_shared --wait",
11+
)
12+
}
13+
14+
func deleteHub() core.AfterFunc {
15+
return core.ExecAfterCmd("scw iot hub delete {{ .Hub.ID }}")
16+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"name":"cli-hub-jovial-bardeen","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","product_plan":"plan_shared","disable_events":null,"events_topic_prefix":null}'
6+
form: {}
7+
headers:
8+
Content-Type:
9+
- application/json
10+
User-Agent:
11+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
12+
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs
13+
method: POST
14+
response:
15+
body: '{"region":"fr-par","id":"e50c1da4-3ade-4c67-9321-ce39d4bbe1ca","organization_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","name":"cli-hub-jovial-bardeen","status":"enabling","product_plan":"plan_shared","endpoint":"iot.fr-par.scw.cloud","created_at":"2022-11-14T11:22:39.323Z","updated_at":"2022-11-14T11:22:39.323Z","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}'
16+
headers:
17+
Content-Length:
18+
- "547"
19+
Content-Security-Policy:
20+
- default-src 'none'; frame-ancestors 'none'
21+
Content-Type:
22+
- application/json
23+
Date:
24+
- Mon, 14 Nov 2022 11:22:40 GMT
25+
Server:
26+
- Scaleway API-Gateway
27+
Strict-Transport-Security:
28+
- max-age=63072000
29+
X-Content-Type-Options:
30+
- nosniff
31+
X-Frame-Options:
32+
- DENY
33+
X-Request-Id:
34+
- 222f5144-1163-4d21-a630-0b46321f6946
35+
status: 200 OK
36+
code: 200
37+
duration: ""
38+
- request:
39+
body: ""
40+
form: {}
41+
headers:
42+
User-Agent:
43+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
44+
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs/e50c1da4-3ade-4c67-9321-ce39d4bbe1ca
45+
method: GET
46+
response:
47+
body: '{"region":"fr-par","id":"e50c1da4-3ade-4c67-9321-ce39d4bbe1ca","organization_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","project_id":"564aa517-68b0-4fd7-8c8c-d21c4bcdcbd5","name":"cli-hub-jovial-bardeen","status":"ready","product_plan":"plan_shared","endpoint":"iot.fr-par.scw.cloud","created_at":"2022-11-14T11:22:39.323Z","updated_at":"2022-11-14T11:22:39.323Z","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}'
48+
headers:
49+
Content-Length:
50+
- "544"
51+
Content-Security-Policy:
52+
- default-src 'none'; frame-ancestors 'none'
53+
Content-Type:
54+
- application/json
55+
Date:
56+
- Mon, 14 Nov 2022 11:22:40 GMT
57+
Server:
58+
- Scaleway API-Gateway
59+
Strict-Transport-Security:
60+
- max-age=63072000
61+
X-Content-Type-Options:
62+
- nosniff
63+
X-Frame-Options:
64+
- DENY
65+
X-Request-Id:
66+
- daf9e801-7561-40a4-918f-f291c72b93c2
67+
status: 200 OK
68+
code: 200
69+
duration: ""
70+
- request:
71+
body: '{"name":"cli-network-jolly-thompson","type":"sigfox","hub_id":"e50c1da4-3ade-4c67-9321-ce39d4bbe1ca","topic_prefix":"foo"}'
72+
form: {}
73+
headers:
74+
Content-Type:
75+
- application/json
76+
User-Agent:
77+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
78+
url: https://api.scaleway.com/iot/v1/regions/fr-par/networks
79+
method: POST
80+
response:
81+
body: '{"network":{"id":"0fc1c42f-050e-421e-86fc-54dcf154cf84","name":"cli-network-jolly-thompson","endpoint":"sigfox.iot.fr-par.scw.cloud","type":"sigfox","hub_id":"e50c1da4-3ade-4c67-9321-ce39d4bbe1ca","created_at":"2022-11-14T11:22:40.273Z","topic_prefix":"foo"},"secret":"eyJOZXRJRCI6IjBmYzFjNDJmLTA1MGUtNDIxZS04NmZjLTU0ZGNmMTU0Y2Y4NCIsIk5ldEtleSI6Ijg0ZWY0MzA4LTBmNjMtNGMyNy04YmJiLTA4YzkzOGU2ZmZkMiJ9"}'
82+
headers:
83+
Content-Length:
84+
- "399"
85+
Content-Security-Policy:
86+
- default-src 'none'; frame-ancestors 'none'
87+
Content-Type:
88+
- application/json
89+
Date:
90+
- Mon, 14 Nov 2022 11:22:40 GMT
91+
Server:
92+
- Scaleway API-Gateway
93+
Strict-Transport-Security:
94+
- max-age=63072000
95+
X-Content-Type-Options:
96+
- nosniff
97+
X-Frame-Options:
98+
- DENY
99+
X-Request-Id:
100+
- dc27a96e-549f-458b-97c0-007028d7f448
101+
status: 200 OK
102+
code: 200
103+
duration: ""
104+
- request:
105+
body: ""
106+
form: {}
107+
headers:
108+
User-Agent:
109+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.19.1; darwin; amd64) cli-e2e-test
110+
url: https://api.scaleway.com/iot/v1/regions/fr-par/hubs/e50c1da4-3ade-4c67-9321-ce39d4bbe1ca
111+
method: DELETE
112+
response:
113+
body: ""
114+
headers:
115+
Content-Security-Policy:
116+
- default-src 'none'; frame-ancestors 'none'
117+
Content-Type:
118+
- application/json
119+
Date:
120+
- Mon, 14 Nov 2022 11:22:40 GMT
121+
Server:
122+
- Scaleway API-Gateway
123+
Strict-Transport-Security:
124+
- max-age=63072000
125+
X-Content-Type-Options:
126+
- nosniff
127+
X-Frame-Options:
128+
- DENY
129+
X-Request-Id:
130+
- 792c4ded-83ce-43c5-ad56-0f495ac95a34
131+
status: 204 No Content
132+
code: 204
133+
duration: ""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
Network:
4+
ID 0fc1c42f-050e-421e-86fc-54dcf154cf84
5+
Name cli-network-jolly-thompson
6+
Type sigfox
7+
Endpoint sigfox.iot.fr-par.scw.cloud
8+
HubID e50c1da4-3ade-4c67-9321-ce39d4bbe1ca
9+
CreatedAt few seconds ago
10+
TopicPrefix foo
11+
12+
WARNING: The secret below is displayed only once, we do not keep it. Make sure to store it securely
13+
14+
Secret: eyJOZXRJRCI6IjBmYzFjNDJmLTA1MGUtNDIxZS04NmZjLTU0ZGNmMTU0Y2Y4NCIsIk5ldEtleSI6Ijg0ZWY0MzA4LTBmNjMtNGMyNy04YmJiLTA4YzkzOGU2ZmZkMiJ9
15+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
16+
{
17+
"network": {
18+
"id": "0fc1c42f-050e-421e-86fc-54dcf154cf84",
19+
"name": "cli-network-jolly-thompson",
20+
"type": "sigfox",
21+
"endpoint": "sigfox.iot.fr-par.scw.cloud",
22+
"hub_id": "e50c1da4-3ade-4c67-9321-ce39d4bbe1ca",
23+
"created_at": "1970-01-01T00:00:00.0Z",
24+
"topic_prefix": "foo"
25+
},
26+
"secret": "eyJOZXRJRCI6IjBmYzFjNDJmLTA1MGUtNDIxZS04NmZjLTU0ZGNmMTU0Y2Y4NCIsIk5ldEtleSI6Ijg0ZWY0MzA4LTBmNjMtNGMyNy04YmJiLTA4YzkzOGU2ZmZkMiJ9"
27+
}

0 commit comments

Comments
 (0)