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

Commit 2524d11

Browse files
committed
Ensure both GatewayIP and NoGateway cannot be set in subnets
1 parent f92ae6c commit 2524d11

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

acceptance/openstack/networking/v2/subnet_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ func TestSubnetCRUD(t *testing.T) {
118118
t.Log("Delete subnet with no gateway")
119119
res = subnets.Delete(Client, subnetID)
120120
th.AssertNoErr(t, res.Err)
121+
122+
// Create subnet with invalid gateway configuration
123+
t.Log("Create subnet with invalid gateway configuration")
124+
opts = subnets.CreateOpts{
125+
NetworkID: networkID,
126+
CIDR: "192.168.199.0/24",
127+
IPVersion: subnets.IPv4,
128+
Name: "my_subnet",
129+
EnableDHCP: &enable,
130+
NoGateway: true,
131+
GatewayIP: "192.168.199.1",
132+
}
133+
_, err = subnets.Create(Client, opts).Extract()
134+
if err == nil {
135+
t.Fatalf("Expected an error, got none")
136+
}
121137
}
122138

123139
func TestBatchCreate(t *testing.T) {

openstack/networking/v2/subnets/errors.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ func err(str string) error {
77
}
88

99
var (
10-
errNetworkIDRequired = err("A network ID is required")
11-
errCIDRRequired = err("A valid CIDR is required")
12-
errInvalidIPType = err("An IP type must either be 4 or 6")
10+
errNetworkIDRequired = err("A network ID is required")
11+
errCIDRRequired = err("A valid CIDR is required")
12+
errInvalidIPType = err("An IP type must either be 4 or 6")
13+
errInvalidGatewayConfig = err("Both disabling the gateway and specifying a gateway is not allowed")
1314
)

openstack/networking/v2/subnets/requests.go

+10
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
129129
return nil, errInvalidIPType
130130
}
131131

132+
// Both GatewayIP and NoGateway should not be set
133+
if opts.GatewayIP != "" && opts.NoGateway {
134+
return nil, errInvalidGatewayConfig
135+
}
136+
132137
s["network_id"] = opts.NetworkID
133138
s["cidr"] = opts.CIDR
134139

@@ -197,6 +202,11 @@ type UpdateOpts struct {
197202
func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
198203
s := make(map[string]interface{})
199204

205+
// Both GatewayIP and NoGateway should not be set
206+
if opts.GatewayIP != "" && opts.NoGateway {
207+
return nil, errInvalidGatewayConfig
208+
}
209+
200210
if opts.EnableDHCP != nil {
201211
s["enable_dhcp"] = &opts.EnableDHCP
202212
}

openstack/networking/v2/subnets/requests_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,60 @@ func TestCreateNoGateway(t *testing.T) {
391391
th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0c")
392392
}
393393

394+
func TestCreateInvalidGatewayConfig(t *testing.T) {
395+
th.SetupHTTP()
396+
defer th.TeardownHTTP()
397+
398+
th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
399+
th.TestMethod(t, r, "POST")
400+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
401+
th.TestHeader(t, r, "Content-Type", "application/json")
402+
th.TestHeader(t, r, "Accept", "application/json")
403+
th.TestJSONRequest(t, r, `
404+
{
405+
"subnet": {
406+
"network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
407+
"ip_version": 4,
408+
"cidr": "192.168.1.0/24",
409+
"gateway_ip": "192.168.1.1",
410+
"allocation_pools": [
411+
{
412+
"start": "192.168.1.2",
413+
"end": "192.168.1.254"
414+
}
415+
]
416+
}
417+
}
418+
`)
419+
420+
w.Header().Add("Content-Type", "application/json")
421+
w.WriteHeader(http.StatusCreated)
422+
})
423+
424+
opts := CreateOpts{
425+
NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
426+
IPVersion: 4,
427+
CIDR: "192.168.1.0/24",
428+
NoGateway: true,
429+
GatewayIP: "192.168.1.1",
430+
AllocationPools: []AllocationPool{
431+
AllocationPool{
432+
Start: "192.168.1.2",
433+
End: "192.168.1.254",
434+
},
435+
},
436+
DNSNameservers: []string{},
437+
}
438+
_, err := Create(fake.ServiceClient(), opts).Extract()
439+
if err == nil {
440+
t.Fatalf("Expected an error, got none")
441+
}
442+
443+
if err != errInvalidGatewayConfig {
444+
t.Fatalf("Exected errInvalidGateway but got: %s", err)
445+
}
446+
}
447+
394448
func TestRequiredCreateOpts(t *testing.T) {
395449
res := Create(fake.ServiceClient(), CreateOpts{})
396450
if res.Err == nil {

0 commit comments

Comments
 (0)