2015-09-24 20:30:12 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ComputeOperationWaiter) State() string {
|
|
|
|
return w.Op.Status
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ComputeOperationWaiter) Error() error {
|
|
|
|
if w.Op.Error != nil {
|
|
|
|
return ComputeOperationError(*w.Op.Error)
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return nil
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ComputeOperationWaiter) SetOp(op interface{}) error {
|
|
|
|
w.Op = op.(*compute.Operation)
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-24 20:30:12 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ComputeOperationWaiter) QueryOp() (interface{}, error) {
|
|
|
|
if w.Op.Zone != "" {
|
|
|
|
zone := GetResourceNameFromSelfLink(w.Op.Zone)
|
|
|
|
return w.Service.ZoneOperations.Get(w.Project, zone, w.Op.Name).Do()
|
|
|
|
} else if w.Op.Region != "" {
|
|
|
|
region := GetResourceNameFromSelfLink(w.Op.Region)
|
|
|
|
return w.Service.RegionOperations.Get(w.Project, region, w.Op.Name).Do()
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return w.Service.GlobalOperations.Get(w.Project, w.Op.Name).Do()
|
|
|
|
}
|
2015-09-24 20:30:12 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ComputeOperationWaiter) OpName() string {
|
|
|
|
return w.Op.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ComputeOperationWaiter) PendingStates() []string {
|
|
|
|
return []string{"PENDING", "RUNNING"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ComputeOperationWaiter) TargetStates() []string {
|
|
|
|
return []string{"DONE"}
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func computeOperationWaitTime(client *compute.Service, op *compute.Operation, project, activity string, timeoutMinutes 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
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
if err := w.SetOp(op); err != nil {
|
|
|
|
return err
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return OperationWait(w, activity, timeoutMinutes)
|
2015-09-24 20:30:12 +00:00
|
|
|
}
|
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
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|