github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_cloudwatch_metric_alarm.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/hashcode" 8 "github.com/hashicorp/terraform/helper/schema" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/service/cloudwatch" 12 ) 13 14 func resourceAwsCloudWatchMetricAlarm() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsCloudWatchMetricAlarmCreate, 17 Read: resourceAwsCloudWatchMetricAlarmRead, 18 Update: resourceAwsCloudWatchMetricAlarmUpdate, 19 Delete: resourceAwsCloudWatchMetricAlarmDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "alarm_name": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 "comparison_operator": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 }, 31 "evaluation_periods": &schema.Schema{ 32 Type: schema.TypeInt, 33 Required: true, 34 }, 35 "metric_name": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 }, 39 "namespace": &schema.Schema{ 40 Type: schema.TypeString, 41 Required: true, 42 }, 43 "period": &schema.Schema{ 44 Type: schema.TypeInt, 45 Required: true, 46 }, 47 "statistic": &schema.Schema{ 48 Type: schema.TypeString, 49 Required: true, 50 }, 51 "threshold": &schema.Schema{ 52 Type: schema.TypeFloat, 53 Required: true, 54 }, 55 "actions_enabled": &schema.Schema{ 56 Type: schema.TypeBool, 57 Optional: true, 58 Default: true, 59 }, 60 "alarm_actions": &schema.Schema{ 61 Type: schema.TypeSet, 62 Optional: true, 63 Elem: &schema.Schema{Type: schema.TypeString}, 64 Set: func(v interface{}) int { 65 return hashcode.String(v.(string)) 66 }, 67 }, 68 "alarm_description": &schema.Schema{ 69 Type: schema.TypeString, 70 Optional: true, 71 }, 72 "dimensions": &schema.Schema{ 73 Type: schema.TypeMap, 74 Optional: true, 75 }, 76 "insufficient_data_actions": &schema.Schema{ 77 Type: schema.TypeSet, 78 Optional: true, 79 Elem: &schema.Schema{Type: schema.TypeString}, 80 Set: func(v interface{}) int { 81 return hashcode.String(v.(string)) 82 }, 83 }, 84 "ok_actions": &schema.Schema{ 85 Type: schema.TypeSet, 86 Optional: true, 87 Elem: &schema.Schema{Type: schema.TypeString}, 88 Set: func(v interface{}) int { 89 return hashcode.String(v.(string)) 90 }, 91 }, 92 "unit": &schema.Schema{ 93 Type: schema.TypeString, 94 Optional: true, 95 }, 96 }, 97 } 98 } 99 100 func resourceAwsCloudWatchMetricAlarmCreate(d *schema.ResourceData, meta interface{}) error { 101 conn := meta.(*AWSClient).cloudwatchconn 102 103 params := getAwsCloudWatchPutMetricAlarmInput(d) 104 105 log.Printf("[DEBUG] Creating CloudWatch Metric Alarm: %#v", params) 106 _, err := conn.PutMetricAlarm(¶ms) 107 if err != nil { 108 return fmt.Errorf("Creating metric alarm failed: %s", err) 109 } 110 d.SetId(d.Get("alarm_name").(string)) 111 log.Println("[INFO] CloudWatch Metric Alarm created") 112 113 return resourceAwsCloudWatchMetricAlarmRead(d, meta) 114 } 115 116 func resourceAwsCloudWatchMetricAlarmRead(d *schema.ResourceData, meta interface{}) error { 117 a, err := getAwsCloudWatchMetricAlarm(d, meta) 118 if err != nil { 119 return err 120 } 121 if a == nil { 122 d.SetId("") 123 return nil 124 } 125 126 log.Printf("[DEBUG] Reading CloudWatch Metric Alarm: %s", d.Get("alarm_name")) 127 128 d.Set("actions_enabled", a.ActionsEnabled) 129 130 if err := d.Set("alarm_actions", _strArrPtrToList(a.AlarmActions)); err != nil { 131 log.Printf("[WARN] Error setting Alarm Actions: %s", err) 132 } 133 d.Set("alarm_description", a.AlarmDescription) 134 d.Set("alarm_name", a.AlarmName) 135 d.Set("comparison_operator", a.ComparisonOperator) 136 d.Set("dimensions", a.Dimensions) 137 d.Set("evaluation_periods", a.EvaluationPeriods) 138 139 if err := d.Set("insufficient_data_actions", _strArrPtrToList(a.InsufficientDataActions)); err != nil { 140 log.Printf("[WARN] Error setting Insufficient Data Actions: %s", err) 141 } 142 d.Set("metric_name", a.MetricName) 143 d.Set("namespace", a.Namespace) 144 145 if err := d.Set("ok_actions", _strArrPtrToList(a.OKActions)); err != nil { 146 log.Printf("[WARN] Error setting OK Actions: %s", err) 147 } 148 d.Set("period", a.Period) 149 d.Set("statistic", a.Statistic) 150 d.Set("threshold", a.Threshold) 151 d.Set("unit", a.Unit) 152 153 return nil 154 } 155 156 func resourceAwsCloudWatchMetricAlarmUpdate(d *schema.ResourceData, meta interface{}) error { 157 conn := meta.(*AWSClient).cloudwatchconn 158 params := getAwsCloudWatchPutMetricAlarmInput(d) 159 160 log.Printf("[DEBUG] Updating CloudWatch Metric Alarm: %#v", params) 161 _, err := conn.PutMetricAlarm(¶ms) 162 if err != nil { 163 return fmt.Errorf("Updating metric alarm failed: %s", err) 164 } 165 log.Println("[INFO] CloudWatch Metric Alarm updated") 166 167 return resourceAwsCloudWatchMetricAlarmRead(d, meta) 168 } 169 170 func resourceAwsCloudWatchMetricAlarmDelete(d *schema.ResourceData, meta interface{}) error { 171 p, err := getAwsCloudWatchMetricAlarm(d, meta) 172 if err != nil { 173 return err 174 } 175 if p == nil { 176 log.Printf("[DEBUG] CloudWatch Metric Alarm %s is already gone", d.Id()) 177 return nil 178 } 179 180 log.Printf("[INFO] Deleting CloudWatch Metric Alarm: %s", d.Id()) 181 182 conn := meta.(*AWSClient).cloudwatchconn 183 params := cloudwatch.DeleteAlarmsInput{ 184 AlarmNames: []*string{aws.String(d.Id())}, 185 } 186 187 if _, err := conn.DeleteAlarms(¶ms); err != nil { 188 return fmt.Errorf("Error deleting CloudWatch Metric Alarm: %s", err) 189 } 190 log.Println("[INFO] CloudWatch Metric Alarm deleted") 191 192 d.SetId("") 193 return nil 194 } 195 196 func getAwsCloudWatchPutMetricAlarmInput(d *schema.ResourceData) cloudwatch.PutMetricAlarmInput { 197 params := cloudwatch.PutMetricAlarmInput{ 198 AlarmName: aws.String(d.Get("alarm_name").(string)), 199 ComparisonOperator: aws.String(d.Get("comparison_operator").(string)), 200 EvaluationPeriods: aws.Int64(int64(d.Get("evaluation_periods").(int))), 201 MetricName: aws.String(d.Get("metric_name").(string)), 202 Namespace: aws.String(d.Get("namespace").(string)), 203 Period: aws.Int64(int64(d.Get("period").(int))), 204 Statistic: aws.String(d.Get("statistic").(string)), 205 Threshold: aws.Float64(d.Get("threshold").(float64)), 206 } 207 208 if v := d.Get("actions_enabled"); v != nil { 209 params.ActionsEnabled = aws.Bool(v.(bool)) 210 } 211 212 if v, ok := d.GetOk("alarm_description"); ok { 213 params.AlarmDescription = aws.String(v.(string)) 214 } 215 216 if v, ok := d.GetOk("unit"); ok { 217 params.Unit = aws.String(v.(string)) 218 } 219 220 var alarmActions []*string 221 if v := d.Get("alarm_actions"); v != nil { 222 for _, v := range v.(*schema.Set).List() { 223 str := v.(string) 224 alarmActions = append(alarmActions, aws.String(str)) 225 } 226 params.AlarmActions = alarmActions 227 } 228 229 var insufficientDataActions []*string 230 if v := d.Get("insufficient_data_actions"); v != nil { 231 for _, v := range v.(*schema.Set).List() { 232 str := v.(string) 233 insufficientDataActions = append(insufficientDataActions, aws.String(str)) 234 } 235 params.InsufficientDataActions = insufficientDataActions 236 } 237 238 var okActions []*string 239 if v := d.Get("ok_actions"); v != nil { 240 for _, v := range v.(*schema.Set).List() { 241 str := v.(string) 242 okActions = append(okActions, aws.String(str)) 243 } 244 params.OKActions = okActions 245 } 246 247 a := d.Get("dimensions").(map[string]interface{}) 248 dimensions := make([]*cloudwatch.Dimension, 0, len(a)) 249 for k, v := range a { 250 dimensions = append(dimensions, &cloudwatch.Dimension{ 251 Name: aws.String(k), 252 Value: aws.String(v.(string)), 253 }) 254 } 255 params.Dimensions = dimensions 256 257 return params 258 } 259 260 func getAwsCloudWatchMetricAlarm(d *schema.ResourceData, meta interface{}) (*cloudwatch.MetricAlarm, error) { 261 conn := meta.(*AWSClient).cloudwatchconn 262 263 params := cloudwatch.DescribeAlarmsInput{ 264 AlarmNames: []*string{aws.String(d.Id())}, 265 } 266 267 resp, err := conn.DescribeAlarms(¶ms) 268 if err != nil { 269 return nil, nil 270 } 271 272 // Find it and return it 273 for idx, ma := range resp.MetricAlarms { 274 if *ma.AlarmName == d.Id() { 275 return resp.MetricAlarms[idx], nil 276 } 277 } 278 279 return nil, nil 280 } 281 282 func _strArrPtrToList(strArrPtr []*string) []string { 283 var result []string 284 for _, elem := range strArrPtr { 285 result = append(result, *elem) 286 } 287 return result 288 }