Skip to content

Commit

Permalink
Merge pull request #1840 from hashicorp/f-aws-asg-handle-scaling-acti…
Browse files Browse the repository at this point in the history
…vity-in-progress-errors

provider/aws: handle in progress errs from ASG deletes
  • Loading branch information
phinze committed May 7, 2015
2 parents 7f93879 + 03530d1 commit a9678bd
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions builtin/providers/aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
}

func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
conn := meta.(*AWSClient).autoscalingconn

var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupInput
autoScalingGroupOpts.AutoScalingGroupName = aws.String(d.Get("name").(string))
Expand Down Expand Up @@ -166,7 +166,7 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
}

log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts)
_, err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
_, err := conn.CreateAutoScalingGroup(&autoScalingGroupOpts)
if err != nil {
return fmt.Errorf("Error creating Autoscaling Group: %s", err)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
}

func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
conn := meta.(*AWSClient).autoscalingconn

opts := autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: aws.String(d.Id()),
Expand All @@ -234,14 +234,14 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
opts.HealthCheckGracePeriod = aws.Long(int64(d.Get("health_check_grace_period").(int)))
}

if err := setAutoscalingTags(autoscalingconn, d); err != nil {
if err := setAutoscalingTags(conn, d); err != nil {
return err
} else {
d.SetPartial("tag")
}

log.Printf("[DEBUG] AutoScaling Group update configuration: %#v", opts)
_, err := autoscalingconn.UpdateAutoScalingGroup(&opts)
_, err := conn.UpdateAutoScalingGroup(&opts)
if err != nil {
d.Partial(true)
return fmt.Errorf("Error updating Autoscaling group: %s", err)
Expand All @@ -251,7 +251,7 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
}

func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
conn := meta.(*AWSClient).autoscalingconn

// Read the autoscaling group first. If it doesn't exist, we're done.
// We need the group in order to check if there are instances attached.
Expand Down Expand Up @@ -281,11 +281,28 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
deleteopts.ForceDelete = aws.Boolean(true)
}

if _, err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts); err != nil {
autoscalingerr, ok := err.(aws.APIError)
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
return nil
// We retry the delete operation to handle InUse/InProgress errors coming
// from scaling operations. We should be able to sneak in a delete in between
// scaling operations within 5m.
err = resource.Retry(5*time.Minute, func() error {
if _, err := conn.DeleteAutoScalingGroup(&deleteopts); err != nil {
if awserr, ok := err.(aws.APIError); ok {
switch awserr.Code {
case "InvalidGroup.NotFound":
// Already gone? Sure!
return nil
case "ResourceInUse", "ScalingActivityInProgress":
// These are retryable
return awserr
}
}
// Didn't recognize the error, so shouldn't retry.
return resource.RetryError{Err: err}
}
// Successful delete
return nil
})
if err != nil {
return err
}

Expand All @@ -300,14 +317,14 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
func getAwsAutoscalingGroup(
d *schema.ResourceData,
meta interface{}) (*autoscaling.AutoScalingGroup, error) {
autoscalingconn := meta.(*AWSClient).autoscalingconn
conn := meta.(*AWSClient).autoscalingconn

describeOpts := autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{aws.String(d.Id())},
}

log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts)
describeGroups, err := conn.DescribeAutoScalingGroups(&describeOpts)
if err != nil {
autoscalingerr, ok := err.(aws.APIError)
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
Expand All @@ -331,7 +348,7 @@ func getAwsAutoscalingGroup(
}

func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn
conn := meta.(*AWSClient).autoscalingconn

// First, set the capacity to zero so the group will drain
log.Printf("[DEBUG] Reducing autoscaling group capacity to zero")
Expand All @@ -341,7 +358,7 @@ func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{})
MinSize: aws.Long(0),
MaxSize: aws.Long(0),
}
if _, err := autoscalingconn.UpdateAutoScalingGroup(&opts); err != nil {
if _, err := conn.UpdateAutoScalingGroup(&opts); err != nil {
return fmt.Errorf("Error setting capacity to zero to drain: %s", err)
}

Expand Down

0 comments on commit a9678bd

Please sign in to comment.