]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/commitdiff
Switch node locations to a set and consider empty string to be empty set
authorMat Schaffer <mat@schaffer.me>
Thu, 1 Feb 2018 07:14:33 +0000 (16:14 +0900)
committerMat Schaffer <mat@schaffer.me>
Thu, 1 Feb 2018 07:14:33 +0000 (16:14 +0900)
Note that we also don't check the individual locations anymore. The key numbers will be randomized and not testing content seems to be the method employed by the AWS provider as well https://github.com/terraform-providers/terraform-provider-aws/blob/81bba6b1f567aed561c6a6a30916504ee0886c68/aws/resource_aws_autoscaling_group_test.go#L404

statuscake/resource_statuscaketest.go
statuscake/resource_statuscaketest_test.go

index 8e67bcdc69ef6cefc49f251a3b2a9a60c4fcbeb9..b2b0c6892ab4de7de4b266e7fc7db5683a00d204 100644 (file)
@@ -10,16 +10,24 @@ import (
        "github.com/hashicorp/terraform/helper/schema"
 )
 
-func castInterfaceToSliceStrings(in interface{}) []string {
-       input := in.([]interface{})
-       res := make([]string, len(input))
+func castSetToSliceStrings(configured []interface{}) []string {
+       res := make([]string, len(configured))
 
-       for i, element := range input {
+       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,
@@ -107,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": {
@@ -219,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:  castInterfaceToSliceStrings(d.Get("node_locations")),
+               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),
@@ -308,7 +317,7 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error {
        d.Set("user_agent", testResp.UserAgent)
        d.Set("status", testResp.Status)
        d.Set("uptime", testResp.Uptime)
-       if err := d.Set("node_locations", testResp.NodeLocations); err != nil {
+       if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil {
                return fmt.Errorf("[WARN] Error setting node locations: %s", err)
        }
        d.Set("ping_url", testResp.PingURL)
@@ -377,7 +386,7 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
                test.UserAgent = v.(string)
        }
        if v, ok := d.GetOk("node_locations"); ok {
-               test.NodeLocations = castInterfaceToSliceStrings(v)
+               test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List())
        }
        if v, ok := d.GetOk("ping_url"); ok {
                test.PingURL = v.(string)
index 62d6bf9681375a1279eb4f43cd345d9599287ccb..555cff13481e00847fd7d8fd0f6f949ed8a94c04 100644 (file)
@@ -79,9 +79,6 @@ func TestAccStatusCake_withUpdate(t *testing.T) {
                                        resource.TestCheckResourceAttr("statuscake_test.google", "status", "Up"),
                                        resource.TestCheckResourceAttr("statuscake_test.google", "uptime", "0"),
                                        resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.#", "3"),
-                                       resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.0", "string16045"),
-                                       resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.1", "string19741"),
-                                       resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.2", "string12122"),
                                        resource.TestCheckResourceAttr("statuscake_test.google", "ping_url", "string8410"),
                                        resource.TestCheckResourceAttr("statuscake_test.google", "basic_user", "string27052"),
                                        resource.TestCheckResourceAttr("statuscake_test.google", "basic_pass", "string5659"),