]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - statuscake/resource_statuscaketest.go
fix reading of test
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscaketest.go
index e82495588ab7e3d358dce70930cf9d4f070dc7b9..f8b69e3dadc4063d64e5251935d475137c27bb72 100644 (file)
@@ -10,22 +10,33 @@ 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,
                Update: UpdateTest,
                Delete: DeleteTest,
                Read:   ReadTest,
+               Importer: &schema.ResourceImporter{
+                       State: schema.ImportStatePassthrough,
+               },
 
                Schema: map[string]*schema.Schema{
                        "test_id": {
@@ -43,9 +54,19 @@ func resourceStatusCakeTest() *schema.Resource {
                                Required: true,
                        },
 
+                       "contact_group": {
+                               Type:          schema.TypeSet,
+                               Elem:          &schema.Schema{Type: schema.TypeString},
+                               Optional:      true,
+                               Set:           schema.HashString,
+                               ConflictsWith: []string{"contact_id"},
+                       },
+
                        "contact_id": {
-                               Type:     schema.TypeInt,
-                               Optional: true,
+                               Type:          schema.TypeInt,
+                               Optional:      true,
+                               ConflictsWith: []string{"contact_group"},
+                               Deprecated:    "use contact_group instead",
                        },
 
                        "check_rate": {
@@ -86,6 +107,7 @@ func resourceStatusCakeTest() *schema.Resource {
                                Optional: true,
                                Default:  5,
                        },
+
                        "custom_header": {
                                Type:     schema.TypeString,
                                Optional: true,
@@ -107,9 +129,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": {
@@ -169,17 +192,15 @@ func resourceStatusCakeTest() *schema.Resource {
                        },
 
                        "test_tags": {
-                               Type:     schema.TypeString,
+                               Type:     schema.TypeSet,
+                               Elem:     &schema.Schema{Type: schema.TypeString},
                                Optional: true,
+                               Set:      schema.HashString,
                        },
 
                        "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": {
@@ -197,6 +218,12 @@ func resourceStatusCakeTest() *schema.Resource {
                                Optional: true,
                        },
 
+                       "enable_ssl_alert": {
+                               Type:     schema.TypeBool,
+                               Optional: true,
+                               Default:  false,
+                       },
+
                        "follow_redirect": {
                                Type:     schema.TypeBool,
                                Optional: true,
@@ -215,7 +242,6 @@ 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").(int),
                Confirmation:   d.Get("confirmations").(int),
                Port:           d.Get("port").(int),
                TriggerRate:    d.Get("trigger_rate").(int),
@@ -223,7 +249,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),
@@ -235,14 +261,21 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
                FindString:     d.Get("find_string").(string),
                DoNotFind:      d.Get("do_not_find").(bool),
                RealBrowser:    d.Get("real_browser").(int),
-               TestTags:       d.Get("test_tags").(string),
+               TestTags:       castSetToSliceStrings(d.Get("test_tags").(*schema.Set).List()),
                StatusCodes:    d.Get("status_codes").(string),
                UseJar:         d.Get("use_jar").(int),
                PostRaw:        d.Get("post_raw").(string),
                FinalEndpoint:  d.Get("final_endpoint").(string),
+               EnableSSLAlert: d.Get("enable_ssl_alert").(bool),
                FollowRedirect: d.Get("follow_redirect").(bool),
        }
 
+       if v, ok := d.GetOk("contact_group"); ok {
+               newTest.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
+       } else if v, ok := d.GetOk("contact_id"); ok {
+               newTest.ContactID = v.(int)
+       }
+
        log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
 
        response, err := client.Tests().Update(newTest)
@@ -298,40 +331,52 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error {
        if err != nil {
                return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
        }
+
+       if v, ok := d.GetOk("contact_group"); ok {
+               d.Set("contact_group", v)
+       } else if v, ok := d.GetOk("contact_id"); ok {
+               d.Set("contact_id", v)
+       }
+
+       d.Set("contact_group", testResp.ContactGroup)
        d.Set("website_name", testResp.WebsiteName)
        d.Set("website_url", testResp.WebsiteURL)
        d.Set("check_rate", testResp.CheckRate)
        d.Set("test_type", testResp.TestType)
        d.Set("paused", testResp.Paused)
        d.Set("timeout", testResp.Timeout)
-       d.Set("contact_id", testResp.ContactID)
        d.Set("confirmations", testResp.Confirmation)
        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)
-       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)
-       d.Set("basic_user", testResp.BasicUser)
-       d.Set("basic_pass", testResp.BasicPass)
-       d.Set("public", testResp.Public)
        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("user_agent", testResp.UserAgent)
        d.Set("post_raw", testResp.PostRaw)
        d.Set("final_endpoint", testResp.FinalEndpoint)
+       d.Set("enable_ssl_alert", testResp.EnableSSLAlert)
        d.Set("follow_redirect", testResp.FollowRedirect)
+       d.Set("ping_url", testResp.PingURL)
+       d.Set("basic_user", testResp.BasicUser)
+       d.Set("basic_pass", testResp.BasicPass)
+       d.Set("public", testResp.Public)
+       d.Set("branding", testResp.Branding)
+       d.Set("virus", testResp.Virus)
+       d.Set("real_browser", testResp.RealBrowser)
+       d.Set("test_tags", testResp.TestTags)
 
        return nil
 }
@@ -353,7 +398,9 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
        if v, ok := d.GetOk("check_rate"); ok {
                test.CheckRate = v.(int)
        }
-       if v, ok := d.GetOk("contact_id"); ok {
+       if v, ok := d.GetOk("contact_group"); ok {
+               test.ContactGroup = castSetToSliceStrings(v.(*schema.Set).List())
+       } else if v, ok := d.GetOk("contact_id"); ok {
                test.ContactID = v.(int)
        }
        if v, ok := d.GetOk("test_type"); ok {
@@ -381,7 +428,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)
@@ -417,7 +464,7 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
                test.RealBrowser = v.(int)
        }
        if v, ok := d.GetOk("test_tags"); ok {
-               test.TestTags = v.(string)
+               test.TestTags = castSetToSliceStrings(v.(*schema.Set).List())
        }
        if v, ok := d.GetOk("status_codes"); ok {
                test.StatusCodes = v.(string)
@@ -431,6 +478,9 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
        if v, ok := d.GetOk("final_endpoint"); ok {
                test.FinalEndpoint = v.(string)
        }
+       if v, ok := d.GetOk("enable_ssl_alert"); ok {
+               test.EnableSSLAlert = v.(bool)
+       }
        if v, ok := d.GetOk("follow_redirect"); ok {
                test.FollowRedirect = v.(bool)
        }