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