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