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