From 7dec75fcd796fa25f655c1f1cda2d202f117eb4b Mon Sep 17 00:00:00 2001 From: stack72 Date: Sat, 3 Oct 2015 23:11:26 +0100 Subject: Adding the update and read functionality for the statuscake client --- provider.go | 6 ++-- resource_statuscaketest.go | 85 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/provider.go b/provider.go index abca376..7d96e0e 100644 --- a/provider.go +++ b/provider.go @@ -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) } diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 8eb5401..6834135 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -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 +} -- cgit v1.2.3