github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/google/operation.go (about) 1 package google 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "google.golang.org/api/compute/v1" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 ) 11 12 // OperationWaitType is an enum specifying what type of operation 13 // we're waiting on. 14 type OperationWaitType byte 15 16 const ( 17 OperationWaitInvalid OperationWaitType = iota 18 OperationWaitGlobal 19 OperationWaitRegion 20 OperationWaitZone 21 ) 22 23 type OperationWaiter struct { 24 Service *compute.Service 25 Op *compute.Operation 26 Project string 27 Region string 28 Zone string 29 Type OperationWaitType 30 } 31 32 func (w *OperationWaiter) RefreshFunc() resource.StateRefreshFunc { 33 return func() (interface{}, string, error) { 34 var op *compute.Operation 35 var err error 36 37 switch w.Type { 38 case OperationWaitGlobal: 39 op, err = w.Service.GlobalOperations.Get( 40 w.Project, w.Op.Name).Do() 41 case OperationWaitRegion: 42 op, err = w.Service.RegionOperations.Get( 43 w.Project, w.Region, w.Op.Name).Do() 44 case OperationWaitZone: 45 op, err = w.Service.ZoneOperations.Get( 46 w.Project, w.Zone, w.Op.Name).Do() 47 default: 48 return nil, "bad-type", fmt.Errorf( 49 "Invalid wait type: %#v", w.Type) 50 } 51 52 if err != nil { 53 return nil, "", err 54 } 55 56 return op, op.Status, nil 57 } 58 } 59 60 func (w *OperationWaiter) Conf() *resource.StateChangeConf { 61 return &resource.StateChangeConf{ 62 Pending: []string{"PENDING", "RUNNING"}, 63 Target: "DONE", 64 Refresh: w.RefreshFunc(), 65 } 66 } 67 68 // OperationError wraps compute.OperationError and implements the 69 // error interface so it can be returned. 70 type OperationError compute.OperationError 71 72 func (e OperationError) Error() string { 73 var buf bytes.Buffer 74 75 for _, err := range e.Errors { 76 buf.WriteString(err.Message + "\n") 77 } 78 79 return buf.String() 80 }