Use resource.Retry for route creation and deletion (#6225)

* Use resource.Retry for route creation and deletion

* Remove uneeded out var in resource_aws_route.go
This commit is contained in:
Rev. C. Bennett Hoffman 2016-04-19 18:23:42 -04:00 committed by Clint
parent 6ebac8403d
commit e108275331
1 changed files with 46 additions and 3 deletions

View File

@ -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"
)
@ -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)
}
@ -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
}