2015-09-29 08:09:45 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-10-29 22:31:56 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-09-29 08:09:45 +02:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-10-29 22:31:56 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-09-29 08:09:45 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
2015-10-29 22:31:56 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-09-29 08:09:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingLifecycleHook() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsAutoscalingLifecycleHookPut,
|
|
|
|
Read: resourceAwsAutoscalingLifecycleHookRead,
|
|
|
|
Update: resourceAwsAutoscalingLifecycleHookPut,
|
|
|
|
Delete: resourceAwsAutoscalingLifecycleHookDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"autoscaling_group_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"default_result": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-01-15 23:34:16 +01:00
|
|
|
Computed: true,
|
2015-09-29 08:09:45 +02:00
|
|
|
},
|
|
|
|
"heartbeat_timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"lifecycle_transition": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"notification_metadata": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"notification_target_arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"role_arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interface{}) error {
|
2015-11-11 17:53:23 +01:00
|
|
|
conn := meta.(*AWSClient).autoscalingconn
|
2015-09-29 08:09:45 +02:00
|
|
|
params := getAwsAutoscalingPutLifecycleHookInput(d)
|
|
|
|
|
2015-11-11 17:53:23 +01:00
|
|
|
log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %s", params)
|
|
|
|
err := resource.Retry(5*time.Minute, func() error {
|
2015-10-29 22:31:56 +01:00
|
|
|
_, 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") {
|
2015-11-11 17:53:23 +01:00
|
|
|
return fmt.Errorf("[DEBUG] Retrying AWS AutoScaling Lifecycle Hook: %s", params)
|
2015-10-29 22:31:56 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-11 17:53:23 +01:00
|
|
|
return resource.RetryError{Err: fmt.Errorf("Error putting lifecycle hook: %s", err)}
|
2015-10-29 22:31:56 +01:00
|
|
|
}
|
2015-11-11 17:53:23 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-29 22:31:56 +01:00
|
|
|
}
|
2015-11-11 17:53:23 +01:00
|
|
|
|
|
|
|
d.SetId(d.Get("name").(string))
|
|
|
|
|
|
|
|
return resourceAwsAutoscalingLifecycleHookRead(d, meta)
|
2015-10-29 22:31:56 +01:00
|
|
|
}
|
|
|
|
|
2015-09-29 08:09:45 +02:00
|
|
|
func resourceAwsAutoscalingLifecycleHookRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
p, err := getAwsAutoscalingLifecycleHook(d, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Read Lifecycle Hook: ASG: %s, SH: %s, Obj: %#v", d.Get("autoscaling_group_name"), d.Get("name"), p)
|
|
|
|
|
|
|
|
d.Set("default_result", p.DefaultResult)
|
|
|
|
d.Set("heartbeat_timeout", p.HeartbeatTimeout)
|
|
|
|
d.Set("lifecycle_transition", p.LifecycleTransition)
|
|
|
|
d.Set("notification_metadata", p.NotificationMetadata)
|
|
|
|
d.Set("notification_target_arn", p.NotificationTargetARN)
|
|
|
|
d.Set("name", p.LifecycleHookName)
|
|
|
|
d.Set("role_arn", p.RoleARN)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingLifecycleHookDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
|
|
p, err := getAwsAutoscalingLifecycleHook(d, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := autoscaling.DeleteLifecycleHookInput{
|
|
|
|
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
|
|
|
LifecycleHookName: aws.String(d.Get("name").(string)),
|
|
|
|
}
|
|
|
|
if _, err := autoscalingconn.DeleteLifecycleHook(¶ms); err != nil {
|
|
|
|
return fmt.Errorf("Autoscaling Lifecycle Hook: %s ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAwsAutoscalingPutLifecycleHookInput(d *schema.ResourceData) autoscaling.PutLifecycleHookInput {
|
|
|
|
var params = autoscaling.PutLifecycleHookInput{
|
|
|
|
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
|
|
|
LifecycleHookName: aws.String(d.Get("name").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("default_result"); ok {
|
|
|
|
params.DefaultResult = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("heartbeat_timeout"); ok {
|
|
|
|
params.HeartbeatTimeout = aws.Int64(int64(v.(int)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("lifecycle_transition"); ok {
|
|
|
|
params.LifecycleTransition = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("notification_metadata"); ok {
|
|
|
|
params.NotificationMetadata = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("notification_target_arn"); ok {
|
|
|
|
params.NotificationTargetARN = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("role_arn"); ok {
|
|
|
|
params.RoleARN = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAwsAutoscalingLifecycleHook(d *schema.ResourceData, meta interface{}) (*autoscaling.LifecycleHook, error) {
|
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
|
|
|
|
|
|
params := autoscaling.DescribeLifecycleHooksInput{
|
|
|
|
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
|
|
|
LifecycleHookNames: []*string{aws.String(d.Get("name").(string))},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] AutoScaling Lifecycle Hook Describe Params: %#v", params)
|
|
|
|
resp, err := autoscalingconn.DescribeLifecycleHooks(¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving lifecycle hooks: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// find lifecycle hooks
|
|
|
|
name := d.Get("name")
|
|
|
|
for idx, sp := range resp.LifecycleHooks {
|
|
|
|
if *sp.LifecycleHookName == name {
|
|
|
|
return resp.LifecycleHooks[idx], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lifecycle hook not found
|
|
|
|
return nil, nil
|
|
|
|
}
|