2014-11-20 18:40:17 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"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,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ip_protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"port_range": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"target": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
region := getOptionalRegion(d, config)
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
frule := &compute.ForwardingRule{
|
2015-02-20 19:22:26 +01:00
|
|
|
IPAddress: d.Get("ip_address").(string),
|
|
|
|
IPProtocol: d.Get("ip_protocol").(string),
|
2014-11-20 18:40:17 +01:00
|
|
|
Description: d.Get("description").(string),
|
2015-02-20 19:22:26 +01:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
PortRange: d.Get("port_range").(string),
|
|
|
|
Target: d.Get("target").(string),
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule)
|
|
|
|
op, err := config.clientCompute.ForwardingRules.Insert(
|
2015-09-04 22:54:18 +02:00
|
|
|
config.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)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, 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)
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
region := getOptionalRegion(d, config)
|
|
|
|
|
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}
|
|
|
|
op, err := config.clientCompute.ForwardingRules.SetTarget(
|
2015-09-04 22:54:18 +02:00
|
|
|
config.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)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, 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)
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
region := getOptionalRegion(d, config)
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
frule, err := config.clientCompute.ForwardingRules.Get(
|
2015-09-04 22:54:18 +02:00
|
|
|
config.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 {
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading ForwardingRule: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-09-04 22:54:18 +02:00
|
|
|
region := getOptionalRegion(d, config)
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
// Delete the ForwardingRule
|
|
|
|
log.Printf("[DEBUG] ForwardingRule delete request")
|
|
|
|
op, err := config.clientCompute.ForwardingRules.Delete(
|
2015-09-04 22:54:18 +02:00
|
|
|
config.Project, region, d.Id()).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting ForwardingRule: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, 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
|
|
|
|
}
|