From b8f05dfc572fcdd53e58e0a1e3851ccc4db8f17e Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 1 Oct 2015 23:09:13 +0100 Subject: Using the latest version of the statuscake client --- provider.go | 9 +++-- resource_statuscaketest.go | 88 ++++++++++++++++++++++++++++++++++++++++- resource_statuscaketest_test.go | 76 +++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 resource_statuscaketest_test.go diff --git a/provider.go b/provider.go index 7402e80..abca376 100644 --- a/provider.go +++ b/provider.go @@ -32,8 +32,9 @@ func Provider() terraform.ResourceProvider { } func providerConfigure(d *schema.ResourceData) (interface{}, error) { - username := d.Get("username").(string) - apiKey := d.Get("apikey").(string) - - return statuscake.New(apiKey, username), nil + auth := statuscake.Auth{ + Username: d.Get("username").(string), + Apikey: d.Get("apikey").(string), + } + return statuscake.New(auth) } diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index f34a97a..8eb5401 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -1,6 +1,14 @@ package statuscake -import "github.com/hashicorp/terraform/helper/schema" +import ( + "fmt" + "strconv" + + "log" + + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/schema" +) func resourceStatusCakeTest() *schema.Resource { return &schema.Resource{ @@ -8,11 +16,73 @@ func resourceStatusCakeTest() *schema.Resource { Update: UpdateTest, Delete: DeleteTest, Read: ReadTest, + + Schema: map[string]*schema.Schema{ + "test_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "website_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "website_url": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "check_rate": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 300, + }, + + "test_type": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "paused": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, } } func CreateTest(d *schema.ResourceData, meta interface{}) error { - return nil + client := meta.(*statuscake.Client) + + newTest := &statuscake.Test{ + WebsiteName: "posters.dreamitget.it", + WebsiteURL: "https://posters.dreamitget.it", + TestType: "HTTP", + CheckRate: 500, + } + + // 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) + if err != nil { + return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) + } + + d.Set("test_id", fmt.Sprintf("%d", response.TestID)) + d.SetId(fmt.Sprintf("%d", response.TestID)) + + return UpdateTest(d, meta) } func UpdateTest(d *schema.ResourceData, meta interface{}) error { @@ -20,6 +90,20 @@ func UpdateTest(d *schema.ResourceData, meta interface{}) error { } func DeleteTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*statuscake.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) + if err != nil { + return err + } + return nil } diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go new file mode 100644 index 0000000..8bbd4b5 --- /dev/null +++ b/resource_statuscaketest_test.go @@ -0,0 +1,76 @@ +package statuscake + +import ( + "fmt" + "testing" + + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccStatusCake_basic(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTestConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + ), + }, + }, + }) +} + +func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[rn] + if !ok { + return fmt.Errorf("resource not found: %s", rn) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("TestID not set") + } + + // client := testAccProvider.Meta().(*statuscake.Client) + // gotProject, err := client.GetProject(rs.Primary.ID) + // if err != nil { + // return fmt.Errorf("error getting project: %s", err) + // } + // + // *project = *gotProject + + return nil + } +} + +func testAccTestCheckDestroy(project *statuscake.Test) resource.TestCheckFunc { + // return func(s *terraform.State) error { + // client := testAccProvider.Meta().(*statuscake.Client) + // // _, err := client.Tests().All() + // // if err == nil { + // // return fmt.Errorf("test still exists") + // // } + // // if _, ok := err.(*statuscake.NotFoundError); !ok { + // // return fmt.Errorf("got something other than NotFoundError (%v) when getting test", err) + // // } + // + // return nil + // } + return nil +} + +const testAccTestConfig_basic = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 300 +} +` -- cgit v1.2.3