Skip to content

Commit

Permalink
Merge pull request hashicorp#9861 from terraform-providers/rfd-retry-s3
Browse files Browse the repository at this point in the history
Final retries for s3 timeouts
  • Loading branch information
ryndaniels authored Aug 28, 2019
2 parents 72e690b + cdba030 commit d8abdce
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 42 deletions.
56 changes: 23 additions & 33 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,7 @@ func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
if awsErr.Code() == "OperationAborted" {
log.Printf("[WARN] Got an error while trying to create S3 bucket %s: %s", bucket, err)
return resource.RetryableError(
fmt.Errorf("Error creating S3 bucket %s, retrying: %s",
bucket, err))
fmt.Errorf("Error creating S3 bucket %s, retrying: %s", bucket, err))
}
}
if err != nil {
Expand All @@ -663,7 +662,9 @@ func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {

return nil
})

if isResourceTimeoutError(err) {
_, err = s3conn.CreateBucket(req)
}
if err != nil {
return fmt.Errorf("Error creating S3 bucket: %s", err)
}
Expand Down Expand Up @@ -1328,17 +1329,18 @@ func resourceAwsS3BucketPolicyUpdate(s3conn *s3.S3, d *schema.ResourceData) erro
}

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
if _, err := s3conn.PutBucketPolicy(params); err != nil {
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "MalformedPolicy" || awserr.Code() == s3.ErrCodeNoSuchBucket {
return resource.RetryableError(awserr)
}
}
_, err := s3conn.PutBucketPolicy(params)
if isAWSErr(err, "MalformedPolicy", "") || isAWSErr(err, s3.ErrCodeNoSuchBucket, "") {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})

