Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit f3d0534

Browse files
committed
Merge pull request #522 from deniszh/master
[rfr] From Port and To Port should accept values of 0
2 parents 285a961 + 6b88f18 commit f3d0534

File tree

7 files changed

+151
-4
lines changed

7 files changed

+151
-4
lines changed

acceptance/openstack/compute/v2/secgroup_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ func addRemoveRules(t *testing.T, client *gophercloud.ServiceClient, id string)
107107
th.AssertNoErr(t, err)
108108

109109
t.Logf("Deleted rule %s from group %s", rule.ID, id)
110+
111+
icmpOpts := secgroups.CreateRuleOpts{
112+
ParentGroupID: id,
113+
FromPort: 0,
114+
ToPort: 0,
115+
IPProtocol: "ICMP",
116+
CIDR: "0.0.0.0/0",
117+
}
118+
119+
icmpRule, err := secgroups.CreateRule(client, icmpOpts).Extract()
120+
th.AssertNoErr(t, err)
121+
122+
t.Logf("Adding ICMP rule %s to group %s", icmpRule.ID, id)
123+
124+
err = secgroups.DeleteRule(client, icmpRule.ID).ExtractErr()
125+
th.AssertNoErr(t, err)
126+
127+
t.Logf("Deleted ICMP rule %s from group %s", icmpRule.ID, id)
110128
}
111129

112130
func findServer(t *testing.T, client *gophercloud.ServiceClient) (string, bool) {

openstack/compute/v2/extensions/defsecrules/fixtures.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,41 @@ func mockCreateRuleResponse(t *testing.T) {
7272
})
7373
}
7474

