github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/datadog/resource_datadog_downtime.go (about) 1 package datadog 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 "gopkg.in/zorkian/go-datadog-api.v2" 11 ) 12 13 func resourceDatadogDowntime() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceDatadogDowntimeCreate, 16 Read: resourceDatadogDowntimeRead, 17 Update: resourceDatadogDowntimeUpdate, 18 Delete: resourceDatadogDowntimeDelete, 19 Exists: resourceDatadogDowntimeExists, 20 Importer: &schema.ResourceImporter{ 21 State: resourceDatadogDowntimeImport, 22 }, 23 24 Schema: map[string]*schema.Schema{ 25 "active": { 26 Type: schema.TypeBool, 27 Optional: true, 28 }, 29 "disabled": { 30 Type: schema.TypeBool, 31 Optional: true, 32 }, 33 "end": { 34 Type: schema.TypeInt, 35 Optional: true, 36 }, 37 "message": { 38 Type: schema.TypeString, 39 Optional: true, 40 StateFunc: func(val interface{}) string { 41 return strings.TrimSpace(val.(string)) 42 }, 43 }, 44 "recurrence": { 45 Type: schema.TypeList, 46 Optional: true, 47 MaxItems: 1, 48 Elem: &schema.Resource{ 49 Schema: map[string]*schema.Schema{ 50 "period": { 51 Type: schema.TypeInt, 52 Required: true, 53 }, 54 "type": { 55 Type: schema.TypeString, 56 Required: true, 57 ValidateFunc: validateDatadogDowntimeRecurrenceType, 58 }, 59 "until_date": { 60 Type: schema.TypeInt, 61 Optional: true, 62 ConflictsWith: []string{"recurrence.until_occurrences"}, 63 }, 64 "until_occurrences": { 65 Type: schema.TypeInt, 66 Optional: true, 67 ConflictsWith: []string{"recurrence.until_date"}, 68 }, 69 "week_days": { 70 Type: schema.TypeList, 71 Optional: true, 72 Elem: &schema.Schema{ 73 Type: schema.TypeString, 74 ValidateFunc: validateDatadogDowntimeRecurrenceWeekDays, 75 }, 76 }, 77 }, 78 }, 79 }, 80 "scope": { 81 Type: schema.TypeList, 82 Required: true, 83 Elem: &schema.Schema{Type: schema.TypeString}, 84 }, 85 "start": { 86 Type: schema.TypeInt, 87 Optional: true, 88 }, 89 }, 90 } 91 } 92 93 func buildDowntimeStruct(d *schema.ResourceData) *datadog.Downtime { 94 var dt datadog.Downtime 95 96 if attr, ok := d.GetOk("active"); ok { 97 dt.SetActive(attr.(bool)) 98 } 99 if attr, ok := d.GetOk("disabled"); ok { 100 dt.SetDisabled(attr.(bool)) 101 } 102 if attr, ok := d.GetOk("end"); ok { 103 dt.SetEnd(attr.(int)) 104 } 105 if attr, ok := d.GetOk("message"); ok { 106 dt.SetMessage(strings.TrimSpace(attr.(string))) 107 } 108 if _, ok := d.GetOk("recurrence"); ok { 109 var recurrence datadog.Recurrence 110 111 if attr, ok := d.GetOk("recurrence.0.period"); ok { 112 recurrence.SetPeriod(attr.(int)) 113 } 114 if attr, ok := d.GetOk("recurrence.0.type"); ok { 115 recurrence.SetType(attr.(string)) 116 } 117 if attr, ok := d.GetOk("recurrence.0.until_date"); ok { 118 recurrence.SetUntilDate(attr.(int)) 119 } 120 if attr, ok := d.GetOk("recurrence.0.until_occurrences"); ok { 121 recurrence.SetUntilOccurrences(attr.(int)) 122 } 123 if attr, ok := d.GetOk("recurrence.0.week_days"); ok { 124 weekDays := make([]string, 0, len(attr.([]interface{}))) 125 for _, weekDay := range attr.([]interface{}) { 126 weekDays = append(weekDays, weekDay.(string)) 127 } 128 recurrence.WeekDays = weekDays 129 } 130 131 dt.SetRecurrence(recurrence) 132 } 133 scope := []string{} 134 for _, s := range d.Get("scope").([]interface{}) { 135 scope = append(scope, s.(string)) 136 } 137 dt.Scope = scope 138 if attr, ok := d.GetOk("start"); ok { 139 dt.SetStart(attr.(int)) 140 } 141 142 return &dt 143 } 144 145 func resourceDatadogDowntimeExists(d *schema.ResourceData, meta interface{}) (b bool, e error) { 146 // Exists - This is called to verify a resource still exists. It is called prior to Read, 147 // and lowers the burden of Read to be able to assume the resource exists. 148 client := meta.(*datadog.Client) 149 150 id, err := strconv.Atoi(d.Id()) 151 if err != nil { 152 return false, err 153 } 154 155 if _, err = client.GetDowntime(id); err != nil { 156 if strings.Contains(err.Error(), "404 Not Found") { 157 return false, nil 158 } 159 return false, err 160 } 161 162 return true, nil 163 } 164 165 func resourceDatadogDowntimeCreate(d *schema.ResourceData, meta interface{}) error { 166 client := meta.(*datadog.Client) 167 168 dts := buildDowntimeStruct(d) 169 dt, err := client.CreateDowntime(dts) 170 if err != nil { 171 return fmt.Errorf("error updating downtime: %s", err.Error()) 172 } 173 174 d.SetId(strconv.Itoa(dt.GetId())) 175 176 return nil 177 } 178 179 func resourceDatadogDowntimeRead(d *schema.ResourceData, meta interface{}) error { 180 client := meta.(*datadog.Client) 181 182 id, err := strconv.Atoi(d.Id()) 183 if err != nil { 184 return err 185 } 186 187 dt, err := client.GetDowntime(id) 188 if err != nil { 189 return err 190 } 191 192 log.Printf("[DEBUG] downtime: %v", dt) 193 d.Set("active", dt.GetActive()) 194 d.Set("disabled", dt.GetDisabled()) 195 d.Set("end", dt.GetEnd()) 196 d.Set("message", dt.GetMessage()) 197 if r, ok := dt.GetRecurrenceOk(); ok { 198 recurrence := make(map[string]interface{}) 199 recurrenceList := make([]map[string]interface{}, 0, 1) 200 201 if attr, ok := r.GetPeriodOk(); ok { 202 recurrence["period"] = strconv.Itoa(attr) 203 } 204 if attr, ok := r.GetTypeOk(); ok { 205 recurrence["type"] = attr 206 } 207 if attr, ok := r.GetUntilDateOk(); ok { 208 recurrence["until_date"] = strconv.Itoa(attr) 209 } 210 if attr, ok := r.GetUntilOccurrencesOk(); ok { 211 recurrence["until_occurrences"] = strconv.Itoa(attr) 212 } 213 if r.WeekDays != nil { 214 weekDays := make([]string, 0, len(r.WeekDays)) 215 for _, weekDay := range r.WeekDays { 216 weekDays = append(weekDays, weekDay) 217 } 218 recurrence["week_days"] = weekDays 219 } 220 recurrenceList = append(recurrenceList, recurrence) 221 d.Set("recurrence", recurrenceList) 222 } 223 d.Set("scope", dt.Scope) 224 d.Set("start", dt.GetStart()) 225 226 return nil 227 } 228 229 func resourceDatadogDowntimeUpdate(d *schema.ResourceData, meta interface{}) error { 230 client := meta.(*datadog.Client) 231 232 var dt datadog.Downtime 233 234 id, err := strconv.Atoi(d.Id()) 235 if err != nil { 236 return err 237 } 238 239 dt.SetId(id) 240 if attr, ok := d.GetOk("active"); ok { 241 dt.SetActive(attr.(bool)) 242 } 243 if attr, ok := d.GetOk("disabled"); ok { 244 dt.SetDisabled(attr.(bool)) 245 } 246 if attr, ok := d.GetOk("end"); ok { 247 dt.SetEnd(attr.(int)) 248 } 249 if attr, ok := d.GetOk("message"); ok { 250 dt.SetMessage(attr.(string)) 251 } 252 253 if _, ok := d.GetOk("recurrence"); ok { 254 var recurrence datadog.Recurrence 255 256 if attr, ok := d.GetOk("recurrence.0.period"); ok { 257 recurrence.SetPeriod(attr.(int)) 258 } 259 if attr, ok := d.GetOk("recurrence.0.type"); ok { 260 recurrence.SetType(attr.(string)) 261 } 262 if attr, ok := d.GetOk("recurrence.0.until_date"); ok { 263 recurrence.SetUntilDate(attr.(int)) 264 } 265 if attr, ok := d.GetOk("recurrence.0.until_occurrences"); ok { 266 recurrence.SetUntilOccurrences(attr.(int)) 267 } 268 if attr, ok := d.GetOk("recurrence.0.week_days"); ok { 269 weekDays := make([]string, 0, len(attr.([]interface{}))) 270 for _, weekDay := range attr.([]interface{}) { 271 weekDays = append(weekDays, weekDay.(string)) 272 } 273 recurrence.WeekDays = weekDays 274 } 275 276 dt.SetRecurrence(recurrence) 277 } 278 279 scope := make([]string, 0) 280 for _, v := range d.Get("scope").([]interface{}) { 281 scope = append(scope, v.(string)) 282 } 283 dt.Scope = scope 284 if attr, ok := d.GetOk("start"); ok { 285 dt.SetStart(attr.(int)) 286 } 287 288 if err = client.UpdateDowntime(&dt); err != nil { 289 return fmt.Errorf("error updating downtime: %s", err.Error()) 290 } 291 292 return resourceDatadogDowntimeRead(d, meta) 293 } 294 295 func resourceDatadogDowntimeDelete(d *schema.ResourceData, meta interface{}) error { 296 client := meta.(*datadog.Client) 297 298 id, err := strconv.Atoi(d.Id()) 299 if err != nil { 300 return err 301 } 302 303 if err = client.DeleteDowntime(id); err != nil { 304 return err 305 } 306 307 return nil 308 } 309 310 func resourceDatadogDowntimeImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 311 if err := resourceDatadogDowntimeRead(d, meta); err != nil { 312 return nil, err 313 } 314 return []*schema.ResourceData{d}, nil 315 } 316 317 func validateDatadogDowntimeRecurrenceType(v interface{}, k string) (ws []string, errors []error) { 318 value := v.(string) 319 switch value { 320 case "days", "months", "weeks", "years": 321 break 322 default: 323 errors = append(errors, fmt.Errorf( 324 "%q contains an invalid recurrence type parameter %q. Valid parameters are days, months, weeks, or years", k, value)) 325 } 326 return 327 } 328 329 func validateDatadogDowntimeRecurrenceWeekDays(v interface{}, k string) (ws []string, errors []error) { 330 value := v.(string) 331 switch value { 332 case "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun": 333 break 334 default: 335 errors = append(errors, fmt.Errorf( 336 "%q contains an invalid recurrence week day parameter %q. Valid parameters are Mon, Tue, Wed, Thu, Fri, Sat, or Sun", k, value)) 337 } 338 return 339 }