github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"target": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: false,
    33  			},
    34  
    35  			"description": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"ip_address": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Optional: true,
    44  				ForceNew: true,
    45  				Computed: true,
    46  			},
    47  
    48  			"ip_protocol": &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Optional: true,
    51  				ForceNew: true,
    52  				Computed: true,
    53  			},
    54  
    55  			"port_range": &schema.Schema{
    56  				Type:     schema.TypeString,
    57  				Optional: true,
    58  				ForceNew: true,
    59  			},
    60  
    61  			"project": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Optional: true,
    64  				ForceNew: true,
    65  				Computed: true,
    66  			},
    67  
    68  			"region": &schema.Schema{
    69  				Type:     schema.TypeString,
    70  				Optional: true,
    71  				ForceNew: true,
    72  				Computed: true,
    73  			},
    74  
    75  			"self_link": &schema.Schema{
    76  				Type:     schema.TypeString,
    77  				Computed: true,
    78  			},
    79  		},
    80  	}
    81  }
    82  
    83  func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
    84  	config := meta.(*Config)
    85  
    86  	region, err := getRegion(d, config)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	project, err := getProject(d, config)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	frule := &compute.ForwardingRule{
    97  		IPAddress:   d.Get("ip_address").(string),
    98  		IPProtocol:  d.Get("ip_protocol").(string),
    99  		Description: d.Get("description").(string),
   100  		Name:        d.Get("name").(string),
   101  		PortRange:   d.Get("port_range").(string),
   102  		Target:      d.Get("target").(string),
   103  	}
   104  
   105  	log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule)
   106  	op, err := config.clientCompute.ForwardingRules.Insert(
   107  		project, region, frule).Do()
   108  	if err != nil {
   109  		return fmt.Errorf("Error creating ForwardingRule: %s", err)
   110  	}
   111  
   112  	// It probably maybe worked, so store the ID now
   113  	d.SetId(frule.Name)
   114  
   115  	err = computeOperationWaitRegion(config, op, project, region, "Creating Fowarding Rule")
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	return resourceComputeForwardingRuleRead(d, meta)
   121  }
   122  
   123  func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
   124  	config := meta.(*Config)
   125  
   126  	region, err := getRegion(d, config)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	project, err := getProject(d, config)
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	d.Partial(true)
   137  
   138  	if d.HasChange("target") {
   139  		target_name := d.Get("target").(string)
   140  		target_ref := &compute.TargetReference{Target: target_name}
   141  		op, err := config.clientCompute.ForwardingRules.SetTarget(
   142  			project, region, d.Id(), target_ref).Do()
   143  		if err != nil {
   144  			return fmt.Errorf("Error updating target: %s", err)
   145  		}
   146  
   147  		err = computeOperationWaitRegion(config, op, project, region, "Updating Forwarding Rule")
   148  		if err != nil {
   149  			return err
   150  		}
   151  
   152  		d.SetPartial("target")
   153  	}
   154  
   155  	d.Partial(false)
   156  
   157  	return resourceComputeForwardingRuleRead(d, meta)
   158  }
   159  
   160  func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
   161  	config := meta.(*Config)
   162  
   163  	region, err := getRegion(d, config)
   164  	if err != nil {
   165  		return err
   166  	}
   167  
   168  	project, err := getProject(d, config)
   169  	if err != nil {
   170  		return err
   171  	}
   172  
   173  	frule, err := config.clientCompute.ForwardingRules.Get(
   174  		project, region, d.Id()).Do()
   175  	if err != nil {
   176  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   177  			log.Printf("[WARN] Removing Forwarding Rule %q because it's gone", d.Get("name").(string))
   178  			// The resource doesn't exist anymore
   179  			d.SetId("")
   180  
   181  			return nil
   182  		}
   183  
   184  		return fmt.Errorf("Error reading ForwardingRule: %s", err)
   185  	}
   186  
   187  	d.Set("name", frule.Name)
   188  	d.Set("target", frule.Target)
   189  	d.Set("description", frule.Description)
   190  	d.Set("port_range", frule.PortRange)
   191  	d.Set("project", project)
   192  	d.Set("region", region)
   193  	d.Set("ip_address", frule.IPAddress)
   194  	d.Set("ip_protocol", frule.IPProtocol)
   195  	d.Set("self_link", frule.SelfLink)
   196  	return nil
   197  }
   198  
   199  func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
   200  	config := meta.(*Config)
   201  
   202  	region, err := getRegion(d, config)
   203  	if err != nil {
   204  		return err
   205  	}
   206  
   207  	project, err := getProject(d, config)
   208  	if err != nil {
   209  		return err
   210  	}
   211  
   212  	// Delete the ForwardingRule
   213  	log.Printf("[DEBUG] ForwardingRule delete request")
   214  	op, err := config.clientCompute.ForwardingRules.Delete(
   215  		project, region, d.Id()).Do()
   216  	if err != nil {
   217  		return fmt.Errorf("Error deleting ForwardingRule: %s", err)
   218  	}
   219  
   220  	err = computeOperationWaitRegion(config, op, project, region, "Deleting Forwarding Rule")
   221  	if err != nil {
   222  		return err
   223  	}
   224  
   225  	d.SetId("")
   226  	return nil
   227  }