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/s3_bucket_accelerate_configuration: new resource #22617

Merged
merged 5 commits into from
Feb 3, 2022
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
3 changes: 3 additions & 0 deletions .changelog/22617.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_s3_bucket_accelerate_configuration
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
182 changes: 182 additions & 0 deletions internal/service/s3/bucket_accelerate_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package s3

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"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"
"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(CreateResourceID(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 := ParseResourceID(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 := ParseResourceID(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 := ParseResourceID(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
}
Loading