Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/namespaces/instance/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func GetCommands() *core.Commands {
//
human.RegisterMarshalerFunc(instance.CreateSecurityGroupRuleResponse{}, marshallNestedField("Rule"))
human.RegisterMarshalerFunc(instance.SecurityGroupRuleAction(""), human.EnumMarshalFunc(securityGroupRuleActionMarshalSpecs))
human.RegisterMarshalerFunc([]*instance.SecurityGroupRule{}, marshalSecurityGroupRules)

//
// Placement Group
Expand Down
69 changes: 40 additions & 29 deletions internal/namespaces/instance/v1/custom_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ var (
}
)

func marshalSecurityGroupRules(i interface{}, opt *human.MarshalOpt) (out string, err error) {
rules := i.([]*instance.SecurityGroupRule)

type humanRule struct {
ID string
Protocol instance.SecurityGroupRuleProtocol
Action instance.SecurityGroupRuleAction
IPRange string
Dest string
}

toHumanRule := func(rule *instance.SecurityGroupRule) *humanRule {
dest := "ALL"
if rule.DestPortFrom != nil {
dest = strconv.Itoa(int(*rule.DestPortFrom))
}
if rule.DestPortTo != nil {
dest += "-" + strconv.Itoa(int(*rule.DestPortTo))
}
return &humanRule{
ID: rule.ID,
Protocol: rule.Protocol,
Action: rule.Action,
IPRange: rule.IPRange.String(),
Dest: dest,
}
}
humanRules := []*humanRule(nil)

for _, rule := range rules {
humanRules = append(humanRules, toHumanRule(rule))
}
Comment thread
Codelax marked this conversation as resolved.
Outdated

return human.Marshal(humanRules, nil)
}

