Skip to content

Commit

Permalink
Use resource.Retry for route creation and deletion (#6225)
Browse files Browse the repository at this point in the history
* Use resource.Retry for route creation and deletion

* Remove uneeded out var in resource_aws_route.go
  • Loading branch information
RevCBH authored and catsby committed Apr 19, 2016
1 parent 6ebac84 commit e108275
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions builtin/providers/aws/resource_aws_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"errors"
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -152,7 +155,26 @@ func resourceAwsRouteCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Route create config: %s", createOpts)

// Create the route
_, err := conn.CreateRoute(createOpts)
var err error

err = resource.Retry(2*time.Minute, func() *resource.RetryError {
_, err = conn.CreateRoute(createOpts)

if err != nil {
ec2err, ok := err.(awserr.Error)
if !ok {
return resource.NonRetryableError(err)
}
if ec2err.Code() == "InvalidParameterException" {
log.Printf("[DEBUG] Trying to create route again: %q", ec2err.Message())
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

return nil
})
if err != nil {
return fmt.Errorf("Error creating route: %s", err)
}
Expand Down Expand Up @@ -269,8 +291,29 @@ func resourceAwsRouteDelete(d *schema.ResourceData, meta interface{}) error {
}
log.Printf("[DEBUG] Route delete opts: %s", deleteOpts)

resp, err := conn.DeleteRoute(deleteOpts)
log.Printf("[DEBUG] Route delete result: %s", resp)
var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
log.Printf("[DEBUG] Trying to delete route with opts %s", deleteOpts)
resp, err := conn.DeleteRoute(deleteOpts)
log.Printf("[DEBUG] Route delete result: %s", resp)

if err == nil {
return nil
}

ec2err, ok := err.(awserr.Error)
if !ok {
return resource.NonRetryableError(err)
}
if ec2err.Code() == "InvalidParameterException" {
log.Printf("[DEBUG] Trying to delete route again: %q",
ec2err.Message())
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
})

if err != nil {
return err
}
Expand Down

0 comments on commit e108275

Please sign in to comment.