]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - statuscake/resource_statuscaketest.go
Merge pull request #36 from nauxliu/importer
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscaketest.go
1 package statuscake
2
3 import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11 )
12
13 func castSetToSliceStrings(configured []interface{}) []string {
14 res := make([]string, len(configured))
15
16 for i, element := range configured {
17 res[i] = element.(string)
18 }
19 return res
20 }
21
22 // Special handling for node_locations since statuscake will return `[""]` for the empty case
23 func considerEmptyStringAsEmptyArray(in []string) []string {
24 if len(in) == 1 && in[0] == "" {
25 return []string{}
26 } else {
27 return in
28 }
29 }
30
31 func resourceStatusCakeTest() *schema.Resource {
32 return &schema.Resource{
33 Create: CreateTest,
34 Update: UpdateTest,
35 Delete: DeleteTest,
36 Read: ReadTest,
37 Importer: &schema.ResourceImporter{
38 State: schema.ImportStatePassthrough,
39 },
40
41 Schema: map[string]*schema.Schema{
42 "test_id": {
43 Type: schema.TypeString,
44 Computed: true,
45 },
46
47 "website_name": {
48 Type: schema.TypeString,
49 Required: true,
50 },
51
52 "website_url": {
53 Type: schema.TypeString,
54 Required: true,
55 },
56
57 "contact_group": {
58 Type: schema.TypeSet,
59 Elem: &schema.Schema{Type: schema.TypeString},
60 Optional: true,
61 Set: schema.HashString,
62 ConflictsWith: []string{"contact_id"},
63 },
64
65 "contact_id": {
66 Type: schema.TypeInt,
67 Optional: true,
68 ConflictsWith: []string{"contact_group"},
69 Deprecated: "use contact_group instead",
70 },
71
72 "check_rate": {
73 Type: schema.TypeInt,
74 Optional: true,
75 Default: 300,
76 },
77
78 "test_type": {
79 Type: schema.TypeString,
80 Required: true,
81 },
82
83 "paused": {
84 Type: schema.TypeBool,
85 Optional: true,
86 Default: false,
87 },
88
89 "timeout": {
90 Type: schema.TypeInt,
91 Optional: true,
92 Default: 40,
93 },
94
95 "confirmations": {
96 Type: schema.TypeInt,
97 Optional: true,
98 },
99
100 "port": {
101 Type: schema.TypeInt,
102 Optional: true,
103 },
104
105 "trigger_rate": {
106 Type: schema.TypeInt,
107 Optional: true,
108 Default: 5,
109 },
110
111 "custom_header": {
112 Type: schema.TypeString,
113 Optional: true,
114 },
115
116 "user_agent": {
117 Type: schema.TypeString,
118 Optional: true,
119 },
120
121 "status": {
122 Type: schema.TypeString,
123 Computed: true,
124 },
125
126 "uptime": {
127 Type: schema.TypeFloat,
128 Computed: true,
129 },
130
131 "node_locations": {
132 Type: schema.TypeSet,
133 Elem: &schema.Schema{Type: schema.TypeString},
134 Optional: true,
135 Set: schema.HashString,
136 },
137
138 "ping_url": {
139 Type: schema.TypeString,
140 Optional: true,
141 },
142
143 "basic_user": {
144 Type: schema.TypeString,
145 Optional: true,
146 },
147
148 "basic_pass": {
149 Type: schema.TypeString,
150 Optional: true,
151 Sensitive: true,
152 },
153
154 "public": {
155 Type: schema.TypeInt,
156 Optional: true,
157 },
158
159 "logo_image": {
160 Type: schema.TypeString,
161 Optional: true,
162 },
163
164 "branding": {
165 Type: schema.TypeInt,
166 Optional: true,
167 },
168
169 "website_host": {
170 Type: schema.TypeString,
171 Optional: true,
172 },
173
174 "virus": {
175 Type: schema.TypeInt,
176 Optional: true,
177 },
178
179 "find_string": {
180 Type: schema.TypeString,
181 Optional: true,
182 },
183
184 "do_not_find": {
185 Type: schema.TypeBool,
186 Optional: true,
187 },
188
189 "real_browser": {
190 Type: schema.TypeInt,
191 Optional: true,
192 },
193
194 "test_tags": {
195 Type: schema.TypeSet,
196 Elem: &schema.Schema{Type: schema.TypeString},
197 Optional: true,
198 Set: schema.HashString,
199 },
200
201 "status_codes": {
202 Type: schema.TypeString,
203 Optional: true,
204 },
205
206 "use_jar": {
207 Type: schema.TypeInt,
208 Optional: true,
209 },
210
211 "post_raw": {
212 Type: schema.TypeString,
213 Optional: true,
214 },
215
216 "final_endpoint": {
217 Type: schema.TypeString,
218 Optional: true,
219 },
220
221 "enable_ssl_alert": {
222 Type: schema.TypeBool,
223 Optional: true,
224 Default: false,
225 },
226
227 "follow_redirect": {
228 Type: schema.TypeBool,
229 Optional: true,
230 },
231 },
232 }
233 }
234
235 func CreateTest(d *schema.ResourceData, meta interface{}) error {
236 client := meta.(*statuscake.Client)
237
238 newTest := &statuscake.Test{
239 WebsiteName: d.Get("website_name").(string),
240 WebsiteURL: d.Get("website_url").(string),
241 CheckRate: d.Get("check_rate").(int),
242 TestType: d.Get("test_type").(string),
243 Paused: d.Get("paused").(bool),
244 Timeout: d.Get("timeout").(int),
245 Confirmation: d.Get("confirmations").(int),
246 Port: d.Get("port").(int),
247 TriggerRate: d.Get("trigger_rate").(int),
248 CustomHeader: d.Get("custom_header").(string),
249 UserAgent: d.Get("user_agent").(string),
250 Status: d.Get("status").(string),
251 Uptime: d.Get("uptime").(float64),
252 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
253 PingURL: d.Get("ping_url").(string),
254 BasicUser: d.Get("basic_user").(string),
255 BasicPass: d.Get("basic_pass").(string),
256 Public: d.Get("public").(int),
257 LogoImage: d.Get("logo_image").(string),
258 Branding: d.Get("branding").(int),
259 WebsiteHost: d.Get("website_host").(string),
260 Virus: d.Get("virus").(int),
261 FindString: d.Get("find_string").(string),
262 DoNotFind: d.Get("do_not_find").(bool),
263 RealBrowser: d.Get("real_browser").(int),
264 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
265 StatusCodes: d.Get("status_codes").(string),
266 UseJar: d.Get("use_jar").(int),
267 PostRaw: d.Get("post_raw").(string),
268 FinalEndpoint: d.Get("final_endpoint").(string),
269 EnableSSLAlert: d.Get("enable_ssl_alert").(bool),
270 FollowRedirect: d.Get("follow_redirect").(bool),
271 }
272
273 if v, ok := d.GetOk("contact_group"); ok {
274 newTest.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
275 } else if v, ok := d.GetOk("contact_id"); ok {
276 newTest.ContactID = v.(int)
277 }
278
279 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
280
281 response, err := client.Tests().Update(newTest)
282 if err != nil {
283 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
284 }
285
286 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
287 d.Set("status", response.Status)
288 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
289 d.SetId(fmt.Sprintf("%d", response.TestID))
290
291 return ReadTest(d, meta)
292 }
293
294 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
295 client := meta.(*statuscake.Client)
296
297 params := getStatusCakeTestInput(d)
298
299 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
300 _, err := client.Tests().Update(params)
301 if err != nil {
302 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
303 }
304 return nil
305 }
306
307 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
308 client := meta.(*statuscake.Client)
309
310 testId, parseErr := strconv.Atoi(d.Id())
311 if parseErr != nil {
312 return parseErr
313 }
314 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
315 err := client.Tests().Delete(testId)
316 if err != nil {
317 return err
318 }
319
320 return nil
321 }
322
323 func ReadTest(d *schema.ResourceData, meta interface{}) error {
324 client := meta.(*statuscake.Client)
325
326 testId, parseErr := strconv.Atoi(d.Id())
327 if parseErr != nil {
328 return parseErr
329 }
330 testResp, err := client.Tests().Detail(testId)
331 if err != nil {
332 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
333 }
334
335 if v, ok := d.GetOk("contact_group"); ok {
336 d.Set("contact_group", v)
337 } else if v, ok := d.GetOk("contact_id"); ok {
338 d.Set("contact_id", v)
339 }
340 d.Set("website_name", testResp.WebsiteName)
341 d.Set("website_url", testResp.WebsiteURL)
342 d.Set("check_rate", testResp.CheckRate)
343 d.Set("test_type", testResp.TestType)
344 d.Set("paused", testResp.Paused)
345 d.Set("timeout", testResp.Timeout)
346 d.Set("confirmations", testResp.Confirmation)
347 d.Set("port", testResp.Port)
348 d.Set("trigger_rate", testResp.TriggerRate)
349 d.Set("custom_header", testResp.CustomHeader)
350 d.Set("status", testResp.Status)
351 d.Set("uptime", testResp.Uptime)
352 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
353 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
354 }
355 d.Set("logo_image", testResp.LogoImage)
356 // Even after WebsiteHost is set, the API returns ""
357 // API docs aren't clear on usage will only override state if we get a non-empty value back
358 if testResp.WebsiteHost != "" {
359 d.Set("website_host", testResp.WebsiteHost)
360 }
361 d.Set("find_string", testResp.FindString)
362 d.Set("do_not_find", testResp.DoNotFind)
363 d.Set("status_codes", testResp.StatusCodes)
364 d.Set("use_jar", testResp.UseJar)
365 d.Set("post_raw", testResp.PostRaw)
366 d.Set("final_endpoint", testResp.FinalEndpoint)
367 d.Set("enable_ssl_alert", testResp.EnableSSLAlert)
368 d.Set("follow_redirect", testResp.FollowRedirect)
369
370 return nil
371 }
372
373 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
374 testId, parseErr := strconv.Atoi(d.Id())
375 if parseErr != nil {
376 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
377 }
378 test := &statuscake.Test{
379 TestID: testId,
380 }
381 if v, ok := d.GetOk("website_name"); ok {
382 test.WebsiteName = v.(string)
383 }
384 if v, ok := d.GetOk("website_url"); ok {
385 test.WebsiteURL = v.(string)
386 }
387 if v, ok := d.GetOk("check_rate"); ok {
388 test.CheckRate = v.(int)
389 }
390 if v, ok := d.GetOk("contact_group"); ok {
391 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
392 } else if v, ok := d.GetOk("contact_id"); ok {
393 test.ContactID = v.(int)
394 }
395 if v, ok := d.GetOk("test_type"); ok {
396 test.TestType = v.(string)
397 }
398 if v, ok := d.GetOk("paused"); ok {
399 test.Paused = v.(bool)
400 }
401 if v, ok := d.GetOk("timeout"); ok {
402 test.Timeout = v.(int)
403 }
404 if v, ok := d.GetOk("confirmations"); ok {
405 test.Confirmation = v.(int)
406 }
407 if v, ok := d.GetOk("port"); ok {
408 test.Port = v.(int)
409 }
410 if v, ok := d.GetOk("trigger_rate"); ok {
411 test.TriggerRate = v.(int)
412 }
413 if v, ok := d.GetOk("custom_header"); ok {
414 test.CustomHeader = v.(string)
415 }
416 if v, ok := d.GetOk("user_agent"); ok {
417 test.UserAgent = v.(string)
418 }
419 if v, ok := d.GetOk("node_locations"); ok {
420 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
421 }
422 if v, ok := d.GetOk("ping_url"); ok {
423 test.PingURL = v.(string)
424 }
425 if v, ok := d.GetOk("basic_user"); ok {
426 test.BasicUser = v.(string)
427 }
428 if v, ok := d.GetOk("basic_pass"); ok {
429 test.BasicPass = v.(string)
430 }
431 if v, ok := d.GetOk("public"); ok {
432 test.Public = v.(int)
433 }
434 if v, ok := d.GetOk("logo_image"); ok {
435 test.LogoImage = v.(string)
436 }
437 if v, ok := d.GetOk("branding"); ok {
438 test.Branding = v.(int)
439 }
440 if v, ok := d.GetOk("website_host"); ok {
441 test.WebsiteHost = v.(string)
442 }
443 if v, ok := d.GetOk("virus"); ok {
444 test.Virus = v.(int)
445 }
446 if v, ok := d.GetOk("find_string"); ok {
447 test.FindString = v.(string)
448 }
449 if v, ok := d.GetOk("do_not_find"); ok {
450 test.DoNotFind = v.(bool)
451 }
452 if v, ok := d.GetOk("real_browser"); ok {
453 test.RealBrowser = v.(int)
454 }
455 if v, ok := d.GetOk("test_tags"); ok {
456 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
457 }
458 if v, ok := d.GetOk("status_codes"); ok {
459 test.StatusCodes = v.(string)
460 }
461 if v, ok := d.GetOk("use_jar"); ok {
462 test.UseJar = v.(int)
463 }
464 if v, ok := d.GetOk("post_raw"); ok {
465 test.PostRaw = v.(string)
466 }
467 if v, ok := d.GetOk("final_endpoint"); ok {
468 test.FinalEndpoint = v.(string)
469 }
470 if v, ok := d.GetOk("enable_ssl_alert"); ok {
471 test.EnableSSLAlert = v.(bool)
472 }
473 if v, ok := d.GetOk("follow_redirect"); ok {
474 test.FollowRedirect = v.(bool)
475 }
476
477 return test
478 }