2017-03-06 22:59:24 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-01-25 21:03:43 +00:00
|
|
|
container "google.golang.org/api/container/v1beta1"
|
2017-03-06 22:59:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContainerOperationWaiter struct {
|
2018-12-27 01:42:37 +00:00
|
|
|
Service *container.Service
|
|
|
|
Op *container.Operation
|
2018-04-05 21:51:35 +00:00
|
|
|
Project string
|
|
|
|
Location string
|
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ContainerOperationWaiter) State() string {
|
2019-01-25 21:03:43 +00:00
|
|
|
if w == nil || w.Op == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return w.Op.Status
|
2017-03-06 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ContainerOperationWaiter) Error() error {
|
2019-01-26 00:28:54 +00:00
|
|
|
if w == nil || w.Op == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error gets called during operation polling to see if there is an error.
|
|
|
|
// Since container's operation doesn't have an "error" field, we must wait
|
|
|
|
// until it's done and check the status message
|
|
|
|
for _, pending := range w.PendingStates() {
|
|
|
|
if w.Op.Status == pending {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Op.StatusMessage != "" {
|
2018-12-27 01:42:37 +00:00
|
|
|
return fmt.Errorf(w.Op.StatusMessage)
|
2018-04-05 21:51:35 +00:00
|
|
|
}
|
2019-01-26 00:28:54 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
return nil
|
2018-04-05 21:51:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ContainerOperationWaiter) SetOp(op interface{}) error {
|
2019-01-25 21:03:43 +00:00
|
|
|
var ok bool
|
|
|
|
w.Op, ok = op.(*container.Operation)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Unable to set operation. Bad type!")
|
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return nil
|
2017-03-06 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ContainerOperationWaiter) QueryOp() (interface{}, error) {
|
2019-01-25 21:03:43 +00:00
|
|
|
if w == nil || w.Op == nil {
|
|
|
|
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
|
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
name := fmt.Sprintf("projects/%s/locations/%s/operations/%s",
|
|
|
|
w.Project, w.Location, w.Op.Name)
|
|
|
|
return w.Service.Projects.Locations.Operations.Get(name).Do()
|
2018-04-05 21:51:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ContainerOperationWaiter) OpName() string {
|
2019-01-25 21:03:43 +00:00
|
|
|
if w == nil || w.Op == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2018-12-27 01:42:37 +00:00
|
|
|
return w.Op.Name
|
|
|
|
}
|
2017-03-06 22:59:24 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ContainerOperationWaiter) PendingStates() []string {
|
|
|
|
return []string{"PENDING", "RUNNING"}
|
2018-04-05 21:51:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func (w *ContainerOperationWaiter) TargetStates() []string {
|
|
|
|
return []string{"DONE"}
|
|
|
|
}
|
2018-06-08 23:04:55 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
func containerOperationWait(config *Config, op *container.Operation, project, location, activity string, timeoutMinutes int) error {
|
|
|
|
w := &ContainerOperationWaiter{
|
2018-04-05 21:51:35 +00:00
|
|
|
Service: config.clientContainerBeta,
|
|
|
|
Op: op,
|
|
|
|
Project: project,
|
|
|
|
Location: location,
|
|
|
|
}
|
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
if err := w.SetOp(op); err != nil {
|
|
|
|
return err
|
2018-03-07 01:44:05 +00:00
|
|
|
}
|
2019-01-26 00:28:54 +00:00
|
|
|
|
2018-12-27 01:42:37 +00:00
|
|
|
return OperationWait(w, activity, timeoutMinutes)
|
2018-03-07 01:44:05 +00:00
|
|
|
}
|