]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - statuscake/resource_statuscaketest.go
Replace contact_id with contact_group
[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
4efdab41
MS
54 "contact_group": {
55 Type: schema.TypeSet,
56 Elem: &schema.Schema{Type: schema.TypeString},
e7b04dd5 57 Optional: true,
4efdab41 58 Set: schema.HashString,
e7b04dd5
LJ
59 },
60
90db3fb0 61 "check_rate": {
b8f05dfc 62 Type: schema.TypeInt,
63 Optional: true,
64 Default: 300,
65 },
66
90db3fb0 67 "test_type": {
b8f05dfc 68 Type: schema.TypeString,
69 Required: true,
70 },
71
90db3fb0 72 "paused": {
b8f05dfc 73 Type: schema.TypeBool,
74 Optional: true,
75 Default: false,
76 },
6ad7af97 77
90db3fb0 78 "timeout": {
7dec75fc 79 Type: schema.TypeInt,
6ad7af97
RG
80 Optional: true,
81 Default: 40,
7dec75fc 82 },
6ad7af97 83
90db3fb0
PS
84 "confirmations": {
85 Type: schema.TypeInt,
86 Optional: true,
87 },
88
89 "port": {
b5815405
SC
90 Type: schema.TypeInt,
91 Optional: true,
92 },
0e962b8e
RG
93
94 "trigger_rate": {
95 Type: schema.TypeInt,
96 Optional: true,
97 Default: 5,
98 },
bc2bde6b 99
754f1420
AG
100 "custom_header": {
101 Type: schema.TypeString,
102 Optional: true,
103 },
104
105 "user_agent": {
106 Type: schema.TypeString,
107 Optional: true,
108 },
109
110 "status": {
111 Type: schema.TypeString,
a6097499 112 Computed: true,
754f1420
AG
113 },
114
115 "uptime": {
116 Type: schema.TypeFloat,
a6097499 117 Computed: true,
754f1420
AG
118 },
119
120 "node_locations": {
89027b6a 121 Type: schema.TypeSet,
754f1420
AG
122 Elem: &schema.Schema{Type: schema.TypeString},
123 Optional: true,
89027b6a 124 Set: schema.HashString,
754f1420
AG
125 },
126
127 "ping_url": {
128 Type: schema.TypeString,
129 Optional: true,
130 },
131
132 "basic_user": {
133 Type: schema.TypeString,
134 Optional: true,
135 },
136
137 "basic_pass": {
ed5e20e8
MS
138 Type: schema.TypeString,
139 Optional: true,
140 Sensitive: true,
754f1420
AG
141 },
142
143 "public": {
144 Type: schema.TypeInt,
145 Optional: true,
146 },
147
148 "logo_image": {
149 Type: schema.TypeString,
150 Optional: true,
151 },
152
153 "branding": {
154 Type: schema.TypeInt,
155 Optional: true,
156 },
157
158 "website_host": {
159 Type: schema.TypeString,
160 Optional: true,
161 },
162
163 "virus": {
164 Type: schema.TypeInt,
165 Optional: true,
166 },
167
168 "find_string": {
169 Type: schema.TypeString,
170 Optional: true,
171 },
172
173 "do_not_find": {
174 Type: schema.TypeBool,
175 Optional: true,
176 },
177
178 "real_browser": {
179 Type: schema.TypeInt,
180 Optional: true,
181 },
182
183 "test_tags": {
bc2bde6b
MS
184 Type: schema.TypeSet,
185 Elem: &schema.Schema{Type: schema.TypeString},
754f1420 186 Optional: true,
bc2bde6b 187 Set: schema.HashString,
754f1420
AG
188 },
189
190 "status_codes": {
191 Type: schema.TypeString,
192 Optional: true,
754f1420
AG
193 },
194
195 "use_jar": {
196 Type: schema.TypeInt,
197 Optional: true,
198 },
199
200 "post_raw": {
201 Type: schema.TypeString,
202 Optional: true,
203 },
204
205 "final_endpoint": {
206 Type: schema.TypeString,
207 Optional: true,
208 },
209
210 "follow_redirect": {
211 Type: schema.TypeBool,
212 Optional: true,
213 },
b8f05dfc 214 },
478e1338 215 }
216}
217
218func CreateTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 219 client := meta.(*statuscake.Client)
b8f05dfc 220
4eeeab64 221 newTest := &statuscake.Test{
754f1420
AG
222 WebsiteName: d.Get("website_name").(string),
223 WebsiteURL: d.Get("website_url").(string),
224 CheckRate: d.Get("check_rate").(int),
225 TestType: d.Get("test_type").(string),
226 Paused: d.Get("paused").(bool),
227 Timeout: d.Get("timeout").(int),
4efdab41 228 ContactGroup: castSetToSliceStrings(d.Get("contact_group").(*schema.Set).List()),
754f1420
AG
229 Confirmation: d.Get("confirmations").(int),
230 Port: d.Get("port").(int),
231 TriggerRate: d.Get("trigger_rate").(int),
232 CustomHeader: d.Get("custom_header").(string),
233 UserAgent: d.Get("user_agent").(string),
234 Status: d.Get("status").(string),
235 Uptime: d.Get("uptime").(float64),
89027b6a 236 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
754f1420
AG
237 PingURL: d.Get("ping_url").(string),
238 BasicUser: d.Get("basic_user").(string),
239 BasicPass: d.Get("basic_pass").(string),
240 Public: d.Get("public").(int),
241 LogoImage: d.Get("logo_image").(string),
242 Branding: d.Get("branding").(int),
243 WebsiteHost: d.Get("website_host").(string),
244 Virus: d.Get("virus").(int),
245 FindString: d.Get("find_string").(string),
246 DoNotFind: d.Get("do_not_find").(bool),
247 RealBrowser: d.Get("real_browser").(int),
bc2bde6b 248 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
754f1420
AG
249 StatusCodes: d.Get("status_codes").(string),
250 UseJar: d.Get("use_jar").(int),
251 PostRaw: d.Get("post_raw").(string),
252 FinalEndpoint: d.Get("final_endpoint").(string),
253 FollowRedirect: d.Get("follow_redirect").(bool),
b8f05dfc 254 }
255
b8f05dfc 256 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
257
32574941 258 response, err := client.Tests().Update(newTest)
b8f05dfc 259 if err != nil {
260 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
261 }
262
263 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
a6097499
AG
264 d.Set("status", response.Status)
265 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
b8f05dfc 266 d.SetId(fmt.Sprintf("%d", response.TestID))
267
7dec75fc 268 return ReadTest(d, meta)
478e1338 269}
270
271func UpdateTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 272 client := meta.(*statuscake.Client)
7dec75fc 273
274 params := getStatusCakeTestInput(d)
275
276 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
32574941 277 _, err := client.Tests().Update(params)
7dec75fc 278 if err != nil {
279 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
280 }
478e1338 281 return nil
282}
283
284func DeleteTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 285 client := meta.(*statuscake.Client)
b8f05dfc 286
287 testId, parseErr := strconv.Atoi(d.Id())
288 if parseErr != nil {
289 return parseErr
290 }
b8f05dfc 291 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
7dec75fc 292 err := client.Tests().Delete(testId)
b8f05dfc 293 if err != nil {
294 return err
295 }
296
478e1338 297 return nil
298}
299
300func ReadTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 301 client := meta.(*statuscake.Client)
7dec75fc 302
303 testId, parseErr := strconv.Atoi(d.Id())
304 if parseErr != nil {
305 return parseErr
306 }
32574941 307 testResp, err := client.Tests().Detail(testId)
7dec75fc 308 if err != nil {
309 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
310 }
c0701076
RG
311 d.Set("website_name", testResp.WebsiteName)
312 d.Set("website_url", testResp.WebsiteURL)
7dec75fc 313 d.Set("check_rate", testResp.CheckRate)
c0701076
RG
314 d.Set("test_type", testResp.TestType)
315 d.Set("paused", testResp.Paused)
316 d.Set("timeout", testResp.Timeout)
4efdab41 317 d.Set("contact_group", testResp.ContactGroup)
b5815405 318 d.Set("confirmations", testResp.Confirmation)
90db3fb0 319 d.Set("port", testResp.Port)
0e962b8e 320 d.Set("trigger_rate", testResp.TriggerRate)
754f1420 321 d.Set("custom_header", testResp.CustomHeader)
754f1420
AG
322 d.Set("status", testResp.Status)
323 d.Set("uptime", testResp.Uptime)
89027b6a 324 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
b0191999
AG
325 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
326 }
754f1420 327 d.Set("logo_image", testResp.LogoImage)
ef20d8d7
MS
328 // Even after WebsiteHost is set, the API returns ""
329 // API docs aren't clear on usage will only override state if we get a non-empty value back
330 if testResp.WebsiteHost != "" {
331 d.Set("website_host", testResp.WebsiteHost)
332 }
754f1420
AG
333 d.Set("find_string", testResp.FindString)
334 d.Set("do_not_find", testResp.DoNotFind)
754f1420
AG
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 }
4efdab41
MS
361 if v, ok := d.GetOk("contact_group"); ok {
362 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
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 {
bc2bde6b 425 test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
754f1420
AG
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}