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