Skip to content

Commit

Permalink
r/s3_bucket_cors_configuration: update ID methods to generic ones; do…
Browse files Browse the repository at this point in the history
…cs formatting
  • Loading branch information
anGie44 committed Jan 27, 2022
1 parent 05f81ed commit 90e21f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
38 changes: 4 additions & 34 deletions internal/service/s3/bucket_cors_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -107,15 +106,15 @@ func resourceBucketCorsConfigurationCreate(ctx context.Context, d *schema.Resour
return diag.FromErr(fmt.Errorf("error creating S3 bucket (%s) CORS configuration: %w", bucket, err))
}

d.SetId(resourceBucketCorsConfigurationCreateResourceID(bucket, expectedBucketOwner))
d.SetId(CreateResourceID(bucket, expectedBucketOwner))

return resourceBucketCorsConfigurationRead(ctx, d, meta)
}

func resourceBucketCorsConfigurationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3Conn

bucket, expectedBucketOwner, err := resourceBucketCorsConfigurationParseResourceID(d.Id())
bucket, expectedBucketOwner, err := ParseResourceID(d.Id())
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -162,7 +161,7 @@ func resourceBucketCorsConfigurationRead(ctx context.Context, d *schema.Resource
func resourceBucketCorsConfigurationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3Conn

bucket, expectedBucketOwner, err := resourceBucketCorsConfigurationParseResourceID(d.Id())
bucket, expectedBucketOwner, err := ParseResourceID(d.Id())
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -190,7 +189,7 @@ func resourceBucketCorsConfigurationUpdate(ctx context.Context, d *schema.Resour
func resourceBucketCorsConfigurationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3Conn

bucket, expectedBucketOwner, err := resourceBucketCorsConfigurationParseResourceID(d.Id())
bucket, expectedBucketOwner, err := ParseResourceID(d.Id())
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -216,35 +215,6 @@ func resourceBucketCorsConfigurationDelete(ctx context.Context, d *schema.Resour
return nil
}

func resourceBucketCorsConfigurationCreateResourceID(bucket, expectedBucketOwner string) string {
if bucket == "" {
return expectedBucketOwner
}

if expectedBucketOwner == "" {
return bucket
}

parts := []string{bucket, expectedBucketOwner}
id := strings.Join(parts, ",")

return id
}

func resourceBucketCorsConfigurationParseResourceID(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)
}

func expandBucketCorsConfigurationCorsRules(l []interface{}) []*s3.CORSRule {
if len(l) == 0 {
return nil
Expand Down
22 changes: 20 additions & 2 deletions internal/service/s3/bucket_cors_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,17 @@ func testAccCheckBucketCorsConfigurationDestroy(s *terraform.State) error {
continue
}

bucket, expectedBucketOwner, err := tfs3.ParseResourceID(rs.Primary.ID)
if err != nil {
return err
}

input := &s3.GetBucketCorsInput{
Bucket: aws.String(rs.Primary.ID),
Bucket: aws.String(bucket),
}

if expectedBucketOwner != "" {
input.ExpectedBucketOwner = aws.String(expectedBucketOwner)
}

output, err := conn.GetBucketCors(input)
Expand Down Expand Up @@ -261,8 +270,17 @@ func testAccCheckBucketCorsConfigurationExists(resourceName string) resource.Tes

conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn

bucket, expectedBucketOwner, err := tfs3.ParseResourceID(rs.Primary.ID)
if err != nil {
return err
}

input := &s3.GetBucketCorsInput{
Bucket: aws.String(rs.Primary.ID),
Bucket: aws.String(bucket),
}

if expectedBucketOwner != "" {
input.ExpectedBucketOwner = aws.String(expectedBucketOwner)
}

output, err := conn.GetBucketCors(input)
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/s3_bucket_cors_configuration.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ subcategory: "S3"
layout: "aws"
page_title: "AWS: aws_s3_bucket_cors_configuration"
description: |-
Provides an S3 bucket CORS configuration resource.
Provides an S3 bucket CORS configuration resource.
---

# Resource: aws_s3_bucket_cors_configuration
Expand Down

0 comments on commit 90e21f1

Please sign in to comment.