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 <krzysztof.wilczynski@linux.com>
This commit is contained in:
Krzysztof Wilczynski 2016-10-18 11:47:25 +01:00
parent 94cbc37312
commit b74de12bd6
No known key found for this signature in database
GPG Key ID: B89F6447B63419A6

View File

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