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

Remove ALL leading '/'s when destroying aws_s3_bucket_object resource #7584

Merged
merged 4 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 21 additions & 1 deletion aws/internal/keyvaluetags/s3_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ 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"
)

Expand Down Expand Up @@ -86,7 +89,24 @@ 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 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