Skip to content

Commit

Permalink
r/aws_route: Return 'NotFoundError' instead of 'nil' when no route fo…
Browse files Browse the repository at this point in the history
…und (hashicorp#15945).

r/aws_route: Implement 'd.IsNewResource()' checksin 'resourceAwsRouteRead' (hashicorp#16796).

Acceptance test output:

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSRoute_basic\|TestAccAWSRoute_disappears' ACCTEST_PARALLELISM=2
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 2 -run=TestAccAWSRoute_basic\|TestAccAWSRoute_disappears -timeout 120m
=== RUN   TestAccAWSRoute_basic
=== PAUSE TestAccAWSRoute_basic
=== RUN   TestAccAWSRoute_disappears
=== PAUSE TestAccAWSRoute_disappears
=== RUN   TestAccAWSRoute_disappears_RouteTable
=== PAUSE TestAccAWSRoute_disappears_RouteTable
=== CONT  TestAccAWSRoute_basic
=== CONT  TestAccAWSRoute_disappears_RouteTable
--- PASS: TestAccAWSRoute_disappears_RouteTable (34.67s)
=== CONT  TestAccAWSRoute_disappears
--- PASS: TestAccAWSRoute_basic (36.39s)
--- PASS: TestAccAWSRoute_disappears (31.85s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	66.620s
  • Loading branch information
ewbankkit committed Jan 22, 2021
1 parent 077f4f1 commit 499b772
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
34 changes: 26 additions & 8 deletions aws/internal/service/ec2/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package finder
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
tfnet "github.com/terraform-providers/terraform-provider-aws/aws/internal/net"
tfec2 "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2"
)
Expand Down Expand Up @@ -76,30 +78,46 @@ func ClientVpnRouteByID(conn *ec2.EC2, routeID string) (*ec2.DescribeClientVpnRo
}

// RouteTableByID returns the route table corresponding to the specified identifier.
// Returns nil if no route table is found.
// Returns NotFoundError if no route table is found.
func RouteTableByID(conn *ec2.EC2, routeTableID string) (*ec2.RouteTable, error) {
input := &ec2.DescribeRouteTablesInput{
RouteTableIds: aws.StringSlice([]string{routeTableID}),
}

return RouteTable(conn, input)
}

func RouteTable(conn *ec2.EC2, input *ec2.DescribeRouteTablesInput) (*ec2.RouteTable, error) {
output, err := conn.DescribeRouteTables(input)

if tfawserr.ErrCodeEquals(err, tfec2.ErrCodeInvalidRouteTableIDNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || len(output.RouteTables) == 0 {
return nil, nil
if output == nil || len(output.RouteTables) == 0 || output.RouteTables[0] == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
LastResponse: output,
}
}

return output.RouteTables[0], nil
}

// RouteFinder returns the route corresponding to the specified destination.
// Returns nil if no route is found.
// Returns NotFoundError if no route is found.
type RouteFinder func(*ec2.EC2, string, string) (*ec2.Route, error)

// RouteByIPv4Destination returns the route corresponding to the specified IPv4 destination.
// Returns nil if no route is found.
// Returns NotFoundError if no route is found.
func RouteByIPv4Destination(conn *ec2.EC2, routeTableID, destinationCidr string) (*ec2.Route, error) {
routeTable, err := RouteTableByID(conn, routeTableID)
if err != nil {
Expand All @@ -112,11 +130,11 @@ func RouteByIPv4Destination(conn *ec2.EC2, routeTableID, destinationCidr string)
}
}

return nil, nil
return nil, &resource.NotFoundError{}
}

// RouteByIPv6Destination returns the route corresponding to the specified IPv6 destination.
// Returns nil if no route is found.
// Returns NotFoundError if no route is found.
func RouteByIPv6Destination(conn *ec2.EC2, routeTableID, destinationIpv6Cidr string) (*ec2.Route, error) {
routeTable, err := RouteTableByID(conn, routeTableID)
if err != nil {
Expand All @@ -129,7 +147,7 @@ func RouteByIPv6Destination(conn *ec2.EC2, routeTableID, destinationIpv6Cidr str
}
}

return nil, nil
return nil, &resource.NotFoundError{}
}

// SecurityGroupByID looks up a security group by ID. When not found, returns nil and potentially an API error.
Expand Down
20 changes: 6 additions & 14 deletions aws/resource_aws_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,31 +252,29 @@ func resourceAwsRouteCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error creating Route for Route Table (%s) with destination (%s): %w", routeTableID, destination, err)
}

var route *ec2.Route

err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
route, err = routeFinder(conn, routeTableID, destination)
_, err = routeFinder(conn, routeTableID, destination)

if err != nil {
return resource.RetryableError(err)
}

if route == nil {
if tfresource.NotFound(err) {
return resource.RetryableError(fmt.Errorf("route not found"))
}

return nil
})

if tfresource.TimedOut(err) {
route, err = routeFinder(conn, routeTableID, destination)
_, err = routeFinder(conn, routeTableID, destination)
}

if err != nil {
return fmt.Errorf("error reading Route for Route Table (%s) with destination (%s): %w", routeTableID, destination, err)
}

if route == nil {
if tfresource.NotFound(err) {
return fmt.Errorf("Route in Route Table (%s) with destination (%s) not found", routeTableID, destination)
}

Expand Down Expand Up @@ -309,8 +307,8 @@ func resourceAwsRouteRead(d *schema.ResourceData, meta interface{}) error {

route, err := routeFinder(conn, routeTableID, destination)

if tfawserr.ErrCodeEquals(err, tfec2.ErrCodeInvalidRouteTableIDNotFound) {
log.Printf("[WARN] Route Table (%s) not found, removing from state", routeTableID)
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Route in Route Table (%s) with destination (%s) not found, removing from state", routeTableID, destination)
d.SetId("")
return nil
}
Expand All @@ -319,12 +317,6 @@ func resourceAwsRouteRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error reading Route for Route Table (%s) with destination (%s): %w", routeTableID, destination, err)
}

if route == nil {
log.Printf("[WARN] Route in Route Table (%s) with destination (%s) not found, removing from state", routeTableID, destination)
d.SetId("")
return nil
}

d.Set("destination_cidr_block", route.DestinationCidrBlock)
d.Set("destination_ipv6_cidr_block", route.DestinationIpv6CidrBlock)
d.Set("destination_prefix_list_id", route.DestinationPrefixListId)
Expand Down

0 comments on commit 499b772

Please sign in to comment.