Skip to content

Commit

Permalink
Remove allocation_pools from networking_subnet_v2
Browse files Browse the repository at this point in the history
Deprecated allocation_pools is removed in favor of
allocation_pool
  • Loading branch information
nikParasyr committed Mar 5, 2024
1 parent c8048eb commit 43b1e09
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 108 deletions.
13 changes: 1 addition & 12 deletions docs/resources/networking_subnet_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ The following arguments are supported:
* `tenant_id` - (Optional) The owner of the subnet. Required if admin wants to
create a subnet for another tenant. Changing this creates a new subnet.

* `allocation_pools` - (**Deprecated** - use `allocation_pool` instead)
A block declaring the start and end range of the IP addresses available for
use with DHCP in this subnet.
The `allocation_pools` block is documented below.

* `allocation_pool` - (Optional) A block declaring the start and end range of
the IP addresses available for use with DHCP in this subnet. Multiple
`allocation_pool` blocks can be declared, providing the subnet with more
Expand Down Expand Up @@ -107,12 +102,6 @@ The following arguments are supported:

* `tags` - (Optional) A set of string tags for the subnet.

The deprecated `allocation_pools` block supports:

* `start` - (Required) The starting address.

* `end` - (Required) The ending address.

The `allocation_pool` block supports:

* `start` - (Required) The starting address.
Expand All @@ -136,7 +125,7 @@ The following attributes are exported:
* `name` - See Argument Reference above.
* `description` - See Argument Reference above.
* `tenant_id` - See Argument Reference above.
* `allocation_pools` - See Argument Reference above.
* `allocation_pool` - See Argument Reference above.
* `gateway_ip` - See Argument Reference above.
* `enable_dhcp` - See Argument Reference above.
* `dns_nameservers` - See Argument Reference above.
Expand Down
33 changes: 0 additions & 33 deletions openstack/networking_subnet_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
Expand Down Expand Up @@ -62,21 +61,6 @@ func networkingSubnetV2StateRefreshFuncDelete(networkingClient *gophercloud.Serv
}
}

// networkingSubnetV2GetRawAllocationPoolsValueToExpand selects the resource argument to populate
// the allocations pool value.
func networkingSubnetV2GetRawAllocationPoolsValueToExpand(d *schema.ResourceData) []interface{} {
// First check allocation_pool since that is the new argument.
result := d.Get("allocation_pool").(*schema.Set).List()

if len(result) == 0 {
// If no allocation_pool was specified, check allocation_pools
// which is the older legacy argument.
result = d.Get("allocation_pools").([]interface{})
}

return result
}

// expandNetworkingSubnetV2AllocationPools returns a slice of subnets.AllocationPool structs.
func expandNetworkingSubnetV2AllocationPools(allocationPools []interface{}) []subnets.AllocationPool {
result := make([]subnets.AllocationPool, len(allocationPools))
Expand Down Expand Up @@ -122,23 +106,6 @@ func expandNetworkingSubnetV2HostRoutes(rawHostRoutes []interface{}) []subnets.H
return result
}

func networkingSubnetV2AllocationPoolsCustomizeDiff(diff *schema.ResourceDiff) error {
if diff.Id() != "" && diff.HasChange("allocation_pools") {
o, n := diff.GetChange("allocation_pools")
oldPools := o.([]interface{})
newPools := n.([]interface{})

samePools := networkingSubnetV2AllocationPoolsMatch(oldPools, newPools)

if samePools {
log.Printf("[DEBUG] allocation_pools have not changed. clearing diff")
return diff.Clear("allocation_pools")
}
}

return nil
}

