Skip to content

Commit

Permalink
Merge pull request #23144 from hashicorp/b-s3-lifecycle-churn
Browse files Browse the repository at this point in the history
s3_bucket_lifecycle_configuration: Fix import, diffs
  • Loading branch information
YakDriver authored Feb 15, 2022
2 parents a6c3cc2 + 3eeb4d0 commit 45719a7
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .changelog/23144.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_s3_bucket_lifecycle_configuration: Fix extraneous diffs especially after import
```
68 changes: 47 additions & 21 deletions internal/service/s3/bucket_lifecycle_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import (
"context"
"fmt"
"log"
"reflect"
"time"

"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/resource"
"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/experimental/nullable"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

Expand Down Expand Up @@ -114,14 +119,12 @@ func ResourceBucketLifecycleConfiguration() *schema.Resource {
},
},
"object_size_greater_than": {
Type: schema.TypeInt,
Type: nullable.TypeNullableInt,
Optional: true,
Default: 0, // API returns 0
},
"object_size_less_than": {
Type: schema.TypeInt,
Type: nullable.TypeNullableInt,
Optional: true,
Default: 0, // API returns 0
},
"prefix": {
Type: schema.TypeString,
Expand Down Expand Up @@ -249,7 +252,7 @@ func resourceBucketLifecycleConfigurationCreate(ctx context.Context, d *schema.R

rules, err := ExpandLifecycleRules(d.Get("rule").([]interface{}))
if err != nil {
return diag.FromErr(fmt.Errorf("error creating S3 Lifecycle Configuration for bucket (%s): %w", bucket, err))
return diag.Errorf("error creating S3 Lifecycle Configuration for bucket (%s): %s", bucket, err)
}

input := &s3.PutBucketLifecycleConfigurationInput{
Expand All @@ -268,11 +271,15 @@ func resourceBucketLifecycleConfigurationCreate(ctx context.Context, d *schema.R
})

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

d.SetId(CreateResourceID(bucket, expectedBucketOwner))

if err = waitForLifecycleConfigurationRulesStatus(ctx, conn, bucket, expectedBucketOwner, rules); err != nil {
return diag.Errorf("error waiting for S3 Lifecycle Configuration for bucket (%s) to reach expected rules status after update: %s", d.Id(), err)
}

return resourceBucketLifecycleConfigurationRead(ctx, d, meta)
}

Expand All @@ -292,30 +299,49 @@ func resourceBucketLifecycleConfigurationRead(ctx context.Context, d *schema.Res
input.ExpectedBucketOwner = aws.String(expectedBucketOwner)
}

output, err := verify.RetryOnAWSCode(ErrCodeNoSuchLifecycleConfiguration, func() (interface{}, error) {
return conn.GetBucketLifecycleConfigurationWithContext(ctx, input)
var lastOutput, output *s3.GetBucketLifecycleConfigurationOutput

err = resource.RetryContext(ctx, lifecycleConfigurationRulesSteadyTimeout, func() *resource.RetryError {
var err error

time.Sleep(lifecycleConfigurationExtraRetryDelay)

output, err = conn.GetBucketLifecycleConfigurationWithContext(ctx, input)

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ErrCodeNoSuchLifecycleConfiguration, s3.ErrCodeNoSuchBucket) {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

if lastOutput == nil || !reflect.DeepEqual(*lastOutput, *output) {
lastOutput = output
return resource.RetryableError(fmt.Errorf("bucket lifecycle configuration has not stablized; trying again"))
}

return nil
})

if tfresource.TimedOut(err) {
output, err = conn.GetBucketLifecycleConfigurationWithContext(ctx, input)
}

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

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

lifecycleConfig, ok := output.(*s3.GetBucketLifecycleConfigurationOutput)

if !ok || lifecycleConfig == nil {
return diag.FromErr(fmt.Errorf("error reading S3 Bucket Lifecycle Configuration (%s): empty output", d.Id()))
return diag.Errorf("error getting S3 Bucket Lifecycle Configuration (%s): %s", d.Id(), err)
}

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

return nil
Expand All @@ -331,7 +357,7 @@ func resourceBucketLifecycleConfigurationUpdate(ctx context.Context, d *schema.R

rules, err := ExpandLifecycleRules(d.Get("rule").([]interface{}))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating S3 Bucket Lifecycle Configuration rule: %w", err))
return diag.Errorf("error updating S3 Bucket Lifecycle Configuration rule: %s", err)
}

input := &s3.PutBucketLifecycleConfigurationInput{
Expand All @@ -350,11 +376,11 @@ func resourceBucketLifecycleConfigurationUpdate(ctx context.Context, d *schema.R
})

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

if err := waitForLifecycleConfigurationRulesStatus(ctx, conn, bucket, expectedBucketOwner, rules); err != nil {
return diag.FromErr(fmt.Errorf("error waiting for S3 Lifecycle Configuration for bucket (%s) to reach expected rules status after update: %w", d.Id(), err))
return diag.Errorf("error waiting for S3 Lifecycle Configuration for bucket (%s) to reach expected rules status after update: %s", d.Id(), err)
}

return resourceBucketLifecycleConfigurationRead(ctx, d, meta)
Expand Down Expand Up @@ -383,7 +409,7 @@ func resourceBucketLifecycleConfigurationDelete(ctx context.Context, d *schema.R
}

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

return nil
Expand Down
Loading

0 comments on commit 45719a7

Please sign in to comment.