aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--provider.go9
-rw-r--r--resource_statuscaketest.go88
-rw-r--r--resource_statuscaketest_test.go76
3 files changed, 167 insertions, 6 deletions
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 {
32} 32}
33 33
34func providerConfigure(d *schema.ResourceData) (interface{}, error) { 34func providerConfigure(d *schema.ResourceData) (interface{}, error) {
35 username := d.Get("username").(string) 35 auth := statuscake.Auth{
36 apiKey := d.Get("apikey").(string) 36 Username: d.Get("username").(string),
37 37 Apikey: d.Get("apikey").(string),
38 return statuscake.New(apiKey, username), nil 38 }
39 return statuscake.New(auth)
39} 40}
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 @@
1package statuscake 1package statuscake
2 2
3import "github.com/hashicorp/terraform/helper/schema" 3import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11)
4 12
5func resourceStatusCakeTest() *schema.Resource { 13func resourceStatusCakeTest() *schema.Resource {
6 return &schema.Resource{ 14 return &schema.Resource{
@@ -8,11 +16,73 @@ func resourceStatusCakeTest() *schema.Resource {
8 Update: UpdateTest, 16 Update: UpdateTest,
9 Delete: DeleteTest, 17 Delete: DeleteTest,
10 Read: ReadTest, 18 Read: ReadTest,
19
20 Schema: map[string]*schema.Schema{
21 "test_id": &schema.Schema{
22 Type: schema.TypeString,
23 Computed: true,
24 },
25
26 "website_name": &schema.Schema{
27 Type: schema.TypeString,
28 Required: true,
29 },
30
31 "website_url": &schema.Schema{
32 Type: schema.TypeString,
33 Required: true,
34 },
35
36 "check_rate": &schema.Schema{
37 Type: schema.TypeInt,
38 Optional: true,
39 Default: 300,
40 },
41
42 "test_type": &schema.Schema{
43 Type: schema.TypeString,
44 Required: true,
45 },
46
47 "paused": &schema.Schema{
48 Type: schema.TypeBool,
49 Optional: true,
50 Default: false,
51 },
52 },
11 } 53 }
12} 54}
13 55
14func CreateTest(d *schema.ResourceData, meta interface{}) error { 56func CreateTest(d *schema.ResourceData, meta interface{}) error {
15 return nil 57 client := meta.(*statuscake.Client)
58
59 newTest := &statuscake.Test{
60 WebsiteName: "posters.dreamitget.it",
61 WebsiteURL: "https://posters.dreamitget.it",
62 TestType: "HTTP",
63 CheckRate: 500,
64 }
65
66 // newTest := &statuscake.Test{
67 // WebsiteName: d.Get("website_name").(string),
68 // WebsiteURL: d.Get("website_url").(string),
69 // TestType: d.Get("test_type").(string),
70 // CheckRate: 500,
71 // }
72
73 log.Printf("[DEBUG] Check Rate: %d", d.Get("check_rate").(int))
74 log.Printf("[DEBUG] TestType: %s", d.Get("test_type").(string))
75 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
76
77 response, err := client.Tests().Put(newTest)
78 if err != nil {
79 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
80 }
81
82 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
83 d.SetId(fmt.Sprintf("%d", response.TestID))
84
85 return UpdateTest(d, meta)
16} 86}
17 87
18func UpdateTest(d *schema.ResourceData, meta interface{}) error { 88func UpdateTest(d *schema.ResourceData, meta interface{}) error {
@@ -20,6 +90,20 @@ func UpdateTest(d *schema.ResourceData, meta interface{}) error {
20} 90}
21 91
22func DeleteTest(d *schema.ResourceData, meta interface{}) error { 92func DeleteTest(d *schema.ResourceData, meta interface{}) error {
93 client := meta.(*statuscake.Client)
94
95 testId, parseErr := strconv.Atoi(d.Id())
96 if parseErr != nil {
97 return parseErr
98 }
99 testIntId := int(testId)
100
101 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
102 err := client.Tests().Delete(testIntId)
103 if err != nil {
104 return err
105 }
106
23 return nil 107 return nil
24} 108}
25 109
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 @@
1package statuscake
2
3import (
4 "fmt"
5 "testing"
6
7 "github.com/DreamItGetIT/statuscake"
8 "github.com/hashicorp/terraform/helper/resource"
9 "github.com/hashicorp/terraform/terraform"
10)
11
12func TestAccStatusCake_basic(t *testing.T) {
13 var test statuscake.Test
14
15 resource.Test(t, resource.TestCase{
16 PreCheck: func() { testAccPreCheck(t) },
17 Providers: testAccProviders,
18 CheckDestroy: testAccTestCheckDestroy(&test),
19 Steps: []resource.TestStep{
20 resource.TestStep{
21 Config: testAccTestConfig_basic,
22 Check: resource.ComposeTestCheckFunc(
23 testAccTestCheckExists("statuscake_test.google", &test),
24 ),
25 },
26 },
27 })
28}
29
30func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCheckFunc {
31 return func(s *terraform.State) error {
32 rs, ok := s.RootModule().Resources[rn]
33 if !ok {
34 return fmt.Errorf("resource not found: %s", rn)
35 }
36
37 if rs.Primary.ID == "" {
38 return fmt.Errorf("TestID not set")
39 }
40
41 // client := testAccProvider.Meta().(*statuscake.Client)
42 // gotProject, err := client.GetProject(rs.Primary.ID)
43 // if err != nil {
44 // return fmt.Errorf("error getting project: %s", err)
45 // }
46 //
47 // *project = *gotProject
48
49 return nil
50 }
51}
52
53func testAccTestCheckDestroy(project *statuscake.Test) resource.TestCheckFunc {
54 // return func(s *terraform.State) error {
55 // client := testAccProvider.Meta().(*statuscake.Client)
56 // // _, err := client.Tests().All()
57 // // if err == nil {
58 // // return fmt.Errorf("test still exists")
59 // // }
60 // // if _, ok := err.(*statuscake.NotFoundError); !ok {
61 // // return fmt.Errorf("got something other than NotFoundError (%v) when getting test", err)
62 // // }
63 //
64 // return nil
65 // }
66 return nil
67}
68
69const testAccTestConfig_basic = `
70resource "statuscake_test" "google" {
71 website_name = "google.com"
72 website_url = "www.google.com"
73 test_type = "HTTP"
74 check_rate = 300
75}
76`