From efbed7f4f2fd89505362f510115e96f338c8df5c Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Sun, 16 Jan 2022 11:40:21 -0500 Subject: [PATCH 1/5] r/s3_bucket_accelerate_configuration: new resource --- internal/provider/provider.go | 1 + .../s3/bucket_accelerate_configuration.go | 212 ++++++++++++++++++ .../bucket_accelerate_configuration_test.go | 190 ++++++++++++++++ ...ket_accelerate_configuration.html.markdown | 52 +++++ 4 files changed, 455 insertions(+) create mode 100644 internal/service/s3/bucket_accelerate_configuration.go create mode 100644 internal/service/s3/bucket_accelerate_configuration_test.go create mode 100644 website/docs/r/s3_bucket_accelerate_configuration.html.markdown diff --git a/internal/provider/provider.go b/internal/provider/provider.go index da8a8567ac6..39ab34ea01e 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1621,6 +1621,7 @@ func Provider() *schema.Provider { "aws_route53_resolver_rule_association": route53resolver.ResourceRuleAssociation(), "aws_s3_bucket": s3.ResourceBucket(), + "aws_s3_bucket_accelerate_configuration": s3.ResourceBucketAccelerateConfiguration(), "aws_s3_bucket_analytics_configuration": s3.ResourceBucketAnalyticsConfiguration(), "aws_s3_bucket_cors_configuration": s3.ResourceBucketCorsConfiguration(), "aws_s3_bucket_intelligent_tiering_configuration": s3.ResourceBucketIntelligentTieringConfiguration(), diff --git a/internal/service/s3/bucket_accelerate_configuration.go b/internal/service/s3/bucket_accelerate_configuration.go new file mode 100644 index 00000000000..aaac7b91c62 --- /dev/null +++ b/internal/service/s3/bucket_accelerate_configuration.go @@ -0,0 +1,212 @@ +package s3 + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/s3" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "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" + "github.com/hashicorp/terraform-provider-aws/internal/verify" +) + +func ResourceBucketAccelerateConfiguration() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceBucketAccelerateConfigurationCreate, + ReadContext: resourceBucketAccelerateConfigurationRead, + UpdateContext: resourceBucketAccelerateConfigurationUpdate, + DeleteContext: resourceBucketAccelerateConfigurationDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: map[string]*schema.Schema{ + "bucket": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 63), + }, + "expected_bucket_owner": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: verify.ValidAccountID, + }, + "status": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice(s3.BucketAccelerateStatus_Values(), false), + }, + }, + } +} + +func resourceBucketAccelerateConfigurationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).S3Conn + + bucket := d.Get("bucket").(string) + expectedBucketOwner := d.Get("expected_bucket_owner").(string) + + input := &s3.PutBucketAccelerateConfigurationInput{ + Bucket: aws.String(bucket), + AccelerateConfiguration: &s3.AccelerateConfiguration{ + Status: aws.String(d.Get("status").(string)), + }, + } + + if expectedBucketOwner != "" { + input.ExpectedBucketOwner = aws.String(expectedBucketOwner) + } + + _, err := verify.RetryOnAWSCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { + return conn.PutBucketAccelerateConfigurationWithContext(ctx, input) + }) + + if err != nil { + return diag.FromErr(fmt.Errorf("error creating S3 bucket (%s) accelerate configuration: %w", bucket, err)) + } + + d.SetId(resourceBucketAccelerateConfigurationCreateResourceID(bucket, expectedBucketOwner)) + + return resourceBucketAccelerateConfigurationRead(ctx, d, meta) +} + +func resourceBucketAccelerateConfigurationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).S3Conn + + bucket, expectedBucketOwner, err := resourceBucketAccelerateConfigurationParseResourceID(d.Id()) + if err != nil { + return diag.FromErr(err) + } + + input := &s3.GetBucketAccelerateConfigurationInput{ + Bucket: aws.String(bucket), + } + + if expectedBucketOwner != "" { + input.ExpectedBucketOwner = aws.String(expectedBucketOwner) + } + + output, err := conn.GetBucketAccelerateConfigurationWithContext(ctx, input) + + if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) { + log.Printf("[WARN] S3 Bucket Accelerate Configuration (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + if err != nil { + return diag.FromErr(fmt.Errorf("error reading S3 bucket accelerate configuration (%s): %w", d.Id(), err)) + } + + if output == nil { + if d.IsNewResource() { + return diag.FromErr(fmt.Errorf("error reading S3 bucket accelerate configuration (%s): empty output", d.Id())) + } + log.Printf("[WARN] S3 Bucket Accelerate Configuration (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + d.Set("bucket", bucket) + d.Set("expected_bucket_owner", expectedBucketOwner) + d.Set("status", output.Status) + + return nil +} + +func resourceBucketAccelerateConfigurationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).S3Conn + + bucket, expectedBucketOwner, err := resourceBucketAccelerateConfigurationParseResourceID(d.Id()) + if err != nil { + return diag.FromErr(err) + } + + input := &s3.PutBucketAccelerateConfigurationInput{ + Bucket: aws.String(bucket), + AccelerateConfiguration: &s3.AccelerateConfiguration{ + Status: aws.String(d.Get("status").(string)), + }, + } + + if expectedBucketOwner != "" { + input.ExpectedBucketOwner = aws.String(expectedBucketOwner) + } + + _, err = conn.PutBucketAccelerateConfigurationWithContext(ctx, input) + + if err != nil { + return diag.FromErr(fmt.Errorf("error updating S3 bucket accelerate configuration (%s): %w", d.Id(), err)) + } + + return resourceBucketAccelerateConfigurationRead(ctx, d, meta) +} + +func resourceBucketAccelerateConfigurationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*conns.AWSClient).S3Conn + + bucket, expectedBucketOwner, err := resourceBucketAccelerateConfigurationParseResourceID(d.Id()) + if err != nil { + return diag.FromErr(err) + } + + input := &s3.PutBucketAccelerateConfigurationInput{ + Bucket: aws.String(bucket), + AccelerateConfiguration: &s3.AccelerateConfiguration{ + Status: aws.String(s3.BucketAccelerateStatusSuspended), + }, + } + + if expectedBucketOwner != "" { + input.ExpectedBucketOwner = aws.String(expectedBucketOwner) + } + + _, err = conn.PutBucketAccelerateConfigurationWithContext(ctx, input) + + if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) { + return nil + } + + if err != nil { + return diag.FromErr(fmt.Errorf("error deleting S3 bucket accelerate configuration (%s): %w", d.Id(), err)) + } + + return nil +} + +func resourceBucketAccelerateConfigurationCreateResourceID(bucket, expectedBucketOwner string) string { + if bucket == "" { + return expectedBucketOwner + } + + if expectedBucketOwner == "" { + return bucket + } + + parts := []string{bucket, expectedBucketOwner} + id := strings.Join(parts, ",") + + return id +} + +func resourceBucketAccelerateConfigurationParseResourceID(id string) (string, string, error) { + parts := strings.Split(id, ",") + + if len(parts) == 1 && parts[0] != "" { + return parts[0], "", nil + } + + if len(parts) == 2 && parts[0] != "" && parts[1] != "" { + return parts[0], parts[1], nil + } + + return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected BUCKET or BUCKET,EXPECTED_BUCKET_OWNER", id) +} diff --git a/internal/service/s3/bucket_accelerate_configuration_test.go b/internal/service/s3/bucket_accelerate_configuration_test.go new file mode 100644 index 00000000000..12beacde409 --- /dev/null +++ b/internal/service/s3/bucket_accelerate_configuration_test.go @@ -0,0 +1,190 @@ +package s3_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/aws/aws-sdk-go/service/s3" + "github.com/hashicorp/aws-sdk-go-base/tfawserr" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + tfs3 "github.com/hashicorp/terraform-provider-aws/internal/service/s3" +) + +func TestAccS3BucketAccelerateConfiguration_basic(t *testing.T) { + bucketName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_s3_bucket_accelerate_configuration.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(t) + acctest.PreCheckPartitionHasService(cloudfront.EndpointsID, t) + }, + ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckBucketAccelerateConfigurationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBucketAccelerateConfigurationBasicConfig(bucketName, s3.BucketAccelerateStatusEnabled), + Check: resource.ComposeTestCheckFunc( + testAccCheckBucketAccelerateConfigurationExists(resourceName), + resource.TestCheckResourceAttrPair(resourceName, "bucket", "aws_s3_bucket.test", "id"), + resource.TestCheckResourceAttr(resourceName, "status", s3.BucketAccelerateStatusEnabled), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccS3BucketAccelerateConfiguration_update(t *testing.T) { + bucketName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_s3_bucket_accelerate_configuration.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(t) + acctest.PreCheckPartitionHasService(cloudfront.EndpointsID, t) + }, + ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckBucketAccelerateConfigurationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBucketAccelerateConfigurationBasicConfig(bucketName, s3.BucketAccelerateStatusEnabled), + Check: resource.ComposeTestCheckFunc( + testAccCheckBucketAccelerateConfigurationExists(resourceName), + resource.TestCheckResourceAttrPair(resourceName, "bucket", "aws_s3_bucket.test", "id"), + resource.TestCheckResourceAttr(resourceName, "status", s3.BucketAccelerateStatusEnabled), + ), + }, + { + Config: testAccBucketAccelerateConfigurationBasicConfig(bucketName, s3.BucketAccelerateStatusSuspended), + Check: resource.ComposeTestCheckFunc( + testAccCheckBucketAccelerateConfigurationExists(resourceName), + resource.TestCheckResourceAttrPair(resourceName, "bucket", "aws_s3_bucket.test", "id"), + resource.TestCheckResourceAttr(resourceName, "status", s3.BucketAccelerateStatusSuspended), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccS3BucketAccelerateConfiguration_disappears(t *testing.T) { + bucketName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_s3_bucket_accelerate_configuration.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(t) + acctest.PreCheckPartitionHasService(cloudfront.EndpointsID, t) + }, + ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckBucketAccelerateConfigurationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccBucketAccelerateConfigurationBasicConfig(bucketName, s3.BucketAccelerateStatusEnabled), + Check: resource.ComposeTestCheckFunc( + testAccCheckBucketAccelerateConfigurationExists(resourceName), + acctest.CheckResourceDisappears(acctest.Provider, tfs3.ResourceBucketAccelerateConfiguration(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckBucketAccelerateConfigurationDestroy(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_s3_bucket_accelerate_configuration" { + continue + } + + input := &s3.GetBucketAccelerateConfigurationInput{ + Bucket: aws.String(rs.Primary.ID), + } + + output, err := conn.GetBucketAccelerateConfiguration(input) + + if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) { + continue + } + + if err != nil { + return fmt.Errorf("error getting S3 Bucket accelerate configuration (%s): %w", rs.Primary.ID, err) + } + + if output != nil { + return fmt.Errorf("S3 Bucket accelerate configuration (%s) still exists", rs.Primary.ID) + } + } + + return nil +} + +func testAccCheckBucketAccelerateConfigurationExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("Resource (%s) ID not set", resourceName) + } + + conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn + + input := &s3.GetBucketAccelerateConfigurationInput{ + Bucket: aws.String(rs.Primary.ID), + } + + output, err := conn.GetBucketAccelerateConfiguration(input) + + if err != nil { + return fmt.Errorf("error getting S3 Bucket accelerate configuration (%s): %w", rs.Primary.ID, err) + } + + if output == nil { + return fmt.Errorf("S3 Bucket accelerate configuration (%s) not found", rs.Primary.ID) + } + + return nil + } +} + +func testAccBucketAccelerateConfigurationBasicConfig(bucketName, status string) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" "test" { + bucket = %[1]q + + lifecycle { + ignore_changes = [ + acceleration_status + ] + } +} + +resource "aws_s3_bucket_accelerate_configuration" "test" { + bucket = aws_s3_bucket.test.id + status = %[2]q +} +`, bucketName, status) +} diff --git a/website/docs/r/s3_bucket_accelerate_configuration.html.markdown b/website/docs/r/s3_bucket_accelerate_configuration.html.markdown new file mode 100644 index 00000000000..c5ced8d92ba --- /dev/null +++ b/website/docs/r/s3_bucket_accelerate_configuration.html.markdown @@ -0,0 +1,52 @@ +--- +subcategory: "S3" +layout: "aws" +page_title: "AWS: aws_s3_bucket_accelerate configuration" +description: |- + Provides an S3 bucket accelerate configuration resource. +--- + +# Resource: aws_s3_bucket_accelerate_configuration + +Provides an S3 bucket accelerate configuration resource. See the [Requirements for using Transfer Acceleration](https://docs.aws.amazon.com/AmazonS3/latest/userguide/transfer-acceleration.html#transfer-acceleration-requirements) for more details. + +## Example Usage + +```terraform +resource "aws_s3_bucket" "mybucket" { + bucket = "mybucket" +} + +resource "aws_s3_bucket_accelerate_configuration" "example" { + bucket = aws_s3_bucket.mybucket.bucket + status = "Enabled" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `bucket` - (Required, Forces new resource) The name of the bucket. +* `expected_bucket_owner` - (Optional, Forces new resource) The account ID of the expected bucket owner. +* `status` - (Required) The transfer acceleration state of the bucket. Valid values: `Enabled`, `Suspended`. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The `bucket` or `bucket` and `expected_bucket_owner` separated by a comma (`,`) if the latter is provided. + +## Import + +S3 bucket accelerate configuration can be imported using the `bucket` e.g., + +``` +$ terraform import aws_s3_bucket_accelerate_configuration.example bucket-name +``` + +In addition, S3 bucket accelerate configuration can be imported using the `bucket` and `expected_bucket_owner` separated by a comma (`,`) e.g., + +``` +$ terraform import aws_s3_bucket_accelerate_configuration.example bucket-name,123456789012 +``` From ecd679a30c0ca5d3a11069b0e0c83bc8e8a0ce6d Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Sun, 16 Jan 2022 11:44:06 -0500 Subject: [PATCH 2/5] Update CHANGELOG for #22617 --- .changelog/22617.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/22617.txt diff --git a/.changelog/22617.txt b/.changelog/22617.txt new file mode 100644 index 00000000000..e735cf26408 --- /dev/null +++ b/.changelog/22617.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_s3_bucket_accelerate_configuration +``` From aaf36622b0f6e2259abbbcdf1a43a812e6a11be2 Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Sun, 16 Jan 2022 13:10:17 -0500 Subject: [PATCH 3/5] terrafmt acctest configs --- internal/service/s3/bucket_accelerate_configuration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/s3/bucket_accelerate_configuration_test.go b/internal/service/s3/bucket_accelerate_configuration_test.go index 12beacde409..db8c55673a7 100644 --- a/internal/service/s3/bucket_accelerate_configuration_test.go +++ b/internal/service/s3/bucket_accelerate_configuration_test.go @@ -177,7 +177,7 @@ resource "aws_s3_bucket" "test" { lifecycle { ignore_changes = [ - acceleration_status + acceleration_status ] } } From 8c2d039c8046f33ae34a08cf8d19230aea0b982b Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Wed, 2 Feb 2022 20:59:17 -0500 Subject: [PATCH 4/5] update imports referencing tfawserr --- internal/service/s3/bucket_accelerate_configuration.go | 2 +- internal/service/s3/bucket_accelerate_configuration_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/s3/bucket_accelerate_configuration.go b/internal/service/s3/bucket_accelerate_configuration.go index aaac7b91c62..2d7c3abedd8 100644 --- a/internal/service/s3/bucket_accelerate_configuration.go +++ b/internal/service/s3/bucket_accelerate_configuration.go @@ -8,7 +8,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/s3" - "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "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/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" diff --git a/internal/service/s3/bucket_accelerate_configuration_test.go b/internal/service/s3/bucket_accelerate_configuration_test.go index db8c55673a7..90a5bc689fe 100644 --- a/internal/service/s3/bucket_accelerate_configuration_test.go +++ b/internal/service/s3/bucket_accelerate_configuration_test.go @@ -7,7 +7,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudfront" "github.com/aws/aws-sdk-go/service/s3" - "github.com/hashicorp/aws-sdk-go-base/tfawserr" + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" From df0a7de4f09e7d74aca632b3996ab826db87b896 Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Thu, 3 Feb 2022 01:32:35 -0500 Subject: [PATCH 5/5] r/s3_bucket_accelerate_configuration: re-use share id create/parse method --- .../s3/bucket_accelerate_configuration.go | 38 ++----------------- .../bucket_accelerate_configuration_test.go | 22 ++++++++++- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/internal/service/s3/bucket_accelerate_configuration.go b/internal/service/s3/bucket_accelerate_configuration.go index 2d7c3abedd8..57084f8a597 100644 --- a/internal/service/s3/bucket_accelerate_configuration.go +++ b/internal/service/s3/bucket_accelerate_configuration.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/s3" @@ -73,7 +72,7 @@ func resourceBucketAccelerateConfigurationCreate(ctx context.Context, d *schema. return diag.FromErr(fmt.Errorf("error creating S3 bucket (%s) accelerate configuration: %w", bucket, err)) } - d.SetId(resourceBucketAccelerateConfigurationCreateResourceID(bucket, expectedBucketOwner)) + d.SetId(CreateResourceID(bucket, expectedBucketOwner)) return resourceBucketAccelerateConfigurationRead(ctx, d, meta) } @@ -81,7 +80,7 @@ func resourceBucketAccelerateConfigurationCreate(ctx context.Context, d *schema. func resourceBucketAccelerateConfigurationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).S3Conn - bucket, expectedBucketOwner, err := resourceBucketAccelerateConfigurationParseResourceID(d.Id()) + bucket, expectedBucketOwner, err := ParseResourceID(d.Id()) if err != nil { return diag.FromErr(err) } @@ -125,7 +124,7 @@ func resourceBucketAccelerateConfigurationRead(ctx context.Context, d *schema.Re func resourceBucketAccelerateConfigurationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).S3Conn - bucket, expectedBucketOwner, err := resourceBucketAccelerateConfigurationParseResourceID(d.Id()) + bucket, expectedBucketOwner, err := ParseResourceID(d.Id()) if err != nil { return diag.FromErr(err) } @@ -153,7 +152,7 @@ func resourceBucketAccelerateConfigurationUpdate(ctx context.Context, d *schema. func resourceBucketAccelerateConfigurationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { conn := meta.(*conns.AWSClient).S3Conn - bucket, expectedBucketOwner, err := resourceBucketAccelerateConfigurationParseResourceID(d.Id()) + bucket, expectedBucketOwner, err := ParseResourceID(d.Id()) if err != nil { return diag.FromErr(err) } @@ -181,32 +180,3 @@ func resourceBucketAccelerateConfigurationDelete(ctx context.Context, d *schema. return nil } - -func resourceBucketAccelerateConfigurationCreateResourceID(bucket, expectedBucketOwner string) string { - if bucket == "" { - return expectedBucketOwner - } - - if expectedBucketOwner == "" { - return bucket - } - - parts := []string{bucket, expectedBucketOwner} - id := strings.Join(parts, ",") - - return id -} - -func resourceBucketAccelerateConfigurationParseResourceID(id string) (string, string, error) { - parts := strings.Split(id, ",") - - if len(parts) == 1 && parts[0] != "" { - return parts[0], "", nil - } - - if len(parts) == 2 && parts[0] != "" && parts[1] != "" { - return parts[0], parts[1], nil - } - - return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected BUCKET or BUCKET,EXPECTED_BUCKET_OWNER", id) -} diff --git a/internal/service/s3/bucket_accelerate_configuration_test.go b/internal/service/s3/bucket_accelerate_configuration_test.go index 90a5bc689fe..389d9d3e7a1 100644 --- a/internal/service/s3/bucket_accelerate_configuration_test.go +++ b/internal/service/s3/bucket_accelerate_configuration_test.go @@ -117,8 +117,17 @@ func testAccCheckBucketAccelerateConfigurationDestroy(s *terraform.State) error continue } + bucket, expectedBucketOwner, err := tfs3.ParseResourceID(rs.Primary.ID) + if err != nil { + return err + } + input := &s3.GetBucketAccelerateConfigurationInput{ - Bucket: aws.String(rs.Primary.ID), + Bucket: aws.String(bucket), + } + + if expectedBucketOwner != "" { + input.ExpectedBucketOwner = aws.String(expectedBucketOwner) } output, err := conn.GetBucketAccelerateConfiguration(input) @@ -152,8 +161,17 @@ func testAccCheckBucketAccelerateConfigurationExists(resourceName string) resour conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn + bucket, expectedBucketOwner, err := tfs3.ParseResourceID(rs.Primary.ID) + if err != nil { + return err + } + input := &s3.GetBucketAccelerateConfigurationInput{ - Bucket: aws.String(rs.Primary.ID), + Bucket: aws.String(bucket), + } + + if expectedBucketOwner != "" { + input.ExpectedBucketOwner = aws.String(expectedBucketOwner) } output, err := conn.GetBucketAccelerateConfiguration(input)