]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - statuscake/resource_statuscaketest.go
Replace contact_id with contact_group
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscaketest.go
1 package statuscake
2
3 import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11 )
12
13 func castSetToSliceStrings(configured []interface{}) []string {
14 res := make([]string, len(configured))
15
16 for i, element := range configured {
17 res[i] = element.(string)
18 }
19 return res
20 }
21
22 // Special handling for node_locations since statuscake will return `[""]` for the empty case
23 func considerEmptyStringAsEmptyArray(in []string) []string {
24 if len(in) == 1 && in[0] == "" {
25 return []string{}
26 } else {
27 return in
28 }
29 }
30
31 func resourceStatusCakeTest() *schema.Resource {
32 return &schema.Resource{
33 Create: CreateTest,
34 Update: UpdateTest,
35 Delete: DeleteTest,
36 Read: ReadTest,
37
38 Schema: map[string]*schema.Schema{
39 "test_id": {
40 Type: schema.TypeString,
41 Computed: true,
42 },
43
44 "website_name": {
45 Type: schema.TypeString,
46 Required: true,
47 },
48
49 "website_url": {
50 Type: schema.TypeString,
51 Required: true,
52 },
53
54 "contact_group": {
55 Type: schema.TypeSet,
56 Elem: &schema.Schema{Type: schema.TypeString},
57 Optional: true,
58 Set: schema.HashString,
59 },
60
61 "check_rate": {
62 Type: schema.TypeInt,
63 Optional: true,
64 Default: 300,
65 },
66
67 "test_type": {
68 Type: schema.TypeString,
69 Required: true,
70 },
71
72 "paused": {
73 Type: schema.TypeBool,
74 Optional: true,
75 Default: false,
76 },
77
78 "timeout": {
79 Type: schema.TypeInt,
80 Optional: true,
81 Default: 40,
82 },
83
84 "confirmations": {
85 Type: schema.TypeInt,
86 Optional: true,
87 },
88
89 "port": {
90 Type: schema.TypeInt,
91 Optional: true,
92 },
93
94 "trigger_rate": {
95 Type: schema.TypeInt,
96 Optional: true,
97 Default: 5,
98 },
99
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,
112 Computed: true,
113 },
114
115 "uptime": {
116 Type: schema.TypeFloat,
117 Computed: true,
118 },
119
120 "node_locations": {
121 Type: schema.TypeSet,
122 Elem: &schema.Schema{Type: schema.TypeString},
123 Optional: true,
124 Set: schema.HashString,
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": {
138 Type: schema.TypeString,
139 Optional: true,
140 Sensitive: true,
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": {
184 Type: schema.TypeSet,
185 Elem: &schema.Schema{Type: schema.TypeString},
186 Optional: true,
187 Set: schema.HashString,
188 },
189
190 "status_codes": {
191 Type: schema.TypeString,
192 Optional: true,
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 },
214 },
215 }
216 }
217
218 func CreateTest(d *schema.ResourceData, meta interface{}) error {
219 client := meta.(*statuscake.Client)
220
221 newTest := &statuscake.Test{
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),
228 ContactGroup: castSetToSliceStrings(d.Get("contact_group").(*schema.Set).List()),
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),
236 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
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),
248 TestTags: castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
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),
254 }
255
256 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
257
258 response, err := client.Tests().Update(newTest)
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))
264 d.Set("status", response.Status)
265 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
266 d.SetId(fmt.Sprintf("%d", response.TestID))
267
268 return ReadTest(d, meta)
269 }
270
271 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
272 client := meta.(*statuscake.Client)
273
274 params := getStatusCakeTestInput(d)
275
276 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
277 _, err := client.Tests().Update(params)
278 if err != nil {
279 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
280 }
281 return nil
282 }
283
284 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
285 client := meta.(*statuscake.Client)
286
287 testId, parseErr := strconv.Atoi(d.Id())
288 if parseErr != nil {
289 return parseErr
290 }
291 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
292 err := client.Tests().Delete(testId)
293 if err != nil {
294 return err
295 }
296
297 return nil
298 }
299
300 func ReadTest(d *schema.ResourceData, meta interface{}) error {
301 client := meta.(*statuscake.Client)
302
303 testId, parseErr := strconv.Atoi(d.Id())
304 if parseErr != nil {
305 return parseErr
306 }
307 testResp, err := client.Tests().Detail(testId)
308 if err != nil {
309 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
310 }
311 d.Set("website_name", testResp.WebsiteName)
312 d.Set("website_url", testResp.WebsiteURL)
313 d.Set("check_rate", testResp.CheckRate)
314 d.Set("test_type", testResp.TestType)
315 d.Set("paused", testResp.Paused)
316 d.Set("timeout", testResp.Timeout)
317 d.Set("contact_group", testResp.ContactGroup)
318 d.Set("confirmations", testResp.Confirmation)
319 d.Set("port", testResp.Port)
320 d.Set("trigger_rate", testResp.TriggerRate)
321 d.Set("custom_header", testResp.CustomHeader)
322 d.Set("status", testResp.Status)
323 d.Set("uptime", testResp.Uptime)
324 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
325 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
326 }
327 d.Set("logo_image", testResp.LogoImage)
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 }
333 d.Set("find_string", testResp.FindString)
334 d.Set("do_not_find", testResp.DoNotFind)
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)
340
341 return nil
342 }
343
344 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
345 testId, parseErr := strconv.Atoi(d.Id())
346 if parseErr != nil {
347 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
348 }
349 test := &statuscake.Test{
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 }
361 if v, ok := d.GetOk("contact_group"); ok {
362 test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
363 }
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 }
373 if v, ok := d.GetOk("confirmations"); ok {
374 test.Confirmation = v.(int)
375 }
376 if v, ok := d.GetOk("port"); ok {
377 test.Port = v.(int)
378 }
379 if v, ok := d.GetOk("trigger_rate"); ok {
380 test.TriggerRate = v.(int)
381 }
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 }
388 if v, ok := d.GetOk("node_locations"); ok {
389 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
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 = castSetToSliceStrings(v.(*schema.Set).List())
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 }
442
443 return test
444 }