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,
182 Type: schema.TypeInt,
187 Type: schema.TypeString,
192 Type: schema.TypeString,
197 Type: schema.TypeBool,
204 func CreateTest(d *schema.ResourceData, meta interface{}) error {
205 client := meta.(*statuscake.Client)
207 newTest := &statuscake.Test{
208 WebsiteName: d.Get("website_name").(string),
209 WebsiteURL: d.Get("website_url").(string),
210 CheckRate: d.Get("check_rate").(int),
211 TestType: d.Get("test_type").(string),
212 Paused: d.Get("paused").(bool),
213 Timeout: d.Get("timeout").(int),
214 ContactID: d.Get("contact_id").(int),
215 Confirmation: d.Get("confirmations").(int),
216 Port: d.Get("port").(int),
217 TriggerRate: d.Get("trigger_rate").(int),
218 CustomHeader: d.Get("custom_header").(string),
219 UserAgent: d.Get("user_agent").(string),
220 Status: d.Get("status").(string),
221 Uptime: d.Get("uptime").(float64),
222 NodeLocations: castInterfaceToSliceStrings(d.Get("node_locations")),
223 PingURL: d.Get("ping_url").(string),
224 BasicUser: d.Get("basic_user").(string),
225 BasicPass: d.Get("basic_pass").(string),
226 Public: d.Get("public").(int),
227 LogoImage: d.Get("logo_image").(string),
228 Branding: d.Get("branding").(int),
229 WebsiteHost: d.Get("website_host").(string),
230 Virus: d.Get("virus").(int),
231 FindString: d.Get("find_string").(string),
232 DoNotFind: d.Get("do_not_find").(bool),
233 RealBrowser: d.Get("real_browser").(int),
234 TestTags: d.Get("test_tags").(string),
235 StatusCodes: d.Get("status_codes").(string),
236 UseJar: d.Get("use_jar").(int),
237 PostRaw: d.Get("post_raw").(string),
238 FinalEndpoint: d.Get("final_endpoint").(string),
239 FollowRedirect: d.Get("follow_redirect").(bool),
242 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
244 response, err := client.Tests().Update(newTest)
246 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
249 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
250 d.Set("status", response.Status)
251 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
252 d.SetId(fmt.Sprintf("%d", response.TestID))
254 return ReadTest(d, meta)
257 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
258 client := meta.(*statuscake.Client)
260 params := getStatusCakeTestInput(d)
262 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
263 _, err := client.Tests().Update(params)
265 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
270 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
271 client := meta.(*statuscake.Client)
273 testId, parseErr := strconv.Atoi(d.Id())
277 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
278 err := client.Tests().Delete(testId)
286 func ReadTest(d *schema.ResourceData, meta interface{}) error {
287 client := meta.(*statuscake.Client)
289 testId, parseErr := strconv.Atoi(d.Id())
293 testResp, err := client.Tests().Detail(testId)
295 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
297 d.Set("website_name", testResp.WebsiteName)
298 d.Set("website_url", testResp.WebsiteURL)
299 d.Set("check_rate", testResp.CheckRate)
300 d.Set("test_type", testResp.TestType)
301 d.Set("paused", testResp.Paused)
302 d.Set("timeout", testResp.Timeout)
303 d.Set("contact_id", testResp.ContactID)
304 d.Set("confirmations", testResp.Confirmation)
305 d.Set("port", testResp.Port)
306 d.Set("trigger_rate", testResp.TriggerRate)
307 d.Set("custom_header", testResp.CustomHeader)
308 d.Set("user_agent", testResp.UserAgent)
309 d.Set("status", testResp.Status)
310 d.Set("uptime", testResp.Uptime)
311 if err := d.Set("node_locations", testResp.NodeLocations); err != nil {
312 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
314 d.Set("ping_url", testResp.PingURL)
315 d.Set("basic_user", testResp.BasicUser)
316 d.Set("basic_pass", testResp.BasicPass)
317 d.Set("public", testResp.Public)
318 d.Set("logo_image", testResp.LogoImage)
319 d.Set("branding", testResp.Branding)
320 d.Set("website_host", testResp.WebsiteHost)
321 d.Set("virus", testResp.Virus)
322 d.Set("find_string", testResp.FindString)
323 d.Set("do_not_find", testResp.DoNotFind)
324 d.Set("real_browser", testResp.RealBrowser)
325 d.Set("test_tags", testResp.TestTags)
326 d.Set("status_codes", testResp.StatusCodes)
327 d.Set("use_jar", testResp.UseJar)
328 d.Set("post_raw", testResp.PostRaw)
329 d.Set("final_endpoint", testResp.FinalEndpoint)
330 d.Set("follow_redirect", testResp.FollowRedirect)
335 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
336 testId, parseErr := strconv.Atoi(d.Id())
338 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
340 test := &statuscake.Test{
343 if v, ok := d.GetOk("website_name"); ok {
344 test.WebsiteName = v.(string)
346 if v, ok := d.GetOk("website_url"); ok {
347 test.WebsiteURL = v.(string)
349 if v, ok := d.GetOk("check_rate"); ok {
350 test.CheckRate = v.(int)
352 if v, ok := d.GetOk("contact_id"); ok {
353 test.ContactID = v.(int)
355 if v, ok := d.GetOk("test_type"); ok {
356 test.TestType = v.(string)
358 if v, ok := d.GetOk("paused"); ok {
359 test.Paused = v.(bool)
361 if v, ok := d.GetOk("timeout"); ok {
362 test.Timeout = v.(int)
364 if v, ok := d.GetOk("confirmations"); ok {
365 test.Confirmation = v.(int)
367 if v, ok := d.GetOk("port"); ok {
370 if v, ok := d.GetOk("trigger_rate"); ok {
371 test.TriggerRate = v.(int)
373 if v, ok := d.GetOk("custom_header"); ok {
374 test.CustomHeader = v.(string)
376 if v, ok := d.GetOk("user_agent"); ok {
377 test.UserAgent = v.(string)
379 if v, ok := d.GetOk("node_locations"); ok {
380 test.NodeLocations = castInterfaceToSliceStrings(v)
382 if v, ok := d.GetOk("ping_url"); ok {
383 test.PingURL = v.(string)
385 if v, ok := d.GetOk("basic_user"); ok {
386 test.BasicUser = v.(string)
388 if v, ok := d.GetOk("basic_pass"); ok {
389 test.BasicPass = v.(string)
391 if v, ok := d.GetOk("public"); ok {
392 test.Public = v.(int)
394 if v, ok := d.GetOk("logo_image"); ok {
395 test.LogoImage = v.(string)
397 if v, ok := d.GetOk("branding"); ok {
398 test.Branding = v.(int)
400 if v, ok := d.GetOk("website_host"); ok {
401 test.WebsiteHost = v.(string)
403 if v, ok := d.GetOk("virus"); ok {
406 if v, ok := d.GetOk("find_string"); ok {
407 test.FindString = v.(string)
409 if v, ok := d.GetOk("do_not_find"); ok {
410 test.DoNotFind = v.(bool)
412 if v, ok := d.GetOk("real_browser"); ok {
413 test.RealBrowser = v.(int)
415 if v, ok := d.GetOk("test_tags"); ok {
416 test.TestTags = v.(string)
418 if v, ok := d.GetOk("status_codes"); ok {
419 test.StatusCodes = v.(string)
421 if v, ok := d.GetOk("use_jar"); ok {
422 test.UseJar = v.(int)
424 if v, ok := d.GetOk("post_raw"); ok {
425 test.PostRaw = v.(string)
427 if v, ok := d.GetOk("final_endpoint"); ok {
428 test.FinalEndpoint = v.(string)
430 if v, ok := d.GetOk("follow_redirect"); ok {
431 test.FollowRedirect = v.(bool)