]>
Commit | Line | Data |
---|---|---|
478e1338 | 1 | package statuscake |
2 | ||
b8f05dfc | 3 | import ( |
4 | "fmt" | |
5 | "strconv" | |
6 | ||
7 | "log" | |
8 | ||
4eeeab64 | 9 | "github.com/DreamItGetIT/statuscake" |
b8f05dfc | 10 | "github.com/hashicorp/terraform/helper/schema" |
11 | ) | |
478e1338 | 12 | |
89027b6a MS |
13 | func castSetToSliceStrings(configured []interface{}) []string { |
14 | res := make([]string, len(configured)) | |
b0191999 | 15 | |
89027b6a | 16 | for i, element := range configured { |
b0191999 AG |
17 | res[i] = element.(string) |
18 | } | |
19 | return res | |
20 | } | |
21 | ||
89027b6a MS |
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 | ||
478e1338 | 31 | func resourceStatusCakeTest() *schema.Resource { |
32 | return &schema.Resource{ | |
33 | Create: CreateTest, | |
34 | Update: UpdateTest, | |
35 | Delete: DeleteTest, | |
36 | Read: ReadTest, | |
cfbd89c0 XL |
37 | Importer: &schema.ResourceImporter{ |
38 | State: schema.ImportStatePassthrough, | |
39 | }, | |
b8f05dfc | 40 | |
41 | Schema: map[string]*schema.Schema{ | |
90db3fb0 | 42 | "test_id": { |
b8f05dfc | 43 | Type: schema.TypeString, |
44 | Computed: true, | |
45 | }, | |
46 | ||
90db3fb0 | 47 | "website_name": { |
b8f05dfc | 48 | Type: schema.TypeString, |
49 | Required: true, | |
50 | }, | |
51 | ||
90db3fb0 | 52 | "website_url": { |
b8f05dfc | 53 | Type: schema.TypeString, |
54 | Required: true, | |
55 | }, | |
56 | ||
4efdab41 | 57 | "contact_group": { |
70c40d15 JCA |
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", | |
e7b04dd5 LJ |
70 | }, |
71 | ||
90db3fb0 | 72 | "check_rate": { |
b8f05dfc | 73 | Type: schema.TypeInt, |
74 | Optional: true, | |
75 | Default: 300, | |
76 | }, | |
77 | ||
90db3fb0 | 78 | "test_type": { |
b8f05dfc | 79 | Type: schema.TypeString, |
80 | Required: true, | |
81 | }, | |
82 | ||
90db3fb0 | 83 | "paused": { |
b8f05dfc | 84 | Type: schema.TypeBool, |
85 | Optional: true, | |
86 | Default: false, | |
87 | }, | |
6ad7af97 | 88 | |
90db3fb0 | 89 | "timeout": { |
7dec75fc | 90 | Type: schema.TypeInt, |
6ad7af97 RG |
91 | Optional: true, |
92 | Default: 40, | |
7dec75fc | 93 | }, |
6ad7af97 | 94 | |
90db3fb0 PS |
95 | "confirmations": { |
96 | Type: schema.TypeInt, | |
97 | Optional: true, | |
98 | }, | |
99 | ||
100 | "port": { | |
b5815405 SC |
101 | Type: schema.TypeInt, |
102 | Optional: true, | |
103 | }, | |
0e962b8e RG |
104 | |
105 | "trigger_rate": { | |
106 | Type: schema.TypeInt, | |
107 | Optional: true, | |
108 | Default: 5, | |
109 | }, | |
bc2bde6b | 110 | |
754f1420 AG |
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, | |
a6097499 | 123 | Computed: true, |
754f1420 AG |
124 | }, |
125 | ||
126 | "uptime": { | |
127 | Type: schema.TypeFloat, | |
a6097499 | 128 | Computed: true, |
754f1420 AG |
129 | }, |
130 | ||
131 | "node_locations": { | |
89027b6a | 132 | Type: schema.TypeSet, |
754f1420 AG |
133 | Elem: &schema.Schema{Type: schema.TypeString}, |
134 | Optional: true, | |
89027b6a | 135 | Set: schema.HashString, |
754f1420 AG |
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": { | |
ed5e20e8 MS |
149 | Type: schema.TypeString, |
150 | Optional: true, | |
151 | Sensitive: true, | |
754f1420 AG |
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": { | |
bc2bde6b MS |
195 | Type: schema.TypeSet, |
196 | Elem: &schema.Schema{Type: schema.TypeString}, | |
754f1420 | 197 | Optional: true, |
bc2bde6b | 198 | Set: schema.HashString, |
754f1420 AG |
199 | }, |
200 | ||
201 | "status_codes": { | |
202 | Type: schema.TypeString, | |
203 | Optional: true, | |
754f1420 AG |
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 | "follow_redirect": { | |
222 | Type: schema.TypeBool, | |
223 | Optional: true, | |
224 | }, | |
b8f05dfc | 225 | }, |
478e1338 | 226 | } |
227 | } | |
228 | ||
229 | func CreateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 230 | client := meta.(*statuscake.Client) |
b8f05dfc | 231 | |
4eeeab64 | 232 | newTest := &statuscake.Test{ |
754f1420 AG |
233 | WebsiteName: d.Get("website_name").(string), |
234 | WebsiteURL: d.Get("website_url").(string), | |
235 | CheckRate: d.Get("check_rate").(int), | |
236 | TestType: d.Get("test_type").(string), | |
237 | Paused: d.Get("paused").(bool), | |
238 | Timeout: d.Get("timeout").(int), | |
754f1420 AG |
239 | Confirmation: d.Get("confirmations").(int), |
240 | Port: d.Get("port").(int), | |
241 | TriggerRate: d.Get("trigger_rate").(int), | |
242 | CustomHeader: d.Get("custom_header").(string), | |
243 | UserAgent: d.Get("user_agent").(string), | |
244 | Status: d.Get("status").(string), | |
245 | Uptime: d.Get("uptime").(float64), | |
89027b6a | 246 | NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()), |
754f1420 AG |
247 | PingURL: d.Get("ping_url").(string), |
248 | BasicUser: d.Get("basic_user").(string), | |
249 | BasicPass: d.Get("basic_pass").(string), | |
250 | Public: d.Get("public").(int), | |
251 | LogoImage: d.Get("logo_image").(string), | |
252 | Branding: d.Get("branding").(int), | |
253 | WebsiteHost: d.Get("website_host").(string), | |
254 | Virus: d.Get("virus").(int), | |
255 | FindString: d.Get("find_string").(string), | |
256 | DoNotFind: d.Get("do_not_find").(bool), | |
257 | RealBrowser: d.Get("real_browser").(int), | |
bc2bde6b | 258 | TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()), |
754f1420 AG |
259 | StatusCodes: d.Get("status_codes").(string), |
260 | UseJar: d.Get("use_jar").(int), | |
261 | PostRaw: d.Get("post_raw").(string), | |
262 | FinalEndpoint: d.Get("final_endpoint").(string), | |
263 | FollowRedirect: d.Get("follow_redirect").(bool), | |
b8f05dfc | 264 | } |
265 | ||
70c40d15 JCA |
266 | if v, ok := d.GetOk("contact_group"); ok { |
267 | newTest.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List()) | |
268 | } else if v, ok := d.GetOk("contact_id"); ok { | |
269 | newTest.ContactID = v.(int) | |
270 | } | |
271 | ||
b8f05dfc | 272 | log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) |
273 | ||
32574941 | 274 | response, err := client.Tests().Update(newTest) |
b8f05dfc | 275 | if err != nil { |
276 | return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) | |
277 | } | |
278 | ||
279 | d.Set("test_id", fmt.Sprintf("%d", response.TestID)) | |
a6097499 AG |
280 | d.Set("status", response.Status) |
281 | d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime)) | |
b8f05dfc | 282 | d.SetId(fmt.Sprintf("%d", response.TestID)) |
283 | ||
7dec75fc | 284 | return ReadTest(d, meta) |
478e1338 | 285 | } |
286 | ||
287 | func UpdateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 288 | client := meta.(*statuscake.Client) |
7dec75fc | 289 | |
290 | params := getStatusCakeTestInput(d) | |
291 | ||
292 | log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) | |
32574941 | 293 | _, err := client.Tests().Update(params) |
7dec75fc | 294 | if err != nil { |
295 | return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) | |
296 | } | |
478e1338 | 297 | return nil |
298 | } | |
299 | ||
300 | func DeleteTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 301 | client := meta.(*statuscake.Client) |
b8f05dfc | 302 | |
303 | testId, parseErr := strconv.Atoi(d.Id()) | |
304 | if parseErr != nil { | |
305 | return parseErr | |
306 | } | |
b8f05dfc | 307 | log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) |
7dec75fc | 308 | err := client.Tests().Delete(testId) |
b8f05dfc | 309 | if err != nil { |
310 | return err | |
311 | } | |
312 | ||
478e1338 | 313 | return nil |
314 | } | |
315 | ||
316 | func ReadTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 317 | client := meta.(*statuscake.Client) |
7dec75fc | 318 | |
319 | testId, parseErr := strconv.Atoi(d.Id()) | |
320 | if parseErr != nil { | |
321 | return parseErr | |
322 | } | |
32574941 | 323 | testResp, err := client.Tests().Detail(testId) |
7dec75fc | 324 | if err != nil { |
325 | return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) | |
326 | } | |
70c40d15 JCA |
327 | |
328 | if v, ok := d.GetOk("contact_group"); ok { | |
329 | d.Set("contact_group", v) | |
330 | } else if v, ok := d.GetOk("contact_id"); ok { | |
331 | d.Set("contact_id", v) | |
332 | } | |
c0701076 RG |
333 | d.Set("website_name", testResp.WebsiteName) |
334 | d.Set("website_url", testResp.WebsiteURL) | |
7dec75fc | 335 | d.Set("check_rate", testResp.CheckRate) |
c0701076 RG |
336 | d.Set("test_type", testResp.TestType) |
337 | d.Set("paused", testResp.Paused) | |
338 | d.Set("timeout", testResp.Timeout) | |
b5815405 | 339 | d.Set("confirmations", testResp.Confirmation) |
90db3fb0 | 340 | d.Set("port", testResp.Port) |
0e962b8e | 341 | d.Set("trigger_rate", testResp.TriggerRate) |
754f1420 | 342 | d.Set("custom_header", testResp.CustomHeader) |
754f1420 AG |
343 | d.Set("status", testResp.Status) |
344 | d.Set("uptime", testResp.Uptime) | |
89027b6a | 345 | if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil { |
b0191999 AG |
346 | return fmt.Errorf("[WARN] Error setting node locations: %s", err) |
347 | } | |
754f1420 | 348 | d.Set("logo_image", testResp.LogoImage) |
ef20d8d7 MS |
349 | // Even after WebsiteHost is set, the API returns "" |
350 | // API docs aren't clear on usage will only override state if we get a non-empty value back | |
351 | if testResp.WebsiteHost != "" { | |
352 | d.Set("website_host", testResp.WebsiteHost) | |
353 | } | |
754f1420 AG |
354 | d.Set("find_string", testResp.FindString) |
355 | d.Set("do_not_find", testResp.DoNotFind) | |
754f1420 AG |
356 | d.Set("status_codes", testResp.StatusCodes) |
357 | d.Set("use_jar", testResp.UseJar) | |
358 | d.Set("post_raw", testResp.PostRaw) | |
359 | d.Set("final_endpoint", testResp.FinalEndpoint) | |
360 | d.Set("follow_redirect", testResp.FollowRedirect) | |
7dec75fc | 361 | |
478e1338 | 362 | return nil |
363 | } | |
7dec75fc | 364 | |
4eeeab64 | 365 | func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { |
7dec75fc | 366 | testId, parseErr := strconv.Atoi(d.Id()) |
367 | if parseErr != nil { | |
368 | log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) | |
369 | } | |
4eeeab64 | 370 | test := &statuscake.Test{ |
7dec75fc | 371 | TestID: testId, |
372 | } | |
373 | if v, ok := d.GetOk("website_name"); ok { | |
374 | test.WebsiteName = v.(string) | |
375 | } | |
376 | if v, ok := d.GetOk("website_url"); ok { | |
377 | test.WebsiteURL = v.(string) | |
378 | } | |
379 | if v, ok := d.GetOk("check_rate"); ok { | |
380 | test.CheckRate = v.(int) | |
381 | } | |
4efdab41 MS |
382 | if v, ok := d.GetOk("contact_group"); ok { |
383 | test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List()) | |
70c40d15 JCA |
384 | } else if v, ok := d.GetOk("contact_id"); ok { |
385 | test.ContactID = v.(int) | |
e7b04dd5 | 386 | } |
7dec75fc | 387 | if v, ok := d.GetOk("test_type"); ok { |
388 | test.TestType = v.(string) | |
389 | } | |
390 | if v, ok := d.GetOk("paused"); ok { | |
391 | test.Paused = v.(bool) | |
392 | } | |
393 | if v, ok := d.GetOk("timeout"); ok { | |
394 | test.Timeout = v.(int) | |
395 | } | |
b5815405 SC |
396 | if v, ok := d.GetOk("confirmations"); ok { |
397 | test.Confirmation = v.(int) | |
398 | } | |
90db3fb0 PS |
399 | if v, ok := d.GetOk("port"); ok { |
400 | test.Port = v.(int) | |
401 | } | |
0e962b8e RG |
402 | if v, ok := d.GetOk("trigger_rate"); ok { |
403 | test.TriggerRate = v.(int) | |
404 | } | |
754f1420 AG |
405 | if v, ok := d.GetOk("custom_header"); ok { |
406 | test.CustomHeader = v.(string) | |
407 | } | |
408 | if v, ok := d.GetOk("user_agent"); ok { | |
409 | test.UserAgent = v.(string) | |
410 | } | |
754f1420 | 411 | if v, ok := d.GetOk("node_locations"); ok { |
89027b6a | 412 | test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List()) |
754f1420 AG |
413 | } |
414 | if v, ok := d.GetOk("ping_url"); ok { | |
415 | test.PingURL = v.(string) | |
416 | } | |
417 | if v, ok := d.GetOk("basic_user"); ok { | |
418 | test.BasicUser = v.(string) | |
419 | } | |
420 | if v, ok := d.GetOk("basic_pass"); ok { | |
421 | test.BasicPass = v.(string) | |
422 | } | |
423 | if v, ok := d.GetOk("public"); ok { | |
424 | test.Public = v.(int) | |
425 | } | |
426 | if v, ok := d.GetOk("logo_image"); ok { | |
427 | test.LogoImage = v.(string) | |
428 | } | |
429 | if v, ok := d.GetOk("branding"); ok { | |
430 | test.Branding = v.(int) | |
431 | } | |
432 | if v, ok := d.GetOk("website_host"); ok { | |
433 | test.WebsiteHost = v.(string) | |
434 | } | |
435 | if v, ok := d.GetOk("virus"); ok { | |
436 | test.Virus = v.(int) | |
437 | } | |
438 | if v, ok := d.GetOk("find_string"); ok { | |
439 | test.FindString = v.(string) | |
440 | } | |
441 | if v, ok := d.GetOk("do_not_find"); ok { | |
442 | test.DoNotFind = v.(bool) | |
443 | } | |
444 | if v, ok := d.GetOk("real_browser"); ok { | |
445 | test.RealBrowser = v.(int) | |
446 | } | |
447 | if v, ok := d.GetOk("test_tags"); ok { | |
bc2bde6b | 448 | test.TestTags = castSetToSliceStrings(v.(*schema.Set).List()) |
754f1420 AG |
449 | } |
450 | if v, ok := d.GetOk("status_codes"); ok { | |
451 | test.StatusCodes = v.(string) | |
452 | } | |
453 | if v, ok := d.GetOk("use_jar"); ok { | |
454 | test.UseJar = v.(int) | |
455 | } | |
456 | if v, ok := d.GetOk("post_raw"); ok { | |
457 | test.PostRaw = v.(string) | |
458 | } | |
459 | if v, ok := d.GetOk("final_endpoint"); ok { | |
460 | test.FinalEndpoint = v.(string) | |
461 | } | |
462 | if v, ok := d.GetOk("follow_redirect"); ok { | |
463 | test.FollowRedirect = v.(bool) | |
464 | } | |
2a6605e9 | 465 | |
7dec75fc | 466 | return test |
467 | } |