github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/autoscaling"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsAutoscalingLifecycleHook() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsAutoscalingLifecycleHookPut,
    19  		Read:   resourceAwsAutoscalingLifecycleHookRead,
    20  		Update: resourceAwsAutoscalingLifecycleHookPut,
    21  		Delete: resourceAwsAutoscalingLifecycleHookDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  			"autoscaling_group_name": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  			},
    33  			"default_result": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Optional: true,
    36  			},
    37  			"heartbeat_timeout": &schema.Schema{
    38  				Type:     schema.TypeInt,
    39  				Optional: true,
    40  			},
    41  			"lifecycle_transition": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  			},
    45  			"notification_metadata": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Optional: true,
    48  			},
    49  			"notification_target_arn": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Required: true,
    52  			},
    53  			"role_arn": &schema.Schema{
    54  				Type:     schema.TypeString,
    55  				Required: true,
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interface{}) error {
    62  	conn := meta.(*AWSClient).autoscalingconn
    63  	params := getAwsAutoscalingPutLifecycleHookInput(d)
    64  
    65  	log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %s", params)
    66  	err := resource.Retry(5*time.Minute, func() error {
    67  		_, err := conn.PutLifecycleHook(&params)
    68  
    69  		if err != nil {
    70  			if awsErr, ok := err.(awserr.Error); ok {
    71  				if strings.Contains(awsErr.Message(), "Unable to publish test message to notification target") {
    72  					return fmt.Errorf("[DEBUG] Retrying AWS AutoScaling Lifecycle Hook: %s", params)
    73  				}
    74  			}
    75  			return resource.RetryError{Err: fmt.Errorf("Error putting lifecycle hook: %s", err)}
    76  		}
    77  		return nil
    78  	})
    79  
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	d.SetId(d.Get("name").(string))
    85  
    86  	return resourceAwsAutoscalingLifecycleHookRead(d, meta)
    87  }
    88  
    89  func resourceAwsAutoscalingLifecycleHookRead(d *schema.ResourceData, meta interface{}) error {
    90  	p, err := getAwsAutoscalingLifecycleHook(d, meta)
    91  	if err != nil {
    92  		return err
    93  	}
    94  	if p == nil {
    95  		d.SetId("")
    96  		return nil
    97  	}
    98  
    99  	log.Printf("[DEBUG] Read Lifecycle Hook: ASG: %s, SH: %s, Obj: %#v", d.Get("autoscaling_group_name"), d.Get("name"), p)
   100  
   101  	d.Set("default_result", p.DefaultResult)
   102  	d.Set("heartbeat_timeout", p.HeartbeatTimeout)
   103  	d.Set("lifecycle_transition", p.LifecycleTransition)
   104  	d.Set("notification_metadata", p.NotificationMetadata)
   105  	d.Set("notification_target_arn", p.NotificationTargetARN)
   106  	d.Set("name", p.LifecycleHookName)
   107  	d.Set("role_arn", p.RoleARN)
   108  
   109  	return nil
   110  }
   111  
   112  func resourceAwsAutoscalingLifecycleHookDelete(d *schema.ResourceData, meta interface{}) error {
   113  	autoscalingconn := meta.(*AWSClient).autoscalingconn
   114  	p, err := getAwsAutoscalingLifecycleHook(d, meta)
   115  	if err != nil {
   116  		return err
   117  	}
   118  	if p == nil {
   119  		return nil
   120  	}
   121  
   122  	params := autoscaling.DeleteLifecycleHookInput{
   123  		AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
   124  		LifecycleHookName:    aws.String(d.Get("name").(string)),
   125  	}
   126  	if _, err := autoscalingconn.DeleteLifecycleHook(&params); err != nil {
   127  		return fmt.Errorf("Autoscaling Lifecycle Hook: %s ", err)
   128  	}
   129  
   130  	d.SetId("")
   131  	return nil
   132  }
   133  
   134  func getAwsAutoscalingPutLifecycleHookInput(d *schema.ResourceData) autoscaling.PutLifecycleHookInput {
   135  	var params = autoscaling.PutLifecycleHookInput{
   136  		AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
   137  		LifecycleHookName:    aws.String(d.Get("name").(string)),
   138  	}
   139  
   140  	if v, ok := d.GetOk("default_result"); ok {
   141  		params.DefaultResult = aws.String(v.(string))
   142  	}
   143  
   144  	if v, ok := d.GetOk("heartbeat_timeout"); ok {
   145  		params.HeartbeatTimeout = aws.Int64(int64(v.(int)))
   146  	}
   147  
   148  	if v, ok := d.GetOk("lifecycle_transition"); ok {
   149  		params.LifecycleTransition = aws.String(v.(string))
   150  	}
   151  
   152  	if v, ok := d.GetOk("notification_metadata"); ok {
   153  		params.NotificationMetadata = aws.String(v.(string))
   154  	}
   155  
   156  	if v, ok := d.GetOk("notification_target_arn"); ok {
   157  		params.NotificationTargetARN = aws.String(v.(string))
   158  	}
   159  
   160  	if v, ok := d.GetOk("role_arn"); ok {
   161  		params.RoleARN = aws.String(v.(string))
   162  	}
   163  
   164  	return params
   165  }
   166  
   167  func getAwsAutoscalingLifecycleHook(d *schema.ResourceData, meta interface{}) (*autoscaling.LifecycleHook, error) {
   168  	autoscalingconn := meta.(*AWSClient).autoscalingconn
   169  
   170  	params := autoscaling.DescribeLifecycleHooksInput{
   171  		AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
   172  		LifecycleHookNames:   []*string{aws.String(d.Get("name").(string))},
   173  	}
   174  
   175  	log.Printf("[DEBUG] AutoScaling Lifecycle Hook Describe Params: %#v", params)
   176  	resp, err := autoscalingconn.DescribeLifecycleHooks(&params)
   177  	if err != nil {
   178  		return nil, fmt.Errorf("Error retrieving lifecycle hooks: %s", err)
   179  	}
   180  
   181  	// find lifecycle hooks
   182  	name := d.Get("name")
   183  	for idx, sp := range resp.LifecycleHooks {
   184  		if *sp.LifecycleHookName == name {
   185  			return resp.LifecycleHooks[idx], nil
   186  		}
   187  	}
   188  
   189  	// lifecycle hook not found
   190  	return nil, nil
   191  }