// MarshalHuman marshals a customSecurityGroupResponse.
func (sg *customSecurityGroupResponse) MarshalHuman() (out string, err error) {
humanSecurityGroup := struct {
Expand Down Expand Up @@ -76,39 +112,14 @@ func (sg *customSecurityGroupResponse) MarshalHuman() (out string, err error) {
}
securityGroupView = terminal.Style("Security Group:\n", color.Bold) + securityGroupView

type humanRule struct {
ID string
Protocol instance.SecurityGroupRuleProtocol
Action instance.SecurityGroupRuleAction
IPRange string
Dest string
}

toHumanRule := func(rule *instance.SecurityGroupRule) *humanRule {
dest := "ALL"
if rule.DestPortFrom != nil {
dest = strconv.Itoa(int(*rule.DestPortFrom))
}
if rule.DestPortTo != nil {
dest += "-" + strconv.Itoa(int(*rule.DestPortTo))
}
return &humanRule{
ID: rule.ID,
Protocol: rule.Protocol,
Action: rule.Action,
IPRange: rule.IPRange.String(),
Dest: dest,
}
}

inboundRules := []*humanRule(nil)
outboundRules := []*humanRule(nil)
inboundRules := []*instance.SecurityGroupRule(nil)
outboundRules := []*instance.SecurityGroupRule(nil)
for _, rule := range sg.Rules {
switch rule.Direction {
case instance.SecurityGroupRuleDirectionInbound:
inboundRules = append(inboundRules, toHumanRule(rule))
inboundRules = append(inboundRules, rule)
case instance.SecurityGroupRuleDirectionOutbound:
outboundRules = append(outboundRules, toHumanRule(rule))
outboundRules = append(outboundRules, rule)
default:
logger.Warningf("invalid security group rule direction: %v", rule.Direction)
}
Expand Down
19 changes: 19 additions & 0 deletions internal/namespaces/instance/v1/custom_security_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package instance

import (
"testing"

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

func Test_SecurityGroupGet(t *testing.T) {
t.Run("Get", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createSecurityGroup("SecurityGroup"),
),
Cmd: "scw instance security-group get {{ .SecurityGroup.ID }}",
Check: core.TestCheckGolden(),
Comment thread
Codelax marked this conversation as resolved.
Outdated
AfterFunc: deleteSecurityGroup("SecurityGroup"),
}))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
---
version: 1
interactions:
- request:
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"}'
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups
method: POST
response:
body: '{"security_group": {"id": "4e02d7f0-1f93-4478-ab2d-e64a6aacbd47", "creation_date":
"2023-03-24T13:57:13.987804+00:00", "modification_date": "2023-03-24T13:57:13.987804+00:00",
"name": "cli-sg-hungry-borg", "description": null, "organization": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
"project": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b", "servers": [], "stateful":
true, "inbound_default_policy": "accept", "outbound_default_policy": "accept",
"organization_default": false, "project_default": false, "enable_default_security":
true, "state": "syncing", "tags": [], "zone": "fr-par-1"}}'
headers:
Content-Length:
- "582"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 24 Mar 2023 13:57:14 GMT
Location:
- https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 1bbf4677-dc3a-495d-a5de-d63c8e7fc3c5
status: 201 Created
code: 201
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47
method: GET
response:
body: '{"security_group": {"id": "4e02d7f0-1f93-4478-ab2d-e64a6aacbd47", "creation_date":
"2023-03-24T13:57:13.987804+00:00", "modification_date": "2023-03-24T13:57:13.987804+00:00",
"name": "cli-sg-hungry-borg", "description": null, "organization": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
"project": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b", "servers": [], "stateful":
true, "inbound_default_policy": "accept", "outbound_default_policy": "accept",
"organization_default": false, "project_default": false, "enable_default_security":
true, "state": "syncing", "tags": [], "zone": "fr-par-1"}}'
headers:
Content-Length:
- "582"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 24 Mar 2023 13:57:14 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- ecf25c55-618a-4b8c-82b8-db578182090f
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47/rules?page=1
method: GET
response:
body: '{"rules": [{"id": "58909be7-d17c-4ac8-9eb3-23d5fc58abc5", "protocol": "TCP",
"direction": "outbound", "ip_range": "0.0.0.0/0", "dest_port_from": 25, "dest_port_to":
null, "position": 1, "editable": false, "action": "drop", "zone": "fr-par-1"},
{"id": "25680235-108b-4bbc-8e25-114303d950bd", "protocol": "TCP", "direction":
"outbound", "ip_range": "0.0.0.0/0", "dest_port_from": 465, "dest_port_to":
null, "position": 2, "editable": false, "action": "drop", "zone": "fr-par-1"},
{"id": "4a31b633-118e-4900-bd52-facf1085fc8d", "protocol": "TCP", "direction":
"outbound", "ip_range": "0.0.0.0/0", "dest_port_from": 587, "dest_port_to":
null, "position": 3, "editable": false, "action": "drop", "zone": "fr-par-1"},
{"id": "e7dd28e8-3747-4c7c-9a4f-35ae3f0ae2cd", "protocol": "TCP", "direction":
"outbound", "ip_range": "::/0", "dest_port_from": 25, "dest_port_to": null,
"position": 4, "editable": false, "action": "drop", "zone": "fr-par-1"}, {"id":
"f37d9e7c-8ed7-4e0f-baff-7f5e7ede0baf", "protocol": "TCP", "direction": "outbound",
"ip_range": "::/0", "dest_port_from": 465, "dest_port_to": null, "position":
5, "editable": false, "action": "drop", "zone": "fr-par-1"}, {"id": "68054851-54e3-46c9-9cd7-83219751248b",
"protocol": "TCP", "direction": "outbound", "ip_range": "::/0", "dest_port_from":
587, "dest_port_to": null, "position": 6, "editable": false, "action": "drop",
"zone": "fr-par-1"}]}'
headers:
Content-Length:
- "1398"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 24 Mar 2023 13:57:14 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 9491aa05-a952-441a-9896-b06a815b009a
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
url: https://api.scaleway.com/instance/v1/zones/fr-par-1/security_groups/4e02d7f0-1f93-4478-ab2d-e64a6aacbd47
method: DELETE
response:
body: ""
headers:
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Date:
- Fri, 24 Mar 2023 13:57:14 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 5cf21a3b-8b21-4675-9f22-af5c25289605
status: 204 No Content
code: 204
duration: ""
Loading