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

provider/aws: Allow tags for kinesis streams #3397

Merged
merged 1 commit into from
Oct 12, 2015
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
33 changes: 31 additions & 2 deletions builtin/providers/aws/resource_aws_kinesis_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -15,6 +16,7 @@ func resourceAwsKinesisStream() *schema.Resource {
return &schema.Resource{
Create: resourceAwsKinesisStreamCreate,
Read: resourceAwsKinesisStreamRead,
Update: resourceAwsKinesisStreamUpdate,
Delete: resourceAwsKinesisStreamDelete,

Schema: map[string]*schema.Schema{
Expand All @@ -35,6 +37,7 @@ func resourceAwsKinesisStream() *schema.Resource {
Optional: true,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -75,13 +78,28 @@ func resourceAwsKinesisStreamCreate(d *schema.ResourceData, meta interface{}) er
d.SetId(*s.StreamARN)
d.Set("arn", s.StreamARN)

return nil
return resourceAwsKinesisStreamUpdate(d, meta)
}

func resourceAwsKinesisStreamUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kinesisconn

d.Partial(true)
if err := setTagsKinesis(conn, d); err != nil {
return err
}

d.SetPartial("tags")
d.Partial(false)

return resourceAwsKinesisStreamRead(d, meta)
}

func resourceAwsKinesisStreamRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kinesisconn
sn := d.Get("name").(string)
describeOpts := &kinesis.DescribeStreamInput{
StreamName: aws.String(d.Get("name").(string)),
StreamName: aws.String(sn),
}
resp, err := conn.DescribeStream(describeOpts)
if err != nil {
Expand All @@ -99,6 +117,17 @@ func resourceAwsKinesisStreamRead(d *schema.ResourceData, meta interface{}) erro
d.Set("arn", *s.StreamARN)
d.Set("shard_count", len(s.Shards))

// set tags
describeTagsOpts := &kinesis.ListTagsForStreamInput{
StreamName: aws.String(sn),
}
tagsResp, err := conn.ListTagsForStream(describeTagsOpts)
if err != nil {
log.Printf("[DEBUG] Error retrieving tags for Stream: %s. %s", sn, err)
} else {
d.Set("tags", tagsToMapKinesis(tagsResp.Tags))
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions builtin/providers/aws/resource_aws_kinesis_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,8 @@ var testAccKinesisStreamConfig = fmt.Sprintf(`
resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test-%d"
shard_count = 2
tags {
Name = "tf-test"
}
}
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
105 changes: 105 additions & 0 deletions builtin/providers/aws/tags_kinesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package aws

import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/hashicorp/terraform/helper/schema"
)

// setTags is a helper to set the tags for a resource. It expects the
// tags field to be named "tags"
func setTagsKinesis(conn *kinesis.Kinesis, d *schema.ResourceData) error {

sn := d.Get("name").(string)

if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
create, remove := diffTagsKinesis(tagsFromMapKinesis(o), tagsFromMapKinesis(n))

// Set tags
if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %#v", remove)
k := make([]*string, len(remove), len(remove))
for i, t := range remove {
k[i] = t.Key
}

_, err := conn.RemoveTagsFromStream(&kinesis.RemoveTagsFromStreamInput{
StreamName: aws.String(sn),
TagKeys: k,
})
if err != nil {
return err
}
}

if len(create) > 0 {

log.Printf("[DEBUG] Creating tags: %#v", create)
t := make(map[string]*string)
for _, tag := range create {
t[*tag.Key] = tag.Value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I ran the acceptance tests, I got a crash here on setting the tags.

The var declaration above declares the storage for the map but it doesn't initialize it. I think you can fix this by changing the declaration to t := make(map[string]*string) .

2015/10/11 10:03:48 [DEBUG] Creating tags: []*kinesis.Tag{{
  Key: "Name",
  Value: "tf-test"
}}
panic: assignment to entry in nil map

goroutine 52 [running]:
github.com/hashicorp/terraform/builtin/providers/aws.setTagsKinesis(0xc20802cb60, 0xc208213440, 0x0, 0x0)
    src/github.com/hashicorp/terraform/builtin/providers/aws/tags_kinesis.go:45 +0x77b
github.com/hashicorp/terraform/builtin/providers/aws.resourceAwsKinesisStreamUpdate(0xc208213440, 0xbd36a0, 0xc2082a2240, 0x0, 0x0)
    src/github.com/hashicorp/terraform/builtin/providers/aws/resource_aws_kinesis_stream.go:88 +0xea
github.com/hashicorp/terraform/builtin/providers/aws.resourceAwsKinesisStreamCreate(0xc208213440, 0xbd36a0, 0xc2082a2240, 0x0, 0x0)
    src/github.com/hashicorp/terraform/builtin/providers/aws/resource_aws_kinesis_stream.go:81 +0x85d
github.com/hashicorp/terraform/helper/schema.(*Resource).Apply(0xc2080ae6c0, 0xc2080bf9e0, 0xc208259e80, 0xbd36a0, 0xc2082a2240, 0x6aa601, 0x0, 0x0)
    src/github.com/hashicorp/terraform/helper/schema/resource.go:145 +0x2c1
github.com/hashicorp/terraform/helper/schema.(*Provider).Apply(0xc2080bf080, 0xc2081368c0, 0xc2080bf9e0, 0xc208259e80, 0x1, 0x0, 0x0)
    src/github.com/hashicorp/terraform/helper/schema/provider.go:162 +0x1da
github.com/hashicorp/terraform/terraform.(*EvalApply).Eval(0xc208136a80, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval_apply.go:67 +0x62a
github.com/hashicorp/terraform/terraform.EvalRaw(0x7fc773203328, 0xc208136a80, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x254
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc20829b9e0, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval_sequence.go:10 +0xf9
github.com/hashicorp/terraform/terraform.EvalRaw(0x7fc773201b28, 0xc20829b9e0, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x254
github.com/hashicorp/terraform/terraform.(*EvalOpFilter).Eval(0xc2080e32f0, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval_filter_operation.go:37 +0x71
github.com/hashicorp/terraform/terraform.EvalRaw(0x7fc773201b50, 0xc2080e32f0, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x254
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc20829a940, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval_sequence.go:10 +0xf9
github.com/hashicorp/terraform/terraform.EvalRaw(0x7fc773201b28, 0xc20829a940, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x254
github.com/hashicorp/terraform/terraform.Eval(0x7fc773201b28, 0xc20829a940, 0x7fc773201960, 0xc2082de9c0, 0x0, 0x0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/eval.go:34 +0x79
github.com/hashicorp/terraform/terraform.func·027(0xfc84c0, 0xc2080e24e0, 0x0, 0x0)
    src/github.com/hashicorp/terraform/terraform/graph.go:198 +0x906
github.com/hashicorp/terraform/dag.func·006(0xfc84c0, 0xc2080e24e0, 0xc2082123c0, 0xc208212420)
    src/github.com/hashicorp/terraform/dag/dag.go:238 +0x106
created by github.com/hashicorp/terraform/dag.(*AcyclicGraph).Walk
    src/github.com/hashicorp/terraform/dag/dag.go:247 +0x877

}

_, err := conn.AddTagsToStream(&kinesis.AddTagsToStreamInput{
StreamName: aws.String(sn),
Tags: t,
})
if err != nil {
return err
}
}
}

return nil
}

// diffTags takes our tags locally and the ones remotely and returns
// the set of tags that must be created, and the set of tags that must
// be destroyed.
func diffTagsKinesis(oldTags, newTags []*kinesis.Tag) ([]*kinesis.Tag, []*kinesis.Tag) {
// First, we're creating everything we have
create := make(map[string]interface{})
for _, t := range newTags {
create[*t.Key] = *t.Value
}

// Build the list of what to remove
var remove []*kinesis.Tag
for _, t := range oldTags {
old, ok := create[*t.Key]
if !ok || old != *t.Value {
// Delete it!
remove = append(remove, t)
}
}

return tagsFromMapKinesis(create), remove
}

// tagsFromMap returns the tags for the given map of data.
func tagsFromMapKinesis(m map[string]interface{}) []*kinesis.Tag {
var result []*kinesis.Tag
for k, v := range m {
result = append(result, &kinesis.Tag{
Key: aws.String(k),
Value: aws.String(v.(string)),
})
}

return result
}

// tagsToMap turns the list of tags into a map.
func tagsToMapKinesis(ts []*kinesis.Tag) map[string]string {
result := make(map[string]string)
for _, t := range ts {
result[*t.Key] = *t.Value
}

return result
}
84 changes: 84 additions & 0 deletions builtin/providers/aws/tags_kinesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package aws

import (
"fmt"
"reflect"
"testing"

"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestDiffTagsKinesis(t *testing.T) {
cases := []struct {
Old, New map[string]interface{}
Create, Remove map[string]string
}{
// Basic add/remove
{
Old: map[string]interface{}{
"foo": "bar",
},
New: map[string]interface{}{
"bar": "baz",
},
Create: map[string]string{
"bar": "baz",
},
Remove: map[string]string{
"foo": "bar",
},
},

// Modify
{
Old: map[string]interface{}{
"foo": "bar",
},
New: map[string]interface{}{
"foo": "baz",
},
Create: map[string]string{
"foo": "baz",
},
Remove: map[string]string{
"foo": "bar",
},
},
}

for i, tc := range cases {
c, r := diffTagsKinesis(tagsFromMapKinesis(tc.Old), tagsFromMapKinesis(tc.New))
cm := tagsToMapKinesis(c)
rm := tagsToMapKinesis(r)
if !reflect.DeepEqual(cm, tc.Create) {
t.Fatalf("%d: bad create: %#v", i, cm)
}
if !reflect.DeepEqual(rm, tc.Remove) {
t.Fatalf("%d: bad remove: %#v", i, rm)
}
}
}

// testAccCheckTags can be used to check the tags on a resource.
func testAccCheckKinesisTags(ts []*kinesis.Tag, key string, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
m := tagsToMapKinesis(ts)
v, ok := m[key]
if value != "" && !ok {
return fmt.Errorf("Missing tag: %s", key)
} else if value == "" && ok {
return fmt.Errorf("Extra tag: %s", key)
}
if value == "" {
return nil
}

if v != value {
return fmt.Errorf("%s: bad value: %s", key, v)
}

return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ For more details, see the [Amazon Kinesis Documentation][1].
resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test"
shard_count = 1
tags {
Environment = "test"
}
}
```

Expand All @@ -31,6 +34,7 @@ AWS account and region the Stream is created in.
* `shard_count` – (Required) The number of shards that the stream will use.
Amazon has guidlines for specifying the Stream size that should be referenced
when creating a Kinesis stream. See [Amazon Kinesis Streams][2] for more.
* `tags` - (Optional) A mapping of tags to assign to the resource.

## Attributes Reference

Expand Down