2014-11-20 18:40:17 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2016-11-22 22:14:28 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/googleapi"
|
2014-11-20 18:40:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeForwardingRule() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeForwardingRuleCreate,
|
|
|
|
Read: resourceComputeForwardingRuleRead,
|
|
|
|
Delete: resourceComputeForwardingRuleDelete,
|
|
|
|
Update: resourceComputeForwardingRuleUpdate,
|
2016-08-04 19:12:09 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2014-11-20 18:40:17 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
2014-11-20 18:40:17 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Required: true,
|
2014-11-20 18:40:17 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"target": &schema.Schema{
|
2014-11-20 18:40:17 +01:00
|
|
|
Type: schema.TypeString,
|
2016-11-18 17:08:26 +01:00
|
|
|
Optional: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
ForceNew: false,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
2016-11-18 17:08:26 +01:00
|
|
|
"backend_service": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"ip_address": &schema.Schema{
|
2014-11-20 18:40:17 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
2014-11-20 18:40:17 +01:00
|
|
|
ForceNew: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"ip_protocol": &schema.Schema{
|
2014-11-20 18:40:17 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
2016-11-18 17:08:26 +01:00
|
|
|
"load_balancing_scheme": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: "EXTERNAL",
|
|
|
|
},
|
|
|
|
|
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-11-22 22:14:28 +01:00
|
|
|
Computed: true,
|
2016-11-18 17:08:26 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"port_range": &schema.Schema{
|
2014-11-20 18:40:17 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2017-03-14 00:56:25 +01:00
|
|
|
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
|
|
|
|
if old == new+"-"+new {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
2016-11-22 22:14:28 +01:00
|
|
|
"ports": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
2014-11-20 18:40:17 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-08-04 19:12:09 +02:00
|
|
|
Computed: true,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-08-04 19:12:09 +02:00
|
|
|
Computed: true,
|
2016-04-10 18:59:57 +02:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"self_link": &schema.Schema{
|
2016-04-10 18:59:57 +02:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2016-04-10 18:59:57 +02:00
|
|
|
},
|
2016-11-18 17:08:26 +01:00
|
|
|
|
|
|
|
"subnetwork": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-11-22 22:14:28 +01:00
|
|
|
Computed: true,
|
2016-11-18 17:08:26 +01:00
|
|
|
},
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-04 22:54:18 +02:00
|
|
|
|
2016-11-22 22:14:28 +01:00
|
|
|
ps := d.Get("ports").(*schema.Set).List()
|
|
|
|
ports := make([]string, 0, len(ps))
|
|
|
|
for _, v := range ps {
|
|
|
|
ports = append(ports, v.(string))
|
|
|
|
}
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
frule := &compute.ForwardingRule{
|
2016-11-18 17:08:26 +01:00
|
|
|
BackendService: d.Get("backend_service").(string),
|
|
|
|
IPAddress: d.Get("ip_address").(string),
|
|
|
|
IPProtocol: d.Get("ip_protocol").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
LoadBalancingScheme: d.Get("load_balancing_scheme").(string),
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Network: d.Get("network").(string),
|
|
|
|
PortRange: d.Get("port_range").(string),
|
2016-11-22 22:14:28 +01:00
|
|
|
Ports: ports,
|
2016-11-18 17:08:26 +01:00
|
|
|
Subnetwork: d.Get("subnetwork").(string),
|
|
|
|
Target: d.Get("target").(string),
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule)
|
2016-11-22 22:14:28 +01:00
|
|
|
op, err := config.clientCompute.ForwardingRules.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, frule).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating ForwardingRule: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(frule.Name)
|
|
|
|
|
2016-11-22 22:14:28 +01:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Creating Fowarding Rule")
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeForwardingRuleRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-04 22:54:18 +02:00
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
if d.HasChange("target") {
|
|
|
|
target_name := d.Get("target").(string)
|
|
|
|
target_ref := &compute.TargetReference{Target: target_name}
|
2016-11-22 22:14:28 +01:00
|
|
|
op, err := config.clientCompute.ForwardingRules.SetTarget(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, d.Id(), target_ref).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating target: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:14:28 +01:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Updating Forwarding Rule")
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("target")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceComputeForwardingRuleRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-04 22:54:18 +02:00
|
|
|
|
2016-11-22 22:14:28 +01:00
|
|
|
frule, err := config.clientCompute.ForwardingRules.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, d.Id()).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing Forwarding Rule %q because it's gone", d.Get("name").(string))
|
2014-11-20 18:40:17 +01:00
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading ForwardingRule: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-08-04 19:12:09 +02:00
|
|
|
d.Set("name", frule.Name)
|
|
|
|
d.Set("target", frule.Target)
|
2016-11-18 17:08:26 +01:00
|
|
|
d.Set("backend_service", frule.BackendService)
|
2016-08-04 19:12:09 +02:00
|
|
|
d.Set("description", frule.Description)
|
2016-11-18 17:08:26 +01:00
|
|
|
d.Set("load_balancing_scheme", frule.LoadBalancingScheme)
|
|
|
|
d.Set("network", frule.Network)
|
2016-08-04 19:12:09 +02:00
|
|
|
d.Set("port_range", frule.PortRange)
|
2016-11-22 22:14:28 +01:00
|
|
|
d.Set("ports", frule.Ports)
|
2016-08-04 19:12:09 +02:00
|
|
|
d.Set("project", project)
|
|
|
|
d.Set("region", region)
|
2016-11-18 17:08:26 +01:00
|
|
|
d.Set("subnetwork", frule.Subnetwork)
|
2014-11-20 18:40:17 +01:00
|
|
|
d.Set("ip_address", frule.IPAddress)
|
|
|
|
d.Set("ip_protocol", frule.IPProtocol)
|
|
|
|
d.Set("self_link", frule.SelfLink)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-04 22:54:18 +02:00
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
// Delete the ForwardingRule
|
|
|
|
log.Printf("[DEBUG] ForwardingRule delete request")
|
2016-11-22 22:14:28 +01:00
|
|
|
op, err := config.clientCompute.ForwardingRules.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, d.Id()).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting ForwardingRule: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:14:28 +01:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Deleting Forwarding Rule")
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|