2014-08-25 21:55:08 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2014-08-26 07:39:29 +02:00
|
|
|
"bytes"
|
2014-08-25 21:55:08 +02:00
|
|
|
"fmt"
|
|
|
|
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
2014-08-25 21:55:08 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OperationWaitType is an enum specifying what type of operation
|
|
|
|
// we're waiting on.
|
|
|
|
type OperationWaitType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
OperationWaitInvalid OperationWaitType = iota
|
|
|
|
OperationWaitGlobal
|
|
|
|
OperationWaitRegion
|
|
|
|
OperationWaitZone
|
|
|
|
)
|
|
|
|
|
|
|
|
type OperationWaiter struct {
|
|
|
|
Service *compute.Service
|
|
|
|
Op *compute.Operation
|
|
|
|
Project string
|
|
|
|
Region string
|
|
|
|
Type OperationWaitType
|
2015-07-28 02:47:10 +02:00
|
|
|
Zone string
|
2014-08-25 21:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *OperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
var op *compute.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"},
|
|
|
|
Target: "DONE",
|
|
|
|
Refresh: w.RefreshFunc(),
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 07:39:29 +02:00
|
|
|
|
|
|
|
// OperationError wraps compute.OperationError and implements the
|
|
|
|
// error interface so it can be returned.
|
|
|
|
type OperationError compute.OperationError
|
|
|
|
|
|
|
|
func (e OperationError) Error() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
for _, err := range e.Errors {
|
|
|
|
buf.WriteString(err.Message + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
2015-02-16 17:06:23 +01:00
|
|
|
|