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