]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/commitdiff
Using the latest version of the statuscake client
authorstack72 <public@paulstack.co.uk>
Thu, 1 Oct 2015 22:09:13 +0000 (23:09 +0100)
committerstack72 <public@paulstack.co.uk>
Fri, 27 Nov 2015 15:03:13 +0000 (15:03 +0000)
provider.go
resource_statuscaketest.go
resource_statuscaketest_test.go [new file with mode: 0644]

index 7402e8079cd0dc170bc0317714bed2f1df00399b..abca37698274568c5ddc4d84d1c18ba5aa4db892 100644 (file)
@@ -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)
 }
index f34a97a7187df56b388ad0833de5dc48bfec08fb..8eb5401f1b05f9d80b7f5b1265b76a3d96964624 100644 (file)
@@ -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 (file)
index 0000000..8bbd4b5
--- /dev/null
@@ -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
+}
+`