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_appautoscaling_policy: retry on rate exceeded during read, update and delete #4594

Merged
merged 1 commit into from
May 21, 2018
Merged
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
49 changes: 42 additions & 7 deletions aws/resource_aws_appautoscaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/applicationautoscaling"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -256,7 +257,7 @@ func resourceAwsAppautoscalingPolicyCreate(d *schema.ResourceData, meta interfac

log.Printf("[DEBUG] ApplicationAutoScaling PutScalingPolicy: %#v", params)
var resp *applicationautoscaling.PutScalingPolicyOutput
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
var err error
resp, err = conn.PutScalingPolicy(&params)
if err != nil {
Expand Down Expand Up @@ -285,10 +286,24 @@ func resourceAwsAppautoscalingPolicyCreate(d *schema.ResourceData, meta interfac
}

func resourceAwsAppautoscalingPolicyRead(d *schema.ResourceData, meta interface{}) error {
p, err := getAwsAppautoscalingPolicy(d, meta)
var p *applicationautoscaling.ScalingPolicy

err := resource.Retry(2*time.Minute, func() *resource.RetryError {
var err error
p, err = getAwsAppautoscalingPolicy(d, meta)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I was not clear earlier. We can still use the isAWSErr() helper here to keep the code cleaner -- I'll fix these on merge.

if isAWSErr(err, applicationautoscaling.ErrCodeFailedResourceAccessException, "") {

if awsErr.Code() == applicationautoscaling.ErrCodeFailedResourceAccessException {
return resource.RetryableError(err)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return resource.NonRetryableError(err) as the last statement inside the if err != nil conditional so its passed up to the returned error, otherwise the outside code will not see these. I'll fix these on merge.

return nil
})
if err != nil {
return err
return fmt.Errorf("Failed to read scaling policy: %s", err)
}

if p == nil {
log.Printf("[WARN] Application AutoScaling Policy (%s) not found, removing from state", d.Id())
d.SetId("")
Expand Down Expand Up @@ -320,7 +335,17 @@ func resourceAwsAppautoscalingPolicyUpdate(d *schema.ResourceData, meta interfac
}

log.Printf("[DEBUG] Application Autoscaling Update Scaling Policy: %#v", params)
_, err := conn.PutScalingPolicy(&params)
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
_, err := conn.PutScalingPolicy(&params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == applicationautoscaling.ErrCodeFailedResourceAccessException {
return resource.RetryableError(err)
}
}
}
return nil
})
if err != nil {
return fmt.Errorf("Failed to update scaling policy: %s", err)
}
Expand All @@ -345,10 +370,20 @@ func resourceAwsAppautoscalingPolicyDelete(d *schema.ResourceData, meta interfac
ServiceNamespace: aws.String(d.Get("service_namespace").(string)),
}
log.Printf("[DEBUG] Deleting Application AutoScaling Policy opts: %#v", params)
if _, err := conn.DeleteScalingPolicy(&params); err != nil {
return fmt.Errorf("Failed to delete autoscaling policy: %s", err)
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
_, err = conn.DeleteScalingPolicy(&params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == applicationautoscaling.ErrCodeFailedResourceAccessException {
return resource.RetryableError(err)
}
}
}
return nil
})
if err != nil {
return fmt.Errorf("Failed to delete scaling policy: %s", err)
}

return nil
}

Expand Down