Skip to content

Commit

Permalink
r/aws_s3control_bucket_lifecycle_configuration: Switch to 'WithoutTim…
Browse files Browse the repository at this point in the history
…eout' CRUD handlers (#15090).

Acceptance test output:

% make testacc TESTARGS='-run=TestAccS3ControlBucketLifecycleConfiguration_' PKG=s3control ACCTEST_PARALLELISM=3
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/s3control/... -v -count 1 -parallel 3  -run=TestAccS3ControlBucketLifecycleConfiguration_ -timeout 180m
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_basic
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_basic
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_disappears
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_disappears
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_RuleAbortIncompleteMultipartUpload_daysAfterInitiation
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_RuleAbortIncompleteMultipartUpload_daysAfterInitiation
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_date
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_date
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_days
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_days
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_expiredObjectDeleteMarker
    acctest.go:71: S3 on Outposts does not error or save it in the API when receiving this parameter
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_expiredObjectDeleteMarker (0.00s)
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_prefix
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_prefix
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_tags
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_tags
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_Rule_id
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_Rule_id
=== RUN   TestAccS3ControlBucketLifecycleConfiguration_Rule_status
=== PAUSE TestAccS3ControlBucketLifecycleConfiguration_Rule_status
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_basic
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_prefix
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_Rule_id
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_prefix
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_prefix (4.26s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_tags
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_basic
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_basic (4.32s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_date
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_Rule_id
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_Rule_id (4.38s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_days
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_tags
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_RuleFilter_tags (0.20s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleAbortIncompleteMultipartUpload_daysAfterInitiation
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_date
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_date (0.18s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_disappears
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_days
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_RuleExpiration_days (0.24s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_Rule_status
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_RuleAbortIncompleteMultipartUpload_daysAfterInitiation
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_RuleAbortIncompleteMultipartUpload_daysAfterInitiation (0.19s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_disappears
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_disappears (0.17s)
=== CONT  TestAccS3ControlBucketLifecycleConfiguration_Rule_status
    acctest.go:1368: skipping since no Outposts found
--- SKIP: TestAccS3ControlBucketLifecycleConfiguration_Rule_status (1.39s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/s3control	12.331s
  • Loading branch information
ewbankkit committed Dec 23, 2022
1 parent 6f101c4 commit edcb1a7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 96 deletions.
1 change: 0 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,6 @@ func New(ctx context.Context) (*schema.Provider, error) {
"aws_s3_bucket_object": s3.ResourceBucketObject(), // DEPRECATED: use aws_s3_object instead

"aws_s3control_bucket": s3control.ResourceBucket(),
"aws_s3control_bucket_lifecycle_configuration": s3control.ResourceBucketLifecycleConfiguration(),
"aws_s3control_bucket_policy": s3control.ResourceBucketPolicy(),
"aws_s3control_multi_region_access_point": s3control.ResourceMultiRegionAccessPoint(),
"aws_s3control_multi_region_access_point_policy": s3control.ResourceMultiRegionAccessPointPolicy(),
Expand Down
119 changes: 62 additions & 57 deletions internal/service/s3control/bucket_lifecycle_configuration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package s3control

import (
"context"
"fmt"
"log"
"time"
Expand All @@ -9,19 +10,26 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/s3control"
"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"
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"
)

func ResourceBucketLifecycleConfiguration() *schema.Resource {
func init() {
_sp.registerSDKResourceFactory("aws_s3control_bucket_lifecycle_configuration", resourceBucketLifecycleConfiguration)
}

func resourceBucketLifecycleConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceBucketLifecycleConfigurationCreate,
Read: resourceBucketLifecycleConfigurationRead,
Update: resourceBucketLifecycleConfigurationUpdate,
Delete: resourceBucketLifecycleConfigurationDelete,
CreateWithoutTimeout: resourceBucketLifecycleConfigurationCreate,
ReadWithoutTimeout: resourceBucketLifecycleConfigurationRead,
UpdateWithoutTimeout: resourceBucketLifecycleConfigurationUpdate,
DeleteWithoutTimeout: resourceBucketLifecycleConfigurationDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -117,19 +125,19 @@ func ResourceBucketLifecycleConfiguration() *schema.Resource {
}
}

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

bucket := d.Get("bucket").(string)

parsedArn, err := arn.Parse(bucket)

if err != nil {
return fmt.Errorf("error parsing S3 Control Bucket ARN (%s): %w", bucket, err)
return diag.FromErr(err)
}

if parsedArn.AccountID == "" {
return fmt.Errorf("error parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
return diag.Errorf("parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
}

input := &s3control.PutBucketLifecycleConfigurationInput{
Expand All @@ -140,83 +148,62 @@ func resourceBucketLifecycleConfigurationCreate(d *schema.ResourceData, meta int
},
}

_, err = conn.PutBucketLifecycleConfiguration(input)
_, err = conn.PutBucketLifecycleConfigurationWithContext(ctx, input)

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

d.SetId(bucket)

return resourceBucketLifecycleConfigurationRead(d, meta)
return resourceBucketLifecycleConfigurationRead(ctx, d, meta)
}

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

parsedArn, err := arn.Parse(d.Id())

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

if parsedArn.AccountID == "" {
return fmt.Errorf("error parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
return diag.Errorf("parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
}

input := &s3control.GetBucketLifecycleConfigurationInput{
AccountId: aws.String(parsedArn.AccountID),
Bucket: aws.String(d.Id()),
}

output, err := conn.GetBucketLifecycleConfiguration(input)
output, err := FindBucketLifecycleConfigurationByTwoPartKey(ctx, conn, parsedArn.AccountID, d.Id())

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

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

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

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

if output == nil {
return fmt.Errorf("error reading S3 Control Lifecycle Configuration (%s): empty response", d.Id())
return diag.Errorf("reading S3 Control Bucket Lifecycle Configuration (%s): %s", d.Id(), err)
}

d.Set("bucket", d.Id())

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

return nil
}

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

parsedArn, err := arn.Parse(d.Id())

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

if parsedArn.AccountID == "" {
return fmt.Errorf("error parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
return diag.Errorf("parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
}

input := &s3control.PutBucketLifecycleConfigurationInput{
Expand All @@ -227,52 +214,70 @@ func resourceBucketLifecycleConfigurationUpdate(d *schema.ResourceData, meta int
},
}

_, err = conn.PutBucketLifecycleConfiguration(input)
_, err = conn.PutBucketLifecycleConfigurationWithContext(ctx, input)

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

return resourceBucketLifecycleConfigurationRead(d, meta)
return resourceBucketLifecycleConfigurationRead(ctx, d, meta)
}

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

parsedArn, err := arn.Parse(d.Id())

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

if parsedArn.AccountID == "" {
return fmt.Errorf("error parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
return diag.Errorf("parsing S3 Control Bucket ARN (%s): unknown format", d.Id())
}

input := &s3control.DeleteBucketLifecycleConfigurationInput{
AccountId: aws.String(parsedArn.AccountID),
Bucket: aws.String(d.Id()),
}

_, err = conn.DeleteBucketLifecycleConfiguration(input)
_, err = conn.DeleteBucketLifecycleConfigurationWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, "NoSuchBucket") {
if tfawserr.ErrCodeEquals(err, errCodeNoSuchBucket, errCodeNoSuchLifecycleConfiguration, errCodeNoSuchOutpost) {
return nil
}

if tfawserr.ErrCodeEquals(err, "NoSuchLifecycleConfiguration") {
return nil
if err != nil {
return diag.Errorf("deleting S3 Control Bucket Lifecycle Configuration (%s): %s", d.Id(), err)
}

if tfawserr.ErrCodeEquals(err, "NoSuchOutpost") {
return nil
return nil
}

func FindBucketLifecycleConfigurationByTwoPartKey(ctx context.Context, conn *s3control.S3Control, accountID, bucket string) (*s3control.GetBucketLifecycleConfigurationOutput, error) {
input := &s3control.GetBucketLifecycleConfigurationInput{
AccountId: aws.String(accountID),
Bucket: aws.String(bucket),
}

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

if tfawserr.ErrCodeEquals(err, errCodeNoSuchBucket, errCodeNoSuchLifecycleConfiguration, errCodeNoSuchOutpost) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

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

return nil
if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}

func expandAbortIncompleteMultipartUpload(tfList []interface{}) *s3control.AbortIncompleteMultipartUpload {
Expand Down
42 changes: 12 additions & 30 deletions internal/service/s3control/bucket_lifecycle_configuration_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package s3control_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/s3control"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfs3control "github.com/hashicorp/terraform-provider-aws/internal/service/s3control"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func TestAccS3ControlBucketLifecycleConfiguration_basic(t *testing.T) {
Expand Down Expand Up @@ -428,63 +428,45 @@ func testAccCheckBucketLifecycleConfigurationDestroy(s *terraform.State) error {
parsedArn, err := arn.Parse(rs.Primary.ID)

if err != nil {
return fmt.Errorf("error parsing S3 Control Bucket ARN (%s): %w", rs.Primary.ID, err)
}

input := &s3control.GetBucketLifecycleConfigurationInput{
AccountId: aws.String(parsedArn.AccountID),
Bucket: aws.String(rs.Primary.ID),
}

_, err = conn.GetBucketLifecycleConfiguration(input)

if tfawserr.ErrCodeEquals(err, "NoSuchBucket") {
continue
return err
}

if tfawserr.ErrCodeEquals(err, "NoSuchLifecycleConfiguration") {
continue
}
_, err = tfs3control.FindBucketLifecycleConfigurationByTwoPartKey(context.Background(), conn, parsedArn.AccountID, rs.Primary.ID)

if tfawserr.ErrCodeEquals(err, "NoSuchOutpost") {
if tfresource.NotFound(err) {
continue
}

if err != nil {
return err
}

return fmt.Errorf("S3 Control Bucket Lifecycle Configuration (%s) still exists", rs.Primary.ID)
return fmt.Errorf("S3 Control Bucket Lifecycle Configuration %s still exists", rs.Primary.ID)
}

return nil
}

func testAccCheckBucketLifecycleConfigurationExists(resourceName string) resource.TestCheckFunc {
func testAccCheckBucketLifecycleConfigurationExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no resource ID is set")
return fmt.Errorf("No S3 Control Bucket Lifecycle Configuration ID is set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).S3ControlConn()

parsedArn, err := arn.Parse(rs.Primary.ID)

if err != nil {
return fmt.Errorf("error parsing S3 Control Bucket ARN (%s): %w", rs.Primary.ID, err)
}

input := &s3control.GetBucketLifecycleConfigurationInput{
AccountId: aws.String(parsedArn.AccountID),
Bucket: aws.String(rs.Primary.ID),
return err
}

_, err = conn.GetBucketLifecycleConfiguration(input)
_, err = tfs3control.FindBucketLifecycleConfigurationByTwoPartKey(context.Background(), conn, parsedArn.AccountID, rs.Primary.ID)

return err
}
Expand Down
10 changes: 6 additions & 4 deletions internal/service/s3control/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package s3control
// Error code constants missing from AWS Go SDK:
// https://docs.aws.amazon.com/sdk-for-go/api/service/s3control/#pkg-constants
const (
errCodeNoSuchAccessPoint = "NoSuchAccessPoint"
errCodeNoSuchAccessPointPolicy = "NoSuchAccessPointPolicy"
errCodeNoSuchAsyncRequest = "NoSuchAsyncRequest"
// errCodeNoSuchConfiguration = "NoSuchConfiguration"
errCodeNoSuchAccessPoint = "NoSuchAccessPoint"
errCodeNoSuchAccessPointPolicy = "NoSuchAccessPointPolicy"
errCodeNoSuchAsyncRequest = "NoSuchAsyncRequest"
errCodeNoSuchBucket = "NoSuchBucket"
errCodeNoSuchLifecycleConfiguration = "NoSuchLifecycleConfiguration"
errCodeNoSuchMultiRegionAccessPoint = "NoSuchMultiRegionAccessPoint"
errCodeNoSuchOutpost = "NoSuchOutpost"
)
9 changes: 5 additions & 4 deletions internal/service/s3control/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package s3control

// Exports for use in tests only.
var (
ResourceAccessPoint = resourceAccessPoint
ResourceAccessPointPolicy = resourceAccessPointPolicy
ResourceAccountPublicAccessBlock = resourceAccountPublicAccessBlock
ResourceStorageLensConfiguration = resourceStorageLensConfiguration
ResourceAccessPoint = resourceAccessPoint
ResourceAccessPointPolicy = resourceAccessPointPolicy
ResourceAccountPublicAccessBlock = resourceAccountPublicAccessBlock
ResourceBucketLifecycleConfiguration = resourceBucketLifecycleConfiguration
ResourceStorageLensConfiguration = resourceStorageLensConfiguration
)

0 comments on commit edcb1a7

Please sign in to comment.