]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/commitdiff
Adding the update and read functionality for the statuscake client
authorstack72 <public@paulstack.co.uk>
Sat, 3 Oct 2015 22:11:26 +0000 (23:11 +0100)
committerstack72 <public@paulstack.co.uk>
Fri, 27 Nov 2015 15:03:13 +0000 (15:03 +0000)
provider.go
resource_statuscaketest.go

index abca37698274568c5ddc4d84d1c18ba5aa4db892..7d96e0ebf987255dd0ac1f3688afca8ecfaa1263 100644 (file)
@@ -1,7 +1,7 @@
 package statuscake
 
 import (
-       "github.com/DreamItGetIT/statuscake"
+       wtf "github.com/DreamItGetIT/statuscake"
        "github.com/hashicorp/terraform/helper/schema"
        "github.com/hashicorp/terraform/terraform"
 )
@@ -32,9 +32,9 @@ func Provider() terraform.ResourceProvider {
 }
 
 func providerConfigure(d *schema.ResourceData) (interface{}, error) {
-       auth := statuscake.Auth{
+       auth := wtf.Auth{
                Username: d.Get("username").(string),
                Apikey:   d.Get("apikey").(string),
        }
-       return statuscake.New(auth)
+       return wtf.New(auth)
 }
index 8eb5401f1b05f9d80b7f5b1265b76a3d96964624..683413583542bcb6652c2e0c51d670f42330cca2 100644 (file)
@@ -6,7 +6,7 @@ import (
 
        "log"
 
-       "github.com/DreamItGetIT/statuscake"
+       wtf "github.com/DreamItGetIT/statuscake"
        "github.com/hashicorp/terraform/helper/schema"
 )
 
@@ -49,29 +49,24 @@ func resourceStatusCakeTest() *schema.Resource {
                                Optional: true,
                                Default:  false,
                        },
+                       "timeout": &schema.Schema{
+                               Type:     schema.TypeInt,
+                               Computed: true,
+                       },
                },
        }
 }
 
 func CreateTest(d *schema.ResourceData, meta interface{}) error {
-       client := meta.(*statuscake.Client)
+       client := meta.(*wtf.Client)
 
-       newTest := &statuscake.Test{
-               WebsiteName: "posters.dreamitget.it",
-               WebsiteURL:  "https://posters.dreamitget.it",
-               TestType:    "HTTP",
-               CheckRate:   500,
+       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),
-       //              TestType:    d.Get("test_type").(string),
-       //              CheckRate:   500,
-       //      }
-
-       log.Printf("[DEBUG] Check Rate: %d", d.Get("check_rate").(int))
-       log.Printf("[DEBUG] TestType: %s", d.Get("test_type").(string))
        log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
 
        response, err := client.Tests().Put(newTest)
@@ -82,24 +77,31 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
        d.Set("test_id", fmt.Sprintf("%d", response.TestID))
        d.SetId(fmt.Sprintf("%d", response.TestID))
 
-       return UpdateTest(d, meta)
+       return ReadTest(d, meta)
 }
 
 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
+       client := meta.(*wtf.Client)
+
+       params := getStatusCakeTestInput(d)
+
+       log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
+       _, err := client.Tests().Put(params)
+       if err != nil {
+               return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
+       }
        return nil
 }
 
 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
-       client := meta.(*statuscake.Client)
+       client := meta.(*wtf.Client)
 
        testId, parseErr := strconv.Atoi(d.Id())
        if parseErr != nil {
                return parseErr
        }
-       testIntId := int(testId)
-
        log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
-       err := client.Tests().Delete(testIntId)
+       err := client.Tests().Delete(testId)
        if err != nil {
                return err
        }
@@ -108,5 +110,46 @@ func DeleteTest(d *schema.ResourceData, meta interface{}) error {
 }
 
 func ReadTest(d *schema.ResourceData, meta interface{}) error {
+       client := meta.(*wtf.Client)
+
+       testId, parseErr := strconv.Atoi(d.Id())
+       if parseErr != nil {
+               return parseErr
+       }
+       testResp, err := client.Tests().Details(testId)
+       if err != nil {
+               return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
+       }
+       d.Set("check_rate", testResp.CheckRate)
+
        return nil
 }
+
+func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test {
+       testId, parseErr := strconv.Atoi(d.Id())
+       if parseErr != nil {
+               log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
+       }
+       test := &wtf.Test{
+               TestID: testId,
+       }
+       if v, ok := d.GetOk("website_name"); ok {
+               test.WebsiteName = v.(string)
+       }
+       if v, ok := d.GetOk("website_url"); ok {
+               test.WebsiteURL = v.(string)
+       }
+       if v, ok := d.GetOk("check_rate"); ok {
+               test.CheckRate = v.(int)
+       }
+       if v, ok := d.GetOk("test_type"); ok {
+               test.TestType = v.(string)
+       }
+       if v, ok := d.GetOk("paused"); ok {
+               test.Paused = v.(bool)
+       }
+       if v, ok := d.GetOk("timeout"); ok {
+               test.Timeout = v.(int)
+       }
+       return test
+}