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