2015-09-04 22:54:18 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-13 21:36:03 +01:00
|
|
|
"log"
|
2015-09-04 22:54:18 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
|
|
|
"google.golang.org/api/compute/v1"
|
2015-11-13 21:36:03 +01:00
|
|
|
"google.golang.org/api/googleapi"
|
2015-09-04 22:54:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeVpnGateway() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
// Unfortunately, the VPNGatewayService does not support update
|
|
|
|
// operations. This is why everything is marked forcenew
|
|
|
|
Create: resourceComputeVpnGatewayCreate,
|
|
|
|
Read: resourceComputeVpnGatewayRead,
|
|
|
|
Delete: resourceComputeVpnGatewayDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
|
|
|
"description": &schema.Schema{
|
2016-04-10 18:59:57 +02:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-04-10 18:59:57 +02:00
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
|
|
|
"project": &schema.Schema{
|
2015-09-04 22:54:18 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
|
|
|
"region": &schema.Schema{
|
2015-09-04 22:54:18 +02:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 18:59:57 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-09-04 22:54:18 +02:00
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-09-04 22:54:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
network := d.Get("network").(string)
|
|
|
|
|
|
|
|
vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
|
|
|
|
|
|
|
|
vpnGateway := &compute.TargetVpnGateway{
|
2015-10-07 22:35:06 +02:00
|
|
|
Name: name,
|
|
|
|
Network: network,
|
2015-09-04 22:54:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
vpnGateway.Description = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := vpnGatewaysService.Insert(project, region, vpnGateway).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Inserting VPN Gateway %s into network %s: %s", name, network, err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Inserting VPN Gateway")
|
2015-09-04 22:54:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Waiting to Insert VPN Gateway %s into network %s: %s", name, network, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeVpnGatewayRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
|
|
|
|
vpnGateway, err := vpnGatewaysService.Get(project, region, name).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
2015-11-13 21:36:03 +01:00
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
log.Printf("[WARN] Removing VPN Gateway %q because it's gone", d.Get("name").(string))
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("self_link", vpnGateway.SelfLink)
|
|
|
|
d.SetId(name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
|
|
|
|
|
|
|
|
op, err := vpnGatewaysService.Delete(project, region, name).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Deleting VPN Gateway")
|
2015-09-04 22:54:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Waiting to Delete VPN Gateway %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|