Skip to content

Commit

Permalink
Merge pull request #6060 from jtopjian/openstack-subnet-no-gateway
Browse files Browse the repository at this point in the history
provider/openstack: Allow subnets with no gateway
  • Loading branch information
jtopjian committed Apr 8, 2016
2 parents 1889ab7 + 28f98c3 commit cd4e5c8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand All @@ -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),
Expand Down Expand Up @@ -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") {
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
}`)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd4e5c8

Please sign in to comment.