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 Routers #467

Merged
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
27 changes: 27 additions & 0 deletions openstack/resource_openstack_networking_floatingip_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/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/pagination"
Expand Down Expand Up @@ -73,6 +74,11 @@ func resourceNetworkingFloatingIPV2() *schema.Resource {
Optional: true,
ForceNew: true,
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand Down Expand Up @@ -126,6 +132,16 @@ func resourceNetworkFloatingIPV2Create(d *schema.ResourceData, meta interface{})

d.SetId(floatingIP.ID)

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

return resourceNetworkFloatingIPV2Read(d, meta)
}

Expand All @@ -152,6 +168,7 @@ func resourceNetworkFloatingIPV2Read(d *schema.ResourceData, meta interface{}) e
d.Set("tenant_id", floatingIP.TenantID)

d.Set("region", GetRegion(d, config))
d.Set("tags", floatingIP.Tags)

return nil
}
Expand All @@ -177,6 +194,16 @@ func resourceNetworkFloatingIPV2Update(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error updating floating IP: %s", err)
}

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

return resourceNetworkFloatingIPV2Read(d, meta)
}

Expand Down
27 changes: 27 additions & 0 deletions openstack/resource_openstack_networking_router_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/extensions/layer3/routers"
)

Expand Down Expand Up @@ -123,6 +124,11 @@ func resourceNetworkingRouterV2() *schema.Resource {
},
},
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand Down Expand Up @@ -234,6 +240,16 @@ func resourceNetworkingRouterV2Create(d *schema.ResourceData, meta interface{})
}
}

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

return resourceNetworkingRouterV2Read(d, meta)
}

Expand Down Expand Up @@ -261,6 +277,7 @@ func resourceNetworkingRouterV2Read(d *schema.ResourceData, meta interface{}) er
d.Set("distributed", n.Distributed)
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 Down Expand Up @@ -364,6 +381,16 @@ func resourceNetworkingRouterV2Update(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err)
}

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

return resourceNetworkingRouterV2Read(d, meta)
}

Expand Down
22 changes: 22 additions & 0 deletions openstack/resource_openstack_networking_tags_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func TestAccNetworkingV2_tags(t *testing.T) {
testAccCheckNetworkingV2Tags(
"openstack_networking_secgroup_v2.secgroup_1",
[]string{"a", "b", "c"}),
testAccCheckNetworkingV2Tags(
"openstack_networking_floatingip_v2.fip_1",
[]string{"a", "b", "c"}),
testAccCheckNetworkingV2Tags(
"openstack_networking_router_v2.router_1",
[]string{"a", "b", "c"}),
),
},
resource.TestStep{
Expand All @@ -51,6 +57,12 @@ func TestAccNetworkingV2_tags(t *testing.T) {
testAccCheckNetworkingV2Tags(
"openstack_networking_secgroup_v2.secgroup_1",
[]string{"a", "b", "c", "d"}),
testAccCheckNetworkingV2Tags(
"openstack_networking_floatingip_v2.fip_1",
[]string{"a", "b", "c", "d"}),
testAccCheckNetworkingV2Tags(
"openstack_networking_router_v2.router_1",
[]string{"a", "b", "c", "d"}),
),
},
},
Expand Down Expand Up @@ -111,6 +123,16 @@ resource "openstack_networking_secgroup_v2" "secgroup_1" {
description = "terraform security group acceptance test"
tags = %[1]s
}

resource "openstack_networking_floatingip_v2" "fip_1" {
tags = %[1]s
}

resource "openstack_networking_router_v2" "router_1" {
name = "router_1"
admin_state_up = "true"
tags = %[1]s
}
`

const testAccNetworkingV2_tags_create = `["a", "b", "c"]`
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/networking_floatingip_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ The following arguments are supported:

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

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

## Attributes Reference

The following attributes are exported:
Expand All @@ -65,6 +67,7 @@ The following attributes are exported:
* `port_id` - ID of associated port.
* `tenant_id` - the ID of the tenant in which to create the floating IP.
* `fixed_ip` - The fixed IP which the floating IP maps to.
* `tags` - See Argument Reference above.

## Import

Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/networking_router_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ The following arguments are supported:

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

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

* `vendor_options` - (Optional) Map of additional vendor-specific options.
Supported options are described below.

Expand Down Expand Up @@ -98,6 +100,7 @@ The following attributes are exported:
* `tenant_id` - See Argument Reference above.
* `value_specs` - See Argument Reference above.
* `availability_zone_hints` - See Argument Reference above.
* `tags` - See Argument Reference above.

## Import

Expand Down