github.com/simonswine/terraform@v0.9.0-beta2/builtin/providers/datadog/resource_datadog_monitor.go (about) 1 package datadog 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "strconv" 8 "strings" 9 10 "github.com/hashicorp/terraform/helper/schema" 11 "gopkg.in/zorkian/go-datadog-api.v2" 12 ) 13 14 func resourceDatadogMonitor() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceDatadogMonitorCreate, 17 Read: resourceDatadogMonitorRead, 18 Update: resourceDatadogMonitorUpdate, 19 Delete: resourceDatadogMonitorDelete, 20 Exists: resourceDatadogMonitorExists, 21 Importer: &schema.ResourceImporter{ 22 State: resourceDatadogMonitorImport, 23 }, 24 25 Schema: map[string]*schema.Schema{ 26 "name": { 27 Type: schema.TypeString, 28 Required: true, 29 }, 30 "message": { 31 Type: schema.TypeString, 32 Required: true, 33 StateFunc: func(val interface{}) string { 34 return strings.TrimSpace(val.(string)) 35 }, 36 }, 37 "escalation_message": { 38 Type: schema.TypeString, 39 Optional: true, 40 StateFunc: func(val interface{}) string { 41 return strings.TrimSpace(val.(string)) 42 }, 43 }, 44 "query": { 45 Type: schema.TypeString, 46 Required: true, 47 StateFunc: func(val interface{}) string { 48 return strings.TrimSpace(val.(string)) 49 }, 50 }, 51 "type": { 52 Type: schema.TypeString, 53 Required: true, 54 }, 55 56 // Options 57 "thresholds": { 58 Type: schema.TypeMap, 59 Optional: true, 60 Elem: &schema.Resource{ 61 Schema: map[string]*schema.Schema{ 62 "ok": { 63 Type: schema.TypeFloat, 64 Optional: true, 65 }, 66 "warning": { 67 Type: schema.TypeFloat, 68 Optional: true, 69 }, 70 "critical": { 71 Type: schema.TypeFloat, 72 Optional: true, 73 }, 74 }, 75 }, 76 DiffSuppressFunc: suppressDataDogFloatIntDiff, 77 }, 78 "notify_no_data": { 79 Type: schema.TypeBool, 80 Optional: true, 81 Default: false, 82 }, 83 "new_host_delay": { 84 Type: schema.TypeInt, 85 Computed: true, 86 Optional: true, 87 }, 88 "no_data_timeframe": { 89 Type: schema.TypeInt, 90 Optional: true, 91 }, 92 "renotify_interval": { 93 Type: schema.TypeInt, 94 Optional: true, 95 }, 96 "notify_audit": { 97 Type: schema.TypeBool, 98 Optional: true, 99 }, 100 "timeout_h": { 101 Type: schema.TypeInt, 102 Optional: true, 103 }, 104 "require_full_window": { 105 Type: schema.TypeBool, 106 Optional: true, 107 Default: true, 108 }, 109 "locked": { 110 Type: schema.TypeBool, 111 Optional: true, 112 }, 113 // TODO should actually be map[string]int 114 "silenced": { 115 Type: schema.TypeMap, 116 Optional: true, 117 Elem: &schema.Schema{ 118 Type: schema.TypeString, 119 Elem: &schema.Schema{ 120 Type: schema.TypeInt}, 121 }, 122 }, 123 "include_tags": { 124 Type: schema.TypeBool, 125 Optional: true, 126 Default: true, 127 }, 128 "tags": { 129 Type: schema.TypeList, 130 Optional: true, 131 Elem: &schema.Schema{Type: schema.TypeString}, 132 }, 133 }, 134 } 135 } 136 137 func buildMonitorStruct(d *schema.ResourceData) *datadog.Monitor { 138 139 var thresholds datadog.ThresholdCount 140 141 if r, ok := d.GetOk("thresholds.ok"); ok { 142 thresholds.SetOk(json.Number(r.(string))) 143 } 144 if r, ok := d.GetOk("thresholds.warning"); ok { 145 thresholds.SetWarning(json.Number(r.(string))) 146 } 147 if r, ok := d.GetOk("thresholds.critical"); ok { 148 thresholds.SetCritical(json.Number(r.(string))) 149 } 150 151 o := datadog.Options{ 152 Thresholds: &thresholds, 153 NotifyNoData: datadog.Bool(d.Get("notify_no_data").(bool)), 154 RequireFullWindow: datadog.Bool(d.Get("require_full_window").(bool)), 155 IncludeTags: datadog.Bool(d.Get("include_tags").(bool)), 156 } 157 if attr, ok := d.GetOk("silenced"); ok { 158 s := make(map[string]int) 159 // TODO: this is not very defensive, test if we can fail on non int input 160 for k, v := range attr.(map[string]interface{}) { 161 s[k], _ = strconv.Atoi(v.(string)) 162 } 163 o.Silenced = s 164 } 165 if attr, ok := d.GetOk("notify_no_data"); ok { 166 o.SetNotifyNoData(attr.(bool)) 167 } 168 if attr, ok := d.GetOk("new_host_delay"); ok { 169 o.SetNewHostDelay(attr.(int)) 170 } 171 if attr, ok := d.GetOk("no_data_timeframe"); ok { 172 o.NoDataTimeframe = datadog.NoDataTimeframe(attr.(int)) 173 } 174 if attr, ok := d.GetOk("renotify_interval"); ok { 175 o.SetRenotifyInterval(attr.(int)) 176 } 177 if attr, ok := d.GetOk("notify_audit"); ok { 178 o.SetNotifyAudit(attr.(bool)) 179 } 180 if attr, ok := d.GetOk("timeout_h"); ok { 181 o.SetTimeoutH(attr.(int)) 182 } 183 if attr, ok := d.GetOk("escalation_message"); ok { 184 o.SetEscalationMessage(attr.(string)) 185 } 186 if attr, ok := d.GetOk("locked"); ok { 187 o.SetLocked(attr.(bool)) 188 } 189 190 m := datadog.Monitor{ 191 Type: datadog.String(d.Get("type").(string)), 192 Query: datadog.String(d.Get("query").(string)), 193 Name: datadog.String(d.Get("name").(string)), 194 Message: datadog.String(d.Get("message").(string)), 195 Options: &o, 196 } 197 198 if attr, ok := d.GetOk("tags"); ok { 199 tags := []string{} 200 for _, s := range attr.([]interface{}) { 201 tags = append(tags, s.(string)) 202 } 203 m.Tags = tags 204 } 205 206 return &m 207 } 208 209 func resourceDatadogMonitorExists(d *schema.ResourceData, meta interface{}) (b bool, e error) { 210 // Exists - This is called to verify a resource still exists. It is called prior to Read, 211 // and lowers the burden of Read to be able to assume the resource exists. 212 client := meta.(*datadog.Client) 213 214 i, err := strconv.Atoi(d.Id()) 215 if err != nil { 216 return false, err 217 } 218 219 if _, err = client.GetMonitor(i); err != nil { 220 if strings.Contains(err.Error(), "404 Not Found") { 221 return false, nil 222 } 223 return false, err 224 } 225 226 return true, nil 227 } 228 229 func resourceDatadogMonitorCreate(d *schema.ResourceData, meta interface{}) error { 230 231 client := meta.(*datadog.Client) 232 233 m := buildMonitorStruct(d) 234 m, err := client.CreateMonitor(m) 235 if err != nil { 236 return fmt.Errorf("error updating monitor: %s", err.Error()) 237 } 238 239 d.SetId(strconv.Itoa(m.GetId())) 240 241 return nil 242 } 243 244 func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error { 245 client := meta.(*datadog.Client) 246 247 i, err := strconv.Atoi(d.Id()) 248 if err != nil { 249 return err 250 } 251 252 m, err := client.GetMonitor(i) 253 if err != nil { 254 return err 255 } 256 257 thresholds := make(map[string]string) 258 for k, v := range map[string]json.Number{ 259 "ok": m.Options.Thresholds.GetOk(), 260 "warning": m.Options.Thresholds.GetWarning(), 261 "critical": m.Options.Thresholds.GetCritical(), 262 } { 263 s := v.String() 264 if s != "" { 265 thresholds[k] = s 266 } 267 } 268 269 tags := []string{} 270 for _, s := range m.Tags { 271 tags = append(tags, s) 272 } 273 274 log.Printf("[DEBUG] monitor: %v", m) 275 d.Set("name", m.GetName()) 276 d.Set("message", m.GetMessage()) 277 d.Set("query", m.GetQuery()) 278 d.Set("type", m.GetType()) 279 d.Set("thresholds", thresholds) 280 281 d.Set("new_host_delay", m.Options.GetNewHostDelay()) 282 d.Set("notify_no_data", m.Options.GetNotifyNoData()) 283 d.Set("no_data_timeframe", m.Options.NoDataTimeframe) 284 d.Set("renotify_interval", m.Options.GetRenotifyInterval()) 285 d.Set("notify_audit", m.Options.GetNotifyAudit()) 286 d.Set("timeout_h", m.Options.GetTimeoutH()) 287 d.Set("escalation_message", m.Options.GetEscalationMessage()) 288 d.Set("silenced", m.Options.Silenced) 289 d.Set("include_tags", m.Options.GetIncludeTags()) 290 d.Set("tags", tags) 291 d.Set("require_full_window", m.Options.GetRequireFullWindow()) // TODO Is this one of those options that we neeed to check? 292 d.Set("locked", m.Options.GetLocked()) 293 294 return nil 295 } 296 297 func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) error { 298 client := meta.(*datadog.Client) 299 300 m := &datadog.Monitor{} 301 302 i, err := strconv.Atoi(d.Id()) 303 if err != nil { 304 return err 305 } 306 307 m.Id = datadog.Int(i) 308 if attr, ok := d.GetOk("name"); ok { 309 m.SetName(attr.(string)) 310 } 311 if attr, ok := d.GetOk("message"); ok { 312 m.SetMessage(attr.(string)) 313 } 314 if attr, ok := d.GetOk("query"); ok { 315 m.SetQuery(attr.(string)) 316 } 317 318 if attr, ok := d.GetOk("tags"); ok { 319 s := make([]string, 0) 320 for _, v := range attr.([]interface{}) { 321 s = append(s, v.(string)) 322 } 323 m.Tags = s 324 } 325 326 o := datadog.Options{ 327 NotifyNoData: datadog.Bool(d.Get("notify_no_data").(bool)), 328 } 329 if attr, ok := d.GetOk("thresholds"); ok { 330 thresholds := attr.(map[string]interface{}) 331 o.Thresholds = &datadog.ThresholdCount{} // TODO: This is a little annoying.. 332 if thresholds["ok"] != nil { 333 o.Thresholds.SetOk(json.Number(thresholds["ok"].(string))) 334 } 335 if thresholds["warning"] != nil { 336 o.Thresholds.SetWarning(json.Number(thresholds["warning"].(string))) 337 } 338 if thresholds["critical"] != nil { 339 o.Thresholds.SetCritical(json.Number(thresholds["critical"].(string))) 340 } 341 } 342 343 if attr, ok := d.GetOk("notify_no_data"); ok { 344 o.SetNotifyNoData(attr.(bool)) 345 } 346 if attr, ok := d.GetOk("new_host_delay"); ok { 347 o.SetNewHostDelay(attr.(int)) 348 } 349 if attr, ok := d.GetOk("no_data_timeframe"); ok { 350 o.NoDataTimeframe = datadog.NoDataTimeframe(attr.(int)) 351 } 352 if attr, ok := d.GetOk("renotify_interval"); ok { 353 o.SetRenotifyInterval(attr.(int)) 354 } 355 if attr, ok := d.GetOk("notify_audit"); ok { 356 o.SetNotifyAudit(attr.(bool)) 357 } 358 if attr, ok := d.GetOk("timeout_h"); ok { 359 o.SetTimeoutH(attr.(int)) 360 } 361 if attr, ok := d.GetOk("escalation_message"); ok { 362 o.SetEscalationMessage(attr.(string)) 363 } 364 if attr, ok := d.GetOk("silenced"); ok { 365 // TODO: this is not very defensive, test if we can fail non int input 366 s := make(map[string]int) 367 for k, v := range attr.(map[string]interface{}) { 368 s[k], _ = strconv.Atoi(v.(string)) 369 } 370 o.Silenced = s 371 } 372 if attr, ok := d.GetOk("include_tags"); ok { 373 o.SetIncludeTags(attr.(bool)) 374 } 375 if attr, ok := d.GetOk("require_full_window"); ok { 376 o.SetRequireFullWindow(attr.(bool)) 377 } 378 if attr, ok := d.GetOk("locked"); ok { 379 o.SetLocked(attr.(bool)) 380 } 381 382 m.Options = &o 383 384 if err = client.UpdateMonitor(m); err != nil { 385 return fmt.Errorf("error updating monitor: %s", err.Error()) 386 } 387 388 return resourceDatadogMonitorRead(d, meta) 389 } 390 391 func resourceDatadogMonitorDelete(d *schema.ResourceData, meta interface{}) error { 392 client := meta.(*datadog.Client) 393 394 i, err := strconv.Atoi(d.Id()) 395 if err != nil { 396 return err 397 } 398 399 if err = client.DeleteMonitor(i); err != nil { 400 return err 401 } 402 403 return nil 404 } 405 406 func resourceDatadogMonitorImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 407 if err := resourceDatadogMonitorRead(d, meta); err != nil { 408 return nil, err 409 } 410 return []*schema.ResourceData{d}, nil 411 } 412 413 // Ignore any diff that results from the mix of ints or floats returned from the 414 // DataDog API. 415 func suppressDataDogFloatIntDiff(k, old, new string, d *schema.ResourceData) bool { 416 oF, err := strconv.ParseFloat(old, 64) 417 if err != nil { 418 log.Printf("Error parsing float of old value (%s): %s", old, err) 419 return false 420 } 421 422 nF, err := strconv.ParseFloat(new, 64) 423 if err != nil { 424 log.Printf("Error parsing float of new value (%s): %s", new, err) 425 return false 426 } 427 428 // if the float values of these attributes are equivalent, ignore this 429 // diff 430 if oF == nF { 431 return true 432 } 433 return false 434 }