Skip to content

Commit 35930a8

Browse files
[VPC] security group rule port range nil value in response (#538)
[VPC] security group rule port range nil value in response Need to get proper values also for null from API response for proper handling of ICMP ranges What this PR does / why we need it Which issue this PR fixes Special notes for your reviewer Reviewed-by: Artem Lifshits Reviewed-by: Vladimir Vshivkov
1 parent 46dc220 commit 35930a8

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

acceptance/openstack/networking/v2/sgs/sg_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,108 @@ import (
66

77
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
88
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
9+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
910
"github.com/opentelekomcloud/gophertelekomcloud/openstack/compute/v2/extensions/secgroups"
1011
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/security/rules"
1112
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
1213
)
1314

15+
func TestICMPSecurityGroupRules(t *testing.T) {
16+
clientNetworking, err := clients.NewNetworkV2Client()
17+
if err != nil {
18+
t.Fatalf("Unable to create a networking client: %v", err)
19+
}
20+
clientCompute, err := clients.NewComputeV2Client()
21+
if err != nil {
22+
t.Fatalf("Unable to create a networking client: %v", err)
23+
}
24+
25+
createSGOpts := secgroups.CreateOpts{
26+
Name: "sg-test-01",
27+
Description: "desc",
28+
}
29+
t.Logf("Attempting to create sg: %s", createSGOpts.Name)
30+
31+
sg, err := secgroups.Create(clientCompute, createSGOpts).Extract()
32+
th.AssertNoErr(t, err)
33+
34+
optsEchoReply := rules.CreateOpts{
35+
Description: "ICMP echo reply",
36+
SecGroupID: sg.ID,
37+
PortRangeMin: pointerto.Int(0),
38+
PortRangeMax: pointerto.Int(0),
39+
Direction: "ingress",
40+
EtherType: "IPv4",
41+
Protocol: "ICMP",
42+
}
43+
log.Print("[DEBUG] Create OpenTelekomCloud Neutron ICMP echo reply Security Group Rule")
44+
echoReply, err := rules.Create(clientNetworking, optsEchoReply).Extract()
45+
th.AssertNoErr(t, err)
46+
47+
getEchoReply, err := rules.Get(clientNetworking, echoReply.ID).Extract()
48+
th.AssertNoErr(t, err)
49+
th.AssertEquals(t, *getEchoReply.PortRangeMin, 0)
50+
th.AssertEquals(t, *getEchoReply.PortRangeMax, 0)
51+
52+
optsAll := rules.CreateOpts{
53+
Description: "ICMP all",
54+
SecGroupID: sg.ID,
55+
PortRangeMin: nil,
56+
PortRangeMax: nil,
57+
Direction: "ingress",
58+
EtherType: "IPv4",
59+
Protocol: "ICMP",
60+
}
61+
log.Print("[DEBUG] Create OpenTelekomCloud Neutron ICMP All Security Group Rule")
62+
all, err := rules.Create(clientNetworking, optsAll).Extract()
63+
th.AssertNoErr(t, err)
64+
65+
getAll, err := rules.Get(clientNetworking, all.ID).Extract()
66+
th.AssertNoErr(t, err)
67+
th.AssertEquals(t, getAll.PortRangeMin, (*int)(nil))
68+
th.AssertEquals(t, getAll.PortRangeMax, (*int)(nil))
69+
70+
optsEcho := rules.CreateOpts{
71+
Description: "ICMP echo",
72+
SecGroupID: sg.ID,
73+
PortRangeMin: pointerto.Int(8),
74+
PortRangeMax: pointerto.Int(0),
75+
Direction: "ingress",
76+
EtherType: "IPv4",
77+
Protocol: "ICMP",
78+
}
79+
log.Print("[DEBUG] Create OpenTelekomCloud Neutron ICMP Echo Security Group Rule")
80+
echo, err := rules.Create(clientNetworking, optsEcho).Extract()
81+
th.AssertNoErr(t, err)
82+
83+
getEcho, err := rules.Get(clientNetworking, echo.ID).Extract()
84+
th.AssertNoErr(t, err)
85+
th.AssertEquals(t, *getEcho.PortRangeMin, 8)
86+
th.AssertEquals(t, *getEcho.PortRangeMax, 0)
87+
88+
optsFragment := rules.CreateOpts{
89+
Description: "ICMP Fragment need DF set",
90+
SecGroupID: sg.ID,
91+
PortRangeMin: pointerto.Int(3),
92+
PortRangeMax: pointerto.Int(4),
93+
Direction: "ingress",
94+
EtherType: "IPv4",
95+
Protocol: "ICMP",
96+
}
97+
log.Print("[DEBUG] Create OpenTelekomCloud Neutron ICMP Fragment need DF set Security Group Rule")
98+
fragment, err := rules.Create(clientNetworking, optsFragment).Extract()
99+
th.AssertNoErr(t, err)
100+
101+
getFragment, err := rules.Get(clientNetworking, fragment.ID).Extract()
102+
th.AssertNoErr(t, err)
103+
th.AssertEquals(t, *getFragment.PortRangeMin, 3)
104+
th.AssertEquals(t, *getFragment.PortRangeMax, 4)
105+
106+
t.Cleanup(func() {
107+
secgroups.Delete(clientCompute, sg.ID)
108+
})
109+
}
110+
14111
func TestThrottlingSgs(t *testing.T) {
15112
t.Skip("please run only manually, long test")
16113
clientNetworking, err := clients.NewNetworkV2Client()

openstack/networking/v2/extensions/security/rules/results.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ type SecGroupRule struct {
3131
// rule. If the protocol is TCP or UDP, this value must be less than or equal
3232
// to the value of the PortRangeMax attribute. If the protocol is ICMP, this
3333
// value must be an ICMP type.
34-
PortRangeMin int `json:"port_range_min"`
34+
PortRangeMin *int `json:"port_range_min"`
3535

3636
// The maximum port number in the range that is matched by the security group
3737
// rule. The PortRangeMin attribute constrains the PortRangeMax attribute. If
3838
// the protocol is ICMP, this value must be an ICMP type.
39-
PortRangeMax int `json:"port_range_max"`
39+
PortRangeMax *int `json:"port_range_max"`
4040

4141
// The protocol that is matched by the security group rule. Valid values are
4242
// "tcp", "udp", "icmp" or an empty string.

openstack/networking/v2/extensions/security/rules/testing/requests_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"testing"
77

8+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
89
fake "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/common"
910
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/security/rules"
1011
"github.com/opentelekomcloud/gophertelekomcloud/pagination"
@@ -29,8 +30,8 @@ func TestList(t *testing.T) {
2930
"direction": "egress",
3031
"ethertype": "IPv6",
3132
"id": "3c0e45ff-adaf-4124-b083-bf390e5482ff",
32-
"port_range_max": null,
33-
"port_range_min": null,
33+
"port_range_max": 0,
34+
"port_range_min": 0,
3435
"protocol": null,
3536
"remote_group_id": null,
3637
"remote_ip_prefix": null,
@@ -69,8 +70,8 @@ func TestList(t *testing.T) {
6970
Direction: "egress",
7071
EtherType: "IPv6",
7172
ID: "3c0e45ff-adaf-4124-b083-bf390e5482ff",
72-
PortRangeMax: 0,
73-
PortRangeMin: 0,
73+
PortRangeMax: pointerto.Int(0),
74+
PortRangeMin: pointerto.Int(0),
7475
Protocol: "",
7576
RemoteGroupID: "",
7677
RemoteIPPrefix: "",
@@ -81,8 +82,8 @@ func TestList(t *testing.T) {
8182
Direction: "egress",
8283
EtherType: "IPv4",
8384
ID: "93aa42e5-80db-4581-9391-3a608bd0e448",
84-
PortRangeMax: 0,
85-
PortRangeMin: 0,
85+
PortRangeMax: (*int)(nil),
86+
PortRangeMin: (*int)(nil),
8687
Protocol: "",
8788
RemoteGroupID: "",
8889
RemoteIPPrefix: "",
@@ -214,8 +215,8 @@ func TestGet(t *testing.T) {
214215
th.AssertEquals(t, "egress", sr.Direction)
215216
th.AssertEquals(t, "IPv6", sr.EtherType)
216217
th.AssertEquals(t, "3c0e45ff-adaf-4124-b083-bf390e5482ff", sr.ID)
217-
th.AssertEquals(t, 0, sr.PortRangeMax)
218-
th.AssertEquals(t, 0, sr.PortRangeMin)
218+
th.AssertEquals(t, (*int)(nil), sr.PortRangeMax)
219+
th.AssertEquals(t, (*int)(nil), sr.PortRangeMin)
219220
th.AssertEquals(t, "", sr.Protocol)
220221
th.AssertEquals(t, "", sr.RemoteGroupID)
221222
th.AssertEquals(t, "", sr.RemoteIPPrefix)

0 commit comments

Comments
 (0)