github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/hashcode"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"google.golang.org/api/compute/v1"
    10  	"google.golang.org/api/googleapi"
    11  )
    12  
    13  func resourceComputeRoute() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceComputeRouteCreate,
    16  		Read:   resourceComputeRouteRead,
    17  		Delete: resourceComputeRouteDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"dest_range": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"network": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"next_hop_ip": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  				ForceNew: true,
    42  			},
    43  
    44  			"next_hop_instance": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Optional: true,
    47  				ForceNew: true,
    48  			},
    49  
    50  			"next_hop_instance_zone": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				ForceNew: true,
    54  			},
    55  
    56  			"next_hop_gateway": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Optional: true,
    59  				ForceNew: true,
    60  			},
    61  
    62  			"next_hop_network": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Optional: true,
    65  				ForceNew: true,
    66  			},
    67  
    68  			"next_hop_vpn_tunnel": &schema.Schema{
    69  				Type:     schema.TypeString,
    70  				Optional: true,
    71  				ForceNew: true,
    72  			},
    73  
    74  			"priority": &schema.Schema{
    75  				Type:     schema.TypeInt,
    76  				Required: true,
    77  				ForceNew: true,
    78  			},
    79  
    80  			"tags": &schema.Schema{
    81  				Type:     schema.TypeSet,
    82  				Optional: true,
    83  				ForceNew: true,
    84  				Elem:     &schema.Schema{Type: schema.TypeString},
    85  				Set: func(v interface{}) int {
    86  					return hashcode.String(v.(string))
    87  				},
    88  			},
    89  
    90  			"self_link": &schema.Schema{
    91  				Type:     schema.TypeString,
    92  				Computed: true,
    93  			},
    94  		},
    95  	}
    96  }
    97  
    98  func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error {
    99  	config := meta.(*Config)
   100  
   101  	// Look up the network to attach the route to
   102  	network, err := config.clientCompute.Networks.Get(
   103  		config.Project, d.Get("network").(string)).Do()
   104  	if err != nil {
   105  		return fmt.Errorf("Error reading network: %s", err)
   106  	}
   107  
   108  	// Next hop data
   109  	var nextHopInstance, nextHopIp, nextHopNetwork, nextHopGateway,
   110  		nextHopVpnTunnel string
   111  	if v, ok := d.GetOk("next_hop_ip"); ok {
   112  		nextHopIp = v.(string)
   113  	}
   114  	if v, ok := d.GetOk("next_hop_gateway"); ok {
   115  		nextHopGateway = v.(string)
   116  	}
   117  	if v, ok := d.GetOk("next_hop_vpn_tunnel"); ok {
   118  		nextHopVpnTunnel = v.(string)
   119  	}
   120  	if v, ok := d.GetOk("next_hop_instance"); ok {
   121  		nextInstance, err := config.clientCompute.Instances.Get(
   122  			config.Project,
   123  			d.Get("next_hop_instance_zone").(string),
   124  			v.(string)).Do()
   125  		if err != nil {
   126  			return fmt.Errorf("Error reading instance: %s", err)
   127  		}
   128  
   129  		nextHopInstance = nextInstance.SelfLink
   130  	}
   131  	if v, ok := d.GetOk("next_hop_network"); ok {
   132  		nextNetwork, err := config.clientCompute.Networks.Get(
   133  			config.Project, v.(string)).Do()
   134  		if err != nil {
   135  			return fmt.Errorf("Error reading network: %s", err)
   136  		}
   137  
   138  		nextHopNetwork = nextNetwork.SelfLink
   139  	}
   140  
   141  	// Tags
   142  	var tags []string
   143  	if v := d.Get("tags").(*schema.Set); v.Len() > 0 {
   144  		tags = make([]string, v.Len())
   145  		for i, v := range v.List() {
   146  			tags[i] = v.(string)
   147  		}
   148  	}
   149  
   150  	// Build the route parameter
   151  	route := &compute.Route{
   152  		Name:             d.Get("name").(string),
   153  		DestRange:        d.Get("dest_range").(string),
   154  		Network:          network.SelfLink,
   155  		NextHopInstance:  nextHopInstance,
   156  		NextHopVpnTunnel: nextHopVpnTunnel,
   157  		NextHopIp:        nextHopIp,
   158  		NextHopNetwork:   nextHopNetwork,
   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  		config.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, "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  	route, err := config.clientCompute.Routes.Get(
   185  		config.Project, d.Id()).Do()
   186  	if err != nil {
   187  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   188  			// The resource doesn't exist anymore
   189  			d.SetId("")
   190  
   191  			return nil
   192  		}
   193  
   194  		return fmt.Errorf("Error reading route: %#v", err)
   195  	}
   196  
   197  	d.Set("self_link", route.SelfLink)
   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  	err = computeOperationWaitGlobal(config, op, "Deleting Route")
   213  	if err != nil {
   214  		return err
   215  	}
   216  
   217  	d.SetId("")
   218  	return nil
   219  }