]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - resource_statuscaketest.go
provider/statuscake: use default status code list when updating test (#12375)
[github/fretlink/terraform-provider-statuscake.git] / resource_statuscaketest.go
index 17488a565254f4727bcc64fcddd00edd540d7486..4912758d4049fb39dbf9e5fed196e36ea12cfdb5 100644 (file)
@@ -6,7 +6,7 @@ import (
 
        "log"
 
-       wtf "github.com/DreamItGetIT/statuscake"
+       "github.com/DreamItGetIT/statuscake"
        "github.com/hashicorp/terraform/helper/schema"
 )
 
@@ -18,53 +18,75 @@ func resourceStatusCakeTest() *schema.Resource {
                Read:   ReadTest,
 
                Schema: map[string]*schema.Schema{
-                       "test_id": &schema.Schema{
+                       "test_id": {
                                Type:     schema.TypeString,
                                Computed: true,
                        },
 
-                       "website_name": &schema.Schema{
+                       "website_name": {
                                Type:     schema.TypeString,
                                Required: true,
                        },
 
-                       "website_url": &schema.Schema{
+                       "website_url": {
                                Type:     schema.TypeString,
                                Required: true,
                        },
 
-                       "check_rate": &schema.Schema{
+                       "contact_id": {
+                               Type:     schema.TypeInt,
+                               Optional: true,
+                       },
+
+                       "check_rate": {
                                Type:     schema.TypeInt,
                                Optional: true,
                                Default:  300,
                        },
 
-                       "test_type": &schema.Schema{
+                       "test_type": {
                                Type:     schema.TypeString,
                                Required: true,
                        },
 
-                       "paused": &schema.Schema{
+                       "paused": {
                                Type:     schema.TypeBool,
                                Optional: true,
                                Default:  false,
                        },
-                       "timeout": &schema.Schema{
+
+                       "timeout": {
                                Type:     schema.TypeInt,
-                               Computed: true,
+                               Optional: true,
+                               Default:  40,
+                       },
+
+                       "confirmations": {
+                               Type:     schema.TypeInt,
+                               Optional: true,
+                       },
+
+                       "port": {
+                               Type:     schema.TypeInt,
+                               Optional: true,
                        },
                },
        }
 }
 
 func CreateTest(d *schema.ResourceData, meta interface{}) error {
-       client := meta.(*wtf.Client)
+       client := meta.(*statuscake.Client)
 
-       newTest := &wtf.Test{
-               WebsiteName: d.Get("website_name").(string),
-               WebsiteURL:  d.Get("website_url").(string),
-               TestType:    d.Get("test_type").(string),
-               CheckRate:   d.Get("check_rate").(int),
+       newTest := &statuscake.Test{
+               WebsiteName:  d.Get("website_name").(string),
+               WebsiteURL:   d.Get("website_url").(string),
+               CheckRate:    d.Get("check_rate").(int),
+               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),
        }
 
        log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
@@ -81,7 +103,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
 }
 
 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
-       client := meta.(*wtf.Client)
+       client := meta.(*statuscake.Client)
 
        params := getStatusCakeTestInput(d)
 
@@ -94,7 +116,7 @@ func UpdateTest(d *schema.ResourceData, meta interface{}) error {
 }
 
 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
-       client := meta.(*wtf.Client)
+       client := meta.(*statuscake.Client)
 
        testId, parseErr := strconv.Atoi(d.Id())
        if parseErr != nil {
@@ -110,7 +132,7 @@ func DeleteTest(d *schema.ResourceData, meta interface{}) error {
 }
 
 func ReadTest(d *schema.ResourceData, meta interface{}) error {
-       client := meta.(*wtf.Client)
+       client := meta.(*statuscake.Client)
 
        testId, parseErr := strconv.Atoi(d.Id())
        if parseErr != nil {
@@ -120,17 +142,25 @@ 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)
        }
+       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)
 
        return nil
 }
 
-func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test {
+func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
        testId, parseErr := strconv.Atoi(d.Id())
        if parseErr != nil {
                log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
        }
-       test := &wtf.Test{
+       test := &statuscake.Test{
                TestID: testId,
        }
        if v, ok := d.GetOk("website_name"); ok {
@@ -142,6 +172,9 @@ func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test {
        if v, ok := d.GetOk("check_rate"); ok {
                test.CheckRate = v.(int)
        }
+       if v, ok := d.GetOk("contact_id"); ok {
+               test.ContactID = v.(int)
+       }
        if v, ok := d.GetOk("test_type"); ok {
                test.TestType = v.(string)
        }
@@ -151,5 +184,22 @@ func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test {
        if v, ok := d.GetOk("timeout"); ok {
                test.Timeout = v.(int)
        }
+       if v, ok := d.GetOk("contact_id"); ok {
+               test.ContactID = v.(int)
+       }
+       if v, ok := d.GetOk("confirmations"); ok {
+               test.Confirmation = v.(int)
+       }
+       if v, ok := d.GetOk("port"); ok {
+               test.Port = v.(int)
+       }
+
+       defaultStatusCodes := "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"
+
+       test.StatusCodes = defaultStatusCodes
+
        return test
 }