Skip to content

Commit

Permalink
Merge pull request #7584 from ewbankkit/issue-7583
Browse files Browse the repository at this point in the history
Remove ALL leading '/'s when destroying aws_s3_bucket_object resource
  • Loading branch information
YakDriver authored Feb 12, 2021
2 parents 9b3b583 + ef28f92 commit 1b94378
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 32 deletions.
26 changes: 25 additions & 1 deletion aws/internal/keyvaluetags/s3_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ package keyvaluetags

import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
tfs3 "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/s3"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

// Custom S3 tag service update functions using the same format as generated code.
Expand Down Expand Up @@ -86,7 +90,27 @@ func S3ObjectListTags(conn *s3.S3, bucket, key string) (KeyValueTags, error) {
Key: aws.String(key),
}

output, err := conn.GetObjectTagging(input)
var output *s3.GetObjectTaggingOutput

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
var err error
output, err = conn.GetObjectTagging(input)
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NoSuchKey" {
return resource.RetryableError(
fmt.Errorf("getting object tagging %s, retrying: %w", bucket, err),
)
}
}
if err != nil {
return resource.NonRetryableError(err)
}

return nil
})
if tfresource.TimedOut(err) {
output, err = conn.GetObjectTagging(input)
}

if tfawserr.ErrCodeEquals(err, tfs3.ErrCodeNoSuchTagSet) {
return New(nil), nil
Expand Down
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 (
"io"
"log"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -462,8 +463,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
Loading

0 comments on commit 1b94378

Please sign in to comment.