Skip to content

Commit 937624f

Browse files
fix(instance): use IPNet type for security group rule ip_range (#240)
1 parent 95bfa30 commit 937624f

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

api/instance/v1/instance_sdk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ type SecurityGroupRule struct {
920920
// Default value: accept
921921
Action SecurityGroupRuleAction `json:"action"`
922922

923-
IPRange string `json:"ip_range"`
923+
IPRange scw.IPNet `json:"ip_range"`
924924

925925
DestPortFrom *uint32 `json:"dest_port_from"`
926926

@@ -3111,7 +3111,7 @@ type CreateSecurityGroupRuleRequest struct {
31113111
// Default value: accept
31123112
Action SecurityGroupRuleAction `json:"action"`
31133113

3114-
IPRange string `json:"ip_range,omitempty"`
3114+
IPRange scw.IPNet `json:"ip_range,omitempty"`
31153115

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

@@ -3270,7 +3270,7 @@ type setSecurityGroupRuleRequest struct {
32703270
// Default value: accept
32713271
Action SecurityGroupRuleAction `json:"action"`
32723272

3273-
IPRange string `json:"ip_range"`
3273+
IPRange scw.IPNet `json:"ip_range"`
32743274

32753275
DestPortFrom *uint32 `json:"dest_port_from"`
32763276

api/instance/v1/security_group_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ type UpdateSecurityGroupRuleRequest struct {
100100
Protocol *SecurityGroupRuleProtocol `json:"protocol"`
101101
Direction *SecurityGroupRuleDirection `json:"direction"`
102102
Action *SecurityGroupRuleAction `json:"action"`
103-
IPRange *string `json:"ip_range"`
103+
IPRange *scw.IPNet `json:"ip_range"`
104104
Position *uint32 `json:"position"`
105105

106106
// If set to 0, DestPortFrom will be removed.

api/instance/v1/security_group_utils_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package instance
22

33
import (
4+
"net"
45
"testing"
56

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

9394
testhelpers.AssertNoError(t, err)
94-
95+
_, ipNet, _ := net.ParseCIDR("8.8.8.8/32")
9596
createRuleResponse, err := instanceAPI.CreateSecurityGroupRule(&CreateSecurityGroupRuleRequest{
9697
Zone: zone,
9798
SecurityGroupID: createSecurityGroupResponse.SecurityGroup.ID,
9899
Direction: SecurityGroupRuleDirectionInbound,
99100
Protocol: SecurityGroupRuleProtocolTCP,
100101
DestPortFrom: scw.Uint32Ptr(1),
101102
DestPortTo: scw.Uint32Ptr(1024),
102-
IPRange: "8.8.8.8/32",
103+
IPRange: scw.IPNet{IPNet: *ipNet},
103104
Action: SecurityGroupRuleActionAccept,
104105
Position: 1,
105106
})
@@ -121,13 +122,13 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
121122
action := SecurityGroupRuleActionDrop
122123
protocol := SecurityGroupRuleProtocolUDP
123124
direction := SecurityGroupRuleDirectionOutbound
124-
125+
_, ipNet, _ := net.ParseCIDR("1.1.1.1/32")
125126
updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{
126127
Zone: zone,
127128
SecurityGroupID: group.ID,
128129
SecurityGroupRuleID: rule.ID,
129130
Action: &action,
130-
IPRange: scw.StringPtr("1.1.1.1/32"),
131+
IPRange: &scw.IPNet{IPNet: *ipNet},
131132
DestPortFrom: scw.Uint32Ptr(1),
132133
DestPortTo: scw.Uint32Ptr(2048),
133134
Protocol: &protocol,
@@ -136,7 +137,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
136137

137138
testhelpers.AssertNoError(t, err)
138139
testhelpers.Equals(t, SecurityGroupRuleActionDrop, updateResponse.Rule.Action)
139-
testhelpers.Equals(t, "1.1.1.1", updateResponse.Rule.IPRange)
140+
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)
140141
testhelpers.Equals(t, scw.Uint32Ptr(1), updateResponse.Rule.DestPortFrom)
141142
testhelpers.Equals(t, scw.Uint32Ptr(2048), updateResponse.Rule.DestPortTo)
142143
testhelpers.Equals(t, SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol)
@@ -151,12 +152,13 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
151152
protocol := SecurityGroupRuleProtocolUDP
152153
direction := SecurityGroupRuleDirectionOutbound
153154

155+
_, ipNet, _ := net.ParseCIDR("1.1.1.1/32")
154156
updateResponse, err := instanceAPI.UpdateSecurityGroupRule(&UpdateSecurityGroupRuleRequest{
155157
Zone: zone,
156158
SecurityGroupID: group.ID,
157159
SecurityGroupRuleID: rule.ID,
158160
Action: &action,
159-
IPRange: scw.StringPtr("1.1.1.1/32"),
161+
IPRange: &scw.IPNet{IPNet: *ipNet},
160162
DestPortFrom: scw.Uint32Ptr(22),
161163
DestPortTo: scw.Uint32Ptr(22),
162164
Protocol: &protocol,
@@ -165,7 +167,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
165167

166168
testhelpers.AssertNoError(t, err)
167169
testhelpers.Equals(t, SecurityGroupRuleActionDrop, updateResponse.Rule.Action)
168-
testhelpers.Equals(t, "1.1.1.1", updateResponse.Rule.IPRange)
170+
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)
169171
testhelpers.Equals(t, uint32(22), *updateResponse.Rule.DestPortFrom)
170172
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo)
171173
testhelpers.Equals(t, SecurityGroupRuleProtocolUDP, updateResponse.Rule.Protocol)
@@ -187,7 +189,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
187189

188190
testhelpers.AssertNoError(t, err)
189191
testhelpers.Equals(t, SecurityGroupRuleActionAccept, updateResponse.Rule.Action)
190-
testhelpers.Equals(t, "8.8.8.8", updateResponse.Rule.IPRange)
192+
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)
191193
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortFrom)
192194
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo)
193195
testhelpers.Equals(t, SecurityGroupRuleProtocolICMP, updateResponse.Rule.Protocol)
@@ -208,7 +210,7 @@ func TestAPI_UpdateSecurityGroupRule(t *testing.T) {
208210

209211
testhelpers.AssertNoError(t, err)
210212
testhelpers.Equals(t, SecurityGroupRuleActionAccept, updateResponse.Rule.Action)
211-
testhelpers.Equals(t, "8.8.8.8", updateResponse.Rule.IPRange)
213+
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)
212214
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortFrom)
213215
testhelpers.Equals(t, (*uint32)(nil), updateResponse.Rule.DestPortTo)
214216
testhelpers.Equals(t, SecurityGroupRuleProtocolTCP, updateResponse.Rule.Protocol)

0 commit comments

Comments
 (0)