75+
func mockCreateRuleResponseICMPZero(t *testing.T) {
76+
th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
77+
th.TestMethod(t, r, "POST")
78+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
79+
80+
th.TestJSONRequest(t, r, `
81+
{
82+
"security_group_default_rule": {
83+
"ip_protocol": "ICMP",
84+
"from_port": 0,
85+
"to_port": 0,
86+
"cidr": "10.10.12.0/24"
87+
}
88+
}
89+
`)
90+
91+
w.Header().Add("Content-Type", "application/json")
92+
w.WriteHeader(http.StatusOK)
93+
94+
fmt.Fprintf(w, `
95+
{
96+
"security_group_default_rule": {
97+
"from_port": 0,
98+
"id": "{ruleID}",
99+
"ip_protocol": "ICMP",
100+
"ip_range": {
101+
"cidr": "10.10.12.0/24"
102+
},
103+
"to_port": 0
104+
}
105+
}
106+
`)
107+
})
108+
}
109+
75110
func mockGetRuleResponse(t *testing.T, ruleID string) {
76111
url := rootPath + "/" + ruleID
77112
th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {

openstack/compute/v2/extensions/defsecrules/requests.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package defsecrules
22

33
import (
44
"errors"
5+
"strings"
56

67
"github.com/rackspace/gophercloud"
78
"github.com/rackspace/gophercloud/pagination"
@@ -42,10 +43,10 @@ type CreateOptsBuilder interface {
4243
func (opts CreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
4344
rule := make(map[string]interface{})
4445

45-
if opts.FromPort == 0 {
46+
if opts.FromPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
4647
return rule, errors.New("A FromPort must be set")
4748
}
48-
if opts.ToPort == 0 {
49+
if opts.ToPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
4950
return rule, errors.New("A ToPort must be set")
5051
}
5152
if opts.IPProtocol == "" {

openstack/compute/v2/extensions/defsecrules/requests_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ func TestCreate(t *testing.T) {
6969
th.AssertDeepEquals(t, expected, group)
7070
}
7171

72+
func TestCreateICMPZero(t *testing.T) {
73+
th.SetupHTTP()
74+
defer th.TeardownHTTP()
75+
76+
mockCreateRuleResponseICMPZero(t)
77+
78+
opts := CreateOpts{
79+
IPProtocol: "ICMP",
80+
FromPort: 0,
81+
ToPort: 0,
82+
CIDR: "10.10.12.0/24",
83+
}
84+
85+
group, err := Create(client.ServiceClient(), opts).Extract()
86+
th.AssertNoErr(t, err)
87+
88+
expected := &DefaultRule{
89+
ID: ruleID,
90+
FromPort: 0,
91+
ToPort: 0,
92+
IPProtocol: "ICMP",
93+
IPRange: secgroups.IPRange{CIDR: "10.10.12.0/24"},
94+
}
95+
th.AssertDeepEquals(t, expected, group)
96+
}
97+
7298
func TestGet(t *testing.T) {
7399
th.SetupHTTP()
74100
defer th.TeardownHTTP()

openstack/compute/v2/extensions/secgroups/fixtures.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,42 @@ func mockAddRuleResponse(t *testing.T) {
216216
})
217217
}
218218

219+
func mockAddRuleResponseICMPZero(t *testing.T) {
220+
th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) {
221+
th.TestMethod(t, r, "POST")
222+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
223+
224+
th.TestJSONRequest(t, r, `
225+
{
226+
"security_group_rule": {
227+
"from_port": 0,
228+
"ip_protocol": "ICMP",
229+
"to_port": 0,
230+
"parent_group_id": "{groupID}",
231+
"cidr": "0.0.0.0/0"
232+
}
233+
} `)
234+
235+
w.Header().Add("Content-Type", "application/json")
236+
w.WriteHeader(http.StatusOK)
237+
238+
fmt.Fprintf(w, `
239+
{
240+
"security_group_rule": {
241+
"from_port": 0,
242+
"group": {},
243+
"ip_protocol": "ICMP",
244+
"to_port": 0,
245+
"parent_group_id": "{groupID}",
246+
"ip_range": {
247+
"cidr": "0.0.0.0/0"
248+
},
249+
"id": "{ruleID}"
250+
}
251+
}`)
252+
})
253+
}
254+
219255
func mockDeleteRuleResponse(t *testing.T, ruleID string) {
220256
url := fmt.Sprintf("/os-security-group-rules/%s", ruleID)
221257
th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {

openstack/compute/v2/extensions/secgroups/requests.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package secgroups
22

33
import (
44
"errors"
5+
"strings"
56

67
"github.com/rackspace/gophercloud"
78
"github.com/rackspace/gophercloud/pagination"
@@ -181,10 +182,10 @@ func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) {
181182
if opts.ParentGroupID == "" {
182183
return rule, errors.New("A ParentGroupID must be set")
183184
}
184-
if opts.FromPort == 0 {
185+
if opts.FromPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
185186
return rule, errors.New("A FromPort must be set")
186187
}
187-
if opts.ToPort == 0 {
188+
if opts.ToPort == 0 && strings.ToUpper(opts.IPProtocol) != "ICMP" {
188189
return rule, errors.New("A ToPort must be set")
189190
}
190191
if opts.IPProtocol == "" {

openstack/compute/v2/extensions/secgroups/requests_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,36 @@ func TestAddRule(t *testing.T) {
217217
th.AssertDeepEquals(t, expected, rule)
218218
}
219219

220+
func TestAddRuleICMPZero(t *testing.T) {
221+
th.SetupHTTP()
222+
defer th.TeardownHTTP()
223+
224+
mockAddRuleResponseICMPZero(t)
225+
226+
opts := CreateRuleOpts{
227+
ParentGroupID: groupID,
228+
FromPort: 0,
229+
ToPort: 0,
230+
IPProtocol: "ICMP",
231+
CIDR: "0.0.0.0/0",
232+
}
233+
234+
rule, err := CreateRule(client.ServiceClient(), opts).Extract()
235+
th.AssertNoErr(t, err)
236+
237+
expected := &Rule{
238+
FromPort: 0,
239+
ToPort: 0,
240+
Group: Group{},
241+
IPProtocol: "ICMP",
242+
ParentGroupID: groupID,
243+
IPRange: IPRange{CIDR: "0.0.0.0/0"},
244+
ID: ruleID,
245+
}
246+
247+
th.AssertDeepEquals(t, expected, rule)
248+
}
249+
220250
func TestDeleteRule(t *testing.T) {
221251
th.SetupHTTP()
222252
defer th.TeardownHTTP()

0 commit comments

Comments
 (0)