github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resourcemanager_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/cloudresourcemanager/v1"
    10  )
    11  
    12  type ResourceManagerOperationWaiter struct {
    13  	Service *cloudresourcemanager.Service
    14  	Op      *cloudresourcemanager.Operation
    15  }
    16  
    17  func (w *ResourceManagerOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
    18  	return func() (interface{}, string, error) {
    19  		op, err := w.Service.Operations.Get(w.Op.Name).Do()
    20  
    21  		if err != nil {
    22  			return nil, "", err
    23  		}
    24  
    25  		log.Printf("[DEBUG] Got %v while polling for operation %s's 'done' status", op.Done, w.Op.Name)
    26  
    27  		return op, fmt.Sprint(op.Done), nil
    28  	}
    29  }
    30  
    31  func (w *ResourceManagerOperationWaiter) Conf() *resource.StateChangeConf {
    32  	return &resource.StateChangeConf{
    33  		Pending: []string{"false"},
    34  		Target:  []string{"true"},
    35  		Refresh: w.RefreshFunc(),
    36  	}
    37  }
    38  
    39  func resourceManagerOperationWait(config *Config, op *cloudresourcemanager.Operation, activity string) error {
    40  	return resourceManagerOperationWaitTime(config, op, activity, 4)
    41  }
    42  
    43  func resourceManagerOperationWaitTime(config *Config, op *cloudresourcemanager.Operation, activity string, timeoutMin int) error {
    44  	w := &ResourceManagerOperationWaiter{
    45  		Service: config.clientResourceManager,
    46  		Op:      op,
    47  	}
    48  
    49  	state := w.Conf()
    50  	state.Delay = 10 * time.Second
    51  	state.Timeout = time.Duration(timeoutMin) * time.Minute
    52  	state.MinTimeout = 2 * time.Second
    53  	opRaw, err := state.WaitForState()
    54  	if err != nil {
    55  		return fmt.Errorf("Error waiting for %s: %s", activity, err)
    56  	}
    57  
    58  	op = opRaw.(*cloudresourcemanager.Operation)
    59  	if op.Error != nil {
    60  		return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message)
    61  	}
    62  
    63  	return nil
    64  }