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{
37 Importer: &schema.ResourceImporter{
38 State: schema.ImportStatePassthrough,
41 Schema: map[string]*schema.Schema{
43 Type: schema.TypeString,
48 Type: schema.TypeString,
53 Type: schema.TypeString,
59 Elem: &schema.Schema{Type: schema.TypeString},
61 Set: schema.HashString,
62 ConflictsWith: []string{"contact_id"},
68 ConflictsWith: []string{"contact_group"},
69 Deprecated: "use contact_group instead",
79 Type: schema.TypeString,
84 Type: schema.TypeBool,
101 Type: schema.TypeInt,
106 Type: schema.TypeInt,
112 Type: schema.TypeString,
117 Type: schema.TypeString,
122 Type: schema.TypeString,
127 Type: schema.TypeFloat,
132 Type: schema.TypeSet,
133 Elem: &schema.Schema{Type: schema.TypeString},
135 Set: schema.HashString,
139 Type: schema.TypeString,
144 Type: schema.TypeString,
149 Type: schema.TypeString,
155 Type: schema.TypeInt,
160 Type: schema.TypeString,
165 Type: schema.TypeInt,
170 Type: schema.TypeString,
175 Type: schema.TypeInt,
180 Type: schema.TypeString,
185 Type: schema.TypeBool,
190 Type: schema.TypeInt,
195 Type: schema.TypeSet,
196 Elem: &schema.Schema{Type: schema.TypeString},
198 Set: schema.HashString,
202 Type: schema.TypeString,
207 Type: schema.TypeInt,
212 Type: schema.TypeString,
217 Type: schema.TypeString,
221 "enable_ssl_alert": {
222 Type: schema.TypeBool,
228 Type: schema.TypeBool,
235 func CreateTest(d *schema.ResourceData, meta interface{}) error {
236 client := meta.(*statuscake.Client)
238 newTest := &statuscake.Test{
239 WebsiteName: d.Get("website_name").(string),
240 WebsiteURL: d.Get("website_url").(string),
241 CheckRate: d.Get("check_rate").(int),
242 TestType: d.Get("test_type").(string),
243 Paused: d.Get("paused").(bool),
244 Timeout: d.Get("timeout").(int),
245 Confirmation: d.Get("confirmations").(int),
246 Port: d.Get("port").(int),
247 TriggerRate: d.Get("trigger_rate").(int),
248 CustomHeader: d.Get("custom_header").(string),
249 UserAgent: d.Get("user_agent").(string),
250 Status: d.Get("status").(string),
251 Uptime: d.Get("uptime").(float64),
252 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
253 PingURL: d.Get("ping_url").(string),
254 BasicUser: d.Get("basic_user").(string),
255 BasicPass: d.Get("basic_pass").(string),
256 Public: d.Get("public").(int),
257 LogoImage: d.Get("logo_image").(string),
258 Branding: d.Get("branding").(int),
259 WebsiteHost: d.Get("website_host").(string),
260 Virus: d.Get("virus").(int),
261 FindString: d.Get("find_string").(string),
262 DoNotFind: d.Get("do_not_find").(bool),
263 RealBrowser: d.Get("real_browser").(int),
264 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
265 StatusCodes: d.Get("status_codes").(string),
266 UseJar: d.Get("use_jar").(int),
267 PostRaw: d.Get("post_raw").(string),
268 FinalEndpoint: d.Get("final_endpoint").(string),
269 EnableSSLAlert: d.Get("enable_ssl_alert").(bool),
270 FollowRedirect: d.Get("follow_redirect").(bool),
273 if v, ok := d.GetOk("contact_group"); ok {
274 newTest.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
275 } else if v, ok := d.GetOk("contact_id"); ok {
276 newTest.ContactID = v.(int)
279 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
281 response, err := client.Tests().Update(newTest)
283 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
286 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
287 d.Set("status", response.Status)
288 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
289 d.SetId(fmt.Sprintf("%d", response.TestID))
291 return ReadTest(d, meta)
294 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
295 client := meta.(*statuscake.Client)
297 params := getStatusCakeTestInput(d)
299 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
300 _, err := client.Tests().Update(params)
302 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
307 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
308 client := meta.(*statuscake.Client)
310 testId, parseErr := strconv.Atoi(d.Id())
314 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
315 err := client.Tests().Delete(testId)
323 func ReadTest(d *schema.ResourceData, meta interface{}) error {
324 client := meta.(*statuscake.Client)
326 testId, parseErr := strconv.Atoi(d.Id())
330 testResp, err := client.Tests().Detail(testId)
332 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
335 if v, ok := d.GetOk("contact_group"); ok {
336 d.Set("contact_group", v)
337 } else if v, ok := d.GetOk("contact_id"); ok {
338 d.Set("contact_id", v)
341 d.Set("contact_group", testResp.ContactGroup)
342 d.Set("website_name", testResp.WebsiteName)
343 d.Set("website_url", testResp.WebsiteURL)
344 d.Set("check_rate", testResp.CheckRate)
345 d.Set("test_type", testResp.TestType)
346 d.Set("paused", testResp.Paused)
347 d.Set("timeout", testResp.Timeout)
348 d.Set("confirmations", testResp.Confirmation)
349 d.Set("port", testResp.Port)
350 d.Set("trigger_rate", testResp.TriggerRate)
351 d.Set("custom_header", testResp.CustomHeader)
352 d.Set("status", testResp.Status)
353 d.Set("uptime", testResp.Uptime)
354 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
355 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
357 d.Set("logo_image", testResp.LogoImage)
358 // Even after WebsiteHost is set, the API returns ""
359 // API docs aren't clear on usage will only override state if we get a non-empty value back
360 if testResp.WebsiteHost != "" {
361 d.Set("website_host", testResp.WebsiteHost)
363 d.Set("find_string", testResp.FindString)
364 d.Set("do_not_find", testResp.DoNotFind)
365 d.Set("status_codes", testResp.StatusCodes)
366 d.Set("use_jar", testResp.UseJar)
367 d.Set("user_agent", testResp.UserAgent)
368 d.Set("post_raw", testResp.PostRaw)
369 d.Set("final_endpoint", testResp.FinalEndpoint)
370 d.Set("enable_ssl_alert", testResp.EnableSSLAlert)
371 d.Set("follow_redirect", testResp.FollowRedirect)
372 d.Set("test_tags", testResp.TestTags)
376 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
377 testId, parseErr := strconv.Atoi(d.Id())
379 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
381 test := &statuscake.Test{
384 if v, ok := d.GetOk("website_name"); ok {
385 test.WebsiteName = v.(string)
387 if v, ok := d.GetOk("website_url"); ok {
388 test.WebsiteURL = v.(string)
390 if v, ok := d.GetOk("check_rate"); ok {
391 test.CheckRate = v.(int)
393 if v, ok := d.GetOk("contact_group"); ok {
394 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
395 } else if v, ok := d.GetOk("contact_id"); ok {
396 test.ContactID = v.(int)
398 if v, ok := d.GetOk("test_type"); ok {
399 test.TestType = v.(string)
401 if v, ok := d.GetOk("paused"); ok {
402 test.Paused = v.(bool)
404 if v, ok := d.GetOk("timeout"); ok {
405 test.Timeout = v.(int)
407 if v, ok := d.GetOk("confirmations"); ok {
408 test.Confirmation = v.(int)
410 if v, ok := d.GetOk("port"); ok {
413 if v, ok := d.GetOk("trigger_rate"); ok {
414 test.TriggerRate = v.(int)
416 if v, ok := d.GetOk("custom_header"); ok {
417 test.CustomHeader = v.(string)
419 if v, ok := d.GetOk("user_agent"); ok {
420 test.UserAgent = v.(string)
422 if v, ok := d.GetOk("node_locations"); ok {
423 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
425 if v, ok := d.GetOk("ping_url"); ok {
426 test.PingURL = v.(string)
428 if v, ok := d.GetOk("basic_user"); ok {
429 test.BasicUser = v.(string)
431 if v, ok := d.GetOk("basic_pass"); ok {
432 test.BasicPass = v.(string)
434 if v, ok := d.GetOk("public"); ok {
435 test.Public = v.(int)
437 if v, ok := d.GetOk("logo_image"); ok {
438 test.LogoImage = v.(string)
440 if v, ok := d.GetOk("branding"); ok {
441 test.Branding = v.(int)
443 if v, ok := d.GetOk("website_host"); ok {
444 test.WebsiteHost = v.(string)
446 if v, ok := d.GetOk("virus"); ok {
449 if v, ok := d.GetOk("find_string"); ok {
450 test.FindString = v.(string)
452 if v, ok := d.GetOk("do_not_find"); ok {
453 test.DoNotFind = v.(bool)
455 if v, ok := d.GetOk("real_browser"); ok {
456 test.RealBrowser = v.(int)
458 if v, ok := d.GetOk("test_tags"); ok {
459 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
461 if v, ok := d.GetOk("status_codes"); ok {
462 test.StatusCodes = v.(string)
464 if v, ok := d.GetOk("use_jar"); ok {
465 test.UseJar = v.(int)
467 if v, ok := d.GetOk("post_raw"); ok {
468 test.PostRaw = v.(string)
470 if v, ok := d.GetOk("final_endpoint"); ok {
471 test.FinalEndpoint = v.(string)
473 if v, ok := d.GetOk("enable_ssl_alert"); ok {
474 test.EnableSSLAlert = v.(bool)
476 if v, ok := d.GetOk("follow_redirect"); ok {
477 test.FollowRedirect = v.(bool)