9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
13 func resourceStatusCakeTest() *schema.Resource {
14 return &schema.Resource{
20 Schema: map[string]*schema.Schema{
22 Type: schema.TypeString,
27 Type: schema.TypeString,
32 Type: schema.TypeString,
37 Type: schema.TypeString,
48 Type: schema.TypeString,
53 Type: schema.TypeBool,
80 Type: schema.TypeString,
85 Type: schema.TypeString,
90 Type: schema.TypeString,
95 Type: schema.TypeFloat,
100 Type: schema.TypeList,
101 Elem: &schema.Schema{Type: schema.TypeString},
106 Type: schema.TypeString,
111 Type: schema.TypeString,
116 Type: schema.TypeString,
121 Type: schema.TypeInt,
126 Type: schema.TypeString,
131 Type: schema.TypeInt,
136 Type: schema.TypeString,
141 Type: schema.TypeInt,
146 Type: schema.TypeString,
151 Type: schema.TypeBool,
156 Type: schema.TypeInt,
161 Type: schema.TypeString,
166 Type: schema.TypeString,
168 Default: "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
169 "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
170 "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
175 Type: schema.TypeInt,
180 Type: schema.TypeString,
185 Type: schema.TypeString,
190 Type: schema.TypeBool,
197 func CreateTest(d *schema.ResourceData, meta interface{}) error {
198 client := meta.(*statuscake.Client)
200 newTest := &statuscake.Test{
201 WebsiteName: d.Get("website_name").(string),
202 WebsiteURL: d.Get("website_url").(string),
203 CheckRate: d.Get("check_rate").(int),
204 TestType: d.Get("test_type").(string),
205 Paused: d.Get("paused").(bool),
206 Timeout: d.Get("timeout").(int),
207 ContactID: d.Get("contact_id").(string),
208 Confirmation: d.Get("confirmations").(int),
209 Port: d.Get("port").(int),
210 TriggerRate: d.Get("trigger_rate").(int),
211 CustomHeader: d.Get("custom_header").(string),
212 UserAgent: d.Get("user_agent").(string),
213 Status: d.Get("status").(string),
214 Uptime: d.Get("uptime").(float64),
215 NodeLocations: d.Get("node_locations").([]string),
216 PingURL: d.Get("ping_url").(string),
217 BasicUser: d.Get("basic_user").(string),
218 BasicPass: d.Get("basic_pass").(string),
219 Public: d.Get("public").(int),
220 LogoImage: d.Get("logo_image").(string),
221 Branding: d.Get("branding").(int),
222 WebsiteHost: d.Get("website_host").(string),
223 Virus: d.Get("virus").(int),
224 FindString: d.Get("find_string").(string),
225 DoNotFind: d.Get("do_not_find").(bool),
226 RealBrowser: d.Get("real_browser").(int),
227 TestTags: d.Get("test_tags").(string),
228 StatusCodes: d.Get("status_codes").(string),
229 UseJar: d.Get("use_jar").(int),
230 PostRaw: d.Get("post_raw").(string),
231 FinalEndpoint: d.Get("final_endpoint").(string),
232 FollowRedirect: d.Get("follow_redirect").(bool),
235 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
237 response, err := client.Tests().Update(newTest)
239 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
242 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
243 d.Set("status", response.Status)
244 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
245 d.SetId(fmt.Sprintf("%d", response.TestID))
247 return ReadTest(d, meta)
250 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
251 client := meta.(*statuscake.Client)
253 params := getStatusCakeTestInput(d)
255 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
256 _, err := client.Tests().Update(params)
258 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
263 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
264 client := meta.(*statuscake.Client)
266 testId, parseErr := strconv.Atoi(d.Id())
270 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
271 err := client.Tests().Delete(testId)
279 func ReadTest(d *schema.ResourceData, meta interface{}) error {
280 client := meta.(*statuscake.Client)
282 testId, parseErr := strconv.Atoi(d.Id())
286 testResp, err := client.Tests().Detail(testId)
288 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
290 d.Set("website_name", testResp.WebsiteName)
291 d.Set("website_url", testResp.WebsiteURL)
292 d.Set("check_rate", testResp.CheckRate)
293 d.Set("test_type", testResp.TestType)
294 d.Set("paused", testResp.Paused)
295 d.Set("timeout", testResp.Timeout)
296 d.Set("contact_id", testResp.ContactID)
297 d.Set("confirmations", testResp.Confirmation)
298 d.Set("port", testResp.Port)
299 d.Set("trigger_rate", testResp.TriggerRate)
300 d.Set("custom_header", testResp.CustomHeader)
301 d.Set("user_agent", testResp.UserAgent)
302 d.Set("status", testResp.Status)
303 d.Set("uptime", testResp.Uptime)
304 d.Set("node_locations", testResp.NodeLocations)
305 d.Set("ping_url", testResp.PingURL)
306 d.Set("basic_user", testResp.BasicUser)
307 d.Set("basic_pass", testResp.BasicPass)
308 d.Set("public", testResp.Public)
309 d.Set("logo_image", testResp.LogoImage)
310 d.Set("branding", testResp.Branding)
311 d.Set("website_host", testResp.WebsiteHost)
312 d.Set("virus", testResp.Virus)
313 d.Set("find_string", testResp.FindString)
314 d.Set("do_not_find", testResp.DoNotFind)
315 d.Set("real_browser", testResp.RealBrowser)
316 d.Set("test_tags", testResp.TestTags)
317 d.Set("status_codes", testResp.StatusCodes)
318 d.Set("use_jar", testResp.UseJar)
319 d.Set("post_raw", testResp.PostRaw)
320 d.Set("final_endpoint", testResp.FinalEndpoint)
321 d.Set("follow_redirect", testResp.FollowRedirect)
326 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
327 testId, parseErr := strconv.Atoi(d.Id())
329 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
331 test := &statuscake.Test{
334 if v, ok := d.GetOk("website_name"); ok {
335 test.WebsiteName = v.(string)
337 if v, ok := d.GetOk("website_url"); ok {
338 test.WebsiteURL = v.(string)
340 if v, ok := d.GetOk("check_rate"); ok {
341 test.CheckRate = v.(int)
343 if v, ok := d.GetOk("contact_id"); ok {
344 test.ContactID = v.(string)
346 if v, ok := d.GetOk("test_type"); ok {
347 test.TestType = v.(string)
349 if v, ok := d.GetOk("paused"); ok {
350 test.Paused = v.(bool)
352 if v, ok := d.GetOk("timeout"); ok {
353 test.Timeout = v.(int)
355 if v, ok := d.GetOk("confirmations"); ok {
356 test.Confirmation = v.(int)
358 if v, ok := d.GetOk("port"); ok {
361 if v, ok := d.GetOk("trigger_rate"); ok {
362 test.TriggerRate = v.(int)
364 if v, ok := d.GetOk("custom_header"); ok {
365 test.CustomHeader = v.(string)
367 if v, ok := d.GetOk("user_agent"); ok {
368 test.UserAgent = v.(string)
370 if v, ok := d.GetOk("node_locations"); ok {
371 test.NodeLocations = v.([]string)
373 if v, ok := d.GetOk("ping_url"); ok {
374 test.PingURL = v.(string)
376 if v, ok := d.GetOk("basic_user"); ok {
377 test.BasicUser = v.(string)
379 if v, ok := d.GetOk("basic_pass"); ok {
380 test.BasicPass = v.(string)
382 if v, ok := d.GetOk("public"); ok {
383 test.Public = v.(int)
385 if v, ok := d.GetOk("logo_image"); ok {
386 test.LogoImage = v.(string)
388 if v, ok := d.GetOk("branding"); ok {
389 test.Branding = v.(int)
391 if v, ok := d.GetOk("website_host"); ok {
392 test.WebsiteHost = v.(string)
394 if v, ok := d.GetOk("virus"); ok {
397 if v, ok := d.GetOk("find_string"); ok {
398 test.FindString = v.(string)
400 if v, ok := d.GetOk("do_not_find"); ok {
401 test.DoNotFind = v.(bool)
403 if v, ok := d.GetOk("real_browser"); ok {
404 test.RealBrowser = v.(int)
406 if v, ok := d.GetOk("test_tags"); ok {
407 test.TestTags = v.(string)
409 if v, ok := d.GetOk("status_codes"); ok {
410 test.StatusCodes = v.(string)
412 if v, ok := d.GetOk("use_jar"); ok {
413 test.UseJar = v.(int)
415 if v, ok := d.GetOk("post_raw"); ok {
416 test.PostRaw = v.(string)
418 if v, ok := d.GetOk("final_endpoint"); ok {
419 test.FinalEndpoint = v.(string)
421 if v, ok := d.GetOk("follow_redirect"); ok {
422 test.FollowRedirect = v.(bool)