terraform-provider-google/google/composer_operation.go

72 lines
1.8 KiB
Go

package google
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
composer "google.golang.org/api/composer/v1beta1"
)
type ComposerOperationWaiter struct {
Service *composer.ProjectsLocationsService
Op *composer.Operation
}
func (w *ComposerOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
op, err := w.Service.Operations.Get(w.Op.Name).Do()
if err != nil {
return nil, "", err
}
log.Printf("[DEBUG] Got %v while polling for operation %s's 'done' status", op.Done, w.Op.Name)
return op, fmt.Sprint(op.Done), nil
}
}
func (w *ComposerOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"false"},
Target: []string{"true"},
Refresh: w.RefreshFunc(),
}
}
func composerOperationWait(service *composer.Service, op *composer.Operation, project, activity string) error {
return composerOperationWaitTime(service, op, project, activity, 10)
}
func composerOperationWaitTime(service *composer.Service, op *composer.Operation, project, activity string, timeoutMin int) error {
if op.Done {
if op.Error != nil {
return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message)
}
return nil
}
w := &ComposerOperationWaiter{
Service: service.Projects.Locations,
Op: op,
}
state := w.Conf()
state.Delay = 10 * time.Second
state.Timeout = time.Duration(timeoutMin) * time.Minute
state.MinTimeout = 2 * time.Second
opRaw, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
}
op = opRaw.(*composer.Operation)
if op.Error != nil {
return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message)
}
return nil
}