Merge pull request #4369 from mitchellh/groupnotfound

Properly wait for security group to exist.
This commit is contained in:
Matthew Hooker 2017-01-05 15:25:20 -08:00 committed by GitHub
commit 2b5a0cc7b3
1 changed files with 33 additions and 18 deletions

View File

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/private/waiter"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
@ -56,7 +56,7 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
log.Printf("[DEBUG] Found security group %s", sg) log.Printf("[DEBUG] Found security group %s", sg)
securityGroupIds[i] = aws.String(sg) securityGroupIds[i] = aws.String(sg)
} else { } else {
err := fmt.Errorf("Timed out waiting for security group %s", sg) err := fmt.Errorf("Timed out waiting for security group %s: %s", sg, err)
log.Printf("[DEBUG] %s", err.Error()) log.Printf("[DEBUG] %s", err.Error())
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
@ -368,21 +368,36 @@ func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) {
} }
func WaitUntilSecurityGroupExists(c *ec2.EC2, input *ec2.DescribeSecurityGroupsInput) error { func WaitUntilSecurityGroupExists(c *ec2.EC2, input *ec2.DescribeSecurityGroupsInput) error {
for i := 0; i < 40; i++ { waiterCfg := waiter.Config{
_, err := c.DescribeSecurityGroups(input) Operation: "DescribeSecurityGroups",
if err != nil { Delay: 15,
// Check if this is just because it doesn't exist yet MaxAttempts: 40,
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSecurityGroupID.NotFound" { Acceptors: []waiter.WaitAcceptor{
log.Printf("[DEBUG] Security group %v doesn't exist, sleeping for a moment", input.GroupIds) {
time.Sleep(15 * time.Second) State: "success",
continue Matcher: "path",
} Argument: "length(SecurityGroups[]) > `0`",
// The error is something else, abort and throw it Expected: true,
return fmt.Errorf("Error looking for security group %v: %s", input.GroupIds, err) },
} {
State: "retry",
// Success! Matcher: "error",
return nil Argument: "",
Expected: "InvalidGroupID.NotFound",
},
{
State: "retry",
Matcher: "error",
Argument: "",
Expected: "InvalidSecurityGroupID.NotFound",
},
},
} }
return fmt.Errorf("Timeout waiting for security group %v to appear", input.GroupIds)
w := waiter.Waiter{
Client: c,
Input: input,
Config: waiterCfg,
}
return w.Wait()
} }