Skip to content

fix(instance): use IPNet type for security group rule ip_range #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions api/instance/v1/instance_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ type SecurityGroupRule struct {
// Default value: accept
Action SecurityGroupRuleAction `json:"action"`

IPRange string `json:"ip_range"`
IPRange scw.IPNet `json:"ip_range"`

DestPortFrom *uint32 `json:"dest_port_from"`

Expand Down Expand Up @@ -3111,7 +3111,7 @@ type CreateSecurityGroupRuleRequest struct {
// Default value: accept
Action SecurityGroupRuleAction `json:"action"`

IPRange string `json:"ip_range,omitempty"`
IPRange scw.IPNet `json:"ip_range,omitempty"`

DestPortFrom *uint32 `json:"dest_port_from,omitempty"`

Expand Down Expand Up @@ -3270,7 +3270,7 @@ type setSecurityGroupRuleRequest struct {
// Default value: accept
Action SecurityGroupRuleAction `json:"action"`

IPRange string `json:"ip_range"`
IPRange scw.IPNet `json:"ip_range"`

DestPortFrom *uint32 `json:"dest_port_from"`

Expand Down
2 changes: 1 addition & 1 deletion api/instance/v1/security_group_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type UpdateSecurityGroupRuleRequest struct {
Protocol *SecurityGroupRuleProtocol `json:"protocol"`
Direction *SecurityGroupRuleDirection `json:"direction"`
Action *SecurityGroupRuleAction `json:"action"`
IPRange *string `json:"ip_range"`
IPRange *scw.IPNet `json:"ip_range"`
Position *uint32 `json:"position"`

// If set to 0, DestPortFrom will be removed.
Expand Down
20 changes: 11 additions & 9 deletions api/instance/v1/security_group_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package instance

import (
"net"
"testing"

"github.com./scaleway/scaleway-sdk-go/internal/testhelpers"
Expand Down Expand Up @@ -91,15 +92,15 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
})

testhelpers.AssertNoError(t, err)

_, ipNet, _ := net.ParseCIDR("8.8.8.8/32")
createRuleResponse, err := instanceAPI.CreateSecurityGroupRule(&CreateSecurityGroupRuleRequest{
Zone: zone,
SecurityGroupID: createSecurityGroupResponse.SecurityGroup.ID,
Direction: SecurityGroupRuleDirectionInbound,
Protocol: SecurityGroupRuleProtocolTCP,
DestPortFrom: scw.Uint32Ptr(1),
DestPortTo: scw.Uint32Ptr(1024),
IPRange: "8.8.8.8/32",
IPRange: scw.IPNet{IPNet: *ipNet},
Action: SecurityGroupRuleActionAccept,
Position: 1,
})
Expand All @@ -121,13 +122,13 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
action := SecurityGroupRuleActionDrop
protocol := SecurityGroupRuleProtocolUDP
direction := SecurityGroupRuleDirectionOutbound

_, ipNet, _ := net.ParseCIDR("1.1.1.1/32")
updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{
Zone: zone,
SecurityGroupID: group.ID,
SecurityGroupRuleID: rule.ID,
Action: &action,
IPRange: scw.StringPtr("1.1.1.1/32"),
IPRange: &scw.IPNet{IPNet: *ipNet},
DestPortFrom: scw.Uint32Ptr(1),
DestPortTo: scw.Uint32Ptr(2048),
Protocol: &protocol,
Expand All @@ -136,7 +137,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {

testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, SecurityGroupRuleActionDrop, updateResponse.Rule.Action)
testhelpers.Equals(t, "1.1.1.1", updateResponse.Rule.IPRange)
testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IP{0x1, 0x1, 0x1, 0x1}, Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange)
testhelpers.Equals(t, scw.Uint32Ptr(1), updateResponse.Rule.DestPortFrom)
testhelpers.Equals(t, scw.Uint32Ptr(2048), updateResponse.Rule.DestPortTo)
testhelpers.Equals(t, SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol)
Expand All @@ -151,12 +152,13 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
protocol := SecurityGroupRuleProtocolUDP
direction := SecurityGroupRuleDirectionOutbound

_, ipNet, _ := net.ParseCIDR("1.1.1.1/32")
updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{
Zone: zone,
SecurityGroupID: group.ID,
SecurityGroupRuleID: rule.ID,
Action: &action,
IPRange: scw.StringPtr("1.1.1.1/32"),
IPRange: &scw.IPNet{IPNet: *ipNet},
DestPortFrom: scw.Uint32Ptr(22),
DestPortTo: scw.Uint32Ptr(22),
Protocol: &protocol,
Expand All @@ -165,7 +167,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {

testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, SecurityGroupRuleActionDrop, updateResponse.Rule.Action)
testhelpers.Equals(t, "1.1.1.1", updateResponse.Rule.IPRange)
testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IP{0x1, 0x1, 0x1, 0x1}, Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange)
testhelpers.Equals(t, uint32(22), *updateResponse.Rule.DestPortFrom)
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo)
testhelpers.Equals(t, SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol)
Expand All @@ -187,7 +189,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {

testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, SecurityGroupRuleActionAccept, updateResponse.Rule.Action)
testhelpers.Equals(t, "8.8.8.8", updateResponse.Rule.IPRange)
testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IP{0x8, 0x8, 0x8, 0x8}, Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange)
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortFrom)
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo)
testhelpers.Equals(t, SecurityGroupRuleProtocolICMP, updateResponse.Rule.Protocol)
Expand All @@ -208,7 +210,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {

testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, SecurityGroupRuleActionAccept, updateResponse.Rule.Action)
testhelpers.Equals(t, "8.8.8.8", updateResponse.Rule.IPRange)
testhelpers.Equals(t, scw.IPNet{IPNet: net.IPNet{IP: net.IP{0x8, 0x8, 0x8, 0x8}, Mask: net.IPMask{0xff, 0xff, 0xff, 0xff}}}, updateResponse.Rule.IPRange)
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortFrom)
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo)
testhelpers.Equals(t, SecurityGroupRuleProtocolTCP, updateResponse.Rule.Protocol)
Expand Down