Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
r/resource_aws_eip: implement retry reading EIPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergiusz Urbaniak committed Jul 4, 2017
1 parent 71e8e71 commit d897c52
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 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 {
return describeAddresses, "found", 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
}
}

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 Expand Up @@ -249,10 +276,6 @@ func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {
if err := resourceAwsEipRead(d, meta); err != nil {
return err
}
if d.Id() == "" {
// This might happen from the read
return nil
}

// If we are attached to an instance or interface, detach first.
if d.Get("instance").(string) != "" || d.Get("association_id").(string) != "" {
Expand Down

0 comments on commit d897c52

Please sign in to comment.