-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
aws: undeprecate min_elb_capacity; restore min capacity waiting #4864
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
builtin/providers/aws/resource_aws_autoscaling_group_waiting.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// waitForASGCapacityTimeout gathers the current numbers of healthy instances | ||
// in the ASG and its attached ELBs and yields these numbers to a | ||
// capacitySatifiedFunction. Loops for up to wait_for_capacity_timeout until | ||
// the capacitySatisfiedFunc returns true. | ||
// | ||
// See "Waiting for Capacity" in docs for more discussion of the feature. | ||
func waitForASGCapacity( | ||
d *schema.ResourceData, | ||
meta interface{}, | ||
satisfiedFunc capacitySatisfiedFunc) error { | ||
wait, err := time.ParseDuration(d.Get("wait_for_capacity_timeout").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if wait == 0 { | ||
log.Printf("[DEBUG] Capacity timeout set to 0, skipping capacity waiting.") | ||
return nil | ||
} | ||
|
||
log.Printf("[DEBUG] Waiting on %s for capacity...", d.Id()) | ||
|
||
return resource.Retry(wait, func() error { | ||
g, err := getAwsAutoscalingGroup(d, meta) | ||
if err != nil { | ||
return resource.RetryError{Err: err} | ||
} | ||
if g == nil { | ||
return nil | ||
} | ||
lbis, err := getLBInstanceStates(g, meta) | ||
if err != nil { | ||
return resource.RetryError{Err: err} | ||
} | ||
|
||
haveASG := 0 | ||
haveELB := 0 | ||
|
||
for _, i := range g.Instances { | ||
if i.HealthStatus == nil || i.InstanceId == nil || i.LifecycleState == nil { | ||
continue | ||
} | ||
|
||
if !strings.EqualFold(*i.HealthStatus, "Healthy") { | ||
continue | ||
} | ||
|
||
if !strings.EqualFold(*i.LifecycleState, "InService") { | ||
continue | ||
} | ||
|
||
haveASG++ | ||
|
||
inAllLbs := true | ||
for _, states := range lbis { | ||
state, ok := states[*i.InstanceId] | ||
if !ok || !strings.EqualFold(state, "InService") { | ||
inAllLbs = false | ||
} | ||
} | ||
if inAllLbs { | ||
haveELB++ | ||
} | ||
} | ||
|
||
satisfied, reason := satisfiedFunc(d, haveASG, haveELB) | ||
|
||
log.Printf("[DEBUG] %q Capacity: %d ASG, %d ELB, satisfied: %t, reason: %q", | ||
d.Id(), haveASG, haveELB, satisfied, reason) | ||
|
||
if satisfied { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%q: Waiting up to %s: %s", d.Id(), wait, reason) | ||
}) | ||
} | ||
|
||
type capacitySatisfiedFunc func(*schema.ResourceData, int, int) (bool, string) | ||
|
||
// capacitySatifiedCreate treats all targets as minimums | ||
func capacitySatifiedCreate(d *schema.ResourceData, haveASG, haveELB int) (bool, string) { | ||
minASG := d.Get("min_size").(int) | ||
if wantASG := d.Get("desired_capacity").(int); wantASG > 0 { | ||
minASG = wantASG | ||
} | ||
if haveASG < minASG { | ||
return false, fmt.Sprintf( | ||
"Need at least %d healthy instances in ASG, have %d", minASG, haveASG) | ||
} | ||
minELB := d.Get("min_elb_capacity").(int) | ||
if wantELB := d.Get("wait_for_elb_capacity").(int); wantELB > 0 { | ||
minELB = wantELB | ||
} | ||
if haveELB < minELB { | ||
return false, fmt.Sprintf( | ||
"Need at least %d healthy instances in ELB, have %d", minELB, haveELB) | ||
} | ||
return true, "" | ||
} | ||
|
||
// capacitySatifiedUpdate only cares about specific targets | ||
func capacitySatifiedUpdate(d *schema.ResourceData, haveASG, haveELB int) (bool, string) { | ||
if wantASG := d.Get("desired_capacity").(int); wantASG > 0 { | ||
if haveASG != wantASG { | ||
return false, fmt.Sprintf( | ||
"Need exactly %d healthy instances in ASG, have %d", wantASG, haveASG) | ||
} | ||
} | ||
if wantELB := d.Get("wait_for_elb_capacity").(int); wantELB > 0 { | ||
if haveELB != wantELB { | ||
return false, fmt.Sprintf( | ||
"Need exactly %d healthy instances in ELB, have %d", wantELB, haveELB) | ||
} | ||
} | ||
return true, "" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is the same as the one removed from the resource file with the exception of these lines.