github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_global_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 resourceComputeGlobalForwardingRule() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeGlobalForwardingRuleCreate,
    15  		Read:   resourceComputeGlobalForwardingRuleRead,
    16  		Update: resourceComputeGlobalForwardingRuleUpdate,
    17  		Delete: resourceComputeGlobalForwardingRuleDelete,
    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  			},
    67  		},
    68  	}
    69  }
    70  
    71  func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
    72  	config := meta.(*Config)
    73  
    74  	frule := &compute.ForwardingRule{
    75  		IPAddress:   d.Get("ip_address").(string),
    76  		IPProtocol:  d.Get("ip_protocol").(string),
    77  		Description: d.Get("description").(string),
    78  		Name:        d.Get("name").(string),
    79  		PortRange:   d.Get("port_range").(string),
    80  		Target:      d.Get("target").(string),
    81  	}
    82  
    83  	op, err := config.clientCompute.GlobalForwardingRules.Insert(
    84  		config.Project, frule).Do()
    85  	if err != nil {
    86  		return fmt.Errorf("Error creating Global Forwarding Rule: %s", err)
    87  	}
    88  
    89  	// It probably maybe worked, so store the ID now
    90  	d.SetId(frule.Name)
    91  
    92  	err = computeOperationWaitGlobal(config, op, "Creating Global Fowarding Rule")
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	return resourceComputeGlobalForwardingRuleRead(d, meta)
    98  }
    99  
   100  func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
   101  	config := meta.(*Config)
   102  
   103  	d.Partial(true)
   104  
   105  	if d.HasChange("target") {
   106  		target_name := d.Get("target").(string)
   107  		target_ref := &compute.TargetReference{Target: target_name}
   108  		op, err := config.clientCompute.GlobalForwardingRules.SetTarget(
   109  			config.Project, d.Id(), target_ref).Do()
   110  		if err != nil {
   111  			return fmt.Errorf("Error updating target: %s", err)
   112  		}
   113  
   114  		err = computeOperationWaitGlobal(config, op, "Updating Global Forwarding Rule")
   115  		if err != nil {
   116  			return err
   117  		}
   118  
   119  		d.SetPartial("target")
   120  	}
   121  
   122  	d.Partial(false)
   123  
   124  	return resourceComputeGlobalForwardingRuleRead(d, meta)
   125  }
   126  
   127  func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
   128  	config := meta.(*Config)
   129  
   130  	frule, err := config.clientCompute.GlobalForwardingRules.Get(
   131  		config.Project, d.Id()).Do()
   132  	if err != nil {
   133  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   134  			// The resource doesn't exist anymore
   135  			d.SetId("")
   136  
   137  			return nil
   138  		}
   139  
   140  		return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
   141  	}
   142  
   143  	d.Set("ip_address", frule.IPAddress)
   144  	d.Set("ip_protocol", frule.IPProtocol)
   145  	d.Set("self_link", frule.SelfLink)
   146  
   147  	return nil
   148  }
   149  
   150  func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
   151  	config := meta.(*Config)
   152  
   153  	// Delete the GlobalForwardingRule
   154  	log.Printf("[DEBUG] GlobalForwardingRule delete request")
   155  	op, err := config.clientCompute.GlobalForwardingRules.Delete(
   156  		config.Project, d.Id()).Do()
   157  	if err != nil {
   158  		return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
   159  	}
   160  
   161  	err = computeOperationWaitGlobal(config, op, "Deleting GlobalForwarding Rule")
   162  	if err != nil {
   163  		return err
   164  	}
   165  
   166  	d.SetId("")
   167  	return nil
   168  }