Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/openstack: Allow subnets with no gateway #6060

Merged
merged 1 commit into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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