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

resource/aws_s3_bucket_object_lock_configuration: Backport resource aws_s3_bucket_object_lock_configuration #23449

Merged
merged 13 commits into from
Mar 15, 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
15 changes: 15 additions & 0 deletions .changelog/23449.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:new-resource
aws_s3_bucket_object_lock_configuration
```

```release-note:enhancement
resource/aws_s3_bucket: Add top-level `object_lock_enabled` parameter
```

```release-note:note
resource/aws_s3_bucket: The `object_lock_configuration.object_lock_enabled` argument has been deprecated. Use the top-level argument `object_lock_enabled` instead.
```

```release-note:note
resource/aws_s3_bucket: The `object_lock_configuration.rule` argument has been deprecated. Use the `aws_s3_bucket_object_lock_configuration` resource instead.
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@ func Provider() *schema.Provider {
"aws_s3_bucket_metric": s3.ResourceBucketMetric(),
"aws_s3_bucket_notification": s3.ResourceBucketNotification(),
"aws_s3_bucket_object": s3.ResourceBucketObject(),
"aws_s3_bucket_object_lock_configuration": s3.ResourceBucketObjectLockConfiguration(),
"aws_s3_bucket_ownership_controls": s3.ResourceBucketOwnershipControls(),
"aws_s3_bucket_policy": s3.ResourceBucketPolicy(),
"aws_s3_bucket_public_access_block": s3.ResourceBucketPublicAccessBlock(),
Expand Down
92 changes: 51 additions & 41 deletions internal/service/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,23 +619,35 @@ func ResourceBucket() *schema.Resource {
},
},

"object_lock_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true, // Can be removed when object_lock_configuration.0.object_lock_enabled is removed
ForceNew: true,
ConflictsWith: []string{"object_lock_configuration"},
},

"object_lock_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_lock_enabled": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(s3.ObjectLockEnabled_Values(), false),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"object_lock_enabled"},
ValidateFunc: validation.StringInSlice(s3.ObjectLockEnabled_Values(), false),
Deprecated: "Use the top-level parameter object_lock_enabled instead",
},

"rule": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_retention": {
Expand Down Expand Up @@ -697,7 +709,8 @@ func resourceBucketCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] S3 bucket create: %s", bucket)

req := &s3.CreateBucketInput{
Bucket: aws.String(bucket),
Bucket: aws.String(bucket),
ObjectLockEnabledForBucket: aws.Bool(d.Get("object_lock_enabled").(bool)),
}

