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_iot_topic_rule: Allow tags with '@' on creation #19677

Merged
merged 3 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions aws/internal/keyvaluetags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,30 @@ func (tags KeyValueTags) UrlEncode() string {
return values.Encode()
}

// UrlQueryString returns the KeyValueTags formatted as URL Query parameters without encoding.
func (tags KeyValueTags) UrlQueryString() string {
keys := make([]string, 0, len(tags))
for k, v := range tags {
if v == nil || v.Value == nil {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)

var buf strings.Builder
for _, k := range keys {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(*tags[k].Value)
}

return buf.String()
}

// New creates KeyValueTags from common types or returns an empty KeyValueTags.
//
// Supports various Terraform Plugin SDK types including map[string]string,
Expand Down
56 changes: 56 additions & 0 deletions aws/internal/keyvaluetags/key_value_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,62 @@ func TestKeyValueTagsUrlEncode(t *testing.T) {
}
}

func TestKeyValueTagsUrlQueryString(t *testing.T) {
testCases := []struct {
name string
tags KeyValueTags
want string
}{
{
name: "empty",
tags: New(map[string]string{}),
want: "",
},
{
name: "nil value",
tags: New(map[string]*string{
"key1": nil,
}),
want: "",
},
{
name: "single",
tags: New(map[string]string{
"key1": "value1",
}),
want: "key1=value1",
},
{
name: "multiple",
tags: New(map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}),
want: "key1=value1&key2=value2&key3=value3",
},
{
name: "multiple_with_encoded",
tags: New(map[string]string{
"key1": "value 1",
"key@2": "value+:2",
"key3": "value3",
}),
want: "key1=value 1&key3=value3&key@2=value+:2",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got := testCase.tags.UrlQueryString()

if got != testCase.want {
t.Errorf("unexpected query string value: %q", got)
}
})
}
}

func TestNew(t *testing.T) {
testCases := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_iot_topic_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ func resourceAwsIotTopicRuleCreate(d *schema.ResourceData, meta interface{}) err

input := &iot.CreateTopicRuleInput{
RuleName: aws.String(ruleName),
Tags: aws.String(tags.IgnoreAws().UrlEncode()),
Tags: aws.String(tags.IgnoreAws().UrlQueryString()),
TopicRulePayload: expandIotTopicRulePayload(d),
}

Expand Down
8 changes: 4 additions & 4 deletions aws/resource_aws_iot_topic_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,11 @@ func TestAccAWSIoTTopicRule_Tags(t *testing.T) {
CheckDestroy: testAccCheckAWSIoTTopicRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSIoTTopicRuleTags1(rName, "key1", "value1"),
Config: testAccAWSIoTTopicRuleTags1(rName, "key1", "user@example"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSIoTTopicRuleExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "user@example"),
),
},
{
Expand All @@ -541,11 +541,11 @@ func TestAccAWSIoTTopicRule_Tags(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccAWSIoTTopicRuleTags2(rName, "key1", "value1updated", "key2", "value2"),
Config: testAccAWSIoTTopicRuleTags2(rName, "key1", "user@example", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSIoTTopicRuleExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "user@example"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
Expand Down
28 changes: 14 additions & 14 deletions aws/resource_aws_s3_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func TestAccAWSS3BucketObject_tags(t *testing.T) {
testAccCheckAWSS3BucketObjectExists(resourceName, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj1, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand All @@ -690,7 +690,7 @@ func TestAccAWSS3BucketObject_tags(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj2, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj2, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "B@BB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "X X"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "E:/"),
Expand All @@ -714,7 +714,7 @@ func TestAccAWSS3BucketObject_tags(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdDiffers(&obj4, &obj3),
testAccCheckAWSS3BucketObjectBody(&obj4, "changed stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand Down Expand Up @@ -742,7 +742,7 @@ func TestAccAWSS3BucketObject_tagsLeadingSingleSlash(t *testing.T) {
testAccCheckAWSS3BucketObjectExists(resourceName, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj1, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand All @@ -755,7 +755,7 @@ func TestAccAWSS3BucketObject_tagsLeadingSingleSlash(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj2, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj2, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "B@BB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "X X"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "E:/"),
Expand All @@ -779,7 +779,7 @@ func TestAccAWSS3BucketObject_tagsLeadingSingleSlash(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdDiffers(&obj4, &obj3),
testAccCheckAWSS3BucketObjectBody(&obj4, "changed stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand Down Expand Up @@ -807,7 +807,7 @@ func TestAccAWSS3BucketObject_tagsLeadingMultipleSlashes(t *testing.T) {
testAccCheckAWSS3BucketObjectExists(resourceName, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj1, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand All @@ -820,7 +820,7 @@ func TestAccAWSS3BucketObject_tagsLeadingMultipleSlashes(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj2, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj2, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "B@BB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "X X"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "E:/"),
Expand All @@ -844,7 +844,7 @@ func TestAccAWSS3BucketObject_tagsLeadingMultipleSlashes(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdDiffers(&obj4, &obj3),
testAccCheckAWSS3BucketObjectBody(&obj4, "changed stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand Down Expand Up @@ -872,7 +872,7 @@ func TestAccAWSS3BucketObject_tagsMultipleSlashes(t *testing.T) {
testAccCheckAWSS3BucketObjectExists(resourceName, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj1, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand All @@ -885,7 +885,7 @@ func TestAccAWSS3BucketObject_tagsMultipleSlashes(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj2, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj2, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "B@BB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "X X"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "E:/"),
Expand All @@ -909,7 +909,7 @@ func TestAccAWSS3BucketObject_tagsMultipleSlashes(t *testing.T) {
testAccCheckAWSS3BucketObjectVersionIdDiffers(&obj4, &obj3),
testAccCheckAWSS3BucketObjectBody(&obj4, "changed stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "A@AA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
Expand Down Expand Up @@ -1707,7 +1707,7 @@ resource "aws_s3_bucket_object" "object" {
content = %[3]q

tags = {
Key1 = "AAA"
Key1 = "A@AA"
Key2 = "BBB"
Key3 = "CCC"
}
Expand All @@ -1731,7 +1731,7 @@ resource "aws_s3_bucket_object" "object" {
content = %[3]q

tags = {
Key2 = "BBB"
Key2 = "B@BB"
Key3 = "X X"
Key4 = "DDD"
Key5 = "E:/"
Expand Down