]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - statuscake/resource_statuscaketest.go
fix type casts
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscaketest.go
CommitLineData
478e1338 1package statuscake
2
b8f05dfc 3import (
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
13func 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 23func 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": {
754f1420 47 Type: schema.TypeString,
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": {
126 Type: schema.TypeString,
127 Optional: true,
128 },
129
130 "public": {
131 Type: schema.TypeInt,
132 Optional: true,
133 },
134
135 "logo_image": {
136 Type: schema.TypeString,
137 Optional: true,
138 },
139
140 "branding": {
141 Type: schema.TypeInt,
142 Optional: true,
143 },
144
145 "website_host": {
146 Type: schema.TypeString,
147 Optional: true,
148 },
149
150 "virus": {
151 Type: schema.TypeInt,
152 Optional: true,
153 },
154
155 "find_string": {
156 Type: schema.TypeString,
157 Optional: true,
158 },
159
160 "do_not_find": {
161 Type: schema.TypeBool,
162 Optional: true,
163 },
164
165 "real_browser": {
166 Type: schema.TypeInt,
167 Optional: true,
168 },
169
170 "test_tags": {
171 Type: schema.TypeString,
172 Optional: true,
173 },
174
175 "status_codes": {
176 Type: schema.TypeString,
177 Optional: true,
178 Default: "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
179 "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
180 "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
181 "598, 599",
182 },
183
184 "use_jar": {
185 Type: schema.TypeInt,
186 Optional: true,
187 },
188
189 "post_raw": {
190 Type: schema.TypeString,
191 Optional: true,
192 },
193
194 "final_endpoint": {
195 Type: schema.TypeString,
196 Optional: true,
197 },
198
199 "follow_redirect": {
200 Type: schema.TypeBool,
201 Optional: true,
202 },
b8f05dfc 203 },
478e1338 204 }
205}
206
207func CreateTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 208 client := meta.(*statuscake.Client)
b8f05dfc 209
4eeeab64 210 newTest := &statuscake.Test{
754f1420
AG
211 WebsiteName: d.Get("website_name").(string),
212 WebsiteURL: d.Get("website_url").(string),
213 CheckRate: d.Get("check_rate").(int),
214 TestType: d.Get("test_type").(string),
215 Paused: d.Get("paused").(bool),
216 Timeout: d.Get("timeout").(int),
217 ContactID: d.Get("contact_id").(string),
218 Confirmation: d.Get("confirmations").(int),
219 Port: d.Get("port").(int),
220 TriggerRate: d.Get("trigger_rate").(int),
221 CustomHeader: d.Get("custom_header").(string),
222 UserAgent: d.Get("user_agent").(string),
223 Status: d.Get("status").(string),
224 Uptime: d.Get("uptime").(float64),
b0191999 225 NodeLocations: castInterfaceToSliceStrings(d.Get("node_locations")),
754f1420
AG
226 PingURL: d.Get("ping_url").(string),
227 BasicUser: d.Get("basic_user").(string),
228 BasicPass: d.Get("basic_pass").(string),
229 Public: d.Get("public").(int),
230 LogoImage: d.Get("logo_image").(string),
231 Branding: d.Get("branding").(int),
232 WebsiteHost: d.Get("website_host").(string),
233 Virus: d.Get("virus").(int),
234 FindString: d.Get("find_string").(string),
235 DoNotFind: d.Get("do_not_find").(bool),
236 RealBrowser: d.Get("real_browser").(int),
237 TestTags: d.Get("test_tags").(string),
238 StatusCodes: d.Get("status_codes").(string),
239 UseJar: d.Get("use_jar").(int),
240 PostRaw: d.Get("post_raw").(string),
241 FinalEndpoint: d.Get("final_endpoint").(string),
242 FollowRedirect: d.Get("follow_redirect").(bool),
b8f05dfc 243 }
244
b8f05dfc 245 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
246
32574941 247 response, err := client.Tests().Update(newTest)
b8f05dfc 248 if err != nil {
249 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
250 }
251
252 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
a6097499
AG
253 d.Set("status", response.Status)
254 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
b8f05dfc 255 d.SetId(fmt.Sprintf("%d", response.TestID))
256
7dec75fc 257 return ReadTest(d, meta)
478e1338 258}
259
260func UpdateTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 261 client := meta.(*statuscake.Client)
7dec75fc 262
263 params := getStatusCakeTestInput(d)
264
265 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
32574941 266 _, err := client.Tests().Update(params)
7dec75fc 267 if err != nil {
268 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
269 }
478e1338 270 return nil
271}
272
273func DeleteTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 274 client := meta.(*statuscake.Client)
b8f05dfc 275
276 testId, parseErr := strconv.Atoi(d.Id())
277 if parseErr != nil {
278 return parseErr
279 }
b8f05dfc 280 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
7dec75fc 281 err := client.Tests().Delete(testId)
b8f05dfc 282 if err != nil {
283 return err
284 }
285
478e1338 286 return nil
287}
288
289func ReadTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 290 client := meta.(*statuscake.Client)
7dec75fc 291
292 testId, parseErr := strconv.Atoi(d.Id())
293 if parseErr != nil {
294 return parseErr
295 }
32574941 296 testResp, err := client.Tests().Detail(testId)
7dec75fc 297 if err != nil {
298 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
299 }
c0701076
RG
300 d.Set("website_name", testResp.WebsiteName)
301 d.Set("website_url", testResp.WebsiteURL)
7dec75fc 302 d.Set("check_rate", testResp.CheckRate)
c0701076
RG
303 d.Set("test_type", testResp.TestType)
304 d.Set("paused", testResp.Paused)
305 d.Set("timeout", testResp.Timeout)
306 d.Set("contact_id", testResp.ContactID)
b5815405 307 d.Set("confirmations", testResp.Confirmation)
90db3fb0 308 d.Set("port", testResp.Port)
0e962b8e 309 d.Set("trigger_rate", testResp.TriggerRate)
754f1420
AG
310 d.Set("custom_header", testResp.CustomHeader)
311 d.Set("user_agent", testResp.UserAgent)
312 d.Set("status", testResp.Status)
313 d.Set("uptime", testResp.Uptime)
b0191999
AG
314 if err := d.Set("node_locations", testResp.NodeLocations); err != nil {
315 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
316 }
754f1420
AG
317 d.Set("ping_url", testResp.PingURL)
318 d.Set("basic_user", testResp.BasicUser)
319 d.Set("basic_pass", testResp.BasicPass)
320 d.Set("public", testResp.Public)
321 d.Set("logo_image", testResp.LogoImage)
322 d.Set("branding", testResp.Branding)
323 d.Set("website_host", testResp.WebsiteHost)
324 d.Set("virus", testResp.Virus)
325 d.Set("find_string", testResp.FindString)
326 d.Set("do_not_find", testResp.DoNotFind)
327 d.Set("real_browser", testResp.RealBrowser)
328 d.Set("test_tags", testResp.TestTags)
329 d.Set("status_codes", testResp.StatusCodes)
330 d.Set("use_jar", testResp.UseJar)
331 d.Set("post_raw", testResp.PostRaw)
332 d.Set("final_endpoint", testResp.FinalEndpoint)
333 d.Set("follow_redirect", testResp.FollowRedirect)
7dec75fc 334
478e1338 335 return nil
336}
7dec75fc 337
4eeeab64 338func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
7dec75fc 339 testId, parseErr := strconv.Atoi(d.Id())
340 if parseErr != nil {
341 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
342 }
4eeeab64 343 test := &statuscake.Test{
7dec75fc 344 TestID: testId,
345 }
346 if v, ok := d.GetOk("website_name"); ok {
347 test.WebsiteName = v.(string)
348 }
349 if v, ok := d.GetOk("website_url"); ok {
350 test.WebsiteURL = v.(string)
351 }
352 if v, ok := d.GetOk("check_rate"); ok {
353 test.CheckRate = v.(int)
354 }
e7b04dd5 355 if v, ok := d.GetOk("contact_id"); ok {
754f1420 356 test.ContactID = v.(string)
e7b04dd5 357 }
7dec75fc 358 if v, ok := d.GetOk("test_type"); ok {
359 test.TestType = v.(string)
360 }
361 if v, ok := d.GetOk("paused"); ok {
362 test.Paused = v.(bool)
363 }
364 if v, ok := d.GetOk("timeout"); ok {
365 test.Timeout = v.(int)
366 }
b5815405
SC
367 if v, ok := d.GetOk("confirmations"); ok {
368 test.Confirmation = v.(int)
369 }
90db3fb0
PS
370 if v, ok := d.GetOk("port"); ok {
371 test.Port = v.(int)
372 }
0e962b8e
RG
373 if v, ok := d.GetOk("trigger_rate"); ok {
374 test.TriggerRate = v.(int)
375 }
754f1420
AG
376 if v, ok := d.GetOk("custom_header"); ok {
377 test.CustomHeader = v.(string)
378 }
379 if v, ok := d.GetOk("user_agent"); ok {
380 test.UserAgent = v.(string)
381 }
754f1420 382 if v, ok := d.GetOk("node_locations"); ok {
b0191999 383 test.NodeLocations = castInterfaceToSliceStrings(v)
754f1420
AG
384 }
385 if v, ok := d.GetOk("ping_url"); ok {
386 test.PingURL = v.(string)
387 }
388 if v, ok := d.GetOk("basic_user"); ok {
389 test.BasicUser = v.(string)
390 }
391 if v, ok := d.GetOk("basic_pass"); ok {
392 test.BasicPass = v.(string)
393 }
394 if v, ok := d.GetOk("public"); ok {
395 test.Public = v.(int)
396 }
397 if v, ok := d.GetOk("logo_image"); ok {
398 test.LogoImage = v.(string)
399 }
400 if v, ok := d.GetOk("branding"); ok {
401 test.Branding = v.(int)
402 }
403 if v, ok := d.GetOk("website_host"); ok {
404 test.WebsiteHost = v.(string)
405 }
406 if v, ok := d.GetOk("virus"); ok {
407 test.Virus = v.(int)
408 }
409 if v, ok := d.GetOk("find_string"); ok {
410 test.FindString = v.(string)
411 }
412 if v, ok := d.GetOk("do_not_find"); ok {
413 test.DoNotFind = v.(bool)
414 }
415 if v, ok := d.GetOk("real_browser"); ok {
416 test.RealBrowser = v.(int)
417 }
418 if v, ok := d.GetOk("test_tags"); ok {
419 test.TestTags = v.(string)
420 }
421 if v, ok := d.GetOk("status_codes"); ok {
422 test.StatusCodes = v.(string)
423 }
424 if v, ok := d.GetOk("use_jar"); ok {
425 test.UseJar = v.(int)
426 }
427 if v, ok := d.GetOk("post_raw"); ok {
428 test.PostRaw = v.(string)
429 }
430 if v, ok := d.GetOk("final_endpoint"); ok {
431 test.FinalEndpoint = v.(string)
432 }
433 if v, ok := d.GetOk("follow_redirect"); ok {
434 test.FollowRedirect = v.(bool)
435 }
2a6605e9 436
7dec75fc 437 return test
438}