github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_vpn_gateway.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 resourceComputeVpnGateway() *schema.Resource {
    12  	return &schema.Resource{
    13  		// Unfortunately, the VPNGatewayService does not support update
    14  		// operations. This is why everything is marked forcenew
    15  		Create: resourceComputeVpnGatewayCreate,
    16  		Read:   resourceComputeVpnGatewayRead,
    17  		Delete: resourceComputeVpnGatewayDelete,
    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  			"network": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  			"region": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				ForceNew: true,
    39  			},
    40  			"self_link": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Computed: true,
    43  			},
    44  		},
    45  	}
    46  }
    47  
    48  func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    49  	config := meta.(*Config)
    50  
    51  	name := d.Get("name").(string)
    52  	network := d.Get("network").(string)
    53  	region := getOptionalRegion(d, config)
    54  	project := config.Project
    55  
    56  	vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
    57  
    58  	vpnGateway := &compute.TargetVpnGateway{
    59  		Name:    name,
    60  		Network: network,
    61  	}
    62  
    63  	if v, ok := d.GetOk("description"); ok {
    64  		vpnGateway.Description = v.(string)
    65  	}
    66  
    67  	op, err := vpnGatewaysService.Insert(project, region, vpnGateway).Do()
    68  	if err != nil {
    69  		return fmt.Errorf("Error Inserting VPN Gateway %s into network %s: %s", name, network, err)
    70  	}
    71  
    72  	err = computeOperationWaitRegion(config, op, region, "Inserting VPN Gateway")
    73  	if err != nil {
    74  		return fmt.Errorf("Error Waiting to Insert VPN Gateway %s into network %s: %s", name, network, err)
    75  	}
    76  
    77  	return resourceComputeVpnGatewayRead(d, meta)
    78  }
    79  
    80  func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
    81  	config := meta.(*Config)
    82  
    83  	name := d.Get("name").(string)
    84  	region := d.Get("region").(string)
    85  	project := config.Project
    86  
    87  	vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
    88  	vpnGateway, err := vpnGatewaysService.Get(project, region, name).Do()
    89  
    90  	if err != nil {
    91  		return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err)
    92  	}
    93  
    94  	d.Set("self_link", vpnGateway.SelfLink)
    95  	d.SetId(name)
    96  
    97  	return nil
    98  }
    99  
   100  func resourceComputeVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
   101  	config := meta.(*Config)
   102  
   103  	name := d.Get("name").(string)
   104  	region := d.Get("region").(string)
   105  	project := config.Project
   106  
   107  	vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
   108  
   109  	op, err := vpnGatewaysService.Delete(project, region, name).Do()
   110  	if err != nil {
   111  		return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err)
   112  	}
   113  
   114  	err = computeOperationWaitRegion(config, op, region, "Deleting VPN Gateway")
   115  	if err != nil {
   116  		return fmt.Errorf("Error Waiting to Delete VPN Gateway %s: %s", name, err)
   117  	}
   118  
   119  	return nil
   120  }