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.

Issue: #453
  • Loading branch information
Steven Hardy committed Oct 23, 2018
1 parent 713665f commit 7309187
Show file tree
Hide file tree
Showing 2 changed files with 68 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("[INFO] 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("[INFO] 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
42 changes: 42 additions & 0 deletions openstack/resource_openstack_networking_network_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ func TestAccNetworkingV2Network_basic(t *testing.T) {
})
}

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

func TestAccNetworkingV2Network_netstack(t *testing.T) {
var network networks.Network
var subnet subnets.Subnet
Expand Down Expand Up @@ -248,6 +274,22 @@ resource "openstack_networking_network_v2" "network_1" {
}
`

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

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

const testAccNetworkingV2Network_netstack = `
resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
Expand Down

0 comments on commit 7309187

Please sign in to comment.