github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_vpn_gateway.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 9 "google.golang.org/api/compute/v1" 10 "google.golang.org/api/googleapi" 11 ) 12 13 func resourceComputeVpnGateway() *schema.Resource { 14 return &schema.Resource{ 15 // Unfortunately, the VPNGatewayService does not support update 16 // operations. This is why everything is marked forcenew 17 Create: resourceComputeVpnGatewayCreate, 18 Read: resourceComputeVpnGatewayRead, 19 Delete: resourceComputeVpnGatewayDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "name": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 28 "network": &schema.Schema{ 29 Type: schema.TypeString, 30 Required: true, 31 ForceNew: true, 32 }, 33 34 "description": &schema.Schema{ 35 Type: schema.TypeString, 36 Optional: true, 37 ForceNew: true, 38 }, 39 40 "project": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 ForceNew: true, 44 }, 45 46 "region": &schema.Schema{ 47 Type: schema.TypeString, 48 Optional: true, 49 ForceNew: true, 50 }, 51 52 "self_link": &schema.Schema{ 53 Type: schema.TypeString, 54 Computed: true, 55 }, 56 }, 57 } 58 } 59 60 func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error { 61 config := meta.(*Config) 62 63 region, err := getRegion(d, config) 64 if err != nil { 65 return err 66 } 67 68 project, err := getProject(d, config) 69 if err != nil { 70 return err 71 } 72 73 name := d.Get("name").(string) 74 network, err := getNetworkLink(d, config, "network") 75 if err != nil { 76 return err 77 } 78 79 vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) 80 81 vpnGateway := &compute.TargetVpnGateway{ 82 Name: name, 83 Network: network, 84 } 85 86 if v, ok := d.GetOk("description"); ok { 87 vpnGateway.Description = v.(string) 88 } 89 90 op, err := vpnGatewaysService.Insert(project, region, vpnGateway).Do() 91 if err != nil { 92 return fmt.Errorf("Error Inserting VPN Gateway %s into network %s: %s", name, network, err) 93 } 94 95 err = computeOperationWaitRegion(config, op, project, region, "Inserting VPN Gateway") 96 if err != nil { 97 return fmt.Errorf("Error Waiting to Insert VPN Gateway %s into network %s: %s", name, network, err) 98 } 99 100 return resourceComputeVpnGatewayRead(d, meta) 101 } 102 103 func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { 104 config := meta.(*Config) 105 106 region, err := getRegion(d, config) 107 if err != nil { 108 return err 109 } 110 111 project, err := getProject(d, config) 112 if err != nil { 113 return err 114 } 115 116 name := d.Get("name").(string) 117 118 vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) 119 vpnGateway, err := vpnGatewaysService.Get(project, region, name).Do() 120 121 if err != nil { 122 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 123 log.Printf("[WARN] Removing VPN Gateway %q because it's gone", d.Get("name").(string)) 124 // The resource doesn't exist anymore 125 d.SetId("") 126 127 return nil 128 } 129 130 return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err) 131 } 132 133 d.Set("self_link", vpnGateway.SelfLink) 134 d.SetId(name) 135 136 return nil 137 } 138 139 func resourceComputeVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error { 140 config := meta.(*Config) 141 142 region, err := getRegion(d, config) 143 if err != nil { 144 return err 145 } 146 147 project, err := getProject(d, config) 148 if err != nil { 149 return err 150 } 151 152 name := d.Get("name").(string) 153 154 vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) 155 156 op, err := vpnGatewaysService.Delete(project, region, name).Do() 157 if err != nil { 158 return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err) 159 } 160 161 err = computeOperationWaitRegion(config, op, project, region, "Deleting VPN Gateway") 162 if err != nil { 163 return fmt.Errorf("Error Waiting to Delete VPN Gateway %s: %s", name, err) 164 } 165 166 return nil 167 }