2015-05-04 18:20:42 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-05-04 18:20:42 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsVpnConnectionRoute() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
// You can't update a route. You can just delete one and make
|
|
|
|
// a new one.
|
|
|
|
Create: resourceAwsVpnConnectionRouteCreate,
|
|
|
|
Update: resourceAwsVpnConnectionRouteCreate,
|
|
|
|
|
|
|
|
Read: resourceAwsVpnConnectionRouteRead,
|
|
|
|
Delete: resourceAwsVpnConnectionRouteDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"destination_cidr_block": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vpn_connection_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnConnectionRouteCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
createOpts := &ec2.CreateVpnConnectionRouteInput{
|
|
|
|
DestinationCidrBlock: aws.String(d.Get("destination_cidr_block").(string)),
|
|
|
|
VpnConnectionId: aws.String(d.Get("vpn_connection_id").(string)),
|
2015-05-04 18:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the route.
|
|
|
|
log.Printf("[DEBUG] Creating VPN connection route")
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.CreateVpnConnectionRoute(createOpts)
|
2015-05-04 18:20:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating VPN connection route: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the ID by the only two data we have available to us.
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(fmt.Sprintf("%s:%s", *createOpts.DestinationCidrBlock, *createOpts.VpnConnectionId))
|
2015-05-04 18:20:42 +02:00
|
|
|
|
|
|
|
return resourceAwsVpnConnectionRouteRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnConnectionRouteRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
cidrBlock, vpnConnectionId := resourceAwsVpnConnectionRouteParseId(d.Id())
|
|
|
|
|
|
|
|
routeFilters := []*ec2.Filter{
|
|
|
|
&ec2.Filter{
|
|
|
|
Name: aws.String("route.destination-cidr-block"),
|
|
|
|
Values: []*string{aws.String(cidrBlock)},
|
|
|
|
},
|
|
|
|
&ec2.Filter{
|
|
|
|
Name: aws.String("vpn-connection-id"),
|
|
|
|
Values: []*string{aws.String(vpnConnectionId)},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Technically, we know everything there is to know about the route
|
|
|
|
// from its ID, but we still want to catch cases where it changes
|
|
|
|
// outside of terraform and results in a stale state file. Hence,
|
|
|
|
// conduct a read.
|
2015-08-17 20:27:16 +02:00
|
|
|
resp, err := conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
|
2015-05-04 18:20:42 +02:00
|
|
|
Filters: routeFilters,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
|
2015-05-04 18:20:42 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
log.Printf("[ERROR] Error finding VPN connection route: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if resp == nil || len(resp.VpnConnections) == 0 {
|
2015-06-27 06:22:08 +02:00
|
|
|
// This is kind of a weird edge case. I'd rather return an error
|
|
|
|
// instead of just blindly setting the ID to ""... since I don't know
|
|
|
|
// what might cause this.
|
|
|
|
return fmt.Errorf("No VPN connections returned")
|
|
|
|
}
|
2015-05-04 18:20:42 +02:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
vpnConnection := resp.VpnConnections[0]
|
2015-05-04 18:20:42 +02:00
|
|
|
|
|
|
|
var found bool
|
|
|
|
for _, r := range vpnConnection.Routes {
|
2015-08-17 20:27:16 +02:00
|
|
|
if *r.DestinationCidrBlock == cidrBlock {
|
|
|
|
d.Set("destination_cidr_block", *r.DestinationCidrBlock)
|
|
|
|
d.Set("vpn_connection_id", *vpnConnection.VpnConnectionId)
|
2015-05-04 18:20:42 +02:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
// Something other than terraform eliminated the route.
|
|
|
|
d.SetId("")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnConnectionRouteDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.DeleteVpnConnectionRoute(&ec2.DeleteVpnConnectionRouteInput{
|
|
|
|
DestinationCidrBlock: aws.String(d.Get("destination_cidr_block").(string)),
|
|
|
|
VpnConnectionId: aws.String(d.Get("vpn_connection_id").(string)),
|
2015-05-04 18:20:42 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
|
2015-05-04 18:20:42 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
log.Printf("[ERROR] Error deleting VPN connection route: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnConnectionRouteParseId(id string) (string, string) {
|
|
|
|
parts := strings.SplitN(id, ":", 2)
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|