Skip to content

Commit

Permalink
Add tags support for Neutron subnets
Browse files Browse the repository at this point in the history
This allows us to set/get tags for subnets via terraform templates,
and requires the recently added changes to gophercloud that enable
the relevant features of the Neutron APIs to be accessed.

Issue: terraform-provider-openstack#453
  • Loading branch information
Steven Hardy committed Oct 24, 2018
1 parent 52a2efc commit de64654
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
27 changes: 27 additions & 0 deletions openstack/resource_openstack_networking_subnet_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
)

Expand Down Expand Up @@ -146,6 +147,11 @@ func resourceNetworkingSubnetV2() *schema.Resource {
Optional: true,
ForceNew: true,
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand Down Expand Up @@ -219,6 +225,16 @@ func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{})

d.SetId(s.ID)

tags := networkV2AttributesTags(d)
if len(tags) > 0 {
tagOpts := attributestags.ReplaceAllOpts{Tags: tags}
tags, err := attributestags.ReplaceAll(networkingClient, "subnets", s.ID, tagOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating Tags on Subnet: %s", err)
}
log.Printf("[DEBUG] Set Tags = %+v on Subnet %+v", tags, s.ID)
}

log.Printf("[DEBUG] Created Subnet %s: %#v", s.ID, s)
return resourceNetworkingSubnetV2Read(d, meta)
}
Expand Down Expand Up @@ -249,6 +265,7 @@ func resourceNetworkingSubnetV2Read(d *schema.ResourceData, meta interface{}) er
d.Set("ipv6_address_mode", s.IPv6AddressMode)
d.Set("ipv6_ra_mode", s.IPv6RAMode)
d.Set("subnetpool_id", s.SubnetPoolID)
d.Set("tags", s.Tags)

// Set the allocation_pools
var allocationPools []map[string]interface{}
Expand Down Expand Up @@ -333,6 +350,16 @@ func resourceNetworkingSubnetV2Update(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error updating OpenStack Neutron Subnet: %s", err)
}

if d.HasChange("tags") {
tags := networkV2AttributesTags(d)
tagOpts := attributestags.ReplaceAllOpts{Tags: tags}
tags, err := attributestags.ReplaceAll(networkingClient, "subnets", d.Id(), tagOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating Tags on Subnet: %s", err)
}
log.Printf("[DEBUG] Updated Tags = %+v on Subnet %+v", tags, d.Id())
}

return resourceNetworkingSubnetV2Read(d, meta)
}

Expand Down
67 changes: 67 additions & 0 deletions openstack/resource_openstack_networking_subnet_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ func TestAccNetworkingV2Subnet_subnetPoolNoCIDR(t *testing.T) {
})
}

func TestAccNetworkingV2Subnet_tags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2SubnetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNetworkingV2Subnet_tags,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2Tags(
"openstack_networking_subnet_v2.subnet_1",
[]string{"a", "b", "c"}),
),
},
resource.TestStep{
Config: testAccNetworkingV2Subnet_tags_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2Tags(
"openstack_networking_subnet_v2.subnet_1",
[]string{"a", "b", "c", "d"}),
),
},
},
})
}

func testAccCheckNetworkingV2SubnetDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
Expand Down Expand Up @@ -291,6 +317,47 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
}
}
`
const testAccNetworkingV2Subnet_tags = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = "192.168.199.0/24"
network_id = "${openstack_networking_network_v2.network_1.id}"
dns_nameservers = ["10.0.16.4", "213.186.33.99"]
allocation_pools {
start = "192.168.199.100"
end = "192.168.199.200"
}
tags = ["a", "b", "c"]
}
`

const testAccNetworkingV2Subnet_tags_update = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
cidr = "192.168.199.0/24"
network_id = "${openstack_networking_network_v2.network_1.id}"
dns_nameservers = ["10.0.16.4", "213.186.33.99"]
allocation_pools {
start = "192.168.199.100"
end = "192.168.199.200"
}
tags = ["a", "b", "c", "d"]
}
`

const testAccNetworkingV2Subnet_enableDHCP = `
resource "openstack_networking_network_v2" "network_1" {
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/networking_subnet_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ The following arguments are supported:

* `value_specs` - (Optional) Map of additional options.

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

The `allocation_pools` block supports:

* `start` - (Required) The starting address.
Expand Down Expand Up @@ -112,6 +114,7 @@ The following attributes are exported:
* `dns_nameservers` - See Argument Reference above.
* `host_routes` - See Argument Reference above.
* `subnetpool_id` - See Argument Reference above.
* `tags` - See Argument Reference above.

## Import

Expand Down

0 comments on commit de64654

Please sign in to comment.