Skip to content

Commit 34b77aa

Browse files
committed
Adding the update and read functionality for the statuscake client
1 parent 97c0aa5 commit 34b77aa

2 files changed

Lines changed: 67 additions & 24 deletions

File tree

builtin/providers/statuscake/provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package statuscake
22

33
import (
4-
"github.com/DreamItGetIT/statuscake"
4+
wtf "github.com/DreamItGetIT/statuscake"
55
"github.com/hashicorp/terraform/helper/schema"
66
"github.com/hashicorp/terraform/terraform"
77
)
@@ -32,9 +32,9 @@ func Provider() terraform.ResourceProvider {
3232
}
3333

3434
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
35-
auth := statuscake.Auth{
35+
auth := wtf.Auth{
3636
Username: d.Get("username").(string),
3737
Apikey: d.Get("apikey").(string),
3838
}
39-
return statuscake.New(auth)
39+
return wtf.New(auth)
4040
}

builtin/providers/statuscake/resource_statuscaketest.go

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"log"
88

9-
"github.com/DreamItGetIT/statuscake"
9+
wtf "github.com/DreamItGetIT/statuscake"
1010
"github.com/hashicorp/terraform/helper/schema"
1111
)
1212

@@ -49,29 +49,24 @@ func resourceStatusCakeTest() *schema.Resource {
4949
Optional: true,
5050
Default: false,
5151
},
52+
"timeout": &schema.Schema{
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
},
5256
},
5357
}
5458
}
5559

5660
func CreateTest(d *schema.ResourceData, meta interface{}) error {
57-
client := meta.(*statuscake.Client)
61+
client := meta.(*wtf.Client)
5862

59-
newTest := &statuscake.Test{
60-
WebsiteName: "posters.dreamitget.it",
61-
WebsiteURL: "https://posters.dreamitget.it",
62-
TestType: "HTTP",
63-
CheckRate: 500,
63+
newTest := &wtf.Test{
64+
WebsiteName: d.Get("website_name").(string),
65+
WebsiteURL: d.Get("website_url").(string),
66+
TestType: d.Get("test_type").(string),
67+
CheckRate: d.Get("check_rate").(int),
6468
}
6569

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))
7570
log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
7671

7772
response, err := client.Tests().Put(newTest)
@@ -82,24 +77,31 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
8277
d.Set("test_id", fmt.Sprintf("%d", response.TestID))
8378
d.SetId(fmt.Sprintf("%d", response.TestID))
8479

85-
return UpdateTest(d, meta)
80+
return ReadTest(d, meta)
8681
}
8782

8883
func UpdateTest(d *schema.ResourceData, meta interface{}) error {
84+
client := meta.(*wtf.Client)
85+
86+
params := getStatusCakeTestInput(d)
87+
88+
log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
89+
_, err := client.Tests().Put(params)
90+
if err != nil {
91+
return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
92+
}
8993
return nil
9094
}
9195

9296
func DeleteTest(d *schema.ResourceData, meta interface{}) error {
93-
client := meta.(*statuscake.Client)
97+
client := meta.(*wtf.Client)
9498

9599
testId, parseErr := strconv.Atoi(d.Id())
96100
if parseErr != nil {
97101
return parseErr
98102
}
99-
testIntId := int(testId)
100-
101103
log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
102-
err := client.Tests().Delete(testIntId)
104+
err := client.Tests().Delete(testId)
103105
if err != nil {
104106
return err
105107
}
@@ -108,5 +110,46 @@ func DeleteTest(d *schema.ResourceData, meta interface{}) error {
108110
}
109111

110112
func ReadTest(d *schema.ResourceData, meta interface{}) error {
113+
client := meta.(*wtf.Client)
114+
115+
testId, parseErr := strconv.Atoi(d.Id())
116+
if parseErr != nil {
117+
return parseErr
118+
}
119+
testResp, err := client.Tests().Details(testId)
120+
if err != nil {
121+
return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
122+
}
123+
d.Set("check_rate", testResp.CheckRate)
124+
111125
return nil
112126
}
127+
128+
func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test {
129+
testId, parseErr := strconv.Atoi(d.Id())
130+
if parseErr != nil {
131+
log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
132+
}
133+
test := &wtf.Test{
134+
TestID: testId,
135+
}
136+
if v, ok := d.GetOk("website_name"); ok {
137+
test.WebsiteName = v.(string)
138+
}
139+
if v, ok := d.GetOk("website_url"); ok {
140+
test.WebsiteURL = v.(string)
141+
}
142+
if v, ok := d.GetOk("check_rate"); ok {
143+
test.CheckRate = v.(int)
144+
}
145+
if v, ok := d.GetOk("test_type"); ok {
146+
test.TestType = v.(string)
147+
}
148+
if v, ok := d.GetOk("paused"); ok {
149+
test.Paused = v.(bool)
150+
}
151+
if v, ok := d.GetOk("timeout"); ok {
152+
test.Timeout = v.(int)
153+
}
154+
return test
155+
}

0 commit comments

Comments
 (0)