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

Add tags support for Neutron Networks #454

Merged
merged 1 commit into from
Oct 30, 2018
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
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` - See Argument Reference above.

## Import

Expand Down