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,
68 Type: schema.TypeString,
73 Type: schema.TypeBool,
101 Type: schema.TypeString,
106 Type: schema.TypeString,
111 Type: schema.TypeString,
116 Type: schema.TypeFloat,
121 Type: schema.TypeSet,
122 Elem: &schema.Schema{Type: schema.TypeString},
124 Set: schema.HashString,
128 Type: schema.TypeString,
133 Type: schema.TypeString,
138 Type: schema.TypeString,
144 Type: schema.TypeInt,
149 Type: schema.TypeString,
154 Type: schema.TypeInt,
159 Type: schema.TypeString,
164 Type: schema.TypeInt,
169 Type: schema.TypeString,
174 Type: schema.TypeBool,
179 Type: schema.TypeInt,
184 Type: schema.TypeSet,
185 Elem: &schema.Schema{Type: schema.TypeString},
187 Set: schema.HashString,
191 Type: schema.TypeString,
196 Type: schema.TypeInt,
201 Type: schema.TypeString,
206 Type: schema.TypeString,
211 Type: schema.TypeBool,
218 func CreateTest(d *schema.ResourceData, meta interface{}) error {
219 client := meta.(*statuscake.Client)
221 newTest := &statuscake.Test{
222 WebsiteName: d.Get("website_name").(string),
223 WebsiteURL: d.Get("website_url").(string),
224 CheckRate: d.Get("check_rate").(int),
225 TestType: d.Get("test_type").(string),
226 Paused: d.Get("paused").(bool),
227 Timeout: d.Get("timeout").(int),
228 ContactGroup: castSetToSliceStrings(d.Get("contact_group").(*schema.Set).List()),
229 Confirmation: d.Get("confirmations").(int),
230 Port: d.Get("port").(int),
231 TriggerRate: d.Get("trigger_rate").(int),
232 CustomHeader: d.Get("custom_header").(string),
233 UserAgent: d.Get("user_agent").(string),
234 Status: d.Get("status").(string),
235 Uptime: d.Get("uptime").(float64),
236 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
237 PingURL: d.Get("ping_url").(string),
238 BasicUser: d.Get("basic_user").(string),
239 BasicPass: d.Get("basic_pass").(string),
240 Public: d.Get("public").(int),
241 LogoImage: d.Get("logo_image").(string),
242 Branding: d.Get("branding").(int),
243 WebsiteHost: d.Get("website_host").(string),
244 Virus: d.Get("virus").(int),
245 FindString: d.Get("find_string").(string),
246 DoNotFind: d.Get("do_not_find").(bool),
247 RealBrowser: d.Get("real_browser").(int),
248 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
249 StatusCodes: d.Get("status_codes").(string),
250 UseJar: d.Get("use_jar").(int),
251 PostRaw: d.Get("post_raw").(string),
252 FinalEndpoint: d.Get("final_endpoint").(string),
253 FollowRedirect: d.Get("follow_redirect").(bool),
256 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
258 response, err := client.Tests().Update(newTest)
260 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
263 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
264 d.Set("status", response.Status)
265 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
266 d.SetId(fmt.Sprintf("%d", response.TestID))
268 return ReadTest(d, meta)
271 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
272 client := meta.(*statuscake.Client)
274 params := getStatusCakeTestInput(d)
276 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
277 _, err := client.Tests().Update(params)
279 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
284 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
285 client := meta.(*statuscake.Client)
287 testId, parseErr := strconv.Atoi(d.Id())
291 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
292 err := client.Tests().Delete(testId)
300 func ReadTest(d *schema.ResourceData, meta interface{}) error {
301 client := meta.(*statuscake.Client)
303 testId, parseErr := strconv.Atoi(d.Id())
307 testResp, err := client.Tests().Detail(testId)
309 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
311 d.Set("website_name", testResp.WebsiteName)
312 d.Set("website_url", testResp.WebsiteURL)
313 d.Set("check_rate", testResp.CheckRate)
314 d.Set("test_type", testResp.TestType)
315 d.Set("paused", testResp.Paused)
316 d.Set("timeout", testResp.Timeout)
317 d.Set("contact_group", testResp.ContactGroup)
318 d.Set("confirmations", testResp.Confirmation)
319 d.Set("port", testResp.Port)
320 d.Set("trigger_rate", testResp.TriggerRate)
321 d.Set("custom_header", testResp.CustomHeader)
322 d.Set("status", testResp.Status)
323 d.Set("uptime", testResp.Uptime)
324 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
325 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
327 d.Set("logo_image", testResp.LogoImage)
328 // Even after WebsiteHost is set, the API returns ""
329 // API docs aren't clear on usage will only override state if we get a non-empty value back
330 if testResp.WebsiteHost != "" {
331 d.Set("website_host", testResp.WebsiteHost)
333 d.Set("find_string", testResp.FindString)
334 d.Set("do_not_find", testResp.DoNotFind)
335 d.Set("status_codes", testResp.StatusCodes)
336 d.Set("use_jar", testResp.UseJar)
337 d.Set("post_raw", testResp.PostRaw)
338 d.Set("final_endpoint", testResp.FinalEndpoint)
339 d.Set("follow_redirect", testResp.FollowRedirect)
344 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
345 testId, parseErr := strconv.Atoi(d.Id())
347 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
349 test := &statuscake.Test{
352 if v, ok := d.GetOk("website_name"); ok {
353 test.WebsiteName = v.(string)
355 if v, ok := d.GetOk("website_url"); ok {
356 test.WebsiteURL = v.(string)
358 if v, ok := d.GetOk("check_rate"); ok {
359 test.CheckRate = v.(int)
361 if v, ok := d.GetOk("contact_group"); ok {
362 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
364 if v, ok := d.GetOk("test_type"); ok {
365 test.TestType = v.(string)
367 if v, ok := d.GetOk("paused"); ok {
368 test.Paused = v.(bool)
370 if v, ok := d.GetOk("timeout"); ok {
371 test.Timeout = v.(int)
373 if v, ok := d.GetOk("confirmations"); ok {
374 test.Confirmation = v.(int)
376 if v, ok := d.GetOk("port"); ok {
379 if v, ok := d.GetOk("trigger_rate"); ok {
380 test.TriggerRate = v.(int)
382 if v, ok := d.GetOk("custom_header"); ok {
383 test.CustomHeader = v.(string)
385 if v, ok := d.GetOk("user_agent"); ok {
386 test.UserAgent = v.(string)
388 if v, ok := d.GetOk("node_locations"); ok {
389 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
391 if v, ok := d.GetOk("ping_url"); ok {
392 test.PingURL = v.(string)
394 if v, ok := d.GetOk("basic_user"); ok {
395 test.BasicUser = v.(string)
397 if v, ok := d.GetOk("basic_pass"); ok {
398 test.BasicPass = v.(string)
400 if v, ok := d.GetOk("public"); ok {
401 test.Public = v.(int)
403 if v, ok := d.GetOk("logo_image"); ok {
404 test.LogoImage = v.(string)
406 if v, ok := d.GetOk("branding"); ok {
407 test.Branding = v.(int)
409 if v, ok := d.GetOk("website_host"); ok {
410 test.WebsiteHost = v.(string)
412 if v, ok := d.GetOk("virus"); ok {
415 if v, ok := d.GetOk("find_string"); ok {
416 test.FindString = v.(string)
418 if v, ok := d.GetOk("do_not_find"); ok {
419 test.DoNotFind = v.(bool)
421 if v, ok := d.GetOk("real_browser"); ok {
422 test.RealBrowser = v.(int)
424 if v, ok := d.GetOk("test_tags"); ok {
425 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
427 if v, ok := d.GetOk("status_codes"); ok {
428 test.StatusCodes = v.(string)
430 if v, ok := d.GetOk("use_jar"); ok {
431 test.UseJar = v.(int)
433 if v, ok := d.GetOk("post_raw"); ok {
434 test.PostRaw = v.(string)
436 if v, ok := d.GetOk("final_endpoint"); ok {
437 test.FinalEndpoint = v.(string)
439 if v, ok := d.GetOk("follow_redirect"); ok {
440 test.FollowRedirect = v.(bool)