github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_route.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "google.golang.org/api/compute/v1" 9 "google.golang.org/api/googleapi" 10 ) 11 12 func resourceComputeRoute() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceComputeRouteCreate, 15 Read: resourceComputeRouteRead, 16 Delete: resourceComputeRouteDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "dest_range": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 25 "name": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 31 "network": &schema.Schema{ 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 }, 36 37 "priority": &schema.Schema{ 38 Type: schema.TypeInt, 39 Required: true, 40 ForceNew: true, 41 }, 42 43 "next_hop_gateway": &schema.Schema{ 44 Type: schema.TypeString, 45 Optional: true, 46 ForceNew: true, 47 }, 48 49 "next_hop_instance": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 ForceNew: true, 53 }, 54 55 "next_hop_instance_zone": &schema.Schema{ 56 Type: schema.TypeString, 57 Optional: true, 58 ForceNew: true, 59 }, 60 61 "next_hop_ip": &schema.Schema{ 62 Type: schema.TypeString, 63 Optional: true, 64 ForceNew: true, 65 }, 66 67 "next_hop_network": &schema.Schema{ 68 Type: schema.TypeString, 69 Computed: true, 70 }, 71 72 "next_hop_vpn_tunnel": &schema.Schema{ 73 Type: schema.TypeString, 74 Optional: true, 75 ForceNew: true, 76 }, 77 78 "project": &schema.Schema{ 79 Type: schema.TypeString, 80 Optional: true, 81 ForceNew: true, 82 }, 83 84 "self_link": &schema.Schema{ 85 Type: schema.TypeString, 86 Computed: true, 87 }, 88 89 "tags": &schema.Schema{ 90 Type: schema.TypeSet, 91 Optional: true, 92 ForceNew: true, 93 Elem: &schema.Schema{Type: schema.TypeString}, 94 Set: schema.HashString, 95 }, 96 }, 97 } 98 } 99 100 func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error { 101 config := meta.(*Config) 102 103 project, err := getProject(d, config) 104 if err != nil { 105 return err 106 } 107 108 // Look up the network to attach the route to 109 network, err := getNetworkLink(d, config, "network") 110 if err != nil { 111 return fmt.Errorf("Error reading network: %s", err) 112 } 113 114 // Next hop data 115 var nextHopInstance, nextHopIp, nextHopGateway, 116 nextHopVpnTunnel string 117 if v, ok := d.GetOk("next_hop_ip"); ok { 118 nextHopIp = v.(string) 119 } 120 if v, ok := d.GetOk("next_hop_gateway"); ok { 121 if v == "default-internet-gateway" { 122 nextHopGateway = fmt.Sprintf("projects/%s/global/gateways/default-internet-gateway", project) 123 } else { 124 nextHopGateway = v.(string) 125 } 126 } 127 if v, ok := d.GetOk("next_hop_vpn_tunnel"); ok { 128 nextHopVpnTunnel = v.(string) 129 } 130 if v, ok := d.GetOk("next_hop_instance"); ok { 131 nextInstance, err := config.clientCompute.Instances.Get( 132 project, 133 d.Get("next_hop_instance_zone").(string), 134 v.(string)).Do() 135 if err != nil { 136 return fmt.Errorf("Error reading instance: %s", err) 137 } 138 139 nextHopInstance = nextInstance.SelfLink 140 } 141 142 // Tags 143 var tags []string 144 if v := d.Get("tags").(*schema.Set); v.Len() > 0 { 145 tags = make([]string, v.Len()) 146 for i, v := range v.List() { 147 tags[i] = v.(string) 148 } 149 } 150 151 // Build the route parameter 152 route := &compute.Route{ 153 Name: d.Get("name").(string), 154 DestRange: d.Get("dest_range").(string), 155 Network: network, 156 NextHopInstance: nextHopInstance, 157 NextHopVpnTunnel: nextHopVpnTunnel, 158 NextHopIp: nextHopIp, 159 NextHopGateway: nextHopGateway, 160 Priority: int64(d.Get("priority").(int)), 161 Tags: tags, 162 } 163 log.Printf("[DEBUG] Route insert request: %#v", route) 164 op, err := config.clientCompute.Routes.Insert( 165 project, route).Do() 166 if err != nil { 167 return fmt.Errorf("Error creating route: %s", err) 168 } 169 170 // It probably maybe worked, so store the ID now 171 d.SetId(route.Name) 172 173 err = computeOperationWaitGlobal(config, op, project, "Creating Route") 174 if err != nil { 175 return err 176 } 177 178 return resourceComputeRouteRead(d, meta) 179 } 180 181 func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error { 182 config := meta.(*Config) 183 184 project, err := getProject(d, config) 185 if err != nil { 186 return err 187 } 188 189 route, err := config.clientCompute.Routes.Get( 190 project, d.Id()).Do() 191 if err != nil { 192 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 193 log.Printf("[WARN] Removing Route %q because it's gone", d.Get("name").(string)) 194 // The resource doesn't exist anymore 195 d.SetId("") 196 197 return nil 198 } 199 200 return fmt.Errorf("Error reading route: %#v", err) 201 } 202 203 d.Set("next_hop_network", route.NextHopNetwork) 204 d.Set("self_link", route.SelfLink) 205 206 return nil 207 } 208 209 func resourceComputeRouteDelete(d *schema.ResourceData, meta interface{}) error { 210 config := meta.(*Config) 211 212 project, err := getProject(d, config) 213 if err != nil { 214 return err 215 } 216 217 // Delete the route 218 op, err := config.clientCompute.Routes.Delete( 219 project, d.Id()).Do() 220 if err != nil { 221 return fmt.Errorf("Error deleting route: %s", err) 222 } 223 224 err = computeOperationWaitGlobal(config, op, project, "Deleting Route") 225 if err != nil { 226 return err 227 } 228 229 d.SetId("") 230 return nil 231 }