Skip to content

Commit

Permalink
aws/resource_aws_eip: implement retry reading EIPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergiusz Urbaniak committed Jun 29, 2017
1 parent f47896b commit fd16f88
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions builtin/providers/aws/resource_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
return resourceAwsEipUpdate(d, meta)
}

func describeAddressesFunc(conn *ec2.EC2, req *ec2.DescribeAddressesInput) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
describeAddresses, err := conn.DescribeAddresses(req)

if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
switch ec2err.Code() {
case "InvalidAllocationID.NotFound":
return nil, "notfound", nil
case "InvalidAddress.NotFound":
return nil, "notfound", nil
}
}

return nil, "notfound", err
}

return describeAddresses, "found", nil
}
}

func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn

Expand All @@ -132,16 +153,22 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
"[DEBUG] EIP describe configuration: %s (domain: %s)",
req, domain)

describeAddresses, err := ec2conn.DescribeAddresses(req)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "InvalidAllocationID.NotFound" || ec2err.Code() == "InvalidAddress.NotFound") {
d.SetId("")
return nil
}
stateConf := &resource.StateChangeConf{
Pending: []string{"notfound"},
Target: []string{"found"},
Timeout: 15 * time.Minute,
Delay: 10 * time.Second,
Refresh: describeAddressesFunc(ec2conn, req),
NotFoundChecks: 90,
}

res, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error retrieving EIP: %s", err)
}

describeAddresses := res.(*ec2.DescribeAddressesOutput)

// Verify AWS returned our EIP
if len(describeAddresses.Addresses) != 1 ||
domain == "vpc" && *describeAddresses.Addresses[0].AllocationId != id ||
Expand Down

0 comments on commit fd16f88

Please sign in to comment.