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