github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/resource_compute_route.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"code.google.com/p/google-api-go-client/compute/v1"
     9  	"code.google.com/p/google-api-go-client/googleapi"
    10  	"github.com/hashicorp/terraform/helper/hashcode"
    11  	"github.com/hashicorp/terraform/helper/schema"
    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  				Elem:     &schema.Schema{Type: schema.TypeString},
    79  				Set: func(v interface{}) int {
    80  					return hashcode.String(v.(string))
    81  				},
    82  			},
    83  		},
    84  	}
    85  }
    86  
    87  func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error {
    88  	config := meta.(*Config)
    89  
    90  	// Look up the network to attach the route to
    91  	network, err := config.clientCompute.Networks.Get(
    92  		config.Project, d.Get("network").(string)).Do()
    93  	if err != nil {
    94  		return fmt.Errorf("Error reading network: %s", err)
    95  	}
    96  
    97  	// Next hop data
    98  	var nextHopInstance, nextHopIp, nextHopNetwork, nextHopGateway string
    99  	if v, ok := d.GetOk("next_hop_ip"); ok {
   100  		nextHopIp = v.(string)
   101  	}
   102  	if v, ok := d.GetOk("next_hop_gateway"); ok {
   103  		nextHopGateway = v.(string)
   104  	}
   105  	if v, ok := d.GetOk("next_hop_instance"); ok {
   106  		nextInstance, err := config.clientCompute.Instances.Get(
   107  			config.Project,
   108  			d.Get("next_hop_instance_zone").(string),
   109  			v.(string)).Do()
   110  		if err != nil {
   111  			return fmt.Errorf("Error reading instance: %s", err)
   112  		}
   113  
   114  		nextHopInstance = nextInstance.SelfLink
   115  	}
   116  	if v, ok := d.GetOk("next_hop_network"); ok {
   117  		nextNetwork, err := config.clientCompute.Networks.Get(
   118  			config.Project, v.(string)).Do()
   119  		if err != nil {
   120  			return fmt.Errorf("Error reading network: %s", err)
   121  		}
   122  
   123  		nextHopNetwork = nextNetwork.SelfLink
   124  	}
   125  
   126  	// Tags
   127  	var tags []string
   128  	if v := d.Get("tags").(*schema.Set); v.Len() > 0 {
   129  		tags = make([]string, v.Len())
   130  		for i, v := range v.List() {
   131  			tags[i] = v.(string)
   132  		}
   133  	}
   134  
   135  	// Build the route parameter
   136  	route := &compute.Route{
   137  		Name:            d.Get("name").(string),
   138  		DestRange:       d.Get("dest_range").(string),
   139  		Network:         network.SelfLink,
   140  		NextHopInstance: nextHopInstance,
   141  		NextHopIp:       nextHopIp,
   142  		NextHopNetwork:  nextHopNetwork,
   143  		NextHopGateway:  nextHopGateway,
   144  		Priority:        int64(d.Get("priority").(int)),
   145  		Tags:            tags,
   146  	}
   147  	log.Printf("[DEBUG] Route insert request: %#v", route)
   148  	op, err := config.clientCompute.Routes.Insert(
   149  		config.Project, route).Do()
   150  	if err != nil {
   151  		return fmt.Errorf("Error creating route: %s", err)
   152  	}
   153  
   154  	// It probably maybe worked, so store the ID now
   155  	d.SetId(route.Name)
   156  
   157  	// Wait for the operation to complete
   158  	w := &OperationWaiter{
   159  		Service: config.clientCompute,
   160  		Op:      op,
   161  		Project: config.Project,
   162  		Type:    OperationWaitGlobal,
   163  	}
   164  	state := w.Conf()
   165  	state.Timeout = 2 * time.Minute
   166  	state.MinTimeout = 1 * time.Second
   167  	opRaw, err := state.WaitForState()
   168  	if err != nil {
   169  		return fmt.Errorf("Error waiting for route to create: %s", err)
   170  	}
   171  	op = opRaw.(*compute.Operation)
   172  	if op.Error != nil {
   173  		// The resource didn't actually create
   174  		d.SetId("")
   175  
   176  		// Return the error
   177  		return OperationError(*op.Error)
   178  	}
   179  
   180  	return resourceComputeRouteRead(d, meta)
   181  }
   182  
   183  func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error {
   184  	config := meta.(*Config)
   185  
   186  	_, err := config.clientCompute.Routes.Get(
   187  		config.Project, d.Id()).Do()
   188  	if err != nil {
   189  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   190  			// The resource doesn't exist anymore
   191  			d.SetId("")
   192  
   193  			return nil
   194  		}
   195  
   196  		return fmt.Errorf("Error reading route: %#v", err)
   197  	}
   198  
   199  	return nil
   200  }
   201  
   202  func resourceComputeRouteDelete(d *schema.ResourceData, meta interface{}) error {
   203  	config := meta.(*Config)
   204  
   205  	// Delete the route
   206  	op, err := config.clientCompute.Routes.Delete(
   207  		config.Project, d.Id()).Do()
   208  	if err != nil {
   209  		return fmt.Errorf("Error deleting route: %s", err)
   210  	}
   211  
   212  	// Wait for the operation to complete
   213  	w := &OperationWaiter{
   214  		Service: config.clientCompute,
   215  		Op:      op,
   216  		Project: config.Project,
   217  		Type:    OperationWaitGlobal,
   218  	}
   219  	state := w.Conf()
   220  	state.Timeout = 2 * time.Minute
   221  	state.MinTimeout = 1 * time.Second
   222  	opRaw, err := state.WaitForState()
   223  	if err != nil {
   224  		return fmt.Errorf("Error waiting for route to delete: %s", err)
   225  	}
   226  	op = opRaw.(*compute.Operation)
   227  	if op.Error != nil {
   228  		// Return the error
   229  		return OperationError(*op.Error)
   230  	}
   231  
   232  	d.SetId("")
   233  	return nil
   234  }