if acl, ok := d.GetOk("acl"); ok {
Expand Down Expand Up @@ -730,9 +743,8 @@ func resourceBucketCreate(d *schema.ResourceData, meta interface{}) error {
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.CreateBucket(req)
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "OperationAborted" {
return resource.RetryableError(
fmt.Errorf("Error creating S3 bucket %s, retrying: %s", bucket, err))
if awsErr.Code() == ErrCodeOperationAborted {
return resource.RetryableError(fmt.Errorf("Error creating S3 bucket %s, retrying: %w", bucket, err))
}
}
if err != nil {
Expand Down Expand Up @@ -853,7 +865,7 @@ func resourceBucketUpdate(d *schema.ResourceData, meta interface{}) error {
}

if d.HasChange("object_lock_configuration") {
if err := resourceBucketObjectLockConfigurationUpdate(conn, d); err != nil {
if err := resourceBucketInternalObjectLockConfigurationUpdate(conn, d); err != nil {
return err
}
}
Expand Down Expand Up @@ -1315,21 +1327,40 @@ func resourceBucketRead(d *schema.ResourceData, meta interface{}) error {
}

// Object Lock configuration.
conf, err := readS3ObjectLockConfiguration(conn, d.Id())
resp, err := verify.RetryOnAWSCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return conn.GetObjectLockConfiguration(&s3.GetObjectLockConfigurationInput{
Bucket: aws.String(d.Id()),
})
})

// The S3 API method calls above can occasionally return no error (i.e. NoSuchBucket)
// after a bucket has been deleted (eventual consistency woes :/), thus, when making extra S3 API calls
// such as GetObjectLockConfiguration, the error should be caught for non-new buckets as follows.
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) {
log.Printf("[WARN] S3 Bucket (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

// Object lock not supported in all partitions (extra guard, also guards in read func)
if err != nil && (meta.(*conns.AWSClient).Partition == endpoints.AwsPartitionID || meta.(*conns.AWSClient).Partition == endpoints.AwsUsGovPartitionID) {
return fmt.Errorf("error getting S3 Bucket Object Lock configuration: %s", err)
if err != nil && !tfawserr.ErrCodeEquals(err, ErrCodeMethodNotAllowed, ErrCodeObjectLockConfigurationNotFound) {
if meta.(*conns.AWSClient).Partition == endpoints.AwsPartitionID || meta.(*conns.AWSClient).Partition == endpoints.AwsUsGovPartitionID {
return fmt.Errorf("error getting S3 Bucket (%s) Object Lock configuration: %w", d.Id(), err)
}
}

if err != nil {
log.Printf("[WARN] Unable to read S3 bucket (%s) object lock configuration: %s", d.Id(), err)
log.Printf("[WARN] Unable to read S3 bucket (%s) Object Lock Configuration: %s", d.Id(), err)
}

if err == nil {
if err := d.Set("object_lock_configuration", conf); err != nil {
return fmt.Errorf("error setting object_lock_configuration: %s", err)
if output, ok := resp.(*s3.GetObjectLockConfigurationOutput); ok && output.ObjectLockConfiguration != nil {
d.Set("object_lock_enabled", aws.StringValue(output.ObjectLockConfiguration.ObjectLockEnabled) == s3.ObjectLockEnabledEnabled)
if err := d.Set("object_lock_configuration", flattenS3ObjectLockConfiguration(output.ObjectLockConfiguration)); err != nil {
return fmt.Errorf("error setting object_lock_configuration: %w", err)
}
} else {
d.Set("object_lock_enabled", false)
d.Set("object_lock_configuration", nil)
}

// Add the region as an attribute
Expand Down Expand Up @@ -2030,7 +2061,7 @@ func resourceBucketInternalServerSideEncryptionConfigurationUpdate(conn *s3.S3,
return nil
}

func resourceBucketObjectLockConfigurationUpdate(conn *s3.S3, d *schema.ResourceData) error {
func resourceBucketInternalObjectLockConfigurationUpdate(conn *s3.S3, d *schema.ResourceData) error {
// S3 Object Lock configuration cannot be deleted, only updated.
req := &s3.PutObjectLockConfigurationInput{
Bucket: aws.String(d.Get("bucket").(string)),
Expand Down Expand Up @@ -2818,27 +2849,6 @@ type S3Website struct {
// S3 Object Lock functions.
//

func readS3ObjectLockConfiguration(conn *s3.S3, bucket string) ([]interface{}, error) {
resp, err := verify.RetryOnAWSCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return conn.GetObjectLockConfiguration(&s3.GetObjectLockConfigurationInput{
Bucket: aws.String(bucket),
})
})
if err != nil {
// Certain S3 implementations do not include this API
if tfawserr.ErrMessageContains(err, "MethodNotAllowed", "") {
return nil, nil
}

if tfawserr.ErrMessageContains(err, "ObjectLockConfigurationNotFoundError", "") {
return nil, nil
}
return nil, err
}

return flattenS3ObjectLockConfiguration(resp.(*s3.GetObjectLockConfigurationOutput).ObjectLockConfiguration), nil
}

func expandS3ObjectLockConfiguration(vConf []interface{}) *s3.ObjectLockConfiguration {
if len(vConf) == 0 || vConf[0] == nil {
return nil
Expand Down
8 changes: 2 additions & 6 deletions internal/service/s3/bucket_object_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,7 @@ resource "aws_s3_bucket" "object_bucket" {
enabled = true
}

object_lock_configuration {
object_lock_enabled = "Enabled"
}
object_lock_enabled = true
}

resource "aws_s3_bucket_object" "object" {
Expand All @@ -647,9 +645,7 @@ resource "aws_s3_bucket" "object_bucket" {
enabled = true
}

object_lock_configuration {
object_lock_enabled = "Enabled"
}
object_lock_enabled = true
}

resource "aws_s3_bucket_object" "object" {
Expand Down
Loading