diff --git a/builtin/providers/statuscake/provider.go b/builtin/providers/statuscake/provider.go index 7402e8079..abca37698 100644 --- a/builtin/providers/statuscake/provider.go +++ b/builtin/providers/statuscake/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/builtin/providers/statuscake/resource_statuscaketest.go b/builtin/providers/statuscake/resource_statuscaketest.go index f34a97a71..8eb5401f1 100644 --- a/builtin/providers/statuscake/resource_statuscaketest.go +++ b/builtin/providers/statuscake/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/builtin/providers/statuscake/resource_statuscaketest_test.go b/builtin/providers/statuscake/resource_statuscaketest_test.go new file mode 100644 index 000000000..8bbd4b5ba --- /dev/null +++ b/builtin/providers/statuscake/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 +} +`