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

     1  package aws
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/aws/aws-sdk-go/aws/awserr"
     7  	"github.com/aws/aws-sdk-go/service/waf"
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  )
    11  
    12  type WafRetryer struct {
    13  	Connection *waf.WAF
    14  	Region     string
    15  }
    16  
    17  type withTokenFunc func(token *string) (interface{}, error)
    18  
    19  func (t *WafRetryer) RetryWithToken(f withTokenFunc) (interface{}, error) {
    20  	awsMutexKV.Lock(t.Region)
    21  	defer awsMutexKV.Unlock(t.Region)
    22  
    23  	var out interface{}
    24  	err := resource.Retry(15*time.Minute, func() *resource.RetryError {
    25  		var err error
    26  		var tokenOut *waf.GetChangeTokenOutput
    27  
    28  		tokenOut, err = t.Connection.GetChangeToken(&waf.GetChangeTokenInput{})
    29  		if err != nil {
    30  			return resource.NonRetryableError(errwrap.Wrapf("Failed to acquire change token: {{err}}", err))
    31  		}
    32  
    33  		out, err = f(tokenOut.ChangeToken)
    34  		if err != nil {
    35  			awsErr, ok := err.(awserr.Error)
    36  			if ok && awsErr.Code() == "WAFStaleDataException" {
    37  				return resource.RetryableError(err)
    38  			}
    39  			return resource.NonRetryableError(err)
    40  		}
    41  		return nil
    42  	})
    43  
    44  	return out, err
    45  }
    46  
    47  func newWafRetryer(conn *waf.WAF, region string) *WafRetryer {
    48  	return &WafRetryer{Connection: conn, Region: region}
    49  }