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,
132 Type: schema.TypeInt,
137 Type: schema.TypeString,
142 Type: schema.TypeInt,
147 Type: schema.TypeString,
152 Type: schema.TypeInt,
157 Type: schema.TypeString,
162 Type: schema.TypeBool,
167 Type: schema.TypeInt,
172 Type: schema.TypeString,
177 Type: schema.TypeString,
179 Default: "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
180 "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
181 "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
186 Type: schema.TypeInt,
191 Type: schema.TypeString,
196 Type: schema.TypeString,
201 Type: schema.TypeBool,
208 func CreateTest(d *schema.ResourceData, meta interface{}) error {
209 client := meta.(*statuscake.Client)
211 newTest := &statuscake.Test{
212 WebsiteName: d.Get("website_name").(string),
213 WebsiteURL: d.Get("website_url").(string),
214 CheckRate: d.Get("check_rate").(int),
215 TestType: d.Get("test_type").(string),
216 Paused: d.Get("paused").(bool),
217 Timeout: d.Get("timeout").(int),
218 ContactID: d.Get("contact_id").(int),
219 Confirmation: d.Get("confirmations").(int),
220 Port: d.Get("port").(int),
221 TriggerRate: d.Get("trigger_rate").(int),
222 CustomHeader: d.Get("custom_header").(string),
223 UserAgent: d.Get("user_agent").(string),
224 Status: d.Get("status").(string),
225 Uptime: d.Get("uptime").(float64),
226 NodeLocations: castInterfaceToSliceStrings(d.Get("node_locations")),
227 PingURL: d.Get("ping_url").(string),
228 BasicUser: d.Get("basic_user").(string),
229 BasicPass: d.Get("basic_pass").(string),
230 Public: d.Get("public").(int),
231 LogoImage: d.Get("logo_image").(string),
232 Branding: d.Get("branding").(int),
233 WebsiteHost: d.Get("website_host").(string),
234 Virus: d.Get("virus").(int),
235 FindString: d.Get("find_string").(string),
236 DoNotFind: d.Get("do_not_find").(bool),
237 RealBrowser: d.Get("real_browser").(int),
238 TestTags: d.Get("test_tags").(string),
239 StatusCodes: d.Get("status_codes").(string),
240 UseJar: d.Get("use_jar").(int),
241 PostRaw: d.Get("post_raw").(string),
242 FinalEndpoint: d.Get("final_endpoint").(string),
243 FollowRedirect: d.Get("follow_redirect").(bool),
246 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
248 response, err := client.Tests().Update(newTest)
250 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
253 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
254 d.Set("status", response.Status)
255 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
256 d.SetId(fmt.Sprintf("%d", response.TestID))
258 return ReadTest(d, meta)
261 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
262 client := meta.(*statuscake.Client)
264 params := getStatusCakeTestInput(d)
266 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
267 _, err := client.Tests().Update(params)
269 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
274 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
275 client := meta.(*statuscake.Client)
277 testId, parseErr := strconv.Atoi(d.Id())
281 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
282 err := client.Tests().Delete(testId)
290 func ReadTest(d *schema.ResourceData, meta interface{}) error {
291 client := meta.(*statuscake.Client)
293 testId, parseErr := strconv.Atoi(d.Id())
297 testResp, err := client.Tests().Detail(testId)
299 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
301 d.Set("website_name", testResp.WebsiteName)
302 d.Set("website_url", testResp.WebsiteURL)
303 d.Set("check_rate", testResp.CheckRate)
304 d.Set("test_type", testResp.TestType)
305 d.Set("paused", testResp.Paused)
306 d.Set("timeout", testResp.Timeout)
307 d.Set("contact_id", testResp.ContactID)
308 d.Set("confirmations", testResp.Confirmation)
309 d.Set("port", testResp.Port)
310 d.Set("trigger_rate", testResp.TriggerRate)
311 d.Set("custom_header", testResp.CustomHeader)
312 d.Set("user_agent", testResp.UserAgent)
313 d.Set("status", testResp.Status)
314 d.Set("uptime", testResp.Uptime)
315 if err := d.Set("node_locations", testResp.NodeLocations); err != nil {
316 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
318 d.Set("ping_url", testResp.PingURL)
319 d.Set("basic_user", testResp.BasicUser)
320 d.Set("basic_pass", testResp.BasicPass)
321 d.Set("public", testResp.Public)
322 d.Set("logo_image", testResp.LogoImage)
323 d.Set("branding", testResp.Branding)
324 d.Set("website_host", testResp.WebsiteHost)
325 d.Set("virus", testResp.Virus)
326 d.Set("find_string", testResp.FindString)
327 d.Set("do_not_find", testResp.DoNotFind)
328 d.Set("real_browser", testResp.RealBrowser)
329 d.Set("test_tags", testResp.TestTags)
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 = castInterfaceToSliceStrings(v)
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)