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

r/aws_s3_object_copy: Migrate to AWS SDK for Go v2 #33330

Merged
merged 26 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bcff8bc
s3: 'FindObjectByThreePartKey' -> 'FindObjectByThreePartKeyV1'.
ewbankkit Sep 5, 2023
54c699c
s3: 'ObjectListTags' -> 'ObjectListTagsV1'.
ewbankkit Sep 5, 2023
74cc696
s3: Generate AWS SDK for Go v2 tagging code.
ewbankkit Sep 5, 2023
3bad4a9
r/aws_s3_object_copy: Migrate (except Delete) to AWS SDK for Go v2.
ewbankkit Sep 5, 2023
798ce6b
s3: Add '-SkipAWSServiceImp' when generating AWS SDK for Go v2 taggin…
ewbankkit Sep 5, 2023
d170f3a
r/aws_s3_object_copy: Migrate acceptance tests to AWS SDK for Go v2.
ewbankkit Sep 5, 2023
917bd94
Run 'go get github.com/hashicorp/aws-sdk-go-base/v2@2572ed001c9c61c7f…
ewbankkit Sep 5, 2023
eb905e6
Use 'tfawserr.ErrHTTPStatusCodeEquals'.
ewbankkit Sep 5, 2023
0a532c2
Call 'resourceObjectCopyRead', not 'resourceObjectRead', from 'resour…
ewbankkit Sep 5, 2023
5108f87
Tidy up 'TestAccS3ObjectCopy_basic'.
ewbankkit Sep 5, 2023
6a7f556
Acceptance test output:
ewbankkit Sep 5, 2023
ae5b011
Add 'TestAccS3ObjectCopy_disappears'.
ewbankkit Sep 5, 2023
271f43e
Acceptance test output:
ewbankkit Sep 5, 2023
3fbece4
Add 'TestAccS3ObjectCopy_tags'.
ewbankkit Sep 6, 2023
d8f56b2
Acceptance test output:
ewbankkit Sep 6, 2023
22b34ed
Add 'TestAccS3ObjectCopy_metadata'.
ewbankkit Sep 6, 2023
e35c8d6
Acceptance test output:
ewbankkit Sep 6, 2023
c08c837
Add 'TestAccS3ObjectCopy_grant'.
ewbankkit Sep 6, 2023
7595d4e
Acceptance test output:
ewbankkit Sep 6, 2023
cd16437
Acceptance test output:
ewbankkit Sep 6, 2023
e66f033
Add 'TestAccS3ObjectCopy_sourceAndTargetWithSlashes'.
ewbankkit Sep 6, 2023
820f6c5
Acceptance test output:
ewbankkit Sep 6, 2023
ee57bb5
External source object in 'TestAccS3ObjectCopy_sourceWithSlashes'.
ewbankkit Sep 6, 2023
a0feb96
Acceptance test output:
ewbankkit Sep 6, 2023
b16ed8f
r/aws_s3_object_copy: Amazon S3 Object Tags are strongly consistent -…
ewbankkit Sep 6, 2023
99defa8
s3: 'propagationTimeout' -> 's3BucketPropagationTimeout'.
ewbankkit Sep 6, 2023
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
4 changes: 2 additions & 2 deletions internal/service/s3/bucket_inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func resourceBucketInventoryPut(ctx context.Context, d *schema.ResourceData, met
}

