aboutsummaryrefslogtreecommitdiffhomepage
path: root/statuscake/resource_statuscaketest.go
diff options
context:
space:
mode:
Diffstat (limited to 'statuscake/resource_statuscaketest.go')
-rw-r--r--statuscake/resource_statuscaketest.go263
1 files changed, 243 insertions, 20 deletions
diff --git a/statuscake/resource_statuscaketest.go b/statuscake/resource_statuscaketest.go
index 101ee83..d7d3f36 100644
--- a/statuscake/resource_statuscaketest.go
+++ b/statuscake/resource_statuscaketest.go
@@ -10,6 +10,24 @@ import (
10 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/helper/schema"
11) 11)
12 12
13func 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
23func considerEmptyStringAsEmptyArray(in []string) []string {
24 if len(in) == 1 && in[0] == "" {
25 return []string{}
26 } else {
27 return in
28 }
29}
30
13func resourceStatusCakeTest() *schema.Resource { 31func resourceStatusCakeTest() *schema.Resource {
14 return &schema.Resource{ 32 return &schema.Resource{
15 Create: CreateTest, 33 Create: CreateTest,
@@ -76,6 +94,118 @@ func resourceStatusCakeTest() *schema.Resource {
76 Optional: true, 94 Optional: true,
77 Default: 5, 95 Default: 5,
78 }, 96 },
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,
109 Computed: true,
110 },
111
112 "uptime": {
113 Type: schema.TypeFloat,
114 Computed: true,
115 },
116
117 "node_locations": {
118 Type: schema.TypeSet,
119 Elem: &schema.Schema{Type: schema.TypeString},
120 Optional: true,
121 Set: schema.HashString,
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": {
135 Type: schema.TypeString,
136 Optional: true,
137 Sensitive: true,
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,
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 },
79 }, 209 },
80 } 210 }
81} 211}
@@ -84,16 +214,38 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
84 client := meta.(*statuscake.Client) 214 client := meta.(*statuscake.Client)
85 215
86 newTest := &statuscake.Test{ 216 newTest := &statuscake.Test{
87 WebsiteName: d.Get("website_name").(string), 217 WebsiteName: d.Get("website_name").(string),
88 WebsiteURL: d.Get("website_url").(string), 218 WebsiteURL: d.Get("website_url").(string),
89 CheckRate: d.Get("check_rate").(int), 219 CheckRate: d.Get("check_rate").(int),
90 TestType: d.Get("test_type").(string), 220 TestType: d.Get("test_type").(string),
91 Paused: d.Get("paused").(bool), 221 Paused: d.Get("paused").(bool),
92 Timeout: d.Get("timeout").(int), 222 Timeout: d.Get("timeout").(int),
93 ContactID: d.Get("contact_id").(int), 223 ContactID: d.Get("contact_id").(int),
94 Confirmation: d.Get("confirmations").(int), 224 Confirmation: d.Get("confirmations").(int),
95 Port: d.Get("port").(int), 225 Port: d.Get("port").(int),
96 TriggerRate: d.Get("trigger_rate").(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),
231 NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
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),
97 } 249 }
98 250
99 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) 251 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
@@ -104,6 +256,8 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
104 } 256 }
105 257
106 d.Set("test_id", fmt.Sprintf("%d", response.TestID)) 258 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
259 d.Set("status", response.Status)
260 d.Set("uptime", fmt.Sprintf("%.3f", response.Uptime))
107 d.SetId(fmt.Sprintf("%d", response.TestID)) 261 d.SetId(fmt.Sprintf("%d", response.TestID))
108 262
109 return ReadTest(d, meta) 263 return ReadTest(d, meta)
@@ -159,6 +313,25 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error {
159 d.Set("confirmations", testResp.Confirmation) 313 d.Set("confirmations", testResp.Confirmation)
160 d.Set("port", testResp.Port) 314 d.Set("port", testResp.Port)
161 d.Set("trigger_rate", testResp.TriggerRate) 315 d.Set("trigger_rate", testResp.TriggerRate)
316 d.Set("custom_header", testResp.CustomHeader)
317 d.Set("status", testResp.Status)
318 d.Set("uptime", testResp.Uptime)
319 if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
320 return fmt.Errorf("[WARN] Error setting node locations: %s", err)
321 }
322 d.Set("logo_image", testResp.LogoImage)
323 // Even after WebsiteHost is set, the API returns ""
324 // API docs aren't clear on usage will only override state if we get a non-empty value back
325 if testResp.WebsiteHost != "" {
326 d.Set("website_host", testResp.WebsiteHost)
327 }
328 d.Set("find_string", testResp.FindString)
329 d.Set("do_not_find", testResp.DoNotFind)
330 d.Set("status_codes", testResp.StatusCodes)
331 d.Set("use_jar", testResp.UseJar)
332 d.Set("post_raw", testResp.PostRaw)
333 d.Set("final_endpoint", testResp.FinalEndpoint)
334 d.Set("follow_redirect", testResp.FollowRedirect)
162 335
163 return nil 336 return nil
164} 337}
@@ -192,9 +365,6 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
192 if v, ok := d.GetOk("timeout"); ok { 365 if v, ok := d.GetOk("timeout"); ok {
193 test.Timeout = v.(int) 366 test.Timeout = v.(int)
194 } 367 }
195 if v, ok := d.GetOk("contact_id"); ok {
196 test.ContactID = v.(int)
197 }
198 if v, ok := d.GetOk("confirmations"); ok { 368 if v, ok := d.GetOk("confirmations"); ok {
199 test.Confirmation = v.(int) 369 test.Confirmation = v.(int)
200 } 370 }
@@ -204,13 +374,66 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
204 if v, ok := d.GetOk("trigger_rate"); ok { 374 if v, ok := d.GetOk("trigger_rate"); ok {
205 test.TriggerRate = v.(int) 375 test.TriggerRate = v.(int)
206 } 376 }
207 377 if v, ok := d.GetOk("custom_header"); ok {
208 defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " + 378 test.CustomHeader = v.(string)
209 "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " + 379 }
210 "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " + 380 if v, ok := d.GetOk("user_agent"); ok {
211 "598, 599" 381 test.UserAgent = v.(string)
212 382 }
213 test.StatusCodes = defaultStatusCodes 383 if v, ok := d.GetOk("node_locations"); ok {
384 test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
385 }
386 if v, ok := d.GetOk("ping_url"); ok {
387 test.PingURL = v.(string)
388 }
389 if v, ok := d.GetOk("basic_user"); ok {
390 test.BasicUser = v.(string)
391 }
392 if v, ok := d.GetOk("basic_pass"); ok {
393 test.BasicPass = v.(string)
394 }
395 if v, ok := d.GetOk("public"); ok {
396 test.Public = v.(int)
397 }
398 if v, ok := d.GetOk("logo_image"); ok {
399 test.LogoImage = v.(string)
400 }
401 if v, ok := d.GetOk("branding"); ok {
402 test.Branding = v.(int)
403 }
404 if v, ok := d.GetOk("website_host"); ok {
405 test.WebsiteHost = v.(string)
406 }
407 if v, ok := d.GetOk("virus"); ok {
408 test.Virus = v.(int)
409 }
410 if v, ok := d.GetOk("find_string"); ok {
411 test.FindString = v.(string)
412 }
413 if v, ok := d.GetOk("do_not_find"); ok {
414 test.DoNotFind = v.(bool)
415 }
416 if v, ok := d.GetOk("real_browser"); ok {
417 test.RealBrowser = v.(int)
418 }
419 if v, ok := d.GetOk("test_tags"); ok {
420 test.TestTags = v.(string)
421 }
422 if v, ok := d.GetOk("status_codes"); ok {
423 test.StatusCodes = v.(string)
424 }
425 if v, ok := d.GetOk("use_jar"); ok {
426 test.UseJar = v.(int)
427 }
428 if v, ok := d.GetOk("post_raw"); ok {
429 test.PostRaw = v.(string)
430 }
431 if v, ok := d.GetOk("final_endpoint"); ok {
432 test.FinalEndpoint = v.(string)
433 }
434 if v, ok := d.GetOk("follow_redirect"); ok {
435 test.FollowRedirect = v.(bool)
436 }
214 437
215 return test 438 return test
216} 439}