]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - statuscake/resource_statuscaketest.go
Add enable_ssl_alert feature for uptime tests
[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 "enable_ssl_alert": {
219 Type: schema.TypeBool,
220 Optional: true,
221 Default: false,
222 },
223
224 "follow_redirect": {
225 Type: schema.TypeBool,
226 Optional: true,
227 },
228 },
229 }
230 }
231
232 func CreateTest(d *schema.ResourceData, meta interface{}) error {
233 client := meta.(*statuscake.Client)
234
235 newTest := &statuscake.Test{
236 WebsiteName: d.Get("website_name").(string),
237 WebsiteURL: d.Get("website_url").(string),
238 CheckRate: d.Get("check_rate").(int),
239 TestType: d.Get("test_type").(string),
240 Paused: d.Get("paused").(bool),
241 Timeout: d.Get("timeout").(int),
242 Confirmation: d.Get("confirmations").(int),
243 Port: d.Get("port").(int),
244 TriggerRate: d.Get("trigger_rate").(int),
245 CustomHeader: d.Get("custom_header").(string),
246 UserAgent: d.Get("user_agent").(string),
247 Status: d.Get("status").(string),
248 Uptime: d.Get("uptime").(float64),
249 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
250 PingURL: d.Get("ping_url").(string),
251 BasicUser: d.Get("basic_user").(string),
252 BasicPass: d.Get("basic_pass").(string),
253 Public: d.Get("public").(int),
254 LogoImage: d.Get("logo_image").(string),
255 Branding: d.Get("branding").(int),
256 WebsiteHost: d.Get("website_host").(string),
257 Virus: d.Get("virus").(int),
258 FindString: d.Get("find_string").(string),
259 DoNotFind: d.Get("do_not_find").(bool),
260 RealBrowser: d.Get("real_browser").(int),
261 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
262 StatusCodes: d.Get("status_codes").(string),
263 UseJar: d.Get("use_jar").(int),
264 PostRaw: d.Get("post_raw").(string),
265 FinalEndpoint: d.Get("final_endpoint").(string),
266 EnableSSLAlert: d.Get("enable_ssl_alert").(bool),
267 FollowRedirect: d.Get("follow_redirect").(bool),
268 }
269
270 if v, ok := d.GetOk("contact_group"); ok {
271 newTest.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
272 } else if v, ok := d.GetOk("contact_id"); ok {
273 newTest.ContactID = v.(int)
274 }
275
276 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
277
278 response, err := client.Tests().Update(newTest)
279 if err != nil {
280 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
281 }
282
283 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
284 d.Set("status", response.Status)
285 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
286 d.SetId(fmt.Sprintf("%d", response.TestID))
287
288 return ReadTest(d, meta)
289 }
290
291 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
292 client := meta.(*statuscake.Client)
293
294 params := getStatusCakeTestInput(d)
295
296 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
297 _, err := client.Tests().Update(params)
298 if err != nil {
299 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
300 }
301 return nil
302 }
303
304 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
305 client := meta.(*statuscake.Client)
306
307 testId, parseErr := strconv.Atoi(d.Id())
308 if parseErr != nil {
309 return parseErr
310 }
311 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
312 err := client.Tests().Delete(testId)
313 if err != nil {
314 return err
315 }
316
317 return nil
318 }
319
320 func ReadTest(d *schema.ResourceData, meta interface{}) error {
321 client := meta.(*statuscake.Client)
322
323 testId, parseErr := strconv.Atoi(d.Id())
324 if parseErr != nil {
325 return parseErr
326 }
327 testResp, err := client.Tests().Detail(testId)
328 if err != nil {
329 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
330 }
331
332 if v, ok := d.GetOk("contact_group"); ok {
333 d.Set("contact_group", v)
334 } else if v, ok := d.GetOk("contact_id"); ok {
335 d.Set("contact_id", v)
336 }
337 d.Set("website_name", testResp.WebsiteName)
338 d.Set("website_url", testResp.WebsiteURL)
339 d.Set("check_rate", testResp.CheckRate)
340 d.Set("test_type", testResp.TestType)
341 d.Set("paused", testResp.Paused)
342 d.Set("timeout", testResp.Timeout)
343 d.Set("confirmations", testResp.Confirmation)
344 d.Set("port", testResp.Port)
345 d.Set("trigger_rate", testResp.TriggerRate)
346 d.Set("custom_header", testResp.CustomHeader)
347 d.Set("status", testResp.Status)
348 d.Set("uptime", testResp.Uptime)
349 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
350 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
351 }
352 d.Set("logo_image", testResp.LogoImage)
353 // Even after WebsiteHost is set, the API returns ""
354 // API docs aren't clear on usage will only override state if we get a non-empty value back
355 if testResp.WebsiteHost != "" {
356 d.Set("website_host", testResp.WebsiteHost)
357 }
358 d.Set("find_string", testResp.FindString)
359 d.Set("do_not_find", testResp.DoNotFind)
360 d.Set("status_codes", testResp.StatusCodes)
361 d.Set("use_jar", testResp.UseJar)
362 d.Set("post_raw", testResp.PostRaw)
363 d.Set("final_endpoint", testResp.FinalEndpoint)
364 d.Set("enable_ssl_alert", testResp.EnableSSLAlert)
365 d.Set("follow_redirect", testResp.FollowRedirect)
366
367 return nil
368 }
369
370 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
371 testId, parseErr := strconv.Atoi(d.Id())
372 if parseErr != nil {
373 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
374 }
375 test := &statuscake.Test{
376 TestID: testId,
377 }
378 if v, ok := d.GetOk("website_name"); ok {
379 test.WebsiteName = v.(string)
380 }
381 if v, ok := d.GetOk("website_url"); ok {
382 test.WebsiteURL = v.(string)
383 }
384 if v, ok := d.GetOk("check_rate"); ok {
385 test.CheckRate = v.(int)
386 }
387 if v, ok := d.GetOk("contact_group"); ok {
388 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
389 } else if v, ok := d.GetOk("contact_id"); ok {
390 test.ContactID = v.(int)
391 }
392 if v, ok := d.GetOk("test_type"); ok {
393 test.TestType = v.(string)
394 }
395 if v, ok := d.GetOk("paused"); ok {
396 test.Paused = v.(bool)
397 }
398 if v, ok := d.GetOk("timeout"); ok {
399 test.Timeout = v.(int)
400 }
401 if v, ok := d.GetOk("confirmations"); ok {
402 test.Confirmation = v.(int)
403 }
404 if v, ok := d.GetOk("port"); ok {
405 test.Port = v.(int)
406 }
407 if v, ok := d.GetOk("trigger_rate"); ok {
408 test.TriggerRate = v.(int)
409 }
410 if v, ok := d.GetOk("custom_header"); ok {
411 test.CustomHeader = v.(string)
412 }
413 if v, ok := d.GetOk("user_agent"); ok {
414 test.UserAgent = v.(string)
415 }
416 if v, ok := d.GetOk("node_locations"); ok {
417 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
418 }
419 if v, ok := d.GetOk("ping_url"); ok {
420 test.PingURL = v.(string)
421 }
422 if v, ok := d.GetOk("basic_user"); ok {
423 test.BasicUser = v.(string)
424 }
425 if v, ok := d.GetOk("basic_pass"); ok {
426 test.BasicPass = v.(string)
427 }
428 if v, ok := d.GetOk("public"); ok {
429 test.Public = v.(int)
430 }
431 if v, ok := d.GetOk("logo_image"); ok {
432 test.LogoImage = v.(string)
433 }
434 if v, ok := d.GetOk("branding"); ok {
435 test.Branding = v.(int)
436 }
437 if v, ok := d.GetOk("website_host"); ok {
438 test.WebsiteHost = v.(string)
439 }
440 if v, ok := d.GetOk("virus"); ok {
441 test.Virus = v.(int)
442 }
443 if v, ok := d.GetOk("find_string"); ok {
444 test.FindString = v.(string)
445 }
446 if v, ok := d.GetOk("do_not_find"); ok {
447 test.DoNotFind = v.(bool)
448 }
449 if v, ok := d.GetOk("real_browser"); ok {
450 test.RealBrowser = v.(int)
451 }
452 if v, ok := d.GetOk("test_tags"); ok {
453 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
454 }
455 if v, ok := d.GetOk("status_codes"); ok {
456 test.StatusCodes = v.(string)
457 }
458 if v, ok := d.GetOk("use_jar"); ok {
459 test.UseJar = v.(int)
460 }
461 if v, ok := d.GetOk("post_raw"); ok {
462 test.PostRaw = v.(string)
463 }
464 if v, ok := d.GetOk("final_endpoint"); ok {
465 test.FinalEndpoint = v.(string)
466 }
467 if v, ok := d.GetOk("enable_ssl_alert"); ok {
468 test.EnableSSLAlert = v.(bool)
469 }
470 if v, ok := d.GetOk("follow_redirect"); ok {
471 test.FollowRedirect = v.(bool)
472 }
473
474 return test
475 }