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.SetId(fmt.Sprintf("%d", response.TestID))
245 return ReadTest(d, meta)
248 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
249 client := meta.(*statuscake.Client)
251 params := getStatusCakeTestInput(d)
253 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
254 _, err := client.Tests().Update(params)
256 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
261 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
262 client := meta.(*statuscake.Client)
264 testId, parseErr := strconv.Atoi(d.Id())
268 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
269 err := client.Tests().Delete(testId)
277 func ReadTest(d *schema.ResourceData, meta interface{}) error {
278 client := meta.(*statuscake.Client)
280 testId, parseErr := strconv.Atoi(d.Id())
284 testResp, err := client.Tests().Detail(testId)
286 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
288 d.Set("website_name", testResp.WebsiteName)
289 d.Set("website_url", testResp.WebsiteURL)
290 d.Set("check_rate", testResp.CheckRate)
291 d.Set("test_type", testResp.TestType)
292 d.Set("paused", testResp.Paused)
293 d.Set("timeout", testResp.Timeout)
294 d.Set("contact_id", testResp.ContactID)
295 d.Set("confirmations", testResp.Confirmation)
296 d.Set("port", testResp.Port)
297 d.Set("trigger_rate", testResp.TriggerRate)
298 d.Set("custom_header", testResp.CustomHeader)
299 d.Set("user_agent", testResp.UserAgent)
300 d.Set("status", testResp.Status)
301 d.Set("uptime", testResp.Uptime)
302 d.Set("node_locations", testResp.NodeLocations)
303 d.Set("ping_url", testResp.PingURL)
304 d.Set("basic_user", testResp.BasicUser)
305 d.Set("basic_pass", testResp.BasicPass)
306 d.Set("public", testResp.Public)
307 d.Set("logo_image", testResp.LogoImage)
308 d.Set("branding", testResp.Branding)
309 d.Set("website_host", testResp.WebsiteHost)
310 d.Set("virus", testResp.Virus)
311 d.Set("find_string", testResp.FindString)
312 d.Set("do_not_find", testResp.DoNotFind)
313 d.Set("real_browser", testResp.RealBrowser)
314 d.Set("test_tags", testResp.TestTags)
315 d.Set("status_codes", testResp.StatusCodes)
316 d.Set("use_jar", testResp.UseJar)
317 d.Set("post_raw", testResp.PostRaw)
318 d.Set("final_endpoint", testResp.FinalEndpoint)
319 d.Set("follow_redirect", testResp.FollowRedirect)
324 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
325 testId, parseErr := strconv.Atoi(d.Id())
327 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
329 test := &statuscake.Test{
332 if v, ok := d.GetOk("website_name"); ok {
333 test.WebsiteName = v.(string)
335 if v, ok := d.GetOk("website_url"); ok {
336 test.WebsiteURL = v.(string)
338 if v, ok := d.GetOk("check_rate"); ok {
339 test.CheckRate = v.(int)
341 if v, ok := d.GetOk("contact_id"); ok {
342 test.ContactID = v.(string)
344 if v, ok := d.GetOk("test_type"); ok {
345 test.TestType = v.(string)
347 if v, ok := d.GetOk("paused"); ok {
348 test.Paused = v.(bool)
350 if v, ok := d.GetOk("timeout"); ok {
351 test.Timeout = v.(int)
353 if v, ok := d.GetOk("confirmations"); ok {
354 test.Confirmation = v.(int)
356 if v, ok := d.GetOk("port"); ok {
359 if v, ok := d.GetOk("trigger_rate"); ok {
360 test.TriggerRate = v.(int)
362 if v, ok := d.GetOk("custom_header"); ok {
363 test.CustomHeader = v.(string)
365 if v, ok := d.GetOk("user_agent"); ok {
366 test.UserAgent = v.(string)
368 if v, ok := d.GetOk("status"); ok {
369 test.Status = v.(string)
371 if v, ok := d.GetOk("uptime"); ok {
372 test.Uptime = v.(float64)
374 if v, ok := d.GetOk("node_locations"); ok {
375 test.NodeLocations = v.([]string)
377 if v, ok := d.GetOk("ping_url"); ok {
378 test.PingURL = v.(string)
380 if v, ok := d.GetOk("basic_user"); ok {
381 test.BasicUser = v.(string)
383 if v, ok := d.GetOk("basic_pass"); ok {
384 test.BasicPass = v.(string)
386 if v, ok := d.GetOk("public"); ok {
387 test.Public = v.(int)
389 if v, ok := d.GetOk("logo_image"); ok {
390 test.LogoImage = v.(string)
392 if v, ok := d.GetOk("branding"); ok {
393 test.Branding = v.(int)
395 if v, ok := d.GetOk("website_host"); ok {
396 test.WebsiteHost = v.(string)
398 if v, ok := d.GetOk("virus"); ok {
401 if v, ok := d.GetOk("find_string"); ok {
402 test.FindString = v.(string)
404 if v, ok := d.GetOk("do_not_find"); ok {
405 test.DoNotFind = v.(bool)
407 if v, ok := d.GetOk("real_browser"); ok {
408 test.RealBrowser = v.(int)
410 if v, ok := d.GetOk("test_tags"); ok {
411 test.TestTags = v.(string)
413 if v, ok := d.GetOk("status_codes"); ok {
414 test.StatusCodes = v.(string)
416 if v, ok := d.GetOk("use_jar"); ok {
417 test.UseJar = v.(int)
419 if v, ok := d.GetOk("post_raw"); ok {
420 test.PostRaw = v.(string)
422 if v, ok := d.GetOk("final_endpoint"); ok {
423 test.FinalEndpoint = v.(string)
425 if v, ok := d.GetOk("follow_redirect"); ok {
426 test.FollowRedirect = v.(bool)