2015-10-30 21:34:14 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeGlobalForwardingRule() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeGlobalForwardingRuleCreate,
|
|
|
|
Read: resourceComputeGlobalForwardingRuleRead,
|
|
|
|
Update: resourceComputeGlobalForwardingRuleUpdate,
|
|
|
|
Delete: resourceComputeGlobalForwardingRuleDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
2015-10-30 21:34:14 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Required: true,
|
2015-10-30 21:34:14 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"target": &schema.Schema{
|
2015-10-30 21:34:14 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Required: true,
|
2015-10-30 21:34:14 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"ip_address": &schema.Schema{
|
2015-10-30 21:34:14 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
2015-10-30 21:34:14 +01:00
|
|
|
ForceNew: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2015-10-30 21:34:14 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"ip_protocol": &schema.Schema{
|
2015-10-30 21:34:14 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2015-10-30 21:34:14 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"port_range": &schema.Schema{
|
2015-10-30 21:34:14 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-10-30 21:34:14 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
2015-10-30 21:34:14 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-10-30 21:34:14 +01:00
|
|
|
},
|
2016-04-10 18:22:44 +02:00
|
|
|
|
|
|
|
"region": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Deprecated: "Please remove this attribute (it was never used)",
|
|
|
|
},
|
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
|
|
|
},
|
2015-10-30 21:34:14 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-30 21:34:14 +01:00
|
|
|
frule := &compute.ForwardingRule{
|
|
|
|
IPAddress: d.Get("ip_address").(string),
|
|
|
|
IPProtocol: d.Get("ip_protocol").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
PortRange: d.Get("port_range").(string),
|
|
|
|
Target: d.Get("target").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientCompute.GlobalForwardingRules.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, frule).Do()
|
2015-10-30 21:34:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Global Forwarding Rule: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(frule.Name)
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Creating Global Fowarding Rule")
|
2015-10-30 21:34:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeGlobalForwardingRuleRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-30 21:34:14 +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.GlobalForwardingRules.SetTarget(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id(), target_ref).Do()
|
2015-10-30 21:34:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating target: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Updating Global Forwarding Rule")
|
2015-10-30 21:34:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("target")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceComputeGlobalForwardingRuleRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-30 21:34:14 +01:00
|
|
|
frule, err := config.clientCompute.GlobalForwardingRules.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2015-10-30 21:34:14 +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 Global Forwarding Rule %q because it's gone", d.Get("name").(string))
|
2015-10-30 21:34:14 +01:00
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("ip_address", frule.IPAddress)
|
|
|
|
d.Set("ip_protocol", frule.IPProtocol)
|
|
|
|
d.Set("self_link", frule.SelfLink)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-30 21:34:14 +01:00
|
|
|
// Delete the GlobalForwardingRule
|
|
|
|
log.Printf("[DEBUG] GlobalForwardingRule delete request")
|
|
|
|
op, err := config.clientCompute.GlobalForwardingRules.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2015-10-30 21:34:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Deleting GlobalForwarding Rule")
|
2015-10-30 21:34:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|