Skip to content

Commit

Permalink
r/s3_bucket_object_lock_configuration: re-use share id create/parse m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
anGie44 committed Feb 3, 2022
1 parent ba102ae commit 53c0e60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
38 changes: 4 additions & 34 deletions internal/service/s3/bucket_object_lock_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 @@ -124,15 +123,15 @@ func resourceBucketObjectLockConfigurationCreate(ctx context.Context, d *schema.
return diag.FromErr(fmt.Errorf("error creating S3 bucket (%s) Object Lock configuration: %w", bucket, err))
}

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

return resourceBucketObjectLockConfigurationRead(ctx, d, meta)
}

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

bucket, expectedBucketOwner, err := resourceBucketObjectLockConfigurationParseResourceID(d.Id())
bucket, expectedBucketOwner, err := ParseResourceID(d.Id())
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -182,7 +181,7 @@ func resourceBucketObjectLockConfigurationRead(ctx context.Context, d *schema.Re
func resourceBucketObjectLockConfigurationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3Conn

bucket, expectedBucketOwner, err := resourceBucketObjectLockConfigurationParseResourceID(d.Id())
bucket, expectedBucketOwner, err := ParseResourceID(d.Id())
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -221,7 +220,7 @@ func resourceBucketObjectLockConfigurationUpdate(ctx context.Context, d *schema.
func resourceBucketObjectLockConfigurationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).S3Conn

bucket, expectedBucketOwner, err := resourceBucketObjectLockConfigurationParseResourceID(d.Id())
bucket, expectedBucketOwner, err := ParseResourceID(d.Id())
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -252,35 +251,6 @@ func resourceBucketObjectLockConfigurationDelete(ctx context.Context, d *schema.
return nil
}

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

if expectedBucketOwner == "" {
return bucket
}

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

return id
}

func resourceBucketObjectLockConfigurationParseResourceID(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 expandBucketObjectLockConfigurationRule(l []interface{}) *s3.ObjectLockRule {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down
22 changes: 20 additions & 2 deletions internal/service/s3/bucket_object_lock_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,17 @@ func testAccCheckBucketObjectLockConfigurationDestroy(s *terraform.State) error
continue
}

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

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

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

output, err := conn.GetObjectLockConfiguration(input)
Expand Down Expand Up @@ -145,8 +154,17 @@ func testAccCheckBucketObjectLockConfigurationExists(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.GetObjectLockConfigurationInput{
Bucket: aws.String(rs.Primary.ID),
Bucket: aws.String(bucket),
}

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

output, err := conn.GetObjectLockConfiguration(input)
Expand Down

0 comments on commit 53c0e60

Please sign in to comment.