github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_vpn_tunnel.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  
     8  	"google.golang.org/api/compute/v1"
     9  )
    10  
    11  func resourceComputeVpnTunnel() *schema.Resource {
    12  	return &schema.Resource{
    13  		// Unfortunately, the VPNTunnelService does not support update
    14  		// operations. This is why everything is marked forcenew
    15  		Create: resourceComputeVpnTunnelCreate,
    16  		Read:   resourceComputeVpnTunnelRead,
    17  		Delete: resourceComputeVpnTunnelDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  			"description": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  			"region": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Optional: true,
    33  				ForceNew: true,
    34  			},
    35  			"peer_ip": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  			},
    40  			"shared_secret": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  				ForceNew: true,
    44  			},
    45  			"target_vpn_gateway": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Required: true,
    48  				ForceNew: true,
    49  			},
    50  			"ike_version": &schema.Schema{
    51  				Type:     schema.TypeInt,
    52  				Optional: true,
    53  				Default:  2,
    54  				ForceNew: true,
    55  			},
    56  			"detailed_status": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Computed: true,
    59  			},
    60  			"self_link": &schema.Schema{
    61  				Type:     schema.TypeString,
    62  				Computed: true,
    63  			},
    64  		},
    65  	}
    66  }
    67  
    68  func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) error {
    69  	config := meta.(*Config)
    70  
    71  	name := d.Get("name").(string)
    72  	region := getOptionalRegion(d, config)
    73  	peerIp := d.Get("peer_ip").(string)
    74  	sharedSecret := d.Get("shared_secret").(string)
    75  	targetVpnGateway := d.Get("target_vpn_gateway").(string)
    76  	ikeVersion := d.Get("ike_version").(int)
    77  	project := config.Project
    78  
    79  	if ikeVersion < 1 || ikeVersion > 2 {
    80  		return fmt.Errorf("Only IKE version 1 or 2 supported, not %d", ikeVersion)
    81  	}
    82  
    83  	vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
    84  
    85  	vpnTunnel := &compute.VpnTunnel{
    86  		Name:             name,
    87  		PeerIp:           peerIp,
    88  		SharedSecret:     sharedSecret,
    89  		TargetVpnGateway: targetVpnGateway,
    90  		IkeVersion:       int64(ikeVersion),
    91  	}
    92  
    93  	if v, ok := d.GetOk("description"); ok {
    94  		vpnTunnel.Description = v.(string)
    95  	}
    96  
    97  	op, err := vpnTunnelsService.Insert(project, region, vpnTunnel).Do()
    98  	if err != nil {
    99  		return fmt.Errorf("Error Inserting VPN Tunnel %s : %s", name, err)
   100  	}
   101  
   102  	err = computeOperationWaitRegion(config, op, region, "Inserting VPN Tunnel")
   103  	if err != nil {
   104  		return fmt.Errorf("Error Waiting to Insert VPN Tunnel %s: %s", name, err)
   105  	}
   106  
   107  	return resourceComputeVpnTunnelRead(d, meta)
   108  }
   109  
   110  func resourceComputeVpnTunnelRead(d *schema.ResourceData, meta interface{}) error {
   111  	config := meta.(*Config)
   112  
   113  	name := d.Get("name").(string)
   114  	region := d.Get("region").(string)
   115  	project := config.Project
   116  
   117  	vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
   118  
   119  	vpnTunnel, err := vpnTunnelsService.Get(project, region, name).Do()
   120  	if err != nil {
   121  		return fmt.Errorf("Error Reading VPN Tunnel %s: %s", name, err)
   122  	}
   123  
   124  	d.Set("detailed_status", vpnTunnel.DetailedStatus)
   125  	d.Set("self_link", vpnTunnel.SelfLink)
   126  
   127  	d.SetId(name)
   128  
   129  	return nil
   130  }
   131  
   132  func resourceComputeVpnTunnelDelete(d *schema.ResourceData, meta interface{}) error {
   133  	config := meta.(*Config)
   134  
   135  	name := d.Get("name").(string)
   136  	region := d.Get("region").(string)
   137  	project := config.Project
   138  
   139  	vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
   140  
   141  	op, err := vpnTunnelsService.Delete(project, region, name).Do()
   142  	if err != nil {
   143  		return fmt.Errorf("Error Reading VPN Tunnel %s: %s", name, err)
   144  	}
   145  
   146  	err = computeOperationWaitRegion(config, op, region, "Deleting VPN Tunnel")
   147  	if err != nil {
   148  		return fmt.Errorf("Error Waiting to Delete VPN Tunnel %s: %s", name, err)
   149  	}
   150  
   151  	return nil
   152  }