From 28f98c370134cf46ed3516e5f266a8041e259ba3 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Fri, 8 Apr 2016 03:12:49 +0000 Subject: [PATCH] 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 --- ...resource_openstack_networking_subnet_v2.go | 23 +++++++ ...rce_openstack_networking_subnet_v2_test.go | 61 +++++++++++++++++++ .../r/networking_subnet_v2.html.markdown | 7 ++- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go b/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go index cef09641b..c3ca944f4 100644 --- a/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go +++ b/builtin/providers/openstack/resource_openstack_networking_subnet_v2.go @@ -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) } diff --git a/builtin/providers/openstack/resource_openstack_networking_subnet_v2_test.go b/builtin/providers/openstack/resource_openstack_networking_subnet_v2_test.go index 1931d80eb..769a12c5f 100644 --- a/builtin/providers/openstack/resource_openstack_networking_subnet_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_networking_subnet_v2_test.go @@ -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" + }`) diff --git a/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown b/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown index 3003cb2c4..a735454a1 100644 --- a/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown +++ b/website/source/docs/providers/openstack/r/networking_subnet_v2.html.markdown @@ -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