if isResourceTimeoutError(err) {
_, err = s3conn.PutBucketPolicy(params)
}
if err != nil {
return fmt.Errorf("Error putting S3 policy: %s", err)
}
Expand Down Expand Up @@ -1769,12 +1771,7 @@ func resourceAwsS3BucketServerSideEncryptionConfigurationUpdate(s3conn *s3.S3, d
Bucket: aws.String(bucket),
}

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
if _, err := s3conn.DeleteBucketEncryption(i); err != nil {
return resource.NonRetryableError(err)
}
return nil
})
_, err := s3conn.DeleteBucketEncryption(i)
if err != nil {
return fmt.Errorf("error removing S3 bucket server side encryption: %s", err)
}
Expand Down Expand Up @@ -1848,12 +1845,7 @@ func resourceAwsS3BucketReplicationConfigurationUpdate(s3conn *s3.S3, d *schema.
Bucket: aws.String(bucket),
}

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
if _, err := s3conn.DeleteBucketReplication(i); err != nil {
return resource.NonRetryableError(err)
}
return nil
})
_, err := s3conn.DeleteBucketReplication(i)
if err != nil {
return fmt.Errorf("Error removing S3 bucket replication: %s", err)
}
Expand Down Expand Up @@ -1974,15 +1966,18 @@ func resourceAwsS3BucketReplicationConfigurationUpdate(s3conn *s3.S3, d *schema.
log.Printf("[DEBUG] S3 put bucket replication configuration: %#v", i)

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
if _, err := s3conn.PutBucketReplication(i); err != nil {
if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") ||
isAWSErr(err, "InvalidRequest", "Versioning must be 'Enabled' on the bucket") {
return resource.RetryableError(err)
}
_, err := s3conn.PutBucketReplication(i)
if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "InvalidRequest", "Versioning must be 'Enabled' on the bucket") {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if isResourceTimeoutError(err) {
_, err = s3conn.PutBucketReplication(i)
}
if err != nil {
return fmt.Errorf("Error putting S3 replication configuration: %s", err)
}
Expand All @@ -2000,12 +1995,7 @@ func resourceAwsS3BucketLifecycleUpdate(s3conn *s3.S3, d *schema.ResourceData) e
Bucket: aws.String(bucket),
}

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
if _, err := s3conn.DeleteBucketLifecycle(i); err != nil {
return resource.NonRetryableError(err)
}
return nil
})
_, err := s3conn.DeleteBucketLifecycle(i)
if err != nil {
return fmt.Errorf("Error removing S3 lifecycle: %s", err)
}
Expand Down
11 changes: 11 additions & 0 deletions aws/resource_aws_s3_bucket_inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ func resourceAwsS3BucketInventoryPut(d *schema.ResourceData, meta interface{}) e
}
return nil
})
if isResourceTimeoutError(err) {
_, err = conn.PutBucketInventoryConfiguration(input)
}
if err != nil {
return fmt.Errorf("Error putting S3 bucket inventory configuration: %s", err)
}
Expand Down Expand Up @@ -306,6 +309,14 @@ func resourceAwsS3BucketInventoryRead(d *schema.ResourceData, meta interface{})
}
return nil
})
if isResourceTimeoutError(err) {
output, err = conn.GetBucketInventoryConfiguration(input)
if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") {
if !d.IsNewResource() {
return nil
}
}
}
if err != nil {
return fmt.Errorf("error getting S3 Bucket Inventory (%s): %s", d.Id(), err)
}
Expand Down
3 changes: 3 additions & 0 deletions aws/resource_aws_s3_bucket_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func resourceAwsS3BucketMetricPut(d *schema.ResourceData, meta interface{}) erro
}
return nil
})
if isResourceTimeoutError(err) {
_, err = conn.PutBucketMetricsConfiguration(input)
}
if err != nil {
return fmt.Errorf("Error putting S3 metric configuration: %s", err)
}
Expand Down
6 changes: 5 additions & 1 deletion aws/resource_aws_s3_bucket_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ func resourceAwsS3BucketNotificationPut(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] S3 bucket: %s, Putting notification: %v", bucket, i)
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
if _, err := s3conn.PutBucketNotificationConfiguration(i); err != nil {
_, err := s3conn.PutBucketNotificationConfiguration(i)
if err != nil {
if awserr, ok := err.(awserr.Error); ok {
switch awserr.Message() {
case "Unable to validate the following destination configurations":
Expand All @@ -323,6 +324,9 @@ func resourceAwsS3BucketNotificationPut(d *schema.ResourceData, meta interface{}
// Successful put configuration
return nil
})
if isResourceTimeoutError(err) {
_, err = s3conn.PutBucketNotificationConfiguration(i)
}
if err != nil {
return fmt.Errorf("Error putting S3 notification configuration: %s", err)
}
Expand Down
15 changes: 8 additions & 7 deletions aws/resource_aws_s3_bucket_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ func resourceAwsS3BucketPolicyPut(d *schema.ResourceData, meta interface{}) erro
}

err := resource.Retry(1*time.Minute, func() *resource.RetryError {
if _, err := s3conn.PutBucketPolicy(params); err != nil {
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "MalformedPolicy" {
return resource.RetryableError(awserr)
}
}
_, err := s3conn.PutBucketPolicy(params)
if isAWSErr(err, "MalformedPolicy", "") {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})

if isResourceTimeoutError(err) {
_, err = s3conn.PutBucketPolicy(params)
}
if err != nil {
return fmt.Errorf("Error putting S3 policy: %s", err)
}
Expand Down
7 changes: 6 additions & 1 deletion aws/resource_aws_s3_bucket_public_access_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func resourceAwsS3BucketPublicAccessBlockCreate(d *schema.ResourceData, meta int

return nil
})
if isResourceTimeoutError(err) {
_, err = s3conn.PutPublicAccessBlock(input)
}
if err != nil {
return fmt.Errorf("error creating public access block policy for S3 bucket (%s): %s", bucket, err)
}
Expand Down Expand Up @@ -117,7 +120,9 @@ func resourceAwsS3BucketPublicAccessBlockRead(d *schema.ResourceData, meta inter

return nil
})

if isResourceTimeoutError(err) {
output, err = s3conn.GetPublicAccessBlock(input)
}
if isAWSErr(err, s3control.ErrCodeNoSuchPublicAccessBlockConfiguration, "") {
log.Printf("[WARN] S3 Bucket Public Access Block (%s) not found, removing from state", d.Id())
d.SetId("")
Expand Down

0 comments on commit d8abdce

Please sign in to comment.