github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  		nextHopGateway = v.(string)
   122  	}
   123  	if v, ok := d.GetOk("next_hop_vpn_tunnel"); ok {
   124  		nextHopVpnTunnel = v.(string)
   125  	}
   126  	if v, ok := d.GetOk("next_hop_instance"); ok {
   127  		nextInstance, err := config.clientCompute.Instances.Get(
   128  			project,
   129  			d.Get("next_hop_instance_zone").(string),
   130  			v.(string)).Do()
   131  		if err != nil {
   132  			return fmt.Errorf("Error reading instance: %s", err)
   133  		}
   134  
   135  		nextHopInstance = nextInstance.SelfLink
   136  	}
   137  
   138  	// Tags
   139  	var tags []string
   140  	if v := d.Get("tags").(*schema.Set); v.Len() > 0 {
   141  		tags = make([]string, v.Len())
   142  		for i, v := range v.List() {
   143  			tags[i] = v.(string)
   144  		}
   145  	}
   146  
   147  	// Build the route parameter
   148  	route := &compute.Route{
   149  		Name:             d.Get("name").(string),
   150  		DestRange:        d.Get("dest_range").(string),
   151  		Network:          network,
   152  		NextHopInstance:  nextHopInstance,
   153  		NextHopVpnTunnel: nextHopVpnTunnel,
   154  		NextHopIp:        nextHopIp,
   155  		NextHopGateway:   nextHopGateway,
   156  		Priority:         int64(d.Get("priority").(int)),
   157  		Tags:             tags,
   158  	}
   159  	log.Printf("[DEBUG] Route insert request: %#v", route)
   160  	op, err := config.clientCompute.Routes.Insert(
   161  		project, route).Do()
   162  	if err != nil {
   163  		return fmt.Errorf("Error creating route: %s", err)
   164  	}
   165  
   166  	// It probably maybe worked, so store the ID now
   167  	d.SetId(route.Name)
   168  
   169  	err = computeOperationWaitGlobal(config, op, project, "Creating Route")
   170  	if err != nil {
   171  		return err
   172  	}
   173  
   174  	return resourceComputeRouteRead(d, meta)
   175  }
   176  
   177  func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error {
   178  	config := meta.(*Config)
   179  
   180  	project, err := getProject(d, config)
   181  	if err != nil {
   182  		return err
   183  	}
   184  
   185  	route, err := config.clientCompute.Routes.Get(
   186  		project, d.Id()).Do()
   187  	if err != nil {
   188  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   189  			log.Printf("[WARN] Removing Route %q because it's gone", d.Get("name").(string))
   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  	d.Set("next_hop_network", route.NextHopNetwork)
   200  	d.Set("self_link", route.SelfLink)
   201  
   202  	return nil
   203  }
   204  
   205  func resourceComputeRouteDelete(d *schema.ResourceData, meta interface{}) error {
   206  	config := meta.(*Config)
   207  
   208  	project, err := getProject(d, config)
   209  	if err != nil {
   210  		return err
   211  	}
   212  
   213  	// Delete the route
   214  	op, err := config.clientCompute.Routes.Delete(
   215  		project, d.Id()).Do()
   216  	if err != nil {
   217  		return fmt.Errorf("Error deleting route: %s", err)
   218  	}
   219  
   220  	err = computeOperationWaitGlobal(config, op, project, "Deleting Route")
   221  	if err != nil {
   222  		return err
   223  	}
   224  
   225  	d.SetId("")
   226  	return nil
   227  }