Skip to content

Commit

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

Note the test is added in a new resource_openstack_networking_tags_v2_test.go
file, so that we can reduce duplication later in the series e.g
subnet/port/trunk resources can share the same template which should be faster.

Issue: terraform-provider-openstack#453
  • Loading branch information
Steven Hardy committed Oct 25, 2018
1 parent 154050d commit a2165fd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
26 changes: 26 additions & 0 deletions openstack/resource_openstack_networking_network_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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/extensions/external"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/provider"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
Expand Down Expand Up @@ -95,6 +96,11 @@ func resourceNetworkingNetworkV2() *schema.Resource {
Optional: true,
ForceNew: true,
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"availability_zone_hints": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -180,6 +186,16 @@ func resourceNetworkingNetworkV2Create(d *schema.ResourceData, meta interface{})

log.Printf("[INFO] Network ID: %s", n.ID)

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

log.Printf("[DEBUG] Waiting for Network (%s) to become available", n.ID)

stateConf := &resource.StateChangeConf{
Expand Down Expand Up @@ -222,6 +238,7 @@ func resourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) e
d.Set("external", strconv.FormatBool(n.External))
d.Set("tenant_id", n.TenantID)
d.Set("region", GetRegion(d, config))
d.Set("tags", n.Tags)

if err := d.Set("availability_zone_hints", n.AvailabilityZoneHints); err != nil {
log.Printf("[DEBUG] unable to set availability_zone_hints: %s", err)
Expand All @@ -241,6 +258,15 @@ func resourceNetworkingNetworkV2Update(d *schema.ResourceData, meta interface{})
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("tags") {
tags := networkV2AttributesTags(d)
tagOpts := attributestags.ReplaceAllOpts{Tags: tags}
tags, err := attributestags.ReplaceAll(networkingClient, "networks", d.Id(), tagOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating Tags on Network: %s", err)
}
log.Printf("[DEBUG] Updated Tags = %+v on Network %+v", tags, d.Id())
}
if d.HasChange("admin_state_up") {
asuRaw := d.Get("admin_state_up").(string)
if asuRaw != "" {
Expand Down
49 changes: 49 additions & 0 deletions openstack/resource_openstack_networking_tags_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package openstack

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccNetworkingV2_tags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNetworkingV2NetworkDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNetworkingV2_tags,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2Tags(
"openstack_networking_network_v2.network_1",
[]string{"a", "b", "c"}),
),
},
resource.TestStep{
Config: testAccNetworkingV2_tags_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingV2Tags(
"openstack_networking_network_v2.network_1",
[]string{"a", "b", "c", "d"}),
),
},
},
})
}

const testAccNetworkingV2_tags = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
tags = ["a", "b", "c"]
}
`

const testAccNetworkingV2_tags_update = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
tags = ["a", "b", "c", "d"]
}
`
3 changes: 3 additions & 0 deletions website/docs/r/networking_network_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ The following arguments are supported:
so that they are scheduled on different availability zones. Changing this
creates a new network.

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

The `segments` block supports:

* `physical_network` - The phisical network where this network is implemented.
Expand All @@ -112,6 +114,7 @@ The following attributes are exported:
* `tenant_id` - See Argument Reference above.
* `admin_state_up` - See Argument Reference above.
* `availability_zone_hints` - See Argument Reference above.
* `tags` - Argument Reference above.

## Import

Expand Down

0 comments on commit a2165fd

Please sign in to comment.