2014-08-26 07:39:29 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
2014-08-26 07:39:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeRoute() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeRouteCreate,
|
|
|
|
Read: resourceComputeRouteRead,
|
|
|
|
Delete: resourceComputeRouteDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"dest_range": &schema.Schema{
|
2014-08-26 07:39:29 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
2014-08-26 07:39:29 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"priority": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"next_hop_gateway": &schema.Schema{
|
2014-08-26 07:39:29 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"next_hop_instance": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"next_hop_instance_zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"next_hop_ip": &schema.Schema{
|
2014-08-26 07:39:29 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"next_hop_network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-03-10 19:57:26 +01:00
|
|
|
Computed: true,
|
2014-08-26 07:39:29 +02:00
|
|
|
},
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
"next_hop_vpn_tunnel": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2014-08-26 07:39:29 +02:00
|
|
|
Optional: true,
|
2015-04-03 16:57:30 +02:00
|
|
|
ForceNew: true,
|
2014-08-26 07:39:29 +02:00
|
|
|
},
|
2015-02-10 12:15:07 +01:00
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"tags": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
2016-04-10 18:59:57 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
2016-04-10 18:59:57 +02:00
|
|
|
},
|
2014-08-26 07:39:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-26 07:39:29 +02:00
|
|
|
// Look up the network to attach the route to
|
|
|
|
network, err := config.clientCompute.Networks.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("network").(string)).Do()
|
2014-08-26 07:39:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading network: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next hop data
|
2016-03-10 19:57:26 +01:00
|
|
|
var nextHopInstance, nextHopIp, nextHopGateway,
|
2015-09-04 22:54:18 +02:00
|
|
|
nextHopVpnTunnel string
|
2014-08-26 07:39:29 +02:00
|
|
|
if v, ok := d.GetOk("next_hop_ip"); ok {
|
|
|
|
nextHopIp = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("next_hop_gateway"); ok {
|
|
|
|
nextHopGateway = v.(string)
|
|
|
|
}
|
2015-09-04 22:54:18 +02:00
|
|
|
if v, ok := d.GetOk("next_hop_vpn_tunnel"); ok {
|
|
|
|
nextHopVpnTunnel = v.(string)
|
|
|
|
}
|
2014-08-26 07:39:29 +02:00
|
|
|
if v, ok := d.GetOk("next_hop_instance"); ok {
|
|
|
|
nextInstance, err := config.clientCompute.Instances.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project,
|
2014-08-26 07:39:29 +02:00
|
|
|
d.Get("next_hop_instance_zone").(string),
|
|
|
|
v.(string)).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nextHopInstance = nextInstance.SelfLink
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tags
|
|
|
|
var tags []string
|
|
|
|
if v := d.Get("tags").(*schema.Set); v.Len() > 0 {
|
|
|
|
tags = make([]string, v.Len())
|
|
|
|
for i, v := range v.List() {
|
|
|
|
tags[i] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the route parameter
|
|
|
|
route := &compute.Route{
|
2015-09-04 22:54:18 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
DestRange: d.Get("dest_range").(string),
|
|
|
|
Network: network.SelfLink,
|
|
|
|
NextHopInstance: nextHopInstance,
|
|
|
|
NextHopVpnTunnel: nextHopVpnTunnel,
|
|
|
|
NextHopIp: nextHopIp,
|
|
|
|
NextHopGateway: nextHopGateway,
|
|
|
|
Priority: int64(d.Get("priority").(int)),
|
|
|
|
Tags: tags,
|
2014-08-26 07:39:29 +02:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Route insert request: %#v", route)
|
|
|
|
op, err := config.clientCompute.Routes.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, route).Do()
|
2014-08-26 07:39:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating route: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(route.Name)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Creating Route")
|
2014-08-26 07:39:29 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeRouteRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-10 12:15:07 +01:00
|
|
|
route, err := config.clientCompute.Routes.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2014-08-26 07:39:29 +02:00
|
|
|
if err != nil {
|
2014-08-26 07:49:14 +02:00
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing Route %q because it's gone", d.Get("name").(string))
|
2014-08-26 07:49:14 +02:00
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 07:39:29 +02:00
|
|
|
return fmt.Errorf("Error reading route: %#v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-10 19:57:26 +01:00
|
|
|
d.Set("next_hop_network", route.NextHopNetwork)
|
2015-02-10 12:15:07 +01:00
|
|
|
d.Set("self_link", route.SelfLink)
|
|
|
|
|
2014-08-26 07:39:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeRouteDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-26 07:39:29 +02:00
|
|
|
// Delete the route
|
|
|
|
op, err := config.clientCompute.Routes.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2014-08-26 07:39:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting route: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Deleting Route")
|
2014-08-26 07:44:27 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
2014-08-26 07:39:29 +02:00
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|