log.Printf("[DEBUG] Putting S3 bucket inventory configuration: %s", input)
err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
err := retry.RetryContext(ctx, s3BucketPropagationTimeout, func() *retry.RetryError {
_, err := conn.PutBucketInventoryConfigurationWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) {
Expand Down Expand Up @@ -310,7 +310,7 @@ func resourceBucketInventoryRead(ctx context.Context, d *schema.ResourceData, me

log.Printf("[DEBUG] Reading S3 bucket inventory configuration: %s", input)
var output *s3.GetBucketInventoryConfigurationOutput
err = retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
err = retry.RetryContext(ctx, s3BucketPropagationTimeout, func() *retry.RetryError {
var err error
output, err = conn.GetBucketInventoryConfigurationWithContext(ctx, input)

Expand Down
2 changes: 1 addition & 1 deletion internal/service/s3/bucket_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func resourceBucketLoggingCreate(ctx context.Context, d *schema.ResourceData, me

d.SetId(CreateResourceID(bucket, expectedBucketOwner))

_, err = tfresource.RetryWhenNotFound(ctx, propagationTimeout, func() (interface{}, error) {
_, err = tfresource.RetryWhenNotFound(ctx, s3BucketPropagationTimeout, func() (interface{}, error) {
return FindBucketLogging(ctx, conn, bucket, expectedBucketOwner)
})

Expand Down
2 changes: 1 addition & 1 deletion internal/service/s3/bucket_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func resourceBucketMetricPut(ctx context.Context, d *schema.ResourceData, meta i
}

log.Printf("[DEBUG] Putting S3 Bucket Metrics Configuration: %s", input)
err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
err := retry.RetryContext(ctx, s3BucketPropagationTimeout, func() *retry.RetryError {
_, err := conn.PutBucketMetricsConfigurationWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/s3/bucket_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func resourceBucketNotificationPut(ctx context.Context, d *schema.ResourceData,
}

log.Printf("[DEBUG] S3 bucket: %s, Putting notification: %v", bucket, i)
err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
err := retry.RetryContext(ctx, s3BucketPropagationTimeout, func() *retry.RetryError {
_, err := conn.PutBucketNotificationConfigurationWithContext(ctx, i)

if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) {
Expand Down
36 changes: 34 additions & 2 deletions internal/service/s3/bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
Expand All @@ -22,8 +23,10 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -209,7 +212,7 @@ func resourceBucketObjectRead(ctx context.Context, d *schema.ResourceData, meta
bucket := d.Get("bucket").(string)
key := d.Get("key").(string)
outputRaw, err := tfresource.RetryWhenNewResourceNotFound(ctx, objectCreationTimeout, func() (interface{}, error) {
return FindObjectByThreePartKey(ctx, conn, bucket, key, "")
return FindObjectByThreePartKeyV1(ctx, conn, bucket, key, "")
}, d.IsNewResource())

if !d.IsNewResource() && tfresource.NotFound(err) {
Expand Down Expand Up @@ -264,7 +267,7 @@ func resourceBucketObjectRead(ctx context.Context, d *schema.ResourceData, meta

// Retry due to S3 eventual consistency
tagsRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, 2*time.Minute, func() (interface{}, error) {
return ObjectListTags(ctx, conn, bucket, key)
return ObjectListTagsV1(ctx, conn, bucket, key)
}, s3.ErrCodeNoSuchBucket)

if err != nil {
Expand Down Expand Up @@ -579,3 +582,32 @@ func hasBucketObjectContentChanges(d verify.ResourceDiffer) bool {
}
return false
}

func FindObjectByThreePartKeyV1(ctx context.Context, conn *s3.S3, bucket, key, etag string) (*s3.HeadObjectOutput, error) {
input := &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
if etag != "" {
input.IfMatch = aws.String(etag)
}

output, err := conn.HeadObjectWithContext(ctx, input)

if tfawserr.ErrStatusCodeEquals(err, http.StatusNotFound) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}
2 changes: 1 addition & 1 deletion internal/service/s3/bucket_object_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func dataSourceBucketObjectRead(ctx context.Context, d *schema.ResourceData, met
log.Printf("[INFO] Ignoring body of S3 object %s with Content-Type %q", uniqueId, contentType)
}

tags, err := ObjectListTags(ctx, conn, bucket, key)
tags, err := ObjectListTagsV1(ctx, conn, bucket, key)

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for S3 Bucket (%s) Object (%s): %s", bucket, key, err)
Expand Down
8 changes: 4 additions & 4 deletions internal/service/s3/bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ func testAccCheckBucketObjectDestroy(ctx context.Context) resource.TestCheckFunc
continue
}

_, err := tfs3.FindObjectByThreePartKey(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"], rs.Primary.Attributes["etag"])
_, err := tfs3.FindObjectByThreePartKeyV1(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"], rs.Primary.Attributes["etag"])

if tfresource.NotFound(err) {
continue
Expand Down Expand Up @@ -1505,7 +1505,7 @@ func testAccCheckBucketObjectStorageClass(ctx context.Context, n, expectedClass
rs := s.RootModule().Resources[n]
conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn(ctx)

out, err := tfs3.FindObjectByThreePartKey(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"], "")
out, err := tfs3.FindObjectByThreePartKeyV1(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"], "")

if err != nil {
return err
Expand All @@ -1532,7 +1532,7 @@ func testAccCheckBucketObjectSSE(ctx context.Context, n, expectedSSE string) res
rs := s.RootModule().Resources[n]
conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn(ctx)

out, err := tfs3.FindObjectByThreePartKey(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"], "")
out, err := tfs3.FindObjectByThreePartKeyV1(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"], "")

if err != nil {
return err
Expand Down Expand Up @@ -1582,7 +1582,7 @@ func testAccCheckBucketObjectCheckTags(ctx context.Context, n string, expectedTa
rs := s.RootModule().Resources[n]
conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn(ctx)

got, err := tfs3.ObjectListTags(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"])
got, err := tfs3.ObjectListTagsV1(ctx, conn, rs.Primary.Attributes["bucket"], rs.Primary.Attributes["key"])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/s3/bucket_public_access_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func resourceBucketPublicAccessBlockRead(ctx context.Context, d *schema.Resource

// Retry for eventual consistency on creation
var output *s3.GetPublicAccessBlockOutput
err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
err := retry.RetryContext(ctx, s3BucketPropagationTimeout, func() *retry.RetryError {
var err error
output, err = conn.GetPublicAccessBlockWithContext(ctx, input)

Expand Down
6 changes: 3 additions & 3 deletions internal/service/s3/bucket_replication_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func resourceBucketReplicationConfigurationCreate(ctx context.Context, d *schema
input.Token = aws.String(v.(string))
}

err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
err := retry.RetryContext(ctx, s3BucketPropagationTimeout, func() *retry.RetryError {
_, err := conn.PutBucketReplicationWithContext(ctx, input)
if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) || tfawserr.ErrMessageContains(err, "InvalidRequest", "Versioning must be 'Enabled' on the bucket") {
return retry.RetryableError(err)
Expand All @@ -349,7 +349,7 @@ func resourceBucketReplicationConfigurationCreate(ctx context.Context, d *schema

d.SetId(bucket)

_, err = tfresource.RetryWhenNotFound(ctx, propagationTimeout, func() (interface{}, error) {
_, err = tfresource.RetryWhenNotFound(ctx, s3BucketPropagationTimeout, func() (interface{}, error) {
return FindBucketReplicationConfigurationByID(ctx, conn, d.Id())
})

Expand Down Expand Up @@ -405,7 +405,7 @@ func resourceBucketReplicationConfigurationUpdate(ctx context.Context, d *schema
input.Token = aws.String(v.(string))
}

err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError {
err := retry.RetryContext(ctx, s3BucketPropagationTimeout, func() *retry.RetryError {
_, err := conn.PutBucketReplicationWithContext(ctx, input)
if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) || tfawserr.ErrMessageContains(err, "InvalidRequest", "Versioning must be 'Enabled' on the bucket") {
return retry.RetryableError(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func resourceBucketServerSideEncryptionConfigurationCreate(ctx context.Context,
input.ExpectedBucketOwner = aws.String(expectedBucketOwner)
}

_, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, propagationTimeout,
_, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, s3BucketPropagationTimeout,
func() (interface{}, error) {
return conn.PutBucketEncryptionWithContext(ctx, input)
},
Expand All @@ -106,7 +106,7 @@ func resourceBucketServerSideEncryptionConfigurationCreate(ctx context.Context,

d.SetId(CreateResourceID(bucket, expectedBucketOwner))

_, err = tfresource.RetryWhenNotFound(ctx, propagationTimeout, func() (interface{}, error) {
_, err = tfresource.RetryWhenNotFound(ctx, s3BucketPropagationTimeout, func() (interface{}, error) {
return FindBucketServerSideEncryptionConfiguration(ctx, conn, bucket, expectedBucketOwner)
})

Expand Down Expand Up @@ -164,7 +164,7 @@ func resourceBucketServerSideEncryptionConfigurationUpdate(ctx context.Context,
input.ExpectedBucketOwner = aws.String(expectedBucketOwner)
}

_, err = tfresource.RetryWhenAWSErrCodeEquals(ctx, propagationTimeout,
_, err = tfresource.RetryWhenAWSErrCodeEquals(ctx, s3BucketPropagationTimeout,
func() (interface{}, error) {
return conn.PutBucketEncryptionWithContext(ctx, input)
},
Expand Down
1 change: 1 addition & 0 deletions internal/service/s3/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -ServiceTagsSlice
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -SkipAWSServiceImp -ServiceTagsSlice -TagsFunc=tagsV2 -KeyValueTagsFunc=keyValueTagsV2 -GetTagsInFunc=getTagsInV2 -SetTagsOutFunc=setTagsOutV2 -- tagsv2_gen.go
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
35 changes: 2 additions & 33 deletions internal/service/s3/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
Expand All @@ -22,7 +21,6 @@ import (
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
Expand Down Expand Up @@ -206,7 +204,7 @@ func resourceObjectRead(ctx context.Context, d *schema.ResourceData, meta interf
bucket := d.Get("bucket").(string)
key := d.Get("key").(string)
outputRaw, err := tfresource.RetryWhenNewResourceNotFound(ctx, objectCreationTimeout, func() (interface{}, error) {
return FindObjectByThreePartKey(ctx, conn, bucket, key, "")
return FindObjectByThreePartKeyV1(ctx, conn, bucket, key, "")
}, d.IsNewResource())

if !d.IsNewResource() && tfresource.NotFound(err) {
Expand Down Expand Up @@ -262,7 +260,7 @@ func resourceObjectRead(ctx context.Context, d *schema.ResourceData, meta interf

// Retry due to S3 eventual consistency
tagsRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, 2*time.Minute, func() (interface{}, error) {
return ObjectListTags(ctx, conn, bucket, key)
return ObjectListTagsV1(ctx, conn, bucket, key)
}, s3.ErrCodeNoSuchBucket)

if err != nil {
Expand Down Expand Up @@ -789,32 +787,3 @@ func flattenObjectDate(t *time.Time) string {

return t.Format(time.RFC3339)
}

func FindObjectByThreePartKey(ctx context.Context, conn *s3.S3, bucket, key, etag string) (*s3.HeadObjectOutput, error) {
input := &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
if etag != "" {
input.IfMatch = aws.String(etag)
}

output, err := conn.HeadObjectWithContext(ctx, input)

if tfawserr.ErrStatusCodeEquals(err, http.StatusNotFound) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}
Loading
Loading