func networkingSubnetV2AllocationPoolsMatch(oldPools, newPools []interface{}) bool {
if len(oldPools) != len(newPools) {
return false
Expand Down
12 changes: 6 additions & 6 deletions openstack/resource_openstack_networking_port_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ resource "openstack_networking_subnet_v2" "vrrp_subnet" {
ip_version = 4
network_id = "${openstack_networking_network_v2.vrrp_network.id}"
allocation_pools {
allocation_pool {
start = "10.0.0.2"
end = "10.0.0.200"
}
Expand Down Expand Up @@ -1325,7 +1325,7 @@ resource "openstack_networking_subnet_v2" "vrrp_subnet" {
ip_version = 4
network_id = "${openstack_networking_network_v2.vrrp_network.id}"
allocation_pools {
allocation_pool {
start = "10.0.0.2"
end = "10.0.0.200"
}
Expand Down Expand Up @@ -1396,7 +1396,7 @@ resource "openstack_networking_subnet_v2" "vrrp_subnet" {
ip_version = 4
network_id = "${openstack_networking_network_v2.vrrp_network.id}"
allocation_pools {
allocation_pool {
start = "10.0.0.2"
end = "10.0.0.200"
}
Expand Down Expand Up @@ -1468,7 +1468,7 @@ resource "openstack_networking_subnet_v2" "vrrp_subnet" {
ip_version = 4
network_id = "${openstack_networking_network_v2.vrrp_network.id}"
allocation_pools {
allocation_pool {
start = "10.0.0.2"
end = "10.0.0.200"
}
Expand Down Expand Up @@ -1535,7 +1535,7 @@ resource "openstack_networking_subnet_v2" "vrrp_subnet" {
ip_version = 4
network_id = "${openstack_networking_network_v2.vrrp_network.id}"
allocation_pools {
allocation_pool {
start = "10.0.0.2"
end = "10.0.0.200"
}
Expand Down Expand Up @@ -2022,7 +2022,7 @@ resource "openstack_networking_subnet_v2" "vrrp_subnet" {
ip_version = 4
network_id = "${openstack_networking_network_v2.vrrp_network.id}"
allocation_pools {
allocation_pool {
start = "10.0.0.2"
end = "10.0.0.200"
}
Expand Down
45 changes: 5 additions & 40 deletions openstack/resource_openstack_networking_subnet_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -84,31 +83,10 @@ func resourceNetworkingSubnetV2() *schema.Resource {
Computed: true,
},

"allocation_pools": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ConflictsWith: []string{"allocation_pool"},
Deprecated: "use allocation_pool instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start": {
Type: schema.TypeString,
Required: true,
},
"end": {
Type: schema.TypeString,
Required: true,
},
},
},
},

"allocation_pool": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ConflictsWith: []string{"allocation_pools"},
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start": {
Expand Down Expand Up @@ -232,13 +210,6 @@ func resourceNetworkingSubnetV2() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
},

CustomizeDiff: customdiff.Sequence(
// Clear the diff if the old and new allocation_pools are the same.
func(ctx context.Context, diff *schema.ResourceDiff, v interface{}) error {
return networkingSubnetV2AllocationPoolsCustomizeDiff(diff)
},
),
}
}

Expand All @@ -255,7 +226,7 @@ func resourceNetworkingSubnetV2Create(ctx context.Context, d *schema.ResourceDat
}

// Get raw allocation pool value.
allocationPool := networkingSubnetV2GetRawAllocationPoolsValueToExpand(d)
allocationPool := d.Get("allocation_pool").(*schema.Set).List()

// Set basic options.
createOpts := SubnetCreateOpts{
Expand Down Expand Up @@ -380,11 +351,8 @@ func resourceNetworkingSubnetV2Read(ctx context.Context, d *schema.ResourceData,

networkingV2ReadAttributesTags(d, s.Tags)

// Set the allocation_pools, allocation_pool attributes.
// Set the allocation_pool attribute
allocationPools := flattenNetworkingSubnetV2AllocationPools(s.AllocationPools)
if err := d.Set("allocation_pools", allocationPools); err != nil {
log.Printf("[DEBUG] Unable to set openstack_networking_subnet_v2 allocation_pools: %s", err)
}
if err := d.Set("allocation_pool", allocationPools); err != nil {
log.Printf("[DEBUG] Unable to set openstack_networking_subnet_v2 allocation_pool: %s", err)
}
Expand Down Expand Up @@ -472,9 +440,6 @@ func resourceNetworkingSubnetV2Update(ctx context.Context, d *schema.ResourceDat
if d.HasChange("allocation_pool") {
hasChange = true
updateOpts.AllocationPools = expandNetworkingSubnetV2AllocationPools(d.Get("allocation_pool").(*schema.Set).List())
} else if d.HasChange("allocation_pools") {
hasChange = true
updateOpts.AllocationPools = expandNetworkingSubnetV2AllocationPools(d.Get("allocation_pools").([]interface{}))
}

if hasChange {
Expand Down
32 changes: 16 additions & 16 deletions openstack/resource_openstack_networking_subnet_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAccNetworkingV2Subnet_basic(t *testing.T) {
testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet),
testAccCheckNetworkingV2SubnetDNSConsistency("openstack_networking_subnet_v2.subnet_1", &subnet),
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "allocation_pools.0.start", "192.168.199.100"),
"openstack_networking_subnet_v2.subnet_1", "allocation_pool.0.start", "192.168.199.100"),
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "description", "my subnet description"),
),
Expand All @@ -42,7 +42,7 @@ func TestAccNetworkingV2Subnet_basic(t *testing.T) {
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "enable_dhcp", "true"),
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "allocation_pools.0.start", "192.168.199.150"),
"openstack_networking_subnet_v2.subnet_1", "allocation_pool.0.start", "192.168.199.150"),
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "description", ""),
),
Expand Down Expand Up @@ -245,21 +245,21 @@ func TestAccNetworkingV2Subnet_multipleAllocationPools(t *testing.T) {
Config: testAccNetworkingV2SubnetMultipleAllocationPools1,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "allocation_pools.#", "2"),
"openstack_networking_subnet_v2.subnet_1", "allocation_pool.#", "2"),
),
},
{
Config: testAccNetworkingV2SubnetMultipleAllocationPools2,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "allocation_pools.#", "2"),
"openstack_networking_subnet_v2.subnet_1", "allocation_pool.#", "2"),
),
},
{
Config: testAccNetworkingV2SubnetMultipleAllocationPools3,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"openstack_networking_subnet_v2.subnet_1", "allocation_pools.#", "2"),
"openstack_networking_subnet_v2.subnet_1", "allocation_pool.#", "2"),
),
},
},
Expand Down Expand Up @@ -455,7 +455,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
dns_nameservers = ["10.0.16.4", "213.186.33.99"]
allocation_pools {
allocation_pool {
start = "192.168.199.100"
end = "192.168.199.200"
}
Expand All @@ -476,7 +476,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
dns_nameservers = ["10.0.16.4", "213.186.33.99"]
allocation_pools {
allocation_pool {
start = "192.168.199.150"
end = "192.168.199.200"
}
Expand Down Expand Up @@ -548,7 +548,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = "192.168.199.0/24"
network_id = "${openstack_networking_network_v2.network_1.id}"
allocation_pools {
allocation_pool {
start = "192.168.199.100"
end = "192.168.199.200"
}
Expand Down Expand Up @@ -638,12 +638,12 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = "10.3.0.0/16"
network_id = "${openstack_networking_network_v2.network_1.id}"
allocation_pools {
allocation_pool {
start = "10.3.0.2"
end = "10.3.0.255"
}
allocation_pools {
allocation_pool {
start = "10.3.255.0"
end = "10.3.255.254"
}
Expand All @@ -661,12 +661,12 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = "10.3.0.0/16"
network_id = "${openstack_networking_network_v2.network_1.id}"
allocation_pools {
allocation_pool {
start = "10.3.255.0"
end = "10.3.255.254"
}
allocation_pools {
allocation_pool {
start = "10.3.0.2"
end = "10.3.0.255"
}
Expand All @@ -684,12 +684,12 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = "10.3.0.0/16"
network_id = "${openstack_networking_network_v2.network_1.id}"
allocation_pools {
allocation_pool {
start = "10.3.255.10"
end = "10.3.255.154"
}
allocation_pools {
allocation_pool {
start = "10.3.0.2"
end = "10.3.0.255"
}
Expand Down Expand Up @@ -754,7 +754,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
dns_nameservers = ["10.0.16.4", "213.186.33.99"]
allocation_pools {
allocation_pool {
start = "192.168.199.100"
end = "192.168.199.200"
}
Expand All @@ -772,7 +772,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = "192.168.199.0/24"
network_id = "${openstack_networking_network_v2.network_1.id}"
allocation_pools {
allocation_pool {
start = "192.168.199.100"
end = "192.168.199.200"
}
Expand Down
2 changes: 1 addition & 1 deletion openstack/resource_openstack_networking_tags_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
dns_nameservers = ["10.0.16.4", "213.186.33.99"]
allocation_pools {
allocation_pool {
start = "192.168.199.100"
end = "192.168.199.200"
}
Expand Down

0 comments on commit 43b1e09

Please sign in to comment.