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

Update provider's S3 bucket lookup to use GetBucketRegion utility #14221

Merged
merged 2 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 6 additions & 10 deletions aws/data_source_aws_s3_bucket.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package aws

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -91,19 +94,12 @@ func dataSourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}

func bucketLocation(client *AWSClient, d *schema.ResourceData, bucket string) error {
location, err := client.s3conn.GetBucketLocation(
&s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
},
)
region, err := s3manager.GetBucketRegionWithClient(context.Background(), client.s3conn, bucket, func(r *request.Request) {
r.Config.S3ForcePathStyle = aws.Bool(false)
})
if err != nil {
return err
}
var region string
if location.LocationConstraint != nil {
region = *location.LocationConstraint
}
region = normalizeRegion(region)
if err := d.Set("region", region); err != nil {
return err
}
Expand Down
20 changes: 8 additions & 12 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -14,7 +15,9 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -1271,23 +1274,16 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}

// Add the region as an attribute

locationResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return s3conn.GetBucketLocation(
&s3.GetBucketLocationInput{
Bucket: aws.String(d.Id()),
},
)
discoveredRegion, err := retryOnAwsCode("NotFound", func() (interface{}, error) {
return s3manager.GetBucketRegionWithClient(context.Background(), s3conn, d.Id(), func(r *request.Request) {
r.Config.S3ForcePathStyle = aws.Bool(false)
})
})
if err != nil {
return fmt.Errorf("error getting S3 Bucket location: %s", err)
}

var region string
if location, ok := locationResponse.(*s3.GetBucketLocationOutput); ok && location.LocationConstraint != nil {
region = aws.StringValue(location.LocationConstraint)
}
region = normalizeRegion(region)
region := discoveredRegion.(string)
if err := d.Set("region", region); err != nil {
return err
}
Expand Down
18 changes: 7 additions & 11 deletions aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -14,8 +15,10 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -126,21 +129,14 @@ func testSweepS3Buckets(region string) error {
}

func testS3BucketRegion(conn *s3.S3, bucket string) (string, error) {
input := &s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
}

output, err := conn.GetBucketLocation(input)

region, err := s3manager.GetBucketRegionWithClient(context.Background(), conn, bucket, func(r *request.Request) {
r.Config.S3ForcePathStyle = aws.Bool(false)
})
if err != nil {
return "", err
}

if output == nil || output.LocationConstraint == nil {
return "us-east-1", nil
}

return aws.StringValue(output.LocationConstraint), nil
return region, nil
}

func testS3BucketObjectLockEnabled(conn *s3.S3, bucket string) (bool, error) {
Expand Down
443 changes: 443 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/service/s3/s3iface/interface.go

Large diffs are not rendered by default.

Loading