]>
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, | |
91 | Optional: true, | |
92 | }, | |
93 | ||
94 | "uptime": { | |
95 | Type: schema.TypeFloat, | |
96 | Optional: true, | |
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)) | |
243 | d.SetId(fmt.Sprintf("%d", response.TestID)) | |
244 | ||
7dec75fc | 245 | return ReadTest(d, meta) |
478e1338 | 246 | } |
247 | ||
248 | func UpdateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 249 | client := meta.(*statuscake.Client) |
7dec75fc | 250 | |
251 | params := getStatusCakeTestInput(d) | |
252 | ||
253 | log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) | |
32574941 | 254 | _, err := client.Tests().Update(params) |
7dec75fc | 255 | if err != nil { |
256 | return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) | |
257 | } | |
478e1338 | 258 | return nil |
259 | } | |
260 | ||
261 | func DeleteTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 262 | client := meta.(*statuscake.Client) |
b8f05dfc | 263 | |
264 | testId, parseErr := strconv.Atoi(d.Id()) | |
265 | if parseErr != nil { | |
266 | return parseErr | |
267 | } | |
b8f05dfc | 268 | log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) |
7dec75fc | 269 | err := client.Tests().Delete(testId) |
b8f05dfc | 270 | if err != nil { |
271 | return err | |
272 | } | |
273 | ||
478e1338 | 274 | return nil |
275 | } | |
276 | ||
277 | func ReadTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 278 | client := meta.(*statuscake.Client) |
7dec75fc | 279 | |
280 | testId, parseErr := strconv.Atoi(d.Id()) | |
281 | if parseErr != nil { | |
282 | return parseErr | |
283 | } | |
32574941 | 284 | testResp, err := client.Tests().Detail(testId) |
7dec75fc | 285 | if err != nil { |
286 | return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) | |
287 | } | |
c0701076 RG |
288 | d.Set("website_name", testResp.WebsiteName) |
289 | d.Set("website_url", testResp.WebsiteURL) | |
7dec75fc | 290 | d.Set("check_rate", testResp.CheckRate) |
c0701076 RG |
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) | |
b5815405 | 295 | d.Set("confirmations", testResp.Confirmation) |
90db3fb0 | 296 | d.Set("port", testResp.Port) |
0e962b8e | 297 | d.Set("trigger_rate", testResp.TriggerRate) |
754f1420 AG |
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) | |
7dec75fc | 320 | |
478e1338 | 321 | return nil |
322 | } | |
7dec75fc | 323 | |
4eeeab64 | 324 | func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { |
7dec75fc | 325 | testId, parseErr := strconv.Atoi(d.Id()) |
326 | if parseErr != nil { | |
327 | log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) | |
328 | } | |
4eeeab64 | 329 | test := &statuscake.Test{ |
7dec75fc | 330 | TestID: testId, |
331 | } | |
332 | if v, ok := d.GetOk("website_name"); ok { | |
333 | test.WebsiteName = v.(string) | |
334 | } | |
335 | if v, ok := d.GetOk("website_url"); ok { | |
336 | test.WebsiteURL = v.(string) | |
337 | } | |
338 | if v, ok := d.GetOk("check_rate"); ok { | |
339 | test.CheckRate = v.(int) | |
340 | } | |
e7b04dd5 | 341 | if v, ok := d.GetOk("contact_id"); ok { |
754f1420 | 342 | test.ContactID = v.(string) |
e7b04dd5 | 343 | } |
7dec75fc | 344 | if v, ok := d.GetOk("test_type"); ok { |
345 | test.TestType = v.(string) | |
346 | } | |
347 | if v, ok := d.GetOk("paused"); ok { | |
348 | test.Paused = v.(bool) | |
349 | } | |
350 | if v, ok := d.GetOk("timeout"); ok { | |
351 | test.Timeout = v.(int) | |
352 | } | |
b5815405 SC |
353 | if v, ok := d.GetOk("confirmations"); ok { |
354 | test.Confirmation = v.(int) | |
355 | } | |
90db3fb0 PS |
356 | if v, ok := d.GetOk("port"); ok { |
357 | test.Port = v.(int) | |
358 | } | |
0e962b8e RG |
359 | if v, ok := d.GetOk("trigger_rate"); ok { |
360 | test.TriggerRate = v.(int) | |
361 | } | |
754f1420 AG |
362 | if v, ok := d.GetOk("custom_header"); ok { |
363 | test.CustomHeader = v.(string) | |
364 | } | |
365 | if v, ok := d.GetOk("user_agent"); ok { | |
366 | test.UserAgent = v.(string) | |
367 | } | |
368 | if v, ok := d.GetOk("status"); ok { | |
369 | test.Status = v.(string) | |
370 | } | |
371 | if v, ok := d.GetOk("uptime"); ok { | |
372 | test.Uptime = v.(float64) | |
373 | } | |
374 | if v, ok := d.GetOk("node_locations"); ok { | |
375 | test.NodeLocations = v.([]string) | |
376 | } | |
377 | if v, ok := d.GetOk("ping_url"); ok { | |
378 | test.PingURL = v.(string) | |
379 | } | |
380 | if v, ok := d.GetOk("basic_user"); ok { | |
381 | test.BasicUser = v.(string) | |
382 | } | |
383 | if v, ok := d.GetOk("basic_pass"); ok { | |
384 | test.BasicPass = v.(string) | |
385 | } | |
386 | if v, ok := d.GetOk("public"); ok { | |
387 | test.Public = v.(int) | |
388 | } | |
389 | if v, ok := d.GetOk("logo_image"); ok { | |
390 | test.LogoImage = v.(string) | |
391 | } | |
392 | if v, ok := d.GetOk("branding"); ok { | |
393 | test.Branding = v.(int) | |
394 | } | |
395 | if v, ok := d.GetOk("website_host"); ok { | |
396 | test.WebsiteHost = v.(string) | |
397 | } | |
398 | if v, ok := d.GetOk("virus"); ok { | |
399 | test.Virus = v.(int) | |
400 | } | |
401 | if v, ok := d.GetOk("find_string"); ok { | |
402 | test.FindString = v.(string) | |
403 | } | |
404 | if v, ok := d.GetOk("do_not_find"); ok { | |
405 | test.DoNotFind = v.(bool) | |
406 | } | |
407 | if v, ok := d.GetOk("real_browser"); ok { | |
408 | test.RealBrowser = v.(int) | |
409 | } | |
410 | if v, ok := d.GetOk("test_tags"); ok { | |
411 | test.TestTags = v.(string) | |
412 | } | |
413 | if v, ok := d.GetOk("status_codes"); ok { | |
414 | test.StatusCodes = v.(string) | |
415 | } | |
416 | if v, ok := d.GetOk("use_jar"); ok { | |
417 | test.UseJar = v.(int) | |
418 | } | |
419 | if v, ok := d.GetOk("post_raw"); ok { | |
420 | test.PostRaw = v.(string) | |
421 | } | |
422 | if v, ok := d.GetOk("final_endpoint"); ok { | |
423 | test.FinalEndpoint = v.(string) | |
424 | } | |
425 | if v, ok := d.GetOk("follow_redirect"); ok { | |
426 | test.FollowRedirect = v.(bool) | |
427 | } | |
2a6605e9 | 428 | |
7dec75fc | 429 | return test |
430 | } |