github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  				Optional: true,
    32  				ForceNew: false,
    33  			},
    34  
    35  			"backend_service": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"description": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Optional: true,
    44  				ForceNew: true,
    45  			},
    46  
    47  			"ip_address": &schema.Schema{
    48  				Type:     schema.TypeString,
    49  				Optional: true,
    50  				ForceNew: true,
    51  				Computed: true,
    52  			},
    53  
    54  			"ip_protocol": &schema.Schema{
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  				ForceNew: true,
    58  				Computed: true,
    59  			},
    60  
    61  			"load_balancing_scheme": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Optional: true,
    64  				ForceNew: true,
    65  				Default:  "EXTERNAL",
    66  			},
    67  
    68  			"network": &schema.Schema{
    69  				Type:     schema.TypeString,
    70  				Optional: true,
    71  				ForceNew: true,
    72  				Computed: true,
    73  			},
    74  
    75  			"port_range": &schema.Schema{
    76  				Type:     schema.TypeString,
    77  				Optional: true,
    78  				ForceNew: true,
    79  				DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
    80  					if old == new+"-"+new {
    81  						return true
    82  					}
    83  					return false
    84  				},
    85  			},
    86  
    87  			"ports": &schema.Schema{
    88  				Type:     schema.TypeSet,
    89  				Elem:     &schema.Schema{Type: schema.TypeString},
    90  				Optional: true,
    91  				ForceNew: true,
    92  				Set:      schema.HashString,
    93  				MaxItems: 5,
    94  			},
    95  
    96  			"project": &schema.Schema{
    97  				Type:     schema.TypeString,
    98  				Optional: true,
    99  				ForceNew: true,
   100  				Computed: true,
   101  			},
   102  
   103  			"region": &schema.Schema{
   104  				Type:     schema.TypeString,
   105  				Optional: true,
   106  				ForceNew: true,
   107  				Computed: true,
   108  			},
   109  
   110  			"self_link": &schema.Schema{
   111  				Type:     schema.TypeString,
   112  				Computed: true,
   113  			},
   114  
   115  			"subnetwork": &schema.Schema{
   116  				Type:     schema.TypeString,
   117  				Optional: true,
   118  				ForceNew: true,
   119  				Computed: true,
   120  			},
   121  		},
   122  	}
   123  }
   124  
   125  func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
   126  	config := meta.(*Config)
   127  
   128  	region, err := getRegion(d, config)
   129  	if err != nil {
   130  		return err
   131  	}
   132  
   133  	project, err := getProject(d, config)
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	ps := d.Get("ports").(*schema.Set).List()
   139  	ports := make([]string, 0, len(ps))
   140  	for _, v := range ps {
   141  		ports = append(ports, v.(string))
   142  	}
   143  
   144  	frule := &compute.ForwardingRule{
   145  		BackendService:      d.Get("backend_service").(string),
   146  		IPAddress:           d.Get("ip_address").(string),
   147  		IPProtocol:          d.Get("ip_protocol").(string),
   148  		Description:         d.Get("description").(string),
   149  		LoadBalancingScheme: d.Get("load_balancing_scheme").(string),
   150  		Name:                d.Get("name").(string),
   151  		Network:             d.Get("network").(string),
   152  		PortRange:           d.Get("port_range").(string),
   153  		Ports:               ports,
   154  		Subnetwork:          d.Get("subnetwork").(string),
   155  		Target:              d.Get("target").(string),
   156  	}
   157  
   158  	log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule)
   159  	op, err := config.clientCompute.ForwardingRules.Insert(
   160  		project, region, frule).Do()
   161  	if err != nil {
   162  		return fmt.Errorf("Error creating ForwardingRule: %s", err)
   163  	}
   164  
   165  	// It probably maybe worked, so store the ID now
   166  	d.SetId(frule.Name)
   167  
   168  	err = computeOperationWaitRegion(config, op, project, region, "Creating Fowarding Rule")
   169  	if err != nil {
   170  		return err
   171  	}
   172  
   173  	return resourceComputeForwardingRuleRead(d, meta)
   174  }
   175  
   176  func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
   177  	config := meta.(*Config)
   178  
   179  	region, err := getRegion(d, config)
   180  	if err != nil {
   181  		return err
   182  	}
   183  
   184  	project, err := getProject(d, config)
   185  	if err != nil {
   186  		return err
   187  	}
   188  
   189  	d.Partial(true)
   190  
   191  	if d.HasChange("target") {
   192  		target_name := d.Get("target").(string)
   193  		target_ref := &compute.TargetReference{Target: target_name}
   194  		op, err := config.clientCompute.ForwardingRules.SetTarget(
   195  			project, region, d.Id(), target_ref).Do()
   196  		if err != nil {
   197  			return fmt.Errorf("Error updating target: %s", err)
   198  		}
   199  
   200  		err = computeOperationWaitRegion(config, op, project, region, "Updating Forwarding Rule")
   201  		if err != nil {
   202  			return err
   203  		}
   204  
   205  		d.SetPartial("target")
   206  	}
   207  
   208  	d.Partial(false)
   209  
   210  	return resourceComputeForwardingRuleRead(d, meta)
   211  }
   212  
   213  func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
   214  	config := meta.(*Config)
   215  
   216  	region, err := getRegion(d, config)
   217  	if err != nil {
   218  		return err
   219  	}
   220  
   221  	project, err := getProject(d, config)
   222  	if err != nil {
   223  		return err
   224  	}
   225  
   226  	frule, err := config.clientCompute.ForwardingRules.Get(
   227  		project, region, d.Id()).Do()
   228  	if err != nil {
   229  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   230  			log.Printf("[WARN] Removing Forwarding Rule %q because it's gone", d.Get("name").(string))
   231  			// The resource doesn't exist anymore
   232  			d.SetId("")
   233  
   234  			return nil
   235  		}
   236  
   237  		return fmt.Errorf("Error reading ForwardingRule: %s", err)
   238  	}
   239  
   240  	d.Set("name", frule.Name)
   241  	d.Set("target", frule.Target)
   242  	d.Set("backend_service", frule.BackendService)
   243  	d.Set("description", frule.Description)
   244  	d.Set("load_balancing_scheme", frule.LoadBalancingScheme)
   245  	d.Set("network", frule.Network)
   246  	d.Set("port_range", frule.PortRange)
   247  	d.Set("ports", frule.Ports)
   248  	d.Set("project", project)
   249  	d.Set("region", region)
   250  	d.Set("subnetwork", frule.Subnetwork)
   251  	d.Set("ip_address", frule.IPAddress)
   252  	d.Set("ip_protocol", frule.IPProtocol)
   253  	d.Set("self_link", frule.SelfLink)
   254  	return nil
   255  }
   256  
   257  func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
   258  	config := meta.(*Config)
   259  
   260  	region, err := getRegion(d, config)
   261  	if err != nil {
   262  		return err
   263  	}
   264  
   265  	project, err := getProject(d, config)
   266  	if err != nil {
   267  		return err
   268  	}
   269  
   270  	// Delete the ForwardingRule
   271  	log.Printf("[DEBUG] ForwardingRule delete request")
   272  	op, err := config.clientCompute.ForwardingRules.Delete(
   273  		project, region, d.Id()).Do()
   274  	if err != nil {
   275  		return fmt.Errorf("Error deleting ForwardingRule: %s", err)
   276  	}
   277  
   278  	err = computeOperationWaitRegion(config, op, project, region, "Deleting Forwarding Rule")
   279  	if err != nil {
   280  		return err
   281  	}
   282  
   283  	d.SetId("")
   284  	return nil
   285  }