Skip to content

Commit

Permalink
Add Beta support & Beta feature ip_version to google_compute_global_f…
Browse files Browse the repository at this point in the history
…orwarding_rule (#265)

* Add versioned Beta support to google_compute_global_forwarding_rule.

* Add Beta support for ip_version in google_compute_global_forwarding_rule.

* Temporary commit with compute_shared_operation.go changes.

* Added a test to see if v1 GFR is still IPV4, moved to a TestCheckFunc

* This API returns the original self links, but let's make sure we don't diff.
  • Loading branch information
rileykarson authored Aug 4, 2017
1 parent 501e95a commit 0d02b11
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 25 deletions.
19 changes: 19 additions & 0 deletions google/compute_shared_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ import (
"google.golang.org/api/compute/v1"
)

func computeSharedOperationWait(config *Config, op interface{}, project string, activity string) error {
return computeSharedOperationWaitTime(config, op, project, 4, activity)
}

func computeSharedOperationWaitTime(config *Config, op interface{}, project string, minutes int, activity string) error {
if op == nil {
panic("Attempted to wait on an Operation that was nil.")
}

switch op.(type) {
case *compute.Operation:
return computeOperationWaitTime(config, op.(*compute.Operation), project, activity, minutes)
case *computeBeta.Operation:
return computeBetaOperationWaitGlobalTime(config, op.(*computeBeta.Operation), project, activity, minutes)
default:
panic("Attempted to wait on an Operation of unknown type.")
}
}

func computeSharedOperationWaitZone(config *Config, op interface{}, project string, zone, activity string) error {
return computeSharedOperationWaitZoneTime(config, op, project, zone, 4, activity)
}
Expand Down
139 changes: 114 additions & 25 deletions google/resource_compute_global_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import (
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
)

var GlobalForwardingRuleBaseApiVersion = v1
var GlobalForwardingRuleVersionedFeatures = []Feature{Feature{Version: v0beta, Item: "ip_version"}}

func resourceComputeGlobalForwardingRule() *schema.Resource {
return &schema.Resource{
Create: resourceComputeGlobalForwardingRuleCreate,
Expand All @@ -23,8 +29,9 @@ func resourceComputeGlobalForwardingRule() *schema.Resource {
},

"target": &schema.Schema{
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: compareSelfLinkRelativePaths,
},

"description": &schema.Schema{
Expand Down Expand Up @@ -53,6 +60,13 @@ func resourceComputeGlobalForwardingRule() *schema.Resource {
ForceNew: true,
},

"ip_version": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6"}, false),
},

"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand All @@ -75,32 +89,54 @@ func resourceComputeGlobalForwardingRule() *schema.Resource {
}

func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersion(d, GlobalForwardingRuleBaseApiVersion, GlobalForwardingRuleVersionedFeatures)
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

frule := &compute.ForwardingRule{
frule := &computeBeta.ForwardingRule{
IPAddress: d.Get("ip_address").(string),
IPProtocol: d.Get("ip_protocol").(string),
IpVersion: d.Get("ip_version").(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(
project, frule).Do()
if err != nil {
return fmt.Errorf("Error creating Global Forwarding Rule: %s", err)
var op interface{}
switch computeApiVersion {
case v1:
v1Frule := &compute.ForwardingRule{}
err = Convert(frule, v1Frule)
if err != nil {
return err
}

op, err = config.clientCompute.GlobalForwardingRules.Insert(project, v1Frule).Do()
if err != nil {
return fmt.Errorf("Error creating Global Forwarding Rule: %s", err)
}
case v0beta:
v0BetaFrule := &computeBeta.ForwardingRule{}
err = Convert(frule, v0BetaFrule)
if err != nil {
return err
}

op, err = config.clientComputeBeta.GlobalForwardingRules.Insert(project, v0BetaFrule).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 = computeOperationWait(config, op, project, "Creating Global Fowarding Rule")
err = computeSharedOperationWait(config, op, project, "Creating Global Fowarding Rule")
if err != nil {
return err
}
Expand All @@ -109,6 +145,7 @@ func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta inte
}

func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersionUpdate(d, GlobalForwardingRuleBaseApiVersion, GlobalForwardingRuleVersionedFeatures, []Feature{})
config := meta.(*Config)

project, err := getProject(d, config)
Expand All @@ -119,15 +156,38 @@ func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta inte
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(
project, d.Id(), target_ref).Do()
if err != nil {
return fmt.Errorf("Error updating target: %s", err)
target := d.Get("target").(string)
targetRef := &computeBeta.TargetReference{Target: target}

var op interface{}
switch computeApiVersion {
case v1:
v1TargetRef := &compute.TargetReference{}
err = Convert(targetRef, v1TargetRef)
if err != nil {
return err
}

op, err = config.clientCompute.GlobalForwardingRules.SetTarget(
project, d.Id(), v1TargetRef).Do()
if err != nil {
return fmt.Errorf("Error updating target: %s", err)
}
case v0beta:
v0BetaTargetRef := &compute.TargetReference{}
err = Convert(targetRef, v0BetaTargetRef)
if err != nil {
return err
}

op, err = config.clientCompute.GlobalForwardingRules.SetTarget(
project, d.Id(), v0BetaTargetRef).Do()
if err != nil {
return fmt.Errorf("Error updating target: %s", err)
}
}

err = computeOperationWait(config, op, project, "Updating Global Forwarding Rule")
err = computeSharedOperationWait(config, op, project, "Updating Global Forwarding Rule")
if err != nil {
return err
}
Expand All @@ -141,27 +201,48 @@ func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta inte
}

func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersion(d, GlobalForwardingRuleBaseApiVersion, GlobalForwardingRuleVersionedFeatures)
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

frule, err := config.clientCompute.GlobalForwardingRules.Get(
project, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Global Forwarding Rule %q", d.Get("name").(string)))
frule := &computeBeta.ForwardingRule{}
switch computeApiVersion {
case v1:
v1Frule, err := config.clientCompute.GlobalForwardingRules.Get(project, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Global Forwarding Rule %q", d.Get("name").(string)))
}

err = Convert(v1Frule, frule)
if err != nil {
return err
}
case v0beta:
v0BetaFrule, err := config.clientComputeBeta.GlobalForwardingRules.Get(project, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Global Forwarding Rule %q", d.Get("name").(string)))
}

err = Convert(v0BetaFrule, frule)
if err != nil {
return err
}
}

d.Set("ip_address", frule.IPAddress)
d.Set("ip_protocol", frule.IPProtocol)
d.Set("self_link", frule.SelfLink)
d.Set("ip_version", frule.IpVersion)
d.Set("self_link", ConvertSelfLinkToV1(frule.SelfLink))

return nil
}

func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
computeApiVersion := getComputeApiVersion(d, GlobalForwardingRuleBaseApiVersion, GlobalForwardingRuleVersionedFeatures)
config := meta.(*Config)

project, err := getProject(d, config)
Expand All @@ -171,13 +252,21 @@ func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta inte

// Delete the GlobalForwardingRule
log.Printf("[DEBUG] GlobalForwardingRule delete request")
op, err := config.clientCompute.GlobalForwardingRules.Delete(
project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
var op interface{}
switch computeApiVersion {
case v1:
op, err = config.clientCompute.GlobalForwardingRules.Delete(project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
}
case v0beta:
op, err = config.clientComputeBeta.GlobalForwardingRules.Delete(project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err)
}
}

err = computeOperationWait(config, op, project, "Deleting GlobalForwarding Rule")
err = computeSharedOperationWait(config, op, project, "Deleting GlobalForwarding Rule")
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 0d02b11

Please sign in to comment.