github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/helper/resource/wait.go (about)

     1  package resource
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // RetryFunc is the function retried until it succeeds.
     8  type RetryFunc func() error
     9  
    10  // Retry is a basic wrapper around StateChangeConf that will just retry
    11  // a function until it no longer returns an error.
    12  func Retry(timeout time.Duration, f RetryFunc) error {
    13  	var err error
    14  	c := &StateChangeConf{
    15  		Pending:    []string{"error"},
    16  		Target:     "success",
    17  		Timeout:    timeout,
    18  		MinTimeout: 500 * time.Millisecond,
    19  		Refresh: func() (interface{}, string, error) {
    20  			err = f()
    21  			if err == nil {
    22  				return 42, "success", nil
    23  			}
    24  
    25  			if rerr, ok := err.(RetryError); ok {
    26  				err = rerr.Err
    27  				return nil, "quit", err
    28  			}
    29  
    30  			return 42, "error", nil
    31  		},
    32  	}
    33  
    34  	c.WaitForState()
    35  	return err
    36  }
    37  
    38  // RetryError, if returned, will quit the retry immediately with the
    39  // Err.
    40  type RetryError struct {
    41  	Err error
    42  }
    43  
    44  func (e RetryError) Error() string {
    45  	return e.Err.Error()
    46  }