]>
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 | |
b0191999 AG |
13 | func castInterfaceToSliceStrings(in interface{}) []string { |
14 | input := in.([]interface{}) | |
15 | res := make([]string, len(input)) | |
16 | ||
17 | for i, element := range input { | |
18 | res[i] = element.(string) | |
19 | } | |
20 | return res | |
21 | } | |
22 | ||
478e1338 | 23 | func resourceStatusCakeTest() *schema.Resource { |
24 | return &schema.Resource{ | |
25 | Create: CreateTest, | |
26 | Update: UpdateTest, | |
27 | Delete: DeleteTest, | |
28 | Read: ReadTest, | |
b8f05dfc | 29 | |
30 | Schema: map[string]*schema.Schema{ | |
90db3fb0 | 31 | "test_id": { |
b8f05dfc | 32 | Type: schema.TypeString, |
33 | Computed: true, | |
34 | }, | |
35 | ||
90db3fb0 | 36 | "website_name": { |
b8f05dfc | 37 | Type: schema.TypeString, |
38 | Required: true, | |
39 | }, | |
40 | ||
90db3fb0 | 41 | "website_url": { |
b8f05dfc | 42 | Type: schema.TypeString, |
43 | Required: true, | |
44 | }, | |
45 | ||
90db3fb0 | 46 | "contact_id": { |
c9458806 | 47 | Type: schema.TypeInt, |
e7b04dd5 LJ |
48 | Optional: true, |
49 | }, | |
50 | ||
90db3fb0 | 51 | "check_rate": { |
b8f05dfc | 52 | Type: schema.TypeInt, |
53 | Optional: true, | |
54 | Default: 300, | |
55 | }, | |
56 | ||
90db3fb0 | 57 | "test_type": { |
b8f05dfc | 58 | Type: schema.TypeString, |
59 | Required: true, | |
60 | }, | |
61 | ||
90db3fb0 | 62 | "paused": { |
b8f05dfc | 63 | Type: schema.TypeBool, |
64 | Optional: true, | |
65 | Default: false, | |
66 | }, | |
6ad7af97 | 67 | |
90db3fb0 | 68 | "timeout": { |
7dec75fc | 69 | Type: schema.TypeInt, |
6ad7af97 RG |
70 | Optional: true, |
71 | Default: 40, | |
7dec75fc | 72 | }, |
6ad7af97 | 73 | |
90db3fb0 PS |
74 | "confirmations": { |
75 | Type: schema.TypeInt, | |
76 | Optional: true, | |
77 | }, | |
78 | ||
79 | "port": { | |
b5815405 SC |
80 | Type: schema.TypeInt, |
81 | Optional: true, | |
82 | }, | |
0e962b8e RG |
83 | |
84 | "trigger_rate": { | |
85 | Type: schema.TypeInt, | |
86 | Optional: true, | |
87 | Default: 5, | |
88 | }, | |
754f1420 AG |
89 | "custom_header": { |
90 | Type: schema.TypeString, | |
91 | Optional: true, | |
92 | }, | |
93 | ||
94 | "user_agent": { | |
95 | Type: schema.TypeString, | |
96 | Optional: true, | |
97 | }, | |
98 | ||
99 | "status": { | |
100 | Type: schema.TypeString, | |
a6097499 | 101 | Computed: true, |
754f1420 AG |
102 | }, |
103 | ||
104 | "uptime": { | |
105 | Type: schema.TypeFloat, | |
a6097499 | 106 | Computed: true, |
754f1420 AG |
107 | }, |
108 | ||
109 | "node_locations": { | |
110 | Type: schema.TypeList, | |
111 | Elem: &schema.Schema{Type: schema.TypeString}, | |
112 | Optional: true, | |
113 | }, | |
114 | ||
115 | "ping_url": { | |
116 | Type: schema.TypeString, | |
117 | Optional: true, | |
118 | }, | |
119 | ||
120 | "basic_user": { | |
121 | Type: schema.TypeString, | |
122 | Optional: true, | |
123 | }, | |
124 | ||
125 | "basic_pass": { | |
ed5e20e8 MS |
126 | Type: schema.TypeString, |
127 | Optional: true, | |
128 | Sensitive: true, | |
754f1420 AG |
129 | }, |
130 | ||
131 | "public": { | |
132 | Type: schema.TypeInt, | |
133 | Optional: true, | |
134 | }, | |
135 | ||
136 | "logo_image": { | |
137 | Type: schema.TypeString, | |
138 | Optional: true, | |
139 | }, | |
140 | ||
141 | "branding": { | |
142 | Type: schema.TypeInt, | |
143 | Optional: true, | |
144 | }, | |
145 | ||
146 | "website_host": { | |
147 | Type: schema.TypeString, | |
148 | Optional: true, | |
149 | }, | |
150 | ||
151 | "virus": { | |
152 | Type: schema.TypeInt, | |
153 | Optional: true, | |
154 | }, | |
155 | ||
156 | "find_string": { | |
157 | Type: schema.TypeString, | |
158 | Optional: true, | |
159 | }, | |
160 | ||
161 | "do_not_find": { | |
162 | Type: schema.TypeBool, | |
163 | Optional: true, | |
164 | }, | |
165 | ||
166 | "real_browser": { | |
167 | Type: schema.TypeInt, | |
168 | Optional: true, | |
169 | }, | |
170 | ||
171 | "test_tags": { | |
172 | Type: schema.TypeString, | |
173 | Optional: true, | |
174 | }, | |
175 | ||
176 | "status_codes": { | |
177 | Type: schema.TypeString, | |
178 | Optional: true, | |
179 | Default: "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " + | |
180 | "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " + | |
181 | "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " + | |
182 | "598, 599", | |
183 | }, | |
184 | ||
185 | "use_jar": { | |
186 | Type: schema.TypeInt, | |
187 | Optional: true, | |
188 | }, | |
189 | ||
190 | "post_raw": { | |
191 | Type: schema.TypeString, | |
192 | Optional: true, | |
193 | }, | |
194 | ||
195 | "final_endpoint": { | |
196 | Type: schema.TypeString, | |
197 | Optional: true, | |
198 | }, | |
199 | ||
200 | "follow_redirect": { | |
201 | Type: schema.TypeBool, | |
202 | Optional: true, | |
203 | }, | |
b8f05dfc | 204 | }, |
478e1338 | 205 | } |
206 | } | |
207 | ||
208 | func CreateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 209 | client := meta.(*statuscake.Client) |
b8f05dfc | 210 | |
4eeeab64 | 211 | newTest := &statuscake.Test{ |
754f1420 AG |
212 | WebsiteName: d.Get("website_name").(string), |
213 | WebsiteURL: d.Get("website_url").(string), | |
214 | CheckRate: d.Get("check_rate").(int), | |
215 | TestType: d.Get("test_type").(string), | |
216 | Paused: d.Get("paused").(bool), | |
217 | Timeout: d.Get("timeout").(int), | |
c9458806 | 218 | ContactID: d.Get("contact_id").(int), |
754f1420 AG |
219 | Confirmation: d.Get("confirmations").(int), |
220 | Port: d.Get("port").(int), | |
221 | TriggerRate: d.Get("trigger_rate").(int), | |
222 | CustomHeader: d.Get("custom_header").(string), | |
223 | UserAgent: d.Get("user_agent").(string), | |
224 | Status: d.Get("status").(string), | |
225 | Uptime: d.Get("uptime").(float64), | |
b0191999 | 226 | NodeLocations: castInterfaceToSliceStrings(d.Get("node_locations")), |
754f1420 AG |
227 | PingURL: d.Get("ping_url").(string), |
228 | BasicUser: d.Get("basic_user").(string), | |
229 | BasicPass: d.Get("basic_pass").(string), | |
230 | Public: d.Get("public").(int), | |
231 | LogoImage: d.Get("logo_image").(string), | |
232 | Branding: d.Get("branding").(int), | |
233 | WebsiteHost: d.Get("website_host").(string), | |
234 | Virus: d.Get("virus").(int), | |
235 | FindString: d.Get("find_string").(string), | |
236 | DoNotFind: d.Get("do_not_find").(bool), | |
237 | RealBrowser: d.Get("real_browser").(int), | |
238 | TestTags: d.Get("test_tags").(string), | |
239 | StatusCodes: d.Get("status_codes").(string), | |
240 | UseJar: d.Get("use_jar").(int), | |
241 | PostRaw: d.Get("post_raw").(string), | |
242 | FinalEndpoint: d.Get("final_endpoint").(string), | |
243 | FollowRedirect: d.Get("follow_redirect").(bool), | |
b8f05dfc | 244 | } |
245 | ||
b8f05dfc | 246 | log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) |
247 | ||
32574941 | 248 | response, err := client.Tests().Update(newTest) |
b8f05dfc | 249 | if err != nil { |
250 | return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) | |
251 | } | |
252 | ||
253 | d.Set("test_id", fmt.Sprintf("%d", response.TestID)) | |
a6097499 AG |
254 | d.Set("status", response.Status) |
255 | d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime)) | |
b8f05dfc | 256 | d.SetId(fmt.Sprintf("%d", response.TestID)) |
257 | ||
7dec75fc | 258 | return ReadTest(d, meta) |
478e1338 | 259 | } |
260 | ||
261 | func UpdateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 262 | client := meta.(*statuscake.Client) |
7dec75fc | 263 | |
264 | params := getStatusCakeTestInput(d) | |
265 | ||
266 | log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) | |
32574941 | 267 | _, err := client.Tests().Update(params) |
7dec75fc | 268 | if err != nil { |
269 | return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) | |
270 | } | |
478e1338 | 271 | return nil |
272 | } | |
273 | ||
274 | func DeleteTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 275 | client := meta.(*statuscake.Client) |
b8f05dfc | 276 | |
277 | testId, parseErr := strconv.Atoi(d.Id()) | |
278 | if parseErr != nil { | |
279 | return parseErr | |
280 | } | |
b8f05dfc | 281 | log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) |
7dec75fc | 282 | err := client.Tests().Delete(testId) |
b8f05dfc | 283 | if err != nil { |
284 | return err | |
285 | } | |
286 | ||
478e1338 | 287 | return nil |
288 | } | |
289 | ||
290 | func ReadTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 291 | client := meta.(*statuscake.Client) |
7dec75fc | 292 | |
293 | testId, parseErr := strconv.Atoi(d.Id()) | |
294 | if parseErr != nil { | |
295 | return parseErr | |
296 | } | |
32574941 | 297 | testResp, err := client.Tests().Detail(testId) |
7dec75fc | 298 | if err != nil { |
299 | return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) | |
300 | } | |
c0701076 RG |
301 | d.Set("website_name", testResp.WebsiteName) |
302 | d.Set("website_url", testResp.WebsiteURL) | |
7dec75fc | 303 | d.Set("check_rate", testResp.CheckRate) |
c0701076 RG |
304 | d.Set("test_type", testResp.TestType) |
305 | d.Set("paused", testResp.Paused) | |
306 | d.Set("timeout", testResp.Timeout) | |
307 | d.Set("contact_id", testResp.ContactID) | |
b5815405 | 308 | d.Set("confirmations", testResp.Confirmation) |
90db3fb0 | 309 | d.Set("port", testResp.Port) |
0e962b8e | 310 | d.Set("trigger_rate", testResp.TriggerRate) |
754f1420 AG |
311 | d.Set("custom_header", testResp.CustomHeader) |
312 | d.Set("user_agent", testResp.UserAgent) | |
313 | d.Set("status", testResp.Status) | |
314 | d.Set("uptime", testResp.Uptime) | |
b0191999 AG |
315 | if err := d.Set("node_locations", testResp.NodeLocations); err != nil { |
316 | return fmt.Errorf("[WARN] Error setting node locations: %s", err) | |
317 | } | |
754f1420 AG |
318 | d.Set("ping_url", testResp.PingURL) |
319 | d.Set("basic_user", testResp.BasicUser) | |
320 | d.Set("basic_pass", testResp.BasicPass) | |
321 | d.Set("public", testResp.Public) | |
322 | d.Set("logo_image", testResp.LogoImage) | |
323 | d.Set("branding", testResp.Branding) | |
324 | d.Set("website_host", testResp.WebsiteHost) | |
325 | d.Set("virus", testResp.Virus) | |
326 | d.Set("find_string", testResp.FindString) | |
327 | d.Set("do_not_find", testResp.DoNotFind) | |
328 | d.Set("real_browser", testResp.RealBrowser) | |
329 | d.Set("test_tags", testResp.TestTags) | |
330 | d.Set("status_codes", testResp.StatusCodes) | |
331 | d.Set("use_jar", testResp.UseJar) | |
332 | d.Set("post_raw", testResp.PostRaw) | |
333 | d.Set("final_endpoint", testResp.FinalEndpoint) | |
334 | d.Set("follow_redirect", testResp.FollowRedirect) | |
7dec75fc | 335 | |
478e1338 | 336 | return nil |
337 | } | |
7dec75fc | 338 | |
4eeeab64 | 339 | func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { |
7dec75fc | 340 | testId, parseErr := strconv.Atoi(d.Id()) |
341 | if parseErr != nil { | |
342 | log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) | |
343 | } | |
4eeeab64 | 344 | test := &statuscake.Test{ |
7dec75fc | 345 | TestID: testId, |
346 | } | |
347 | if v, ok := d.GetOk("website_name"); ok { | |
348 | test.WebsiteName = v.(string) | |
349 | } | |
350 | if v, ok := d.GetOk("website_url"); ok { | |
351 | test.WebsiteURL = v.(string) | |
352 | } | |
353 | if v, ok := d.GetOk("check_rate"); ok { | |
354 | test.CheckRate = v.(int) | |
355 | } | |
e7b04dd5 | 356 | if v, ok := d.GetOk("contact_id"); ok { |
c9458806 | 357 | test.ContactID = v.(int) |
e7b04dd5 | 358 | } |
7dec75fc | 359 | if v, ok := d.GetOk("test_type"); ok { |
360 | test.TestType = v.(string) | |
361 | } | |
362 | if v, ok := d.GetOk("paused"); ok { | |
363 | test.Paused = v.(bool) | |
364 | } | |
365 | if v, ok := d.GetOk("timeout"); ok { | |
366 | test.Timeout = v.(int) | |
367 | } | |
b5815405 SC |
368 | if v, ok := d.GetOk("confirmations"); ok { |
369 | test.Confirmation = v.(int) | |
370 | } | |
90db3fb0 PS |
371 | if v, ok := d.GetOk("port"); ok { |
372 | test.Port = v.(int) | |
373 | } | |
0e962b8e RG |
374 | if v, ok := d.GetOk("trigger_rate"); ok { |
375 | test.TriggerRate = v.(int) | |
376 | } | |
754f1420 AG |
377 | if v, ok := d.GetOk("custom_header"); ok { |
378 | test.CustomHeader = v.(string) | |
379 | } | |
380 | if v, ok := d.GetOk("user_agent"); ok { | |
381 | test.UserAgent = v.(string) | |
382 | } | |
754f1420 | 383 | if v, ok := d.GetOk("node_locations"); ok { |
b0191999 | 384 | test.NodeLocations = castInterfaceToSliceStrings(v) |
754f1420 AG |
385 | } |
386 | if v, ok := d.GetOk("ping_url"); ok { | |
387 | test.PingURL = v.(string) | |
388 | } | |
389 | if v, ok := d.GetOk("basic_user"); ok { | |
390 | test.BasicUser = v.(string) | |
391 | } | |
392 | if v, ok := d.GetOk("basic_pass"); ok { | |
393 | test.BasicPass = v.(string) | |
394 | } | |
395 | if v, ok := d.GetOk("public"); ok { | |
396 | test.Public = v.(int) | |
397 | } | |
398 | if v, ok := d.GetOk("logo_image"); ok { | |
399 | test.LogoImage = v.(string) | |
400 | } | |
401 | if v, ok := d.GetOk("branding"); ok { | |
402 | test.Branding = v.(int) | |
403 | } | |
404 | if v, ok := d.GetOk("website_host"); ok { | |
405 | test.WebsiteHost = v.(string) | |
406 | } | |
407 | if v, ok := d.GetOk("virus"); ok { | |
408 | test.Virus = v.(int) | |
409 | } | |
410 | if v, ok := d.GetOk("find_string"); ok { | |
411 | test.FindString = v.(string) | |
412 | } | |
413 | if v, ok := d.GetOk("do_not_find"); ok { | |
414 | test.DoNotFind = v.(bool) | |
415 | } | |
416 | if v, ok := d.GetOk("real_browser"); ok { | |
417 | test.RealBrowser = v.(int) | |
418 | } | |
419 | if v, ok := d.GetOk("test_tags"); ok { | |
420 | test.TestTags = v.(string) | |
421 | } | |
422 | if v, ok := d.GetOk("status_codes"); ok { | |
423 | test.StatusCodes = v.(string) | |
424 | } | |
425 | if v, ok := d.GetOk("use_jar"); ok { | |
426 | test.UseJar = v.(int) | |
427 | } | |
428 | if v, ok := d.GetOk("post_raw"); ok { | |
429 | test.PostRaw = v.(string) | |
430 | } | |
431 | if v, ok := d.GetOk("final_endpoint"); ok { | |
432 | test.FinalEndpoint = v.(string) | |
433 | } | |
434 | if v, ok := d.GetOk("follow_redirect"); ok { | |
435 | test.FollowRedirect = v.(bool) | |
436 | } | |
2a6605e9 | 437 | |
7dec75fc | 438 | return test |
439 | } |