Skip to content

Commit cd1366b

Browse files
fix(instance): split security group marshalers (#2939)
Co-authored-by: Nathanael Demacon <[email protected]>
1 parent ae6463e commit cd1366b

File tree

5 files changed

+347
-29
lines changed

5 files changed

+347
-29
lines changed

internal/namespaces/instance/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func GetCommands() *core.Commands {
149149
//
150150
human.RegisterMarshalerFunc(instance.CreateSecurityGroupRuleResponse{}, marshallNestedField("Rule"))
151151
human.RegisterMarshalerFunc(instance.SecurityGroupRuleAction(""), human.EnumMarshalFunc(securityGroupRuleActionMarshalSpecs))
152+
human.RegisterMarshalerFunc([]*instance.SecurityGroupRule{}, marshalSecurityGroupRules)
152153

153154
//
154155
// Placement Group

internal/namespaces/instance/v1/custom_security_group.go

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@ var (
4040
}
4141
)
4242

43+
func marshalSecurityGroupRules(i interface{}, opt *human.MarshalOpt) (out string, err error) {
44+
rules := i.([]*instance.SecurityGroupRule)
45+
46+
type humanRule struct {
47+
ID string
48+
Protocol instance.SecurityGroupRuleProtocol
49+
Action instance.SecurityGroupRuleAction
50+
IPRange string
51+
Dest string
52+
}
53+
54+
toHumanRule := func(rule *instance.SecurityGroupRule) *humanRule {
55+
dest := "ALL"
56+
if rule.DestPortFrom != nil {
57+
dest = strconv.Itoa(int(*rule.DestPortFrom))
58+
}
59+
if rule.DestPortTo != nil {
60+
dest += "-" + strconv.Itoa(int(*rule.DestPortTo))
61+
}
62+
return &humanRule{
63+
ID: rule.ID,
64+
Protocol: rule.Protocol,
65+
Action: rule.Action,
66+
IPRange: rule.IPRange.String(),
67+
Dest: dest,
68+
}
69+
}
70+
humanRules := make([]*humanRule, len(rules))
71+
72+
for i, rule := range rules {
73+
humanRules[i] = toHumanRule(rule)
74+
}
75+
76+
return human.Marshal(humanRules, nil)
77+
}
78+
4379
// MarshalHuman marshals a customSecurityGroupResponse.
4480
func (sg *customSecurityGroupResponse) MarshalHuman() (out string, err error) {
4581
humanSecurityGroup := struct {
@@ -76,39 +112,14 @@ func (sg *customSecurityGroupResponse) MarshalHuman() (out string, err error) {
76112
}
77113
securityGroupView = terminal.Style("Security Group:\n", color.Bold) + securityGroupView
78114

79-
type humanRule struct {
80-
ID string
81-
Protocol instance.SecurityGroupRuleProtocol
82-
Action instance.SecurityGroupRuleAction
83-
IPRange string
84-
Dest string
85-
}
86-
87-
toHumanRule := func(rule *instance.SecurityGroupRule) *humanRule {
88-
dest := "ALL"
89-
if rule.DestPortFrom != nil {
90-
dest = strconv.Itoa(int(*rule.DestPortFrom))
91-
}
92-
if rule.DestPortTo != nil {
93-
dest += "-" + strconv.Itoa(int(*rule.DestPortTo))
94-
}
95-
return &humanRule{
96-
ID: rule.ID,
97-
Protocol: rule.Protocol,
98-
Action: rule.Action,
99-
IPRange: rule.IPRange.String(),
100-
Dest: dest,
101-
}
102-
}
103-
104-
inboundRules := []*humanRule(nil)
105-
outboundRules := []*humanRule(nil)
115+
inboundRules := []*instance.SecurityGroupRule(nil)
116+
outboundRules := []*instance.SecurityGroupRule(nil)
106117
for _, rule := range sg.Rules {
107118
switch rule.Direction {
108119
case instance.SecurityGroupRuleDirectionInbound:
109-
inboundRules = append(inboundRules, toHumanRule(rule))
120+
inboundRules = append(inboundRules, rule)
110121
case instance.SecurityGroupRuleDirectionOutbound:
111-
outboundRules = append(outboundRules, toHumanRule(rule))
122+
outboundRules = append(outboundRules, rule)
112123
default:
113124
logger.Warningf("invalid security group rule direction: %v", rule.Direction)
114125
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package instance
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/v2/internal/core"
7+
)
8+
9+
func Test_SecurityGroupGet(t *testing.T) {
10+
t.Run("Get", core.Test(&core.TestConfig{
11+
Commands: GetCommands(),
12+
BeforeFunc: core.BeforeFuncCombine(
13+
createSecurityGroup("SecurityGroup"),
14+
),
15+
Cmd: "scw instance security-group get {{ .SecurityGroup.ID }}",
16+
Check: core.TestCheckCombine(
17+
core.TestCheckGolden(),
18+
core.TestCheckExitCode(0),
19+
),
20+
AfterFunc: deleteSecurityGroup("SecurityGroup"),
21+
}))
22+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"name":"cli-sg-hungry-borg","project":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","organization_default":false,"project_default":false,"stateful":true,"inbound_default_policy":"accept","outbound_default_policy":"accept"}'
6+
form: {}
7+
headers:
8+
Content-Type:
9+
- application/json
10+
User-Agent:
11+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
12+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups
13+
method: POST
14+
response:
15+
body: '{"security_group": {"id": "4e02d7f0-1f93-4478-ab2d-e64a6aacbd47", "creation_date":
16+
"2023-03-24T13:57:13.987804+00:00", "modification_date": "2023-03-24T13:57:13.987804+00:00",
17+
"name": "cli-sg-hungry-borg", "description": null, "organization": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
18+
"project": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b", "servers": [], "stateful":
19+
true, "inbound_default_policy": "accept", "outbound_default_policy": "accept",
20+
"organization_default": false, "project_default": false, "enable_default_security":
21+
true, "state": "syncing", "tags": [], "zone": "fr-par-1"}}'
22+
headers:
23+
Content-Length:
24+
- "582"
25+
Content-Security-Policy:
26+
- default-src 'none'; frame-ancestors 'none'
27+
Content-Type:
28+
- application/json
29+
Date:
30+
- Fri, 24 Mar 2023 13:57:14 GMT
31+
Location:
32+
- https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47
33+
Server:
34+
- Scaleway API-Gateway
35+
Strict-Transport-Security:
36+
- max-age=63072000
37+
X-Content-Type-Options:
38+
- nosniff
39+
X-Frame-Options:
40+
- DENY
41+
X-Request-Id:
42+
- 1bbf4677-dc3a-495d-a5de-d63c8e7fc3c5
43+
status: 201 Created
44+
code: 201
45+
duration: ""
46+
- request:
47+
body: ""
48+
form: {}
49+
headers:
50+
User-Agent:
51+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
52+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47
53+
method: GET
54+
response:
55+
body: '{"security_group": {"id": "4e02d7f0-1f93-4478-ab2d-e64a6aacbd47", "creation_date":
56+
"2023-03-24T13:57:13.987804+00:00", "modification_date": "2023-03-24T13:57:13.987804+00:00",
57+
"name": "cli-sg-hungry-borg", "description": null, "organization": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
58+
"project": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b", "servers": [], "stateful":
59+
true, "inbound_default_policy": "accept", "outbound_default_policy": "accept",
60+
"organization_default": false, "project_default": false, "enable_default_security":
61+
true, "state": "syncing", "tags": [], "zone": "fr-par-1"}}'
62+
headers:
63+
Content-Length:
64+
- "582"
65+
Content-Security-Policy:
66+
- default-src 'none'; frame-ancestors 'none'
67+
Content-Type:
68+
- application/json
69+
Date:
70+
- Fri, 24 Mar 2023 13:57:14 GMT
71+
Server:
72+
- Scaleway API-Gateway
73+
Strict-Transport-Security:
74+
- max-age=63072000
75+
X-Content-Type-Options:
76+
- nosniff
77+
X-Frame-Options:
78+
- DENY
79+
X-Request-Id:
80+
- ecf25c55-618a-4b8c-82b8-db578182090f
81+
status: 200 OK
82+
code: 200
83+
duration: ""
84+
- request:
85+
body: ""
86+
form: {}
87+
headers:
88+
User-Agent:
89+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
90+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47/rules?page=1
91+
method: GET
92+
response:
93+
body: '{"rules": [{"id": "58909be7-d17c-4ac8-9eb3-23d5fc58abc5", "protocol": "TCP",
94+
"direction": "outbound", "ip_range": "0.0.0.0/0", "dest_port_from": 25, "dest_port_to":
95+
null, "position": 1, "editable": false, "action": "drop", "zone": "fr-par-1"},
96+
{"id": "25680235-108b-4bbc-8e25-114303d950bd", "protocol": "TCP", "direction":
97+
"outbound", "ip_range": "0.0.0.0/0", "dest_port_from": 465, "dest_port_to":
98+
null, "position": 2, "editable": false, "action": "drop", "zone": "fr-par-1"},
99+
{"id": "4a31b633-118e-4900-bd52-facf1085fc8d", "protocol": "TCP", "direction":
100+
"outbound", "ip_range": "0.0.0.0/0", "dest_port_from": 587, "dest_port_to":
101+
null, "position": 3, "editable": false, "action": "drop", "zone": "fr-par-1"},
102+
{"id": "e7dd28e8-3747-4c7c-9a4f-35ae3f0ae2cd", "protocol": "TCP", "direction":
103+
"outbound", "ip_range": "::/0", "dest_port_from": 25, "dest_port_to": null,
104+
"position": 4, "editable": false, "action": "drop", "zone": "fr-par-1"}, {"id":
105+
"f37d9e7c-8ed7-4e0f-baff-7f5e7ede0baf", "protocol": "TCP", "direction": "outbound",
106+
"ip_range": "::/0", "dest_port_from": 465, "dest_port_to": null, "position":
107+
5, "editable": false, "action": "drop", "zone": "fr-par-1"}, {"id": "68054851-54e3-46c9-9cd7-83219751248b",
108+
"protocol": "TCP", "direction": "outbound", "ip_range": "::/0", "dest_port_from":
109+
587, "dest_port_to": null, "position": 6, "editable": false, "action": "drop",
110+
"zone": "fr-par-1"}]}'
111+
headers:
112+
Content-Length:
113+
- "1398"
114+
Content-Security-Policy:
115+
- default-src 'none'; frame-ancestors 'none'
116+
Content-Type:
117+
- application/json
118+
Date:
119+
- Fri, 24 Mar 2023 13:57:14 GMT
120+
Server:
121+
- Scaleway API-Gateway
122+
Strict-Transport-Security:
123+
- max-age=63072000
124+
X-Content-Type-Options:
125+
- nosniff
126+
X-Frame-Options:
127+
- DENY
128+
X-Request-Id:
129+
- 9491aa05-a952-441a-9896-b06a815b009a
130+
status: 200 OK
131+
code: 200
132+
duration: ""
133+
- request:
134+
body: ""
135+
form: {}
136+
headers:
137+
User-Agent:
138+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
139+
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47
140+
method: DELETE
141+
response:
142+
body: ""
143+
headers:
144+
Content-Security-Policy:
145+
- default-src 'none'; frame-ancestors 'none'
146+
Date:
147+
- Fri, 24 Mar 2023 13:57:14 GMT
148+
Server:
149+
- Scaleway API-Gateway
150+
Strict-Transport-Security:
151+
- max-age=63072000
152+
X-Content-Type-Options:
153+
- nosniff
154+
X-Frame-Options:
155+
- DENY
156+
X-Request-Id:
157+
- 5cf21a3b-8b21-4675-9f22-af5c25289605
158+
status: 204 No Content
159+
code: 204
160+
duration: ""

0 commit comments

Comments
 (0)