mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-24 23:46:26 -06:00
170 lines
4.1 KiB
Go
170 lines
4.1 KiB
Go
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{
|
|
"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,
|
|
},
|
|
|
|
"region": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"self_link": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
|
|
"target": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
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(
|
|
config.Project, frule).Do()
|
|
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)
|
|
|
|
err = computeOperationWaitGlobal(config, op, "Creating Global Fowarding Rule")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceComputeGlobalForwardingRuleRead(d, meta)
|
|
}
|
|
|
|
func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
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(
|
|
config.Project, d.Id(), target_ref).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error updating target: %s", err)
|
|
}
|
|
|
|
err = computeOperationWaitGlobal(config, op, "Updating Global Forwarding Rule")
|
|
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)
|
|
|
|
frule, err := config.clientCompute.GlobalForwardingRules.Get(
|
|
config.Project, d.Id()).Do()
|
|
if err != nil {
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
log.Printf("[WARN] Removing Global Forwarding Rule %q because it's gone", d.Get("name").(string))
|
|
// 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)
|
|
|
|
// Delete the GlobalForwardingRule
|
|
log.Printf("[DEBUG] GlobalForwardingRule delete request")
|
|
op, err := config.clientCompute.GlobalForwardingRules.Delete(
|
|
config.Project, d.Id()).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
|
|
}
|
|
|
|
err = computeOperationWaitGlobal(config, op, "Deleting GlobalForwarding Rule")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId("")
|
|
return nil
|
|
}
|