From 1ddfd7ced36942448617de71e5123e789af069c3 Mon Sep 17 00:00:00 2001 From: clint shryock Date: Thu, 29 Oct 2015 16:31:56 -0500 Subject: [PATCH 1/2] provider/aws: Add a retry function to rescue an error in creating Lifecycle Hooks --- ...resource_aws_autoscaling_lifecycle_hook.go | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go b/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go index faacadb7a..7deda9eac 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go +++ b/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go @@ -3,9 +3,13 @@ package aws import ( "fmt" "log" + "strings" + "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/autoscaling" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -55,14 +59,20 @@ func resourceAwsAutoscalingLifecycleHook() *schema.Resource { } func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interface{}) error { - autoscalingconn := meta.(*AWSClient).autoscalingconn - params := getAwsAutoscalingPutLifecycleHookInput(d) log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %#v", params) - _, err := autoscalingconn.PutLifecycleHook(¶ms) + stateConf := &resource.StateChangeConf{ + Pending: []string{"retrying"}, + Target: "success", + Refresh: resourceAwsAutoscalingLifecycleHookRefreshFunc(meta, params), + Timeout: 1 * time.Minute, + MinTimeout: 3 * time.Second, + } + + _, err := stateConf.WaitForState() if err != nil { - return fmt.Errorf("Error putting lifecycle hook: %s", err) + return err } d.SetId(d.Get("name").(string)) @@ -70,6 +80,25 @@ func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interfa return resourceAwsAutoscalingLifecycleHookRead(d, meta) } +func resourceAwsAutoscalingLifecycleHookRefreshFunc( + meta interface{}, params autoscaling.PutLifecycleHookInput) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + conn := meta.(*AWSClient).autoscalingconn + _, err := conn.PutLifecycleHook(¶ms) + + if err != nil { + if awsErr, ok := err.(awserr.Error); ok { + if strings.Contains(awsErr.Message(), "Unable to publish test message to notification target") { + log.Printf("[DEBUG] Retrying AWS AutoScaling Lifecycle Hook: %s", params) + return 41, "retrying", nil + } + } + return nil, "failed", fmt.Errorf("Error putting lifecycle hook: %s", err) + } + return 42, "success", nil + } +} + func resourceAwsAutoscalingLifecycleHookRead(d *schema.ResourceData, meta interface{}) error { p, err := getAwsAutoscalingLifecycleHook(d, meta) if err != nil { From b51f425dac503d60f8a3cfa0ada4b380f254cc1e Mon Sep 17 00:00:00 2001 From: clint shryock Date: Wed, 11 Nov 2015 10:53:23 -0600 Subject: [PATCH 2/2] replace big retry func with resource.Retry --- ...resource_aws_autoscaling_lifecycle_hook.go | 43 +++++++------------ 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go b/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go index 7deda9eac..5c3458acf 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go +++ b/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go @@ -59,18 +59,24 @@ func resourceAwsAutoscalingLifecycleHook() *schema.Resource { } func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).autoscalingconn params := getAwsAutoscalingPutLifecycleHookInput(d) - log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %#v", params) - stateConf := &resource.StateChangeConf{ - Pending: []string{"retrying"}, - Target: "success", - Refresh: resourceAwsAutoscalingLifecycleHookRefreshFunc(meta, params), - Timeout: 1 * time.Minute, - MinTimeout: 3 * time.Second, - } + log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %s", params) + err := resource.Retry(5*time.Minute, func() error { + _, err := conn.PutLifecycleHook(¶ms) + + if err != nil { + if awsErr, ok := err.(awserr.Error); ok { + if strings.Contains(awsErr.Message(), "Unable to publish test message to notification target") { + return fmt.Errorf("[DEBUG] Retrying AWS AutoScaling Lifecycle Hook: %s", params) + } + } + return resource.RetryError{Err: fmt.Errorf("Error putting lifecycle hook: %s", err)} + } + return nil + }) - _, err := stateConf.WaitForState() if err != nil { return err } @@ -80,25 +86,6 @@ func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interfa return resourceAwsAutoscalingLifecycleHookRead(d, meta) } -func resourceAwsAutoscalingLifecycleHookRefreshFunc( - meta interface{}, params autoscaling.PutLifecycleHookInput) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - conn := meta.(*AWSClient).autoscalingconn - _, err := conn.PutLifecycleHook(¶ms) - - if err != nil { - if awsErr, ok := err.(awserr.Error); ok { - if strings.Contains(awsErr.Message(), "Unable to publish test message to notification target") { - log.Printf("[DEBUG] Retrying AWS AutoScaling Lifecycle Hook: %s", params) - return 41, "retrying", nil - } - } - return nil, "failed", fmt.Errorf("Error putting lifecycle hook: %s", err) - } - return 42, "success", nil - } -} - func resourceAwsAutoscalingLifecycleHookRead(d *schema.ResourceData, meta interface{}) error { p, err := getAwsAutoscalingLifecycleHook(d, meta) if err != nil {