9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
13 func castSetToSliceStrings(configured []interface{}) []string {
14 res := make([]string, len(configured))
16 for i, element := range configured {
17 res[i] = element.(string)
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] == "" {
31 func resourceStatusCakeTest() *schema.Resource {
32 return &schema.Resource{
38 Schema: map[string]*schema.Schema{
40 Type: schema.TypeString,
45 Type: schema.TypeString,
50 Type: schema.TypeString,
56 Elem: &schema.Schema{Type: schema.TypeString},
58 Set: schema.HashString,
59 ConflictsWith: []string{"contact_id"},
65 ConflictsWith: []string{"contact_group"},
66 Deprecated: "use contact_group instead",
76 Type: schema.TypeString,
81 Type: schema.TypeBool,
103 Type: schema.TypeInt,
109 Type: schema.TypeString,
114 Type: schema.TypeString,
119 Type: schema.TypeString,
124 Type: schema.TypeFloat,
129 Type: schema.TypeSet,
130 Elem: &schema.Schema{Type: schema.TypeString},
132 Set: schema.HashString,
136 Type: schema.TypeString,
141 Type: schema.TypeString,
146 Type: schema.TypeString,
152 Type: schema.TypeInt,
157 Type: schema.TypeString,
162 Type: schema.TypeInt,
167 Type: schema.TypeString,
172 Type: schema.TypeInt,
177 Type: schema.TypeString,
182 Type: schema.TypeBool,
187 Type: schema.TypeInt,
192 Type: schema.TypeSet,
193 Elem: &schema.Schema{Type: schema.TypeString},
195 Set: schema.HashString,
199 Type: schema.TypeString,
204 Type: schema.TypeInt,
209 Type: schema.TypeString,
214 Type: schema.TypeString,
218 "enable_ssl_alert": {
219 Type: schema.TypeBool,
225 Type: schema.TypeBool,
232 func CreateTest(d *schema.ResourceData, meta interface{}) error {
233 client := meta.(*statuscake.Client)
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),
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)
276 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
278 response, err := client.Tests().Update(newTest)
280 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
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))
288 return ReadTest(d, meta)
291 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
292 client := meta.(*statuscake.Client)
294 params := getStatusCakeTestInput(d)
296 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
297 _, err := client.Tests().Update(params)
299 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
304 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
305 client := meta.(*statuscake.Client)
307 testId, parseErr := strconv.Atoi(d.Id())
311 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
312 err := client.Tests().Delete(testId)
320 func ReadTest(d *schema.ResourceData, meta interface{}) error {
321 client := meta.(*statuscake.Client)
323 testId, parseErr := strconv.Atoi(d.Id())
327 testResp, err := client.Tests().Detail(testId)
329 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
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)
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)
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)
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)
370 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
371 testId, parseErr := strconv.Atoi(d.Id())
373 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
375 test := &statuscake.Test{
378 if v, ok := d.GetOk("website_name"); ok {
379 test.WebsiteName = v.(string)
381 if v, ok := d.GetOk("website_url"); ok {
382 test.WebsiteURL = v.(string)
384 if v, ok := d.GetOk("check_rate"); ok {
385 test.CheckRate = v.(int)
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)
392 if v, ok := d.GetOk("test_type"); ok {
393 test.TestType = v.(string)
395 if v, ok := d.GetOk("paused"); ok {
396 test.Paused = v.(bool)
398 if v, ok := d.GetOk("timeout"); ok {
399 test.Timeout = v.(int)
401 if v, ok := d.GetOk("confirmations"); ok {
402 test.Confirmation = v.(int)
404 if v, ok := d.GetOk("port"); ok {
407 if v, ok := d.GetOk("trigger_rate"); ok {
408 test.TriggerRate = v.(int)
410 if v, ok := d.GetOk("custom_header"); ok {
411 test.CustomHeader = v.(string)
413 if v, ok := d.GetOk("user_agent"); ok {
414 test.UserAgent = v.(string)
416 if v, ok := d.GetOk("node_locations"); ok {
417 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
419 if v, ok := d.GetOk("ping_url"); ok {
420 test.PingURL = v.(string)
422 if v, ok := d.GetOk("basic_user"); ok {
423 test.BasicUser = v.(string)
425 if v, ok := d.GetOk("basic_pass"); ok {
426 test.BasicPass = v.(string)
428 if v, ok := d.GetOk("public"); ok {
429 test.Public = v.(int)
431 if v, ok := d.GetOk("logo_image"); ok {
432 test.LogoImage = v.(string)
434 if v, ok := d.GetOk("branding"); ok {
435 test.Branding = v.(int)
437 if v, ok := d.GetOk("website_host"); ok {
438 test.WebsiteHost = v.(string)
440 if v, ok := d.GetOk("virus"); ok {
443 if v, ok := d.GetOk("find_string"); ok {
444 test.FindString = v.(string)
446 if v, ok := d.GetOk("do_not_find"); ok {
447 test.DoNotFind = v.(bool)
449 if v, ok := d.GetOk("real_browser"); ok {
450 test.RealBrowser = v.(int)
452 if v, ok := d.GetOk("test_tags"); ok {
453 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
455 if v, ok := d.GetOk("status_codes"); ok {
456 test.StatusCodes = v.(string)
458 if v, ok := d.GetOk("use_jar"); ok {
459 test.UseJar = v.(int)
461 if v, ok := d.GetOk("post_raw"); ok {
462 test.PostRaw = v.(string)
464 if v, ok := d.GetOk("final_endpoint"); ok {
465 test.FinalEndpoint = v.(string)
467 if v, ok := d.GetOk("enable_ssl_alert"); ok {
468 test.EnableSSLAlert = v.(bool)
470 if v, ok := d.GetOk("follow_redirect"); ok {
471 test.FollowRedirect = v.(bool)