From b74de12bd63de01aa365b3b9aa53610ec362abee Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 18 Oct 2016 11:47:25 +0100 Subject: [PATCH] Handle the case where Route Table is already gone. This commit changes the behaviour of the `ExistsFunc`, where by default lack of a route table (e.g. already removed, etc.) would cause an error to be thrown. This makes is hard to carry out any action e.g. plan, refresh, or destroy, that rely on the route table existance check. Also, make error messages a little better in terms of wording, etc. Signed-off-by: Krzysztof Wilczynski --- builtin/providers/aws/resource_aws_route.go | 24 +++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/builtin/providers/aws/resource_aws_route.go b/builtin/providers/aws/resource_aws_route.go index 965680d1ca..8c4238fe75 100644 --- a/builtin/providers/aws/resource_aws_route.go +++ b/builtin/providers/aws/resource_aws_route.go @@ -150,7 +150,7 @@ func resourceAwsRouteCreate(d *schema.ResourceData, meta interface{}) error { VpcPeeringConnectionId: aws.String(d.Get("vpc_peering_connection_id").(string)), } default: - return fmt.Errorf("Error: invalid target type specified.") + return fmt.Errorf("An invalid target type specified: %s", setTarget) } log.Printf("[DEBUG] Route create config: %s", createOpts) @@ -195,10 +195,13 @@ func resourceAwsRouteCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsRouteRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn - route, err := findResourceRoute(conn, d.Get("route_table_id").(string), d.Get("destination_cidr_block").(string)) + routeTableId := d.Get("route_table_id").(string) + + route, err := findResourceRoute(conn, routeTableId, d.Get("destination_cidr_block").(string)) if err != nil { if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidRouteTableID.NotFound" { - log.Printf("[WARN] AWS RouteTable not found. Removing Route from state") + log.Printf("[WARN] Route Table %q could not be found. Removing Route from state.", + routeTableId) d.SetId("") return nil } @@ -289,7 +292,7 @@ func resourceAwsRouteUpdate(d *schema.ResourceData, meta interface{}) error { VpcPeeringConnectionId: aws.String(d.Get("vpc_peering_connection_id").(string)), } default: - return fmt.Errorf("Error: invalid target type specified.") + return fmt.Errorf("An invalid target type specified: %s", setTarget) } log.Printf("[DEBUG] Route replace config: %s", replaceOpts) @@ -352,11 +355,15 @@ func resourceAwsRouteExists(d *schema.ResourceData, meta interface{}) (bool, err res, err := conn.DescribeRouteTables(findOpts) if err != nil { + if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidRouteTableID.NotFound" { + log.Printf("[WARN] Route Table %q could not be found.", routeTableId) + return false, nil + } return false, fmt.Errorf("Error while checking if route exists: %s", err) } if len(res.RouteTables) < 1 || res.RouteTables[0] == nil { - log.Printf("[WARN] Route table %s is gone, so route does not exist.", + log.Printf("[WARN] Route Table %q is gone, or route does not exist.", routeTableId) return false, nil } @@ -390,7 +397,7 @@ func findResourceRoute(conn *ec2.EC2, rtbid string, cidr string) (*ec2.Route, er } if len(resp.RouteTables) < 1 || resp.RouteTables[0] == nil { - return nil, fmt.Errorf("Route table %s is gone, so route does not exist.", + return nil, fmt.Errorf("Route Table %q is gone, or route does not exist.", routeTableID) } @@ -400,7 +407,6 @@ func findResourceRoute(conn *ec2.EC2, rtbid string, cidr string) (*ec2.Route, er } } - return nil, fmt.Errorf( - `error finding matching route for Route table (%s) and destination CIDR block (%s)`, - rtbid, cidr) + return nil, fmt.Errorf("Unable to find matching route for Route Table (%s) "+ + "and destination CIDR block (%s).", rtbid, cidr) }