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