]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - statuscake/resource_statuscaketest.go
Merge pull request #34 from jcalonso/fix/contact-group-backwards-compatible
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscaketest.go
1 package statuscake
2
3 import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11 )
12
13 func castSetToSliceStrings(configured []interface{}) []string {
14 res := make([]string, len(configured))
15
16 for i, element := range configured {
17 res[i] = element.(string)
18 }
19 return res
20 }
21
22 // Special handling for node_locations since statuscake will return `[""]` for the empty case
23 func considerEmptyStringAsEmptyArray(in []string) []string {
24 if len(in) == 1 && in[0] == "" {
25 return []string{}
26 } else {
27 return in
28 }
29 }
30
31 func resourceStatusCakeTest() *schema.Resource {
32 return &schema.Resource{
33 Create: CreateTest,
34 Update: UpdateTest,
35 Delete: DeleteTest,
36 Read: ReadTest,
37
38 Schema: map[string]*schema.Schema{
39 "test_id": {
40 Type: schema.TypeString,
41 Computed: true,
42 },
43
44 "website_name": {
45 Type: schema.TypeString,
46 Required: true,
47 },
48
49 "website_url": {
50 Type: schema.TypeString,
51 Required: true,
52 },
53
54 "contact_group": {
55 Type: schema.TypeSet,
56 Elem: &schema.Schema{Type: schema.TypeString},
57 Optional: true,
58 Set: schema.HashString,
59 ConflictsWith: []string{"contact_id"},
60 },
61
62 "contact_id": {
63 Type: schema.TypeInt,
64 Optional: true,
65 ConflictsWith: []string{"contact_group"},
66 Deprecated: "use contact_group instead",
67 },
68
69 "check_rate": {
70 Type: schema.TypeInt,
71 Optional: true,
72 Default: 300,
73 },
74
75 "test_type": {
76 Type: schema.TypeString,
77 Required: true,
78 },
79
80 "paused": {
81 Type: schema.TypeBool,
82 Optional: true,
83 Default: false,
84 },
85
86 "timeout": {
87 Type: schema.TypeInt,
88 Optional: true,
89 Default: 40,
90 },
91
92 "confirmations": {
93 Type: schema.TypeInt,
94 Optional: true,
95 },
96
97 "port": {
98 Type: schema.TypeInt,
99 Optional: true,
100 },
101
102 "trigger_rate": {
103 Type: schema.TypeInt,
104 Optional: true,
105 Default: 5,
106 },
107
108 "custom_header": {
109 Type: schema.TypeString,
110 Optional: true,
111 },
112
113 "user_agent": {
114 Type: schema.TypeString,
115 Optional: true,
116 },
117
118 "status": {
119 Type: schema.TypeString,
120 Computed: true,
121 },
122
123 "uptime": {
124 Type: schema.TypeFloat,
125 Computed: true,
126 },
127
128 "node_locations": {
129 Type: schema.TypeSet,
130 Elem: &schema.Schema{Type: schema.TypeString},
131 Optional: true,
132 Set: schema.HashString,
133 },
134
135 "ping_url": {
136 Type: schema.TypeString,
137 Optional: true,
138 },
139
140 "basic_user": {
141 Type: schema.TypeString,
142 Optional: true,
143 },
144
145 "basic_pass": {
146 Type: schema.TypeString,
147 Optional: true,
148 Sensitive: true,
149 },
150
151 "public": {
152 Type: schema.TypeInt,
153 Optional: true,
154 },
155
156 "logo_image": {
157 Type: schema.TypeString,
158 Optional: true,
159 },
160
161 "branding": {
162 Type: schema.TypeInt,
163 Optional: true,
164 },
165
166 "website_host": {
167 Type: schema.TypeString,
168 Optional: true,
169 },
170
171 "virus": {
172 Type: schema.TypeInt,
173 Optional: true,
174 },
175
176 "find_string": {
177 Type: schema.TypeString,
178 Optional: true,
179 },
180
181 "do_not_find": {
182 Type: schema.TypeBool,
183 Optional: true,
184 },
185
186 "real_browser": {
187 Type: schema.TypeInt,
188 Optional: true,
189 },
190
191 "test_tags": {
192 Type: schema.TypeSet,
193 Elem: &schema.Schema{Type: schema.TypeString},
194 Optional: true,
195 Set: schema.HashString,
196 },
197
198 "status_codes": {
199 Type: schema.TypeString,
200 Optional: true,
201 },
202
203 "use_jar": {
204 Type: schema.TypeInt,
205 Optional: true,
206 },
207
208 "post_raw": {
209 Type: schema.TypeString,
210 Optional: true,
211 },
212
213 "final_endpoint": {
214 Type: schema.TypeString,
215 Optional: true,
216 },
217
218 "follow_redirect": {
219 Type: schema.TypeBool,
220 Optional: true,
221 },
222 },
223 }
224 }
225
226 func CreateTest(d *schema.ResourceData, meta interface{}) error {
227 client := meta.(*statuscake.Client)
228
229 newTest := &statuscake.Test{
230 WebsiteName: d.Get("website_name").(string),
231 WebsiteURL: d.Get("website_url").(string),
232 CheckRate: d.Get("check_rate").(int),
233 TestType: d.Get("test_type").(string),
234 Paused: d.Get("paused").(bool),
235 Timeout: d.Get("timeout").(int),
236 Confirmation: d.Get("confirmations").(int),
237 Port: d.Get("port").(int),
238 TriggerRate: d.Get("trigger_rate").(int),
239 CustomHeader: d.Get("custom_header").(string),
240 UserAgent: d.Get("user_agent").(string),
241 Status: d.Get("status").(string),
242 Uptime: d.Get("uptime").(float64),
243 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
244 PingURL: d.Get("ping_url").(string),
245 BasicUser: d.Get("basic_user").(string),
246 BasicPass: d.Get("basic_pass").(string),
247 Public: d.Get("public").(int),
248 LogoImage: d.Get("logo_image").(string),
249 Branding: d.Get("branding").(int),
250 WebsiteHost: d.Get("website_host").(string),
251 Virus: d.Get("virus").(int),
252 FindString: d.Get("find_string").(string),
253 DoNotFind: d.Get("do_not_find").(bool),
254 RealBrowser: d.Get("real_browser").(int),
255 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
256 StatusCodes: d.Get("status_codes").(string),
257 UseJar: d.Get("use_jar").(int),
258 PostRaw: d.Get("post_raw").(string),
259 FinalEndpoint: d.Get("final_endpoint").(string),
260 FollowRedirect: d.Get("follow_redirect").(bool),
261 }
262
263 if v, ok := d.GetOk("contact_group"); ok {
264 newTest.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
265 } else if v, ok := d.GetOk("contact_id"); ok {
266 newTest.ContactID = v.(int)
267 }
268
269 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
270
271 response, err := client.Tests().Update(newTest)
272 if err != nil {
273 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
274 }
275
276 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
277 d.Set("status", response.Status)
278 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
279 d.SetId(fmt.Sprintf("%d", response.TestID))
280
281 return ReadTest(d, meta)
282 }
283
284 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
285 client := meta.(*statuscake.Client)
286
287 params := getStatusCakeTestInput(d)
288
289 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
290 _, err := client.Tests().Update(params)
291 if err != nil {
292 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
293 }
294 return nil
295 }
296
297 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
298 client := meta.(*statuscake.Client)
299
300 testId, parseErr := strconv.Atoi(d.Id())
301 if parseErr != nil {
302 return parseErr
303 }
304 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
305 err := client.Tests().Delete(testId)
306 if err != nil {
307 return err
308 }
309
310 return nil
311 }
312
313 func ReadTest(d *schema.ResourceData, meta interface{}) error {
314 client := meta.(*statuscake.Client)
315
316 testId, parseErr := strconv.Atoi(d.Id())
317 if parseErr != nil {
318 return parseErr
319 }
320 testResp, err := client.Tests().Detail(testId)
321 if err != nil {
322 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
323 }
324
325 if v, ok := d.GetOk("contact_group"); ok {
326 d.Set("contact_group", v)
327 } else if v, ok := d.GetOk("contact_id"); ok {
328 d.Set("contact_id", v)
329 }
330 d.Set("website_name", testResp.WebsiteName)
331 d.Set("website_url", testResp.WebsiteURL)
332 d.Set("check_rate", testResp.CheckRate)
333 d.Set("test_type", testResp.TestType)
334 d.Set("paused", testResp.Paused)
335 d.Set("timeout", testResp.Timeout)
336 d.Set("confirmations", testResp.Confirmation)
337 d.Set("port", testResp.Port)
338 d.Set("trigger_rate", testResp.TriggerRate)
339 d.Set("custom_header", testResp.CustomHeader)
340 d.Set("status", testResp.Status)
341 d.Set("uptime", testResp.Uptime)
342 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
343 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
344 }
345 d.Set("logo_image", testResp.LogoImage)
346 // Even after WebsiteHost is set, the API returns ""
347 // API docs aren't clear on usage will only override state if we get a non-empty value back
348 if testResp.WebsiteHost != "" {
349 d.Set("website_host", testResp.WebsiteHost)
350 }
351 d.Set("find_string", testResp.FindString)
352 d.Set("do_not_find", testResp.DoNotFind)
353 d.Set("status_codes", testResp.StatusCodes)
354 d.Set("use_jar", testResp.UseJar)
355 d.Set("post_raw", testResp.PostRaw)
356 d.Set("final_endpoint", testResp.FinalEndpoint)
357 d.Set("follow_redirect", testResp.FollowRedirect)
358
359 return nil
360 }
361
362 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
363 testId, parseErr := strconv.Atoi(d.Id())
364 if parseErr != nil {
365 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
366 }
367 test := &statuscake.Test{
368 TestID: testId,
369 }
370 if v, ok := d.GetOk("website_name"); ok {
371 test.WebsiteName = v.(string)
372 }
373 if v, ok := d.GetOk("website_url"); ok {
374 test.WebsiteURL = v.(string)
375 }
376 if v, ok := d.GetOk("check_rate"); ok {
377 test.CheckRate = v.(int)
378 }
379 if v, ok := d.GetOk("contact_group"); ok {
380 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
381 } else if v, ok := d.GetOk("contact_id"); ok {
382 test.ContactID = v.(int)
383 }
384 if v, ok := d.GetOk("test_type"); ok {
385 test.TestType = v.(string)
386 }
387 if v, ok := d.GetOk("paused"); ok {
388 test.Paused = v.(bool)
389 }
390 if v, ok := d.GetOk("timeout"); ok {
391 test.Timeout = v.(int)
392 }
393 if v, ok := d.GetOk("confirmations"); ok {
394 test.Confirmation = v.(int)
395 }
396 if v, ok := d.GetOk("port"); ok {
397 test.Port = v.(int)
398 }
399 if v, ok := d.GetOk("trigger_rate"); ok {
400 test.TriggerRate = v.(int)
401 }
402 if v, ok := d.GetOk("custom_header"); ok {
403 test.CustomHeader = v.(string)
404 }
405 if v, ok := d.GetOk("user_agent"); ok {
406 test.UserAgent = v.(string)
407 }
408 if v, ok := d.GetOk("node_locations"); ok {
409 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
410 }
411 if v, ok := d.GetOk("ping_url"); ok {
412 test.PingURL = v.(string)
413 }
414 if v, ok := d.GetOk("basic_user"); ok {
415 test.BasicUser = v.(string)
416 }
417 if v, ok := d.GetOk("basic_pass"); ok {
418 test.BasicPass = v.(string)
419 }
420 if v, ok := d.GetOk("public"); ok {
421 test.Public = v.(int)
422 }
423 if v, ok := d.GetOk("logo_image"); ok {
424 test.LogoImage = v.(string)
425 }
426 if v, ok := d.GetOk("branding"); ok {
427 test.Branding = v.(int)
428 }
429 if v, ok := d.GetOk("website_host"); ok {
430 test.WebsiteHost = v.(string)
431 }
432 if v, ok := d.GetOk("virus"); ok {
433 test.Virus = v.(int)
434 }
435 if v, ok := d.GetOk("find_string"); ok {
436 test.FindString = v.(string)
437 }
438 if v, ok := d.GetOk("do_not_find"); ok {
439 test.DoNotFind = v.(bool)
440 }
441 if v, ok := d.GetOk("real_browser"); ok {
442 test.RealBrowser = v.(int)
443 }
444 if v, ok := d.GetOk("test_tags"); ok {
445 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
446 }
447 if v, ok := d.GetOk("status_codes"); ok {
448 test.StatusCodes = v.(string)
449 }
450 if v, ok := d.GetOk("use_jar"); ok {
451 test.UseJar = v.(int)
452 }
453 if v, ok := d.GetOk("post_raw"); ok {
454 test.PostRaw = v.(string)
455 }
456 if v, ok := d.GetOk("final_endpoint"); ok {
457 test.FinalEndpoint = v.(string)
458 }
459 if v, ok := d.GetOk("follow_redirect"); ok {
460 test.FollowRedirect = v.(bool)
461 }
462
463 return test
464 }