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,
66 Type: schema.TypeString,
71 Type: schema.TypeBool,
98 Type: schema.TypeString,
103 Type: schema.TypeString,
108 Type: schema.TypeString,
113 Type: schema.TypeFloat,
118 Type: schema.TypeSet,
119 Elem: &schema.Schema{Type: schema.TypeString},
121 Set: schema.HashString,
125 Type: schema.TypeString,
130 Type: schema.TypeString,
135 Type: schema.TypeString,
141 Type: schema.TypeInt,
146 Type: schema.TypeString,
151 Type: schema.TypeInt,
156 Type: schema.TypeString,
161 Type: schema.TypeInt,
166 Type: schema.TypeString,
171 Type: schema.TypeBool,
176 Type: schema.TypeInt,
181 Type: schema.TypeString,
186 Type: schema.TypeString,
191 Type: schema.TypeInt,
196 Type: schema.TypeString,
201 Type: schema.TypeString,
206 Type: schema.TypeBool,
213 func CreateTest(d *schema.ResourceData, meta interface{}) error {
214 client := meta.(*statuscake.Client)
216 newTest := &statuscake.Test{
217 WebsiteName: d.Get("website_name").(string),
218 WebsiteURL: d.Get("website_url").(string),
219 CheckRate: d.Get("check_rate").(int),
220 TestType: d.Get("test_type").(string),
221 Paused: d.Get("paused").(bool),
222 Timeout: d.Get("timeout").(int),
223 ContactID: d.Get("contact_id").(int),
224 Confirmation: d.Get("confirmations").(int),
225 Port: d.Get("port").(int),
226 TriggerRate: d.Get("trigger_rate").(int),
227 CustomHeader: d.Get("custom_header").(string),
228 UserAgent: d.Get("user_agent").(string),
229 Status: d.Get("status").(string),
230 Uptime: d.Get("uptime").(float64),
231 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
232 PingURL: d.Get("ping_url").(string),
233 BasicUser: d.Get("basic_user").(string),
234 BasicPass: d.Get("basic_pass").(string),
235 Public: d.Get("public").(int),
236 LogoImage: d.Get("logo_image").(string),
237 Branding: d.Get("branding").(int),
238 WebsiteHost: d.Get("website_host").(string),
239 Virus: d.Get("virus").(int),
240 FindString: d.Get("find_string").(string),
241 DoNotFind: d.Get("do_not_find").(bool),
242 RealBrowser: d.Get("real_browser").(int),
243 TestTags: d.Get("test_tags").(string),
244 StatusCodes: d.Get("status_codes").(string),
245 UseJar: d.Get("use_jar").(int),
246 PostRaw: d.Get("post_raw").(string),
247 FinalEndpoint: d.Get("final_endpoint").(string),
248 FollowRedirect: d.Get("follow_redirect").(bool),
251 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
253 response, err := client.Tests().Update(newTest)
255 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
258 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
259 d.Set("status", response.Status)
260 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
261 d.SetId(fmt.Sprintf("%d", response.TestID))
263 return ReadTest(d, meta)
266 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
267 client := meta.(*statuscake.Client)
269 params := getStatusCakeTestInput(d)
271 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
272 _, err := client.Tests().Update(params)
274 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
279 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
280 client := meta.(*statuscake.Client)
282 testId, parseErr := strconv.Atoi(d.Id())
286 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
287 err := client.Tests().Delete(testId)
295 func ReadTest(d *schema.ResourceData, meta interface{}) error {
296 client := meta.(*statuscake.Client)
298 testId, parseErr := strconv.Atoi(d.Id())
302 testResp, err := client.Tests().Detail(testId)
304 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
306 d.Set("website_name", testResp.WebsiteName)
307 d.Set("website_url", testResp.WebsiteURL)
308 d.Set("check_rate", testResp.CheckRate)
309 d.Set("test_type", testResp.TestType)
310 d.Set("paused", testResp.Paused)
311 d.Set("timeout", testResp.Timeout)
312 d.Set("contact_id", testResp.ContactID)
313 d.Set("confirmations", testResp.Confirmation)
314 d.Set("port", testResp.Port)
315 d.Set("trigger_rate", testResp.TriggerRate)
316 d.Set("custom_header", testResp.CustomHeader)
317 d.Set("status", testResp.Status)
318 d.Set("uptime", testResp.Uptime)
319 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
320 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
322 d.Set("logo_image", testResp.LogoImage)
323 // Even after WebsiteHost is set, the API returns ""
324 // API docs aren't clear on usage will only override state if we get a non-empty value back
325 if testResp.WebsiteHost != "" {
326 d.Set("website_host", testResp.WebsiteHost)
328 d.Set("find_string", testResp.FindString)
329 d.Set("do_not_find", testResp.DoNotFind)
330 d.Set("status_codes", testResp.StatusCodes)
331 d.Set("use_jar", testResp.UseJar)
332 d.Set("post_raw", testResp.PostRaw)
333 d.Set("final_endpoint", testResp.FinalEndpoint)
334 d.Set("follow_redirect", testResp.FollowRedirect)
339 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
340 testId, parseErr := strconv.Atoi(d.Id())
342 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
344 test := &statuscake.Test{
347 if v, ok := d.GetOk("website_name"); ok {
348 test.WebsiteName = v.(string)
350 if v, ok := d.GetOk("website_url"); ok {
351 test.WebsiteURL = v.(string)
353 if v, ok := d.GetOk("check_rate"); ok {
354 test.CheckRate = v.(int)
356 if v, ok := d.GetOk("contact_id"); ok {
357 test.ContactID = v.(int)
359 if v, ok := d.GetOk("test_type"); ok {
360 test.TestType = v.(string)
362 if v, ok := d.GetOk("paused"); ok {
363 test.Paused = v.(bool)
365 if v, ok := d.GetOk("timeout"); ok {
366 test.Timeout = v.(int)
368 if v, ok := d.GetOk("confirmations"); ok {
369 test.Confirmation = v.(int)
371 if v, ok := d.GetOk("port"); ok {
374 if v, ok := d.GetOk("trigger_rate"); ok {
375 test.TriggerRate = v.(int)
377 if v, ok := d.GetOk("custom_header"); ok {
378 test.CustomHeader = v.(string)
380 if v, ok := d.GetOk("user_agent"); ok {
381 test.UserAgent = v.(string)
383 if v, ok := d.GetOk("node_locations"); ok {
384 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
386 if v, ok := d.GetOk("ping_url"); ok {
387 test.PingURL = v.(string)
389 if v, ok := d.GetOk("basic_user"); ok {
390 test.BasicUser = v.(string)
392 if v, ok := d.GetOk("basic_pass"); ok {
393 test.BasicPass = v.(string)
395 if v, ok := d.GetOk("public"); ok {
396 test.Public = v.(int)
398 if v, ok := d.GetOk("logo_image"); ok {
399 test.LogoImage = v.(string)
401 if v, ok := d.GetOk("branding"); ok {
402 test.Branding = v.(int)
404 if v, ok := d.GetOk("website_host"); ok {
405 test.WebsiteHost = v.(string)
407 if v, ok := d.GetOk("virus"); ok {
410 if v, ok := d.GetOk("find_string"); ok {
411 test.FindString = v.(string)
413 if v, ok := d.GetOk("do_not_find"); ok {
414 test.DoNotFind = v.(bool)
416 if v, ok := d.GetOk("real_browser"); ok {
417 test.RealBrowser = v.(int)
419 if v, ok := d.GetOk("test_tags"); ok {
420 test.TestTags = v.(string)
422 if v, ok := d.GetOk("status_codes"); ok {
423 test.StatusCodes = v.(string)
425 if v, ok := d.GetOk("use_jar"); ok {
426 test.UseJar = v.(int)
428 if v, ok := d.GetOk("post_raw"); ok {
429 test.PostRaw = v.(string)
431 if v, ok := d.GetOk("final_endpoint"); ok {
432 test.FinalEndpoint = v.(string)
434 if v, ok := d.GetOk("follow_redirect"); ok {
435 test.FollowRedirect = v.(bool)