Refactoring the getAwsAutoscalingGroup func to accept only the arguments it needs rather than ResourceData and meta. This makes it more portable and reusable

This commit is contained in:
stack72 2016-01-28 17:52:35 +00:00
parent 4d1f96af7f
commit bd4f8ed800
2 changed files with 19 additions and 13 deletions

View File

@ -229,11 +229,15 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
} }
func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) error {
g, err := getAwsAutoscalingGroup(d, meta) conn := meta.(*AWSClient).autoscalingconn
g, err := getAwsAutoscalingGroup(d.Id(), conn)
if err != nil { if err != nil {
return err return err
} }
if g == nil { if g == nil {
log.Printf("[INFO] Autoscaling Group %q not found", d.Id())
d.SetId("")
return nil return nil
} }
@ -388,11 +392,13 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
// Read the autoscaling group first. If it doesn't exist, we're done. // 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. // We need the group in order to check if there are instances attached.
// If so, we need to remove those first. // If so, we need to remove those first.
g, err := getAwsAutoscalingGroup(d, meta) g, err := getAwsAutoscalingGroup(d.Id(), conn)
if err != nil { if err != nil {
return err return err
} }
if g == nil { if g == nil {
log.Printf("[INFO] Autoscaling Group %q not found", d.Id())
d.SetId("")
return nil return nil
} }
if len(g.Instances) > 0 || *g.DesiredCapacity > 0 { if len(g.Instances) > 0 || *g.DesiredCapacity > 0 {
@ -433,7 +439,7 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
} }
return resource.Retry(5*time.Minute, func() error { return resource.Retry(5*time.Minute, func() error {
if g, _ = getAwsAutoscalingGroup(d, meta); g != nil { if g, _ = getAwsAutoscalingGroup(d.Id(), conn); g != nil {
return fmt.Errorf("Auto Scaling Group still exists") return fmt.Errorf("Auto Scaling Group still exists")
} }
return nil return nil
@ -441,12 +447,11 @@ func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{})
} }
func getAwsAutoscalingGroup( func getAwsAutoscalingGroup(
d *schema.ResourceData, asgName string,
meta interface{}) (*autoscaling.Group, error) { conn *autoscaling.AutoScaling) (*autoscaling.Group, error) {
conn := meta.(*AWSClient).autoscalingconn
describeOpts := autoscaling.DescribeAutoScalingGroupsInput{ describeOpts := autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{aws.String(d.Id())}, AutoScalingGroupNames: []*string{aws.String(asgName)},
} }
log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts) log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
@ -454,7 +459,6 @@ func getAwsAutoscalingGroup(
if err != nil { if err != nil {
autoscalingerr, ok := err.(awserr.Error) autoscalingerr, ok := err.(awserr.Error)
if ok && autoscalingerr.Code() == "InvalidGroup.NotFound" { if ok && autoscalingerr.Code() == "InvalidGroup.NotFound" {
d.SetId("")
return nil, nil return nil, nil
} }
@ -463,13 +467,11 @@ func getAwsAutoscalingGroup(
// Search for the autoscaling group // Search for the autoscaling group
for idx, asc := range describeGroups.AutoScalingGroups { for idx, asc := range describeGroups.AutoScalingGroups {
if *asc.AutoScalingGroupName == d.Id() { if *asc.AutoScalingGroupName == asgName {
return describeGroups.AutoScalingGroups[idx], nil return describeGroups.AutoScalingGroups[idx], nil
} }
} }
// ASG not found
d.SetId("")
return nil, nil return nil, nil
} }
@ -496,11 +498,13 @@ func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{})
// Next, wait for the autoscale group to drain // Next, wait for the autoscale group to drain
log.Printf("[DEBUG] Waiting for group to have zero instances") log.Printf("[DEBUG] Waiting for group to have zero instances")
return resource.Retry(10*time.Minute, func() error { return resource.Retry(10*time.Minute, func() error {
g, err := getAwsAutoscalingGroup(d, meta) g, err := getAwsAutoscalingGroup(d.Id(), conn)
if err != nil { if err != nil {
return resource.RetryError{Err: err} return resource.RetryError{Err: err}
} }
if g == nil { if g == nil {
log.Printf("[INFO] Autoscaling Group %q not found", d.Id())
d.SetId("")
return nil return nil
} }

View File

@ -33,11 +33,13 @@ func waitForASGCapacity(
log.Printf("[DEBUG] Waiting on %s for capacity...", d.Id()) log.Printf("[DEBUG] Waiting on %s for capacity...", d.Id())
return resource.Retry(wait, func() error { return resource.Retry(wait, func() error {
g, err := getAwsAutoscalingGroup(d, meta) g, err := getAwsAutoscalingGroup(d.Id(), meta.(*AWSClient).autoscalingconn)
if err != nil { if err != nil {
return resource.RetryError{Err: err} return resource.RetryError{Err: err}
} }
if g == nil { if g == nil {
log.Printf("[INFO] Autoscaling Group %q not found", d.Id())
d.SetId("")
return nil return nil
} }
lbis, err := getLBInstanceStates(g, meta) lbis, err := getLBInstanceStates(g, meta)