github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_forwarding_rule.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 resourceComputeForwardingRule() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeForwardingRuleCreate,
    15  		Read:   resourceComputeForwardingRuleRead,
    16  		Delete: resourceComputeForwardingRuleDelete,
    17  		Update: resourceComputeForwardingRuleUpdate,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"ip_address": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  				ForceNew: true,
    24  				Computed: true,
    25  			},
    26  
    27  			"ip_protocol": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  				Computed: true,
    32  			},
    33  
    34  			"description": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"name": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  				ForceNew: true,
    44  			},
    45  
    46  			"port_range": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Optional: true,
    49  				ForceNew: true,
    50  			},
    51  
    52  			"region": &schema.Schema{
    53  				Type:     schema.TypeString,
    54  				Optional: true,
    55  				ForceNew: true,
    56  			},
    57  
    58  			"self_link": &schema.Schema{
    59  				Type:     schema.TypeString,
    60  				Computed: true,
    61  			},
    62  
    63  			"target": &schema.Schema{
    64  				Type:     schema.TypeString,
    65  				Required: true,
    66  				ForceNew: false,
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
    73  	config := meta.(*Config)
    74  
    75  	region := getOptionalRegion(d, config)
    76  
    77  	frule := &compute.ForwardingRule{
    78  		IPAddress:   d.Get("ip_address").(string),
    79  		IPProtocol:  d.Get("ip_protocol").(string),
    80  		Description: d.Get("description").(string),
    81  		Name:        d.Get("name").(string),
    82  		PortRange:   d.Get("port_range").(string),
    83  		Target:      d.Get("target").(string),
    84  	}
    85  
    86  	log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule)
    87  	op, err := config.clientCompute.ForwardingRules.Insert(
    88  		config.Project, region, frule).Do()
    89  	if err != nil {
    90  		return fmt.Errorf("Error creating ForwardingRule: %s", err)
    91  	}
    92  
    93  	// It probably maybe worked, so store the ID now
    94  	d.SetId(frule.Name)
    95  
    96  	err = computeOperationWaitRegion(config, op, region, "Creating Fowarding Rule")
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	return resourceComputeForwardingRuleRead(d, meta)
   102  }
   103  
   104  func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
   105  	config := meta.(*Config)
   106  
   107  	region := getOptionalRegion(d, config)
   108  
   109  	d.Partial(true)
   110  
   111  	if d.HasChange("target") {
   112  		target_name := d.Get("target").(string)
   113  		target_ref := &compute.TargetReference{Target: target_name}
   114  		op, err := config.clientCompute.ForwardingRules.SetTarget(
   115  			config.Project, region, d.Id(), target_ref).Do()
   116  		if err != nil {
   117  			return fmt.Errorf("Error updating target: %s", err)
   118  		}
   119  
   120  		err = computeOperationWaitRegion(config, op, region, "Updating Forwarding Rule")
   121  		if err != nil {
   122  			return err
   123  		}
   124  
   125  		d.SetPartial("target")
   126  	}
   127  
   128  	d.Partial(false)
   129  
   130  	return resourceComputeForwardingRuleRead(d, meta)
   131  }
   132  
   133  func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
   134  	config := meta.(*Config)
   135  
   136  	region := getOptionalRegion(d, config)
   137  
   138  	frule, err := config.clientCompute.ForwardingRules.Get(
   139  		config.Project, region, d.Id()).Do()
   140  	if err != nil {
   141  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   142  			// The resource doesn't exist anymore
   143  			d.SetId("")
   144  
   145  			return nil
   146  		}
   147  
   148  		return fmt.Errorf("Error reading ForwardingRule: %s", err)
   149  	}
   150  
   151  	d.Set("ip_address", frule.IPAddress)
   152  	d.Set("ip_protocol", frule.IPProtocol)
   153  	d.Set("self_link", frule.SelfLink)
   154  
   155  	return nil
   156  }
   157  
   158  func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
   159  	config := meta.(*Config)
   160  
   161  	region := getOptionalRegion(d, config)
   162  
   163  	// Delete the ForwardingRule
   164  	log.Printf("[DEBUG] ForwardingRule delete request")
   165  	op, err := config.clientCompute.ForwardingRules.Delete(
   166  		config.Project, region, d.Id()).Do()
   167  	if err != nil {
   168  		return fmt.Errorf("Error deleting ForwardingRule: %s", err)
   169  	}
   170  
   171  	err = computeOperationWaitRegion(config, op, region, "Deleting Forwarding Rule")
   172  	if err != nil {
   173  		return err
   174  	}
   175  
   176  	d.SetId("")
   177  	return nil
   178  }