9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
13 func castInterfaceToSliceStrings(in interface{}) []string {
14 input := in.([]interface{})
15 res := make([]string, len(input))
17 for i, element := range input {
18 res[i] = element.(string)
23 func resourceStatusCakeTest() *schema.Resource {
24 return &schema.Resource{
30 Schema: map[string]*schema.Schema{
32 Type: schema.TypeString,
37 Type: schema.TypeString,
42 Type: schema.TypeString,
58 Type: schema.TypeString,
63 Type: schema.TypeBool,
90 Type: schema.TypeString,
95 Type: schema.TypeString,
100 Type: schema.TypeString,
105 Type: schema.TypeFloat,
110 Type: schema.TypeList,
111 Elem: &schema.Schema{Type: schema.TypeString},
116 Type: schema.TypeString,
121 Type: schema.TypeString,
126 Type: schema.TypeString,
131 Type: schema.TypeInt,
136 Type: schema.TypeString,
141 Type: schema.TypeInt,
146 Type: schema.TypeString,
151 Type: schema.TypeInt,
156 Type: schema.TypeString,
161 Type: schema.TypeBool,
166 Type: schema.TypeInt,
171 Type: schema.TypeString,
176 Type: schema.TypeString,
178 Default: "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
179 "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
180 "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
185 Type: schema.TypeInt,
190 Type: schema.TypeString,
195 Type: schema.TypeString,
200 Type: schema.TypeBool,
207 func CreateTest(d *schema.ResourceData, meta interface{}) error {
208 client := meta.(*statuscake.Client)
210 newTest := &statuscake.Test{
211 WebsiteName: d.Get("website_name").(string),
212 WebsiteURL: d.Get("website_url").(string),
213 CheckRate: d.Get("check_rate").(int),
214 TestType: d.Get("test_type").(string),
215 Paused: d.Get("paused").(bool),
216 Timeout: d.Get("timeout").(int),
217 ContactID: d.Get("contact_id").(int),
218 Confirmation: d.Get("confirmations").(int),
219 Port: d.Get("port").(int),
220 TriggerRate: d.Get("trigger_rate").(int),
221 CustomHeader: d.Get("custom_header").(string),
222 UserAgent: d.Get("user_agent").(string),
223 Status: d.Get("status").(string),
224 Uptime: d.Get("uptime").(float64),
225 NodeLocations: castInterfaceToSliceStrings(d.Get("node_locations")),
226 PingURL: d.Get("ping_url").(string),
227 BasicUser: d.Get("basic_user").(string),
228 BasicPass: d.Get("basic_pass").(string),
229 Public: d.Get("public").(int),
230 LogoImage: d.Get("logo_image").(string),
231 Branding: d.Get("branding").(int),
232 WebsiteHost: d.Get("website_host").(string),
233 Virus: d.Get("virus").(int),
234 FindString: d.Get("find_string").(string),
235 DoNotFind: d.Get("do_not_find").(bool),
236 RealBrowser: d.Get("real_browser").(int),
237 TestTags: d.Get("test_tags").(string),
238 StatusCodes: d.Get("status_codes").(string),
239 UseJar: d.Get("use_jar").(int),
240 PostRaw: d.Get("post_raw").(string),
241 FinalEndpoint: d.Get("final_endpoint").(string),
242 FollowRedirect: d.Get("follow_redirect").(bool),
245 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
247 response, err := client.Tests().Update(newTest)
249 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
252 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
253 d.Set("status", response.Status)
254 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
255 d.SetId(fmt.Sprintf("%d", response.TestID))
257 return ReadTest(d, meta)
260 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
261 client := meta.(*statuscake.Client)
263 params := getStatusCakeTestInput(d)
265 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
266 _, err := client.Tests().Update(params)
268 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
273 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
274 client := meta.(*statuscake.Client)
276 testId, parseErr := strconv.Atoi(d.Id())
280 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
281 err := client.Tests().Delete(testId)
289 func ReadTest(d *schema.ResourceData, meta interface{}) error {
290 client := meta.(*statuscake.Client)
292 testId, parseErr := strconv.Atoi(d.Id())
296 testResp, err := client.Tests().Detail(testId)
298 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
300 d.Set("website_name", testResp.WebsiteName)
301 d.Set("website_url", testResp.WebsiteURL)
302 d.Set("check_rate", testResp.CheckRate)
303 d.Set("test_type", testResp.TestType)
304 d.Set("paused", testResp.Paused)
305 d.Set("timeout", testResp.Timeout)
306 d.Set("contact_id", testResp.ContactID)
307 d.Set("confirmations", testResp.Confirmation)
308 d.Set("port", testResp.Port)
309 d.Set("trigger_rate", testResp.TriggerRate)
310 d.Set("custom_header", testResp.CustomHeader)
311 d.Set("user_agent", testResp.UserAgent)
312 d.Set("status", testResp.Status)
313 d.Set("uptime", testResp.Uptime)
314 if err := d.Set("node_locations", testResp.NodeLocations); err != nil {
315 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
317 d.Set("ping_url", testResp.PingURL)
318 d.Set("basic_user", testResp.BasicUser)
319 d.Set("basic_pass", testResp.BasicPass)
320 d.Set("public", testResp.Public)
321 d.Set("logo_image", testResp.LogoImage)
322 d.Set("branding", testResp.Branding)
323 d.Set("website_host", testResp.WebsiteHost)
324 d.Set("virus", testResp.Virus)
325 d.Set("find_string", testResp.FindString)
326 d.Set("do_not_find", testResp.DoNotFind)
327 d.Set("real_browser", testResp.RealBrowser)
328 d.Set("test_tags", testResp.TestTags)
329 d.Set("status_codes", testResp.StatusCodes)
330 d.Set("use_jar", testResp.UseJar)
331 d.Set("post_raw", testResp.PostRaw)
332 d.Set("final_endpoint", testResp.FinalEndpoint)
333 d.Set("follow_redirect", testResp.FollowRedirect)
338 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
339 testId, parseErr := strconv.Atoi(d.Id())
341 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
343 test := &statuscake.Test{
346 if v, ok := d.GetOk("website_name"); ok {
347 test.WebsiteName = v.(string)
349 if v, ok := d.GetOk("website_url"); ok {
350 test.WebsiteURL = v.(string)
352 if v, ok := d.GetOk("check_rate"); ok {
353 test.CheckRate = v.(int)
355 if v, ok := d.GetOk("contact_id"); ok {
356 test.ContactID = v.(int)
358 if v, ok := d.GetOk("test_type"); ok {
359 test.TestType = v.(string)
361 if v, ok := d.GetOk("paused"); ok {
362 test.Paused = v.(bool)
364 if v, ok := d.GetOk("timeout"); ok {
365 test.Timeout = v.(int)
367 if v, ok := d.GetOk("confirmations"); ok {
368 test.Confirmation = v.(int)
370 if v, ok := d.GetOk("port"); ok {
373 if v, ok := d.GetOk("trigger_rate"); ok {
374 test.TriggerRate = v.(int)
376 if v, ok := d.GetOk("custom_header"); ok {
377 test.CustomHeader = v.(string)
379 if v, ok := d.GetOk("user_agent"); ok {
380 test.UserAgent = v.(string)
382 if v, ok := d.GetOk("node_locations"); ok {
383 test.NodeLocations = castInterfaceToSliceStrings(v)
385 if v, ok := d.GetOk("ping_url"); ok {
386 test.PingURL = v.(string)
388 if v, ok := d.GetOk("basic_user"); ok {
389 test.BasicUser = v.(string)
391 if v, ok := d.GetOk("basic_pass"); ok {
392 test.BasicPass = v.(string)
394 if v, ok := d.GetOk("public"); ok {
395 test.Public = v.(int)
397 if v, ok := d.GetOk("logo_image"); ok {
398 test.LogoImage = v.(string)
400 if v, ok := d.GetOk("branding"); ok {
401 test.Branding = v.(int)
403 if v, ok := d.GetOk("website_host"); ok {
404 test.WebsiteHost = v.(string)
406 if v, ok := d.GetOk("virus"); ok {
409 if v, ok := d.GetOk("find_string"); ok {
410 test.FindString = v.(string)
412 if v, ok := d.GetOk("do_not_find"); ok {
413 test.DoNotFind = v.(bool)
415 if v, ok := d.GetOk("real_browser"); ok {
416 test.RealBrowser = v.(int)
418 if v, ok := d.GetOk("test_tags"); ok {
419 test.TestTags = v.(string)
421 if v, ok := d.GetOk("status_codes"); ok {
422 test.StatusCodes = v.(string)
424 if v, ok := d.GetOk("use_jar"); ok {
425 test.UseJar = v.(int)
427 if v, ok := d.GetOk("post_raw"); ok {
428 test.PostRaw = v.(string)
430 if v, ok := d.GetOk("final_endpoint"); ok {
431 test.FinalEndpoint = v.(string)
433 if v, ok := d.GetOk("follow_redirect"); ok {
434 test.FollowRedirect = v.(bool)