]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - statuscake/resource_statuscaketest.go
Move template interpolation deeper into acceptance tests
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscaketest.go
index 50e16f9ffb145418e201c496cfab8db73bd99f81..d7d3f36c2d857228e2313b0fd62720fe6d99081f 100644 (file)
@@ -10,6 +10,24 @@ import (
        "github.com/hashicorp/terraform/helper/schema"
 )
 
+func castSetToSliceStrings(configured []interface{}) []string {
+       res := make([]string, len(configured))
+
+       for i, element := range configured {
+               res[i] = element.(string)
+       }
+       return res
+}
+
+// Special handling for node_locations since statuscake will return `[""]` for the empty case
+func considerEmptyStringAsEmptyArray(in []string) []string {
+       if len(in) == 1 && in[0] == "" {
+               return []string{}
+       } else {
+               return in
+       }
+}
+
 func resourceStatusCakeTest() *schema.Resource {
        return &schema.Resource{
                Create: CreateTest,
@@ -34,7 +52,7 @@ func resourceStatusCakeTest() *schema.Resource {
                        },
 
                        "contact_id": {
-                               Type:     schema.TypeString,
+                               Type:     schema.TypeInt,
                                Optional: true,
                        },
 
@@ -97,9 +115,10 @@ func resourceStatusCakeTest() *schema.Resource {
                        },
 
                        "node_locations": {
-                               Type:     schema.TypeList,
+                               Type:     schema.TypeSet,
                                Elem:     &schema.Schema{Type: schema.TypeString},
                                Optional: true,
+                               Set:      schema.HashString,
                        },
 
                        "ping_url": {
@@ -113,8 +132,9 @@ func resourceStatusCakeTest() *schema.Resource {
                        },
 
                        "basic_pass": {
-                               Type:     schema.TypeString,
-                               Optional: true,
+                               Type:      schema.TypeString,
+                               Optional:  true,
+                               Sensitive: true,
                        },
 
                        "public": {
@@ -165,10 +185,6 @@ func resourceStatusCakeTest() *schema.Resource {
                        "status_codes": {
                                Type:     schema.TypeString,
                                Optional: true,
-                               Default: "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
-                                       "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
-                                       "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
-                                       "598, 599",
                        },
 
                        "use_jar": {
@@ -204,7 +220,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
                TestType:       d.Get("test_type").(string),
                Paused:         d.Get("paused").(bool),
                Timeout:        d.Get("timeout").(int),
-               ContactID:      d.Get("contact_id").(string),
+               ContactID:      d.Get("contact_id").(int),
                Confirmation:   d.Get("confirmations").(int),
                Port:           d.Get("port").(int),
                TriggerRate:    d.Get("trigger_rate").(int),
@@ -212,7 +228,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
                UserAgent:      d.Get("user_agent").(string),
                Status:         d.Get("status").(string),
                Uptime:         d.Get("uptime").(float64),
-               NodeLocations:  d.Get("node_locations").([]string),
+               NodeLocations:  castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()),
                PingURL:        d.Get("ping_url").(string),
                BasicUser:      d.Get("basic_user").(string),
                BasicPass:      d.Get("basic_pass").(string),
@@ -298,22 +314,19 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error {
        d.Set("port", testResp.Port)
        d.Set("trigger_rate", testResp.TriggerRate)
        d.Set("custom_header", testResp.CustomHeader)
-       d.Set("user_agent", testResp.UserAgent)
        d.Set("status", testResp.Status)
        d.Set("uptime", testResp.Uptime)
-       d.Set("node_locations", testResp.NodeLocations)
-       d.Set("ping_url", testResp.PingURL)
-       d.Set("basic_user", testResp.BasicUser)
-       d.Set("basic_pass", testResp.BasicPass)
-       d.Set("public", testResp.Public)
+       if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
+               return fmt.Errorf("[WARN] Error setting node locations: %s", err)
+       }
        d.Set("logo_image", testResp.LogoImage)
-       d.Set("branding", testResp.Branding)
-       d.Set("website_host", testResp.WebsiteHost)
-       d.Set("virus", testResp.Virus)
+       // Even after WebsiteHost is set, the API returns ""
+       // API docs aren't clear on usage will only override state if we get a non-empty value back
+       if testResp.WebsiteHost != "" {
+               d.Set("website_host", testResp.WebsiteHost)
+       }
        d.Set("find_string", testResp.FindString)
        d.Set("do_not_find", testResp.DoNotFind)
-       d.Set("real_browser", testResp.RealBrowser)
-       d.Set("test_tags", testResp.TestTags)
        d.Set("status_codes", testResp.StatusCodes)
        d.Set("use_jar", testResp.UseJar)
        d.Set("post_raw", testResp.PostRaw)
@@ -341,7 +354,7 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
                test.CheckRate = v.(int)
        }
        if v, ok := d.GetOk("contact_id"); ok {
-               test.ContactID = v.(string)
+               test.ContactID = v.(int)
        }
        if v, ok := d.GetOk("test_type"); ok {
                test.TestType = v.(string)
@@ -368,7 +381,7 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
                test.UserAgent = v.(string)
        }
        if v, ok := d.GetOk("node_locations"); ok {
-               test.NodeLocations = v.([]string)
+               test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
        }
        if v, ok := d.GetOk("ping_url"); ok {
                test.PingURL = v.(string)