2017-03-06 22:59:24 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"google.golang.org/api/container/v1"
|
2018-03-07 01:44:05 +00:00
|
|
|
containerBeta "google.golang.org/api/container/v1beta1"
|
2017-03-06 22:59:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContainerOperationWaiter struct {
|
|
|
|
Service *container.Service
|
|
|
|
Op *container.Operation
|
|
|
|
Project string
|
|
|
|
Zone string
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:51:35 +00:00
|
|
|
type ContainerBetaOperationWaiter struct {
|
|
|
|
Service *containerBeta.Service
|
|
|
|
Op *containerBeta.Operation
|
|
|
|
Project string
|
|
|
|
Location string
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:59:24 +00:00
|
|
|
func (w *ContainerOperationWaiter) Conf() *resource.StateChangeConf {
|
|
|
|
return &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING", "RUNNING"},
|
|
|
|
Target: []string{"DONE"},
|
|
|
|
Refresh: w.RefreshFunc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:51:35 +00:00
|
|
|
func (w *ContainerBetaOperationWaiter) Conf() *resource.StateChangeConf {
|
|
|
|
return &resource.StateChangeConf{
|
|
|
|
Pending: []string{"PENDING", "RUNNING"},
|
|
|
|
Target: []string{"DONE"},
|
|
|
|
Refresh: w.RefreshFunc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:59:24 +00:00
|
|
|
func (w *ContainerOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := w.Service.Projects.Zones.Operations.Get(
|
|
|
|
w.Project, w.Zone, w.Op.Name).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2017-08-08 18:31:12 +00:00
|
|
|
if resp.StatusMessage != "" {
|
|
|
|
return resp, resp.Status, fmt.Errorf(resp.StatusMessage)
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:59:24 +00:00
|
|
|
log.Printf("[DEBUG] Progress of operation %q: %q", w.Op.Name, resp.Status)
|
|
|
|
|
|
|
|
return resp, resp.Status, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:51:35 +00:00
|
|
|
func (w *ContainerBetaOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
name := fmt.Sprintf("projects/%s/locations/%s/operations/%s",
|
|
|
|
w.Project, w.Location, w.Op.Name)
|
|
|
|
resp, err := w.Service.Projects.Locations.Operations.Get(name).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusMessage != "" {
|
|
|
|
return resp, resp.Status, fmt.Errorf(resp.StatusMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Progress of operation %q: %q", w.Op.Name, resp.Status)
|
|
|
|
|
|
|
|
return resp, resp.Status, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:59:24 +00:00
|
|
|
func containerOperationWait(config *Config, op *container.Operation, project, zone, activity string, timeoutMinutes, minTimeoutSeconds int) error {
|
2018-06-08 23:04:55 +00:00
|
|
|
if op.Status == "DONE" {
|
|
|
|
if op.StatusMessage != "" {
|
|
|
|
return fmt.Errorf(op.StatusMessage)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:59:24 +00:00
|
|
|
w := &ContainerOperationWaiter{
|
|
|
|
Service: config.clientContainer,
|
|
|
|
Op: op,
|
|
|
|
Project: project,
|
|
|
|
Zone: zone,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := w.Conf()
|
2018-04-05 21:51:35 +00:00
|
|
|
return waitForState(state, activity, timeoutMinutes, minTimeoutSeconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerBetaOperationWait(config *Config, op *containerBeta.Operation, project, location, activity string, timeoutMinutes, minTimeoutSeconds int) error {
|
2018-06-08 23:04:55 +00:00
|
|
|
if op.Status == "DONE" {
|
|
|
|
if op.StatusMessage != "" {
|
|
|
|
return fmt.Errorf(op.StatusMessage)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:51:35 +00:00
|
|
|
w := &ContainerBetaOperationWaiter{
|
|
|
|
Service: config.clientContainerBeta,
|
|
|
|
Op: op,
|
|
|
|
Project: project,
|
|
|
|
Location: location,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := w.Conf()
|
|
|
|
return waitForState(state, activity, timeoutMinutes, minTimeoutSeconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForState(state *resource.StateChangeConf, activity string, timeoutMinutes, minTimeoutSeconds int) error {
|
2017-03-06 22:59:24 +00:00
|
|
|
state.Timeout = time.Duration(timeoutMinutes) * time.Minute
|
|
|
|
state.MinTimeout = time.Duration(minTimeoutSeconds) * time.Second
|
|
|
|
_, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for %s: %s", activity, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-07 01:44:05 +00:00
|
|
|
|
2018-04-05 21:51:35 +00:00
|
|
|
func containerSharedOperationWait(config *Config, op interface{}, project, location, activity string, timeoutMinutes, minTimeoutSeconds int) error {
|
2018-03-07 01:44:05 +00:00
|
|
|
if op == nil {
|
|
|
|
panic("Attempted to wait on an Operation that was nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op.(type) {
|
|
|
|
case *container.Operation:
|
2018-04-05 21:51:35 +00:00
|
|
|
return containerOperationWait(config, op.(*container.Operation), project, location, activity, timeoutMinutes, minTimeoutSeconds)
|
2018-03-07 01:44:05 +00:00
|
|
|
case *containerBeta.Operation:
|
2018-04-05 21:51:35 +00:00
|
|
|
return containerBetaOperationWait(config, op.(*containerBeta.Operation), project, location, activity, timeoutMinutes, minTimeoutSeconds)
|
2018-03-07 01:44:05 +00:00
|
|
|
default:
|
|
|
|
panic("Attempted to wait on an Operation of unknown type.")
|
|
|
|
}
|
|
|
|
}
|