Ensure both GatewayIP and NoGateway cannot be set in subnets
diff --git a/acceptance/openstack/networking/v2/subnet_test.go b/acceptance/openstack/networking/v2/subnet_test.go
index 882ea67..e6a789e 100644
--- a/acceptance/openstack/networking/v2/subnet_test.go
+++ b/acceptance/openstack/networking/v2/subnet_test.go
@@ -118,6 +118,22 @@
t.Log("Delete subnet with no gateway")
res = subnets.Delete(Client, subnetID)
th.AssertNoErr(t, res.Err)
+
+ // Create subnet with invalid gateway configuration
+ t.Log("Create subnet with invalid gateway configuration")
+ opts = subnets.CreateOpts{
+ NetworkID: networkID,
+ CIDR: "192.168.199.0/24",
+ IPVersion: subnets.IPv4,
+ Name: "my_subnet",
+ EnableDHCP: &enable,
+ NoGateway: true,
+ GatewayIP: "192.168.199.1",
+ }
+ _, err = subnets.Create(Client, opts).Extract()
+ if err == nil {
+ t.Fatalf("Expected an error, got none")
+ }
}
func TestBatchCreate(t *testing.T) {
diff --git a/openstack/networking/v2/subnets/errors.go b/openstack/networking/v2/subnets/errors.go
index 0db0a6e..d2f7b46 100644
--- a/openstack/networking/v2/subnets/errors.go
+++ b/openstack/networking/v2/subnets/errors.go
@@ -7,7 +7,8 @@
}
var (
- errNetworkIDRequired = err("A network ID is required")
- errCIDRRequired = err("A valid CIDR is required")
- errInvalidIPType = err("An IP type must either be 4 or 6")
+ errNetworkIDRequired = err("A network ID is required")
+ errCIDRRequired = err("A valid CIDR is required")
+ errInvalidIPType = err("An IP type must either be 4 or 6")
+ errInvalidGatewayConfig = err("Both disabling the gateway and specifying a gateway is not allowed")
)
diff --git a/openstack/networking/v2/subnets/requests.go b/openstack/networking/v2/subnets/requests.go
index 1c7ef7b..8fa1e6d 100644
--- a/openstack/networking/v2/subnets/requests.go
+++ b/openstack/networking/v2/subnets/requests.go
@@ -129,6 +129,11 @@
return nil, errInvalidIPType
}
+ // Both GatewayIP and NoGateway should not be set
+ if opts.GatewayIP != "" && opts.NoGateway {
+ return nil, errInvalidGatewayConfig
+ }
+
s["network_id"] = opts.NetworkID
s["cidr"] = opts.CIDR
@@ -197,6 +202,11 @@
func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
s := make(map[string]interface{})
+ // Both GatewayIP and NoGateway should not be set
+ if opts.GatewayIP != "" && opts.NoGateway {
+ return nil, errInvalidGatewayConfig
+ }
+
if opts.EnableDHCP != nil {
s["enable_dhcp"] = &opts.EnableDHCP
}
diff --git a/openstack/networking/v2/subnets/requests_test.go b/openstack/networking/v2/subnets/requests_test.go
index 6c25d33..4a67a13 100644
--- a/openstack/networking/v2/subnets/requests_test.go
+++ b/openstack/networking/v2/subnets/requests_test.go
@@ -391,6 +391,60 @@
th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0c")
}
+func TestCreateInvalidGatewayConfig(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "subnet": {
+ "network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a23",
+ "ip_version": 4,
+ "cidr": "192.168.1.0/24",
+ "gateway_ip": "192.168.1.1",
+ "allocation_pools": [
+ {
+ "start": "192.168.1.2",
+ "end": "192.168.1.254"
+ }
+ ]
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ })
+
+ opts := CreateOpts{
+ NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23",
+ IPVersion: 4,
+ CIDR: "192.168.1.0/24",
+ NoGateway: true,
+ GatewayIP: "192.168.1.1",
+ AllocationPools: []AllocationPool{
+ AllocationPool{
+ Start: "192.168.1.2",
+ End: "192.168.1.254",
+ },
+ },
+ DNSNameservers: []string{},
+ }
+ _, err := Create(fake.ServiceClient(), opts).Extract()
+ if err == nil {
+ t.Fatalf("Expected an error, got none")
+ }
+
+ if err != errInvalidGatewayConfig {
+ t.Fatalf("Exected errInvalidGateway but got: %s", err)
+ }
+}
+
func TestRequiredCreateOpts(t *testing.T) {
res := Create(fake.ServiceClient(), CreateOpts{})
if res.Err == nil {