github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/container_operation.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"google.golang.org/api/container/v1"
    10  )
    11  
    12  type ContainerOperationWaiter struct {
    13  	Service *container.Service
    14  	Op      *container.Operation
    15  	Project string
    16  	Zone    string
    17  }
    18  
    19  func (w *ContainerOperationWaiter) Conf() *resource.StateChangeConf {
    20  	return &resource.StateChangeConf{
    21  		Pending: []string{"PENDING", "RUNNING"},
    22  		Target:  []string{"DONE"},
    23  		Refresh: w.RefreshFunc(),
    24  	}
    25  }
    26  
    27  func (w *ContainerOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
    28  	return func() (interface{}, string, error) {
    29  		resp, err := w.Service.Projects.Zones.Operations.Get(
    30  			w.Project, w.Zone, w.Op.Name).Do()
    31  
    32  		if err != nil {
    33  			return nil, "", err
    34  		}
    35  
    36  		log.Printf("[DEBUG] Progress of operation %q: %q", w.Op.Name, resp.Status)
    37  
    38  		return resp, resp.Status, err
    39  	}
    40  }
    41  
    42  func containerOperationWait(config *Config, op *container.Operation, project, zone, activity string, timeoutMinutes, minTimeoutSeconds int) error {
    43  	w := &ContainerOperationWaiter{
    44  		Service: config.clientContainer,
    45  		Op:      op,
    46  		Project: project,
    47  		Zone:    zone,
    48  	}
    49  
    50  	state := w.Conf()
    51  	state.Timeout = time.Duration(timeoutMinutes) * time.Minute
    52  	state.MinTimeout = time.Duration(minTimeoutSeconds) * time.Second
    53  	_, err := state.WaitForState()
    54  	if err != nil {
    55  		return fmt.Errorf("Error waiting for %s: %s", activity, err)
    56  	}
    57  
    58  	return nil
    59  }