github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go (about)

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