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,
222 Type: schema.TypeBool,
229 func CreateTest(d *schema.ResourceData, meta interface{}) error {
230 client := meta.(*statuscake.Client)
232 newTest := &statuscake.Test{
233 WebsiteName: d.Get("website_name").(string),
234 WebsiteURL: d.Get("website_url").(string),
235 CheckRate: d.Get("check_rate").(int),
236 TestType: d.Get("test_type").(string),
237 Paused: d.Get("paused").(bool),
238 Timeout: d.Get("timeout").(int),
239 Confirmation: d.Get("confirmations").(int),
240 Port: d.Get("port").(int),
241 TriggerRate: d.Get("trigger_rate").(int),
242 CustomHeader: d.Get("custom_header").(string),
243 UserAgent: d.Get("user_agent").(string),
244 Status: d.Get("status").(string),
245 Uptime: d.Get("uptime").(float64),
246 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
247 PingURL: d.Get("ping_url").(string),
248 BasicUser: d.Get("basic_user").(string),
249 BasicPass: d.Get("basic_pass").(string),
250 Public: d.Get("public").(int),
251 LogoImage: d.Get("logo_image").(string),
252 Branding: d.Get("branding").(int),
253 WebsiteHost: d.Get("website_host").(string),
254 Virus: d.Get("virus").(int),
255 FindString: d.Get("find_string").(string),
256 DoNotFind: d.Get("do_not_find").(bool),
257 RealBrowser: d.Get("real_browser").(int),
258 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
259 StatusCodes: d.Get("status_codes").(string),
260 UseJar: d.Get("use_jar").(int),
261 PostRaw: d.Get("post_raw").(string),
262 FinalEndpoint: d.Get("final_endpoint").(string),
263 FollowRedirect: d.Get("follow_redirect").(bool),
266 if v, ok := d.GetOk("contact_group"); ok {
267 newTest.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
268 } else if v, ok := d.GetOk("contact_id"); ok {
269 newTest.ContactID = v.(int)
272 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
274 response, err := client.Tests().Update(newTest)
276 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
279 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
280 d.Set("status", response.Status)
281 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
282 d.SetId(fmt.Sprintf("%d", response.TestID))
284 return ReadTest(d, meta)
287 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
288 client := meta.(*statuscake.Client)
290 params := getStatusCakeTestInput(d)
292 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
293 _, err := client.Tests().Update(params)
295 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
300 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
301 client := meta.(*statuscake.Client)
303 testId, parseErr := strconv.Atoi(d.Id())
307 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
308 err := client.Tests().Delete(testId)
316 func ReadTest(d *schema.ResourceData, meta interface{}) error {
317 client := meta.(*statuscake.Client)
319 testId, parseErr := strconv.Atoi(d.Id())
323 testResp, err := client.Tests().Detail(testId)
325 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
328 if v, ok := d.GetOk("contact_group"); ok {
329 d.Set("contact_group", v)
330 } else if v, ok := d.GetOk("contact_id"); ok {
331 d.Set("contact_id", v)
333 d.Set("website_name", testResp.WebsiteName)
334 d.Set("website_url", testResp.WebsiteURL)
335 d.Set("check_rate", testResp.CheckRate)
336 d.Set("test_type", testResp.TestType)
337 d.Set("paused", testResp.Paused)
338 d.Set("timeout", testResp.Timeout)
339 d.Set("confirmations", testResp.Confirmation)
340 d.Set("port", testResp.Port)
341 d.Set("trigger_rate", testResp.TriggerRate)
342 d.Set("custom_header", testResp.CustomHeader)
343 d.Set("status", testResp.Status)
344 d.Set("uptime", testResp.Uptime)
345 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
346 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
348 d.Set("logo_image", testResp.LogoImage)
349 // Even after WebsiteHost is set, the API returns ""
350 // API docs aren't clear on usage will only override state if we get a non-empty value back
351 if testResp.WebsiteHost != "" {
352 d.Set("website_host", testResp.WebsiteHost)
354 d.Set("find_string", testResp.FindString)
355 d.Set("do_not_find", testResp.DoNotFind)
356 d.Set("status_codes", testResp.StatusCodes)
357 d.Set("use_jar", testResp.UseJar)
358 d.Set("post_raw", testResp.PostRaw)
359 d.Set("final_endpoint", testResp.FinalEndpoint)
360 d.Set("follow_redirect", testResp.FollowRedirect)
365 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
366 testId, parseErr := strconv.Atoi(d.Id())
368 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
370 test := &statuscake.Test{
373 if v, ok := d.GetOk("website_name"); ok {
374 test.WebsiteName = v.(string)
376 if v, ok := d.GetOk("website_url"); ok {
377 test.WebsiteURL = v.(string)
379 if v, ok := d.GetOk("check_rate"); ok {
380 test.CheckRate = v.(int)
382 if v, ok := d.GetOk("contact_group"); ok {
383 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
384 } else if v, ok := d.GetOk("contact_id"); ok {
385 test.ContactID = v.(int)
387 if v, ok := d.GetOk("test_type"); ok {
388 test.TestType = v.(string)
390 if v, ok := d.GetOk("paused"); ok {
391 test.Paused = v.(bool)
393 if v, ok := d.GetOk("timeout"); ok {
394 test.Timeout = v.(int)
396 if v, ok := d.GetOk("confirmations"); ok {
397 test.Confirmation = v.(int)
399 if v, ok := d.GetOk("port"); ok {
402 if v, ok := d.GetOk("trigger_rate"); ok {
403 test.TriggerRate = v.(int)
405 if v, ok := d.GetOk("custom_header"); ok {
406 test.CustomHeader = v.(string)
408 if v, ok := d.GetOk("user_agent"); ok {
409 test.UserAgent = v.(string)
411 if v, ok := d.GetOk("node_locations"); ok {
412 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
414 if v, ok := d.GetOk("ping_url"); ok {
415 test.PingURL = v.(string)
417 if v, ok := d.GetOk("basic_user"); ok {
418 test.BasicUser = v.(string)
420 if v, ok := d.GetOk("basic_pass"); ok {
421 test.BasicPass = v.(string)
423 if v, ok := d.GetOk("public"); ok {
424 test.Public = v.(int)
426 if v, ok := d.GetOk("logo_image"); ok {
427 test.LogoImage = v.(string)
429 if v, ok := d.GetOk("branding"); ok {
430 test.Branding = v.(int)
432 if v, ok := d.GetOk("website_host"); ok {
433 test.WebsiteHost = v.(string)
435 if v, ok := d.GetOk("virus"); ok {
438 if v, ok := d.GetOk("find_string"); ok {
439 test.FindString = v.(string)
441 if v, ok := d.GetOk("do_not_find"); ok {
442 test.DoNotFind = v.(bool)
444 if v, ok := d.GetOk("real_browser"); ok {
445 test.RealBrowser = v.(int)
447 if v, ok := d.GetOk("test_tags"); ok {
448 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
450 if v, ok := d.GetOk("status_codes"); ok {
451 test.StatusCodes = v.(string)
453 if v, ok := d.GetOk("use_jar"); ok {
454 test.UseJar = v.(int)
456 if v, ok := d.GetOk("post_raw"); ok {
457 test.PostRaw = v.(string)
459 if v, ok := d.GetOk("final_endpoint"); ok {
460 test.FinalEndpoint = v.(string)
462 if v, ok := d.GetOk("follow_redirect"); ok {
463 test.FollowRedirect = v.(bool)