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