diff --git a/builtin/providers/google/operation.go b/builtin/providers/google/operation.go index b1f2f255bc54..b9d33475262d 100644 --- a/builtin/providers/google/operation.go +++ b/builtin/providers/google/operation.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + computeBeta "google.golang.org/api/compute/v0.beta" "google.golang.org/api/compute/v1" "github.com/hashicorp/terraform/helper/resource" @@ -29,6 +30,15 @@ type OperationWaiter struct { Type OperationWaitType } +type OperationWaiterBeta struct { + Service *computeBeta.Service + Op *computeBeta.Operation + Project string + Region string + Zone string + Type OperationWaitType +} + func (w *OperationWaiter) RefreshFunc() resource.StateRefreshFunc { return func() (interface{}, string, error) { var op *compute.Operation @@ -57,6 +67,34 @@ func (w *OperationWaiter) RefreshFunc() resource.StateRefreshFunc { } } +func (w *OperationWaiterBeta) RefreshFunc() resource.StateRefreshFunc { + return func() (interface{}, string, error) { + var op *computeBeta.Operation + var err error + + switch w.Type { + case OperationWaitGlobal: + op, err = w.Service.GlobalOperations.Get( + w.Project, w.Op.Name).Do() + case OperationWaitRegion: + op, err = w.Service.RegionOperations.Get( + w.Project, w.Region, w.Op.Name).Do() + case OperationWaitZone: + op, err = w.Service.ZoneOperations.Get( + w.Project, w.Zone, w.Op.Name).Do() + default: + return nil, "bad-type", fmt.Errorf( + "Invalid wait type: %#v", w.Type) + } + + if err != nil { + return nil, "", err + } + + return op, op.Status, nil + } +} + func (w *OperationWaiter) Conf() *resource.StateChangeConf { return &resource.StateChangeConf{ Pending: []string{"PENDING", "RUNNING"}, @@ -65,10 +103,22 @@ func (w *OperationWaiter) Conf() *resource.StateChangeConf { } } +func (w *OperationWaiterBeta) Conf() *resource.StateChangeConf { + return &resource.StateChangeConf{ + Pending: []string{"PENDING", "RUNNING"}, + Target: "DONE", + Refresh: w.RefreshFunc(), + } +} + // OperationError wraps compute.OperationError and implements the // error interface so it can be returned. type OperationError compute.OperationError +// OperationErrorBeta wraps computeBeta.OperationError and implements the +// error interface so it can be returned. +type OperationErrorBeta computeBeta.OperationError + func (e OperationError) Error() string { var buf bytes.Buffer @@ -78,3 +128,13 @@ func (e OperationError) Error() string { return buf.String() } + +func (e OperationErrorBeta) Error() string { + var buf bytes.Buffer + + for _, err := range e.Errors { + buf.WriteString(err.Message + "\n") + } + + return buf.String() +} diff --git a/builtin/providers/google/resource_compute_https_health_check.go b/builtin/providers/google/resource_compute_https_health_check.go index ead462ca7506..de7ebc7b0bcf 100644 --- a/builtin/providers/google/resource_compute_https_health_check.go +++ b/builtin/providers/google/resource_compute_https_health_check.go @@ -122,7 +122,7 @@ func resourceComputeHttpsHealthCheckCreate(d *schema.ResourceData, meta interfac d.SetId(hchk.Name) // Wait for the operation to complete - w := &OperationWaiter{ + w := &OperationWaiterBeta{ Service: config.clientComputeBeta, Op: op, Project: config.Project, @@ -141,7 +141,7 @@ func resourceComputeHttpsHealthCheckCreate(d *schema.ResourceData, meta interfac d.SetId("") // Return the error - return OperationError(*op.Error) + return OperationErrorBeta(*op.Error) } return resourceComputeHttpsHealthCheckRead(d, meta) @@ -191,7 +191,7 @@ func resourceComputeHttpsHealthCheckUpdate(d *schema.ResourceData, meta interfac d.SetId(hchk.Name) // Wait for the operation to complete - w := &OperationWaiter{ + w := &OperationWaiterBeta{ Service: config.clientComputeBeta, Op: op, Project: config.Project, @@ -210,7 +210,7 @@ func resourceComputeHttpsHealthCheckUpdate(d *schema.ResourceData, meta interfac d.SetId("") // Return the error - return OperationError(*op.Error) + return OperationErrorBeta(*op.Error) } return resourceComputeHttpsHealthCheckRead(d, meta) @@ -255,7 +255,7 @@ func resourceComputeHttpsHealthCheckDelete(d *schema.ResourceData, meta interfac } // Wait for the operation to complete - w := &OperationWaiter{ + w := &OperationWaiterBeta{ Service: config.clientComputeBeta, Op: op, Project: config.Project, @@ -271,7 +271,7 @@ func resourceComputeHttpsHealthCheckDelete(d *schema.ResourceData, meta interfac op = opRaw.(*compute.Operation) if op.Error != nil { // Return the error - return OperationError(*op.Error) + return OperationErrorBeta(*op.Error) } d.SetId("")