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

Add resource aws_s3_bucket_versioning #5132

Merged
merged 3 commits into from
Jan 21, 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/5132.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_s3_bucket_versioning
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,7 @@ func Provider() *schema.Provider {
"aws_s3_bucket_policy": s3.ResourceBucketPolicy(),
"aws_s3_bucket_public_access_block": s3.ResourceBucketPublicAccessBlock(),
"aws_s3_bucket_replication_configuration": s3.ResourceBucketReplicationConfiguration(),
"aws_s3_bucket_versioning": s3.ResourceBucketVersioning(),
"aws_s3_object_copy": s3.ResourceObjectCopy(),

"aws_s3_access_point": s3control.ResourceAccessPoint(),
Expand Down
6 changes: 3 additions & 3 deletions internal/service/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,13 +781,13 @@ func resourceBucketUpdate(d *schema.ResourceData, meta interface{}) error {

if d.IsNewResource() {
if versioning := expandVersioningWhenIsNewResource(v); versioning != nil {
err := resourceBucketVersioningUpdate(conn, d.Id(), versioning)
err := resourceBucketInternalVersioningUpdate(conn, d.Id(), versioning)
if err != nil {
return err
}
}
} else {
if err := resourceBucketVersioningUpdate(conn, d.Id(), expandVersioning(v)); err != nil {
if err := resourceBucketInternalVersioningUpdate(conn, d.Id(), expandVersioning(v)); err != nil {
return err
}
}
Expand Down Expand Up @@ -1851,7 +1851,7 @@ func resourceBucketACLUpdate(conn *s3.S3, d *schema.ResourceData) error {
return nil
}

func resourceBucketVersioningUpdate(conn *s3.S3, bucket string, versioningConfig *s3.VersioningConfiguration) error {
func resourceBucketInternalVersioningUpdate(conn *s3.S3, bucket string, versioningConfig *s3.VersioningConfiguration) error {
input := &s3.PutBucketVersioningInput{
Bucket: aws.String(bucket),
VersioningConfiguration: versioningConfig,
Expand Down
249 changes: 249 additions & 0 deletions internal/service/s3/bucket_versioning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
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/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 ResourceBucketVersioning() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBucketVersioningCreate,
ReadContext: resourceBucketVersioningRead,
UpdateContext: resourceBucketVersioningUpdate,
DeleteContext: resourceBucketVersioningDelete,
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,
},
"mfa": {
Type: schema.TypeString,
Optional: true,
},
"versioning_configuration": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mfa_delete": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(s3.MFADelete_Values(), false),
},
"status": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(s3.BucketVersioningStatus_Values(), false),
},
},
},
},
},
}
}

func resourceBucketVersioningCreate(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.PutBucketVersioningInput{
Bucket: aws.String(bucket),
VersioningConfiguration: expandBucketVersioningConfiguration(d.Get("versioning_configuration").([]interface{})),
}

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

if v, ok := d.GetOk("mfa"); ok {
input.MFA = aws.String(v.(string))
}

_, err := verify.RetryOnAWSCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return conn.PutBucketVersioningWithContext(ctx, input)
})

if err != nil {
return diag.FromErr(fmt.Errorf("error creating S3 bucket versioning for %s: %w", bucket, err))
}

d.SetId(CreateResourceID(bucket, expectedBucketOwner))

return resourceBucketVersioningRead(ctx, d, meta)
}

func resourceBucketVersioningRead(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.GetBucketVersioningInput{
Bucket: aws.String(bucket),
}

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

output, err := conn.GetBucketVersioningWithContext(ctx, input)

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) {
log.Printf("[WARN] S3 Bucket Versioning (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return diag.FromErr(fmt.Errorf("error getting S3 bucket versioning (%s): %w", d.Id(), err))
}

if output == nil {
if d.IsNewResource() {
return diag.FromErr(fmt.Errorf("error getting S3 bucket versioning (%s): empty output", d.Id()))
}
log.Printf("[WARN] S3 Bucket Versioning (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("bucket", bucket)
d.Set("expected_bucket_owner", expectedBucketOwner)
if err := d.Set("versioning_configuration", flattenBucketVersioningConfiguration(output)); err != nil {
return diag.FromErr(fmt.Errorf("error setting versioning_configuration: %w", err))
}

return nil
}

func resourceBucketVersioningUpdate(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.PutBucketVersioningInput{
Bucket: aws.String(bucket),
VersioningConfiguration: expandBucketVersioningConfiguration(d.Get("versioning_configuration").([]interface{})),
}

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

if v, ok := d.GetOk("mfa"); ok {
input.MFA = aws.String(v.(string))
}

_, err = conn.PutBucketVersioningWithContext(ctx, input)

if err != nil {
return diag.FromErr(fmt.Errorf("error updating S3 bucket versioning (%s): %w", d.Id(), err))
}

return resourceBucketVersioningRead(ctx, d, meta)
}

func resourceBucketVersioningDelete(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.PutBucketVersioningInput{
Bucket: aws.String(bucket),
VersioningConfiguration: &s3.VersioningConfiguration{
// Status must be provided thus to "remove" this resource,
// we suspend versioning
Status: aws.String(s3.BucketVersioningStatusSuspended),
},
}

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

_, err = conn.PutBucketVersioningWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) {
return nil
}

if err != nil {
return diag.FromErr(fmt.Errorf("error deleting S3 bucket versioning (%s): %w", d.Id(), err))
}

return nil
}

func expandBucketVersioningConfiguration(l []interface{}) *s3.VersioningConfiguration {
if len(l) == 0 || l[0] == nil {
return nil
}

tfMap, ok := l[0].(map[string]interface{})
if !ok {
return nil
}

result := &s3.VersioningConfiguration{}

if v, ok := tfMap["mfa_delete"].(string); ok && v != "" {
result.MFADelete = aws.String(v)
}

if v, ok := tfMap["status"].(string); ok && v != "" {
result.Status = aws.String(v)
}

return result
}

func flattenBucketVersioningConfiguration(config *s3.GetBucketVersioningOutput) []interface{} {
if config == nil {
return []interface{}{}
}

m := make(map[string]interface{})

if config.MFADelete != nil {
m["mfa_delete"] = aws.StringValue(config.MFADelete)
}

if config.Status != nil {
m["status"] = aws.StringValue(config.Status)
}

return []interface{}{m}
}
Loading