Skip to content

Commit

Permalink
Remove ALL leading '/'s when destroying aws_s3_bucket_object resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Oct 4, 2019
1 parent e94c64a commit 2230871
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
7 changes: 5 additions & 2 deletions aws/resource_aws_s3_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/url"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -484,8 +485,10 @@ func resourceAwsS3BucketObjectDelete(d *schema.ResourceData, meta interface{}) e

bucket := d.Get("bucket").(string)
key := d.Get("key").(string)
// We are effectively ignoring any leading '/' in the key name as aws.Config.DisableRestProtocolURICleaning is false
key = strings.TrimPrefix(key, "/")
// We are effectively ignoring all leading '/'s in the key name and
// treating multiple '/'s as a single '/' as aws.Config.DisableRestProtocolURICleaning is false
key = strings.TrimLeft(key, "/")
key = regexp.MustCompile(`/+`).ReplaceAllString(key, "/")

var err error
if _, ok := d.GetOk("version_id"); ok {
Expand Down
130 changes: 129 additions & 1 deletion aws/resource_aws_s3_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func TestAccAWSS3BucketObject_tags(t *testing.T) {
})
}

func TestAccAWSS3BucketObject_tagsLeadingSlash(t *testing.T) {
func TestAccAWSS3BucketObject_tagsLeadingSingleSlash(t *testing.T) {
var obj1, obj2, obj3, obj4 s3.GetObjectOutput
resourceName := "aws_s3_bucket_object.object"
rInt := acctest.RandInt()
Expand Down Expand Up @@ -726,6 +726,134 @@ func TestAccAWSS3BucketObject_tagsLeadingSlash(t *testing.T) {
})
}

func TestAccAWSS3BucketObject_tagsLeadingMultipleSlashes(t *testing.T) {
var obj1, obj2, obj3, obj4 s3.GetObjectOutput
resourceName := "aws_s3_bucket_object.object"
rInt := acctest.RandInt()
key := "/////test-key"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withTags(rInt, key, "stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj1, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
},
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withUpdatedTags(rInt, key, "stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj2),
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj2, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj2, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "XXX"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "EEE"),
),
},
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withNoTags(rInt, key, "stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj3),
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj3, &obj2),
testAccCheckAWSS3BucketObjectBody(&obj3, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withTags(rInt, key, "changed stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj4),
testAccCheckAWSS3BucketObjectVersionIdDiffers(&obj4, &obj3),
testAccCheckAWSS3BucketObjectBody(&obj4, "changed stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
},
},
})
}

func TestAccAWSS3BucketObject_tagsMultipleSlashes(t *testing.T) {
var obj1, obj2, obj3, obj4 s3.GetObjectOutput
resourceName := "aws_s3_bucket_object.object"
rInt := acctest.RandInt()
key := "first//second///third//"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withTags(rInt, key, "stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj1, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
},
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withUpdatedTags(rInt, key, "stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj2),
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj2, &obj1),
testAccCheckAWSS3BucketObjectBody(&obj2, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "XXX"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "EEE"),
),
},
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withNoTags(rInt, key, "stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj3),
testAccCheckAWSS3BucketObjectVersionIdEquals(&obj3, &obj2),
testAccCheckAWSS3BucketObjectBody(&obj3, "stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
PreConfig: func() {},
Config: testAccAWSS3BucketObjectConfig_withTags(rInt, key, "changed stuff"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists(resourceName, &obj4),
testAccCheckAWSS3BucketObjectVersionIdDiffers(&obj4, &obj3),
testAccCheckAWSS3BucketObjectBody(&obj4, "changed stuff"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
},
},
})
}

func TestAccAWSS3BucketObject_ObjectLockLegalHoldStartWithNone(t *testing.T) {
var obj1, obj2, obj3 s3.GetObjectOutput
resourceName := "aws_s3_bucket_object.object"
Expand Down

0 comments on commit 2230871

Please sign in to comment.