github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/aws/resource_vpn_connection_route.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/awslabs/aws-sdk-go/aws"
     9  	"github.com/awslabs/aws-sdk-go/aws/awserr"
    10  	"github.com/awslabs/aws-sdk-go/service/ec2"
    11  
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsVpnConnectionRoute() *schema.Resource {
    16  	return &schema.Resource{
    17  		// You can't update a route. You can just delete one and make
    18  		// a new one.
    19  		Create: resourceAwsVpnConnectionRouteCreate,
    20  		Update: resourceAwsVpnConnectionRouteCreate,
    21  
    22  		Read:   resourceAwsVpnConnectionRouteRead,
    23  		Delete: resourceAwsVpnConnectionRouteDelete,
    24  
    25  		Schema: map[string]*schema.Schema{
    26  			"destination_cidr_block": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"vpn_connection_id": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceAwsVpnConnectionRouteCreate(d *schema.ResourceData, meta interface{}) error {
    42  	conn := meta.(*AWSClient).ec2conn
    43  
    44  	createOpts := &ec2.CreateVPNConnectionRouteInput{
    45  		DestinationCIDRBlock: aws.String(d.Get("destination_cidr_block").(string)),
    46  		VPNConnectionID:      aws.String(d.Get("vpn_connection_id").(string)),
    47  	}
    48  
    49  	// Create the route.
    50  	log.Printf("[DEBUG] Creating VPN connection route")
    51  	_, err := conn.CreateVPNConnectionRoute(createOpts)
    52  	if err != nil {
    53  		return fmt.Errorf("Error creating VPN connection route: %s", err)
    54  	}
    55  
    56  	// Store the ID by the only two data we have available to us.
    57  	d.SetId(fmt.Sprintf("%s:%s", *createOpts.DestinationCIDRBlock, *createOpts.VPNConnectionID))
    58  
    59  	return resourceAwsVpnConnectionRouteRead(d, meta)
    60  }
    61  
    62  func resourceAwsVpnConnectionRouteRead(d *schema.ResourceData, meta interface{}) error {
    63  	conn := meta.(*AWSClient).ec2conn
    64  
    65  	cidrBlock, vpnConnectionId := resourceAwsVpnConnectionRouteParseId(d.Id())
    66  
    67  	routeFilters := []*ec2.Filter{
    68  		&ec2.Filter{
    69  			Name:   aws.String("route.destination-cidr-block"),
    70  			Values: []*string{aws.String(cidrBlock)},
    71  		},
    72  		&ec2.Filter{
    73  			Name:   aws.String("vpn-connection-id"),
    74  			Values: []*string{aws.String(vpnConnectionId)},
    75  		},
    76  	}
    77  
    78  	// Technically, we know everything there is to know about the route
    79  	// from its ID, but we still want to catch cases where it changes
    80  	// outside of terraform and results in a stale state file. Hence,
    81  	// conduct a read.
    82  	resp, err := conn.DescribeVPNConnections(&ec2.DescribeVPNConnectionsInput{
    83  		Filters: routeFilters,
    84  	})
    85  	if err != nil {
    86  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
    87  			d.SetId("")
    88  			return nil
    89  		} else {
    90  			log.Printf("[ERROR] Error finding VPN connection route: %s", err)
    91  			return err
    92  		}
    93  	}
    94  
    95  	vpnConnection := resp.VPNConnections[0]
    96  
    97  	var found bool
    98  	for _, r := range vpnConnection.Routes {
    99  		if *r.DestinationCIDRBlock == cidrBlock {
   100  			d.Set("destination_cidr_block", *r.DestinationCIDRBlock)
   101  			d.Set("vpn_connection_id", *vpnConnection.VPNConnectionID)
   102  			found = true
   103  		}
   104  	}
   105  	if !found {
   106  		// Something other than terraform eliminated the route.
   107  		d.SetId("")
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  func resourceAwsVpnConnectionRouteDelete(d *schema.ResourceData, meta interface{}) error {
   114  	conn := meta.(*AWSClient).ec2conn
   115  
   116  	_, err := conn.DeleteVPNConnectionRoute(&ec2.DeleteVPNConnectionRouteInput{
   117  		DestinationCIDRBlock: aws.String(d.Get("destination_cidr_block").(string)),
   118  		VPNConnectionID:      aws.String(d.Get("vpn_connection_id").(string)),
   119  	})
   120  	if err != nil {
   121  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
   122  			d.SetId("")
   123  			return nil
   124  		} else {
   125  			log.Printf("[ERROR] Error deleting VPN connection route: %s", err)
   126  			return err
   127  		}
   128  	}
   129  
   130  	return nil
   131  }
   132  
   133  func resourceAwsVpnConnectionRouteParseId(id string) (string, string) {
   134  	parts := strings.SplitN(id, ":", 2)
   135  	return parts[0], parts[1]
   136  }