2015-09-24 20:30:12 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-07-25 23:22:27 +00:00
|
|
|
"strings"
|
2015-09-24 20:30:12 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
|
2017-08-09 22:02:54 +00:00
|
|
|
computeBeta "google.golang.org/api/compute/v0.beta"
|
2017-07-25 23:22:27 +00:00
|
|
|
"google.golang.org/api/compute/v1"
|
2015-09-24 20:30:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ComputeOperationWaiter struct {
|
|
|
|
Service *compute.Service
|
|
|
|
Op *compute.Operation
|
|
|
|
Project string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ComputeOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
var op *compute.Operation
|
|
|
|
var err error
|
|
|
|
|
2017-07-25 23:22:27 +00:00
|
|
|
if w.Op.Zone != "" {
|
|
|
|
zoneURLParts := strings.Split(w.Op.Zone, "/")
|
|
|
|
zone := zoneURLParts[len(zoneURLParts)-1]
|
|
|
|
op, err = w.Service.ZoneOperations.Get(w.Project, zone, w.Op.Name).Do()
|
|
|
|
} else if w.Op.Region != "" {
|
|
|
|
regionURLParts := strings.Split(w.Op.Region, "/")
|
|
|
|
region := regionURLParts[len(regionURLParts)-1]
|
|
|
|
op, err = w.Service.RegionOperations.Get(w.Project, region, w.Op.Name).Do()
|
|
|
|
} else {
|
|
|
|
op, err = w.Service.GlobalOperations.Get(w.Project, w.Op.Name).Do()
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Got %q when asking for operation %q", op.Status, w.Op.Name)
|
|
|
|
return op, op.Status, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ComputeOperationWaiter) Conf() *resource.StateChangeConf {
|
|
|
|
return &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING", "RUNNING"},
|
2016-01-21 01:20:41 +00:00
|
|
|
Target: []string{"DONE"},
|
2015-09-24 20:30:12 +00:00
|
|
|
Refresh: w.RefreshFunc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComputeOperationError wraps compute.OperationError and implements the
|
|
|
|
// error interface so it can be returned.
|
|
|
|
type ComputeOperationError compute.OperationError
|
|
|
|
|
|
|
|
func (e ComputeOperationError) Error() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, err := range e.Errors {
|
|
|
|
buf.WriteString(err.Message + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:36:03 +00:00
|
|
|
func computeOperationWait(client *compute.Service, op *compute.Operation, project, activity string) error {
|
|
|
|
return computeOperationWaitTime(client, op, project, activity, 4)
|
2016-11-13 02:01:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 22:36:03 +00:00
|
|
|
func computeOperationWaitTime(client *compute.Service, op *compute.Operation, project, activity string, timeoutMin int) error {
|
2015-09-24 20:30:12 +00:00
|
|
|
w := &ComputeOperationWaiter{
|
2017-10-13 22:36:03 +00:00
|
|
|
Service: client,
|
2015-09-24 20:30:12 +00:00
|
|
|
Op: op,
|
2016-06-06 17:35:13 +00:00
|
|
|
Project: project,
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state := w.Conf()
|
|
|
|
state.Delay = 10 * time.Second
|
2017-07-25 23:22:27 +00:00
|
|
|
state.Timeout = time.Duration(timeoutMin) * time.Minute
|
2015-09-24 20:30:12 +00:00
|
|
|
state.MinTimeout = 2 * time.Second
|
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for %s: %s", activity, err)
|
|
|
|
}
|
2017-07-25 23:22:27 +00:00
|
|
|
|
2017-07-27 15:48:25 +00:00
|
|
|
resultOp := opRaw.(*compute.Operation)
|
|
|
|
if resultOp.Error != nil {
|
|
|
|
return ComputeOperationError(*resultOp.Error)
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
2017-07-25 23:22:27 +00:00
|
|
|
|
2015-09-24 20:30:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-09 22:02:54 +00:00
|
|
|
|
2017-10-13 22:36:03 +00:00
|
|
|
func computeBetaOperationWaitTime(client *compute.Service, op *computeBeta.Operation, project, activity string, timeoutMin int) error {
|
2017-08-09 22:02:54 +00:00
|
|
|
opV1 := &compute.Operation{}
|
|
|
|
err := Convert(op, opV1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:36:03 +00:00
|
|
|
return computeOperationWaitTime(client, opV1, project, activity, timeoutMin)
|
2017-08-09 22:02:54 +00:00
|
|
|
}
|