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

resource/aws_kinesis_stream: Handle tag additions/removals of more than 10 tags #4574

Merged
merged 2 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions aws/resource_aws_kinesis_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,35 @@ func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) {
})
}

func TestAccAWSKinesisStream_Tags(t *testing.T) {
var stream kinesis.StreamDescription
resourceName := "aws_kinesis_stream.test"

rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckKinesisStreamDestroy,
Steps: []resource.TestStep{
{
Config: testAccKinesisStreamConfig_Tags(rInt, 21),
Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists(resourceName, &stream),
resource.TestCheckResourceAttr(resourceName, "tags.%", "21"),
),
},
{
Config: testAccKinesisStreamConfig_Tags(rInt, 9),
Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists(resourceName, &stream),
resource.TestCheckResourceAttr(resourceName, "tags.%", "9"),
),
},
},
})
}

func testAccCheckKinesisStreamExists(n string, stream *kinesis.StreamDescription) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -492,3 +521,19 @@ resource "aws_kinesis_stream" "test_stream" {
]
}`, rInt)
}

func testAccKinesisStreamConfig_Tags(rInt, tagCount int) string {
var tagPairs string
for i := 1; i <= tagCount; i++ {
tagPairs = tagPairs + fmt.Sprintf("tag%d = \"tag%dvalue\"\n", i, i)
}

return fmt.Sprintf(`
resource "aws_kinesis_stream" "test" {
name = "terraform-kinesis-test-%d"
shard_count = 2
tags {
%s
}
}`, rInt, tagPairs)
}
61 changes: 41 additions & 20 deletions aws/tags_kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,55 @@ func setTagsKinesis(conn *kinesis.Kinesis, d *schema.ResourceData) error {
// 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
// Kinesis requires these operations be split into 10 tag batches
tagKeysBatchLimit := 10
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on pulling this out and making it a constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That seems fair -- I'll update 👍

tagKeysBatch := make([]*string, 0, tagKeysBatchLimit)
tagKeysBatches := make([][]*string, 0, len(remove)/tagKeysBatchLimit+1)
for _, tag := range remove {
if len(tagKeysBatch) == tagKeysBatchLimit {
tagKeysBatches = append(tagKeysBatches, tagKeysBatch)
tagKeysBatch = make([]*string, 0, tagKeysBatchLimit)
}
tagKeysBatch = append(tagKeysBatch, tag.Key)
}
tagKeysBatches = append(tagKeysBatches, tagKeysBatch)

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

if len(create) > 0 {

log.Printf("[DEBUG] Creating tags: %#v", create)
t := make(map[string]*string)

// Kinesis requires these operations be split into 10 tag batches
tagsBatchLimit := 10
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

tagsBatch := make(map[string]*string)
tagsBatches := make([]map[string]*string, 0, len(create)/tagsBatchLimit+1)
for _, tag := range create {
t[*tag.Key] = tag.Value
if len(tagsBatch) == tagsBatchLimit {
tagsBatches = append(tagsBatches, tagsBatch)
tagsBatch = make(map[string]*string)
}
tagsBatch[aws.StringValue(tag.Key)] = tag.Value
}

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

for _, tags := range tagsBatches {
_, err := conn.AddTagsToStream(&kinesis.AddTagsToStreamInput{
StreamName: aws.String(sn),
Tags: tags,
})
if err != nil {
return err
}
}
}
}
Expand Down