provider/openstack: Allow subnets with no gateway

This commit adds a no_gateway attribute. When set, the subnet will
not have a gateway. This is different than not specifying a
gateway_ip since that will cause a default gateway of .1 to be used.
This behavior mirrors the OpenStack Neutron command-line tool.

Fixes #6031
This commit is contained in:
Joe Topjian 2016-04-08 03:12:49 +00:00
parent 1889ab7bb3
commit 28f98c3701
3 changed files with 90 additions and 1 deletions

View File

@ -70,6 +70,11 @@ func resourceNetworkingSubnetV2() *schema.Resource {
ForceNew: false,
Computed: true,
},
"no_gateway": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: false,
},
"ip_version": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
@ -117,6 +122,12 @@ func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
if _, ok := d.GetOk("gateway_ip"); ok {
if _, ok2 := d.GetOk("no_gateway"); ok2 {
return fmt.Errorf("Both gateway_ip and no_gateway cannot be set.")
}
}
enableDHCP := d.Get("enable_dhcp").(bool)
createOpts := subnets.CreateOpts{
@ -126,6 +137,7 @@ func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{})
TenantID: d.Get("tenant_id").(string),
AllocationPools: resourceSubnetAllocationPoolsV2(d),
GatewayIP: d.Get("gateway_ip").(string),
NoGateway: d.Get("no_gateway").(bool),
IPVersion: d.Get("ip_version").(int),
DNSNameservers: resourceSubnetDNSNameserversV2(d),
HostRoutes: resourceSubnetHostRoutesV2(d),
@ -190,6 +202,13 @@ func resourceNetworkingSubnetV2Update(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
// Check if both gateway_ip and no_gateway are set
if _, ok := d.GetOk("gateway_ip"); ok {
if _, ok2 := d.GetOk("no_gateway"); ok2 {
return fmt.Errorf("Both gateway_ip and no_gateway cannot be set.")
}
}
var updateOpts subnets.UpdateOpts
if d.HasChange("name") {
@ -200,6 +219,10 @@ func resourceNetworkingSubnetV2Update(d *schema.ResourceData, meta interface{})
updateOpts.GatewayIP = d.Get("gateway_ip").(string)
}
if d.HasChange("no_gateway") {
updateOpts.NoGateway = d.Get("no_gateway").(bool)
}
if d.HasChange("dns_nameservers") {
updateOpts.DNSNameservers = resourceSubnetDNSNameserversV2(d)
}

View File

@ -55,6 +55,44 @@ func TestAccNetworkingV2Subnet_enableDHCP(t *testing.T) {
})
}
func TestAccNetworkingV2Subnet_noGateway(t *testing.T) {
var subnet subnets.Subnet
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2SubnetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNetworkingV2Subnet_noGateway,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "gateway_ip", ""),
),
},
},
})
}
func TestAccNetworkingV2Subnet_impliedGateway(t *testing.T) {
var subnet subnets.Subnet
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2SubnetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNetworkingV2Subnet_impliedGateway,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
resource.TestCheckResourceAttr("openstack_networking_subnet_v2.subnet_1", "gateway_ip", "192.168.199.1"),
),
},
},
})
}
func testAccCheckNetworkingV2SubnetDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
@ -145,3 +183,26 @@ var testAccNetworkingV2Subnet_enableDHCP = fmt.Sprintf(`
gateway_ip = "192.168.199.1"
enable_dhcp = true
}`)
var testAccNetworkingV2Subnet_noGateway = fmt.Sprintf(`
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
name = "tf-test-subnet"
network_id = "${openstack_networking_network_v2.network_1.id}"
cidr = "192.168.199.0/24"
no_gateway = true
}`)
var testAccNetworkingV2Subnet_impliedGateway = fmt.Sprintf(`
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
name = "tf-test-subnet"
network_id = "${openstack_networking_network_v2.network_1.id}"
cidr = "192.168.199.0/24"
}`)

View File

@ -53,7 +53,12 @@ The following arguments are supported:
documented below. Changing this creates a new subnet.
* `gateway_ip` - (Optional) Default gateway used by devices in this subnet.
Changing this updates the gateway IP of the existing subnet.
Leaving this blank and not setting `no_gateway` will cause a default
gateway of `.1` to be used. Changing this updates the gateway IP of the
existing subnet.
* `no_gateway` - (Optional) Do not set a gateway IP on this subnet. Changing
this removes or adds a default gateway IP of the existing subnet.
* `enable_dhcp` - (Optional) The administrative state of the network.
Acceptable values are "true" and "false". Changing this value enables or