aboutsummaryrefslogtreecommitdiffhomepage
path: root/statuscake/resource_statuscaketest.go
diff options
context:
space:
mode:
Diffstat (limited to 'statuscake/resource_statuscaketest.go')
-rw-r--r--statuscake/resource_statuscaketest.go216
1 files changed, 216 insertions, 0 deletions
diff --git a/statuscake/resource_statuscaketest.go b/statuscake/resource_statuscaketest.go
new file mode 100644
index 0000000..101ee83
--- /dev/null
+++ b/statuscake/resource_statuscaketest.go
@@ -0,0 +1,216 @@
1package statuscake
2
3import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11)
12
13func resourceStatusCakeTest() *schema.Resource {
14 return &schema.Resource{
15 Create: CreateTest,
16 Update: UpdateTest,
17 Delete: DeleteTest,
18 Read: ReadTest,
19
20 Schema: map[string]*schema.Schema{
21 "test_id": {
22 Type: schema.TypeString,
23 Computed: true,
24 },
25
26 "website_name": {
27 Type: schema.TypeString,
28 Required: true,
29 },
30
31 "website_url": {
32 Type: schema.TypeString,
33 Required: true,
34 },
35
36 "contact_id": {
37 Type: schema.TypeInt,
38 Optional: true,
39 },
40
41 "check_rate": {
42 Type: schema.TypeInt,
43 Optional: true,
44 Default: 300,
45 },
46
47 "test_type": {
48 Type: schema.TypeString,
49 Required: true,
50 },
51
52 "paused": {
53 Type: schema.TypeBool,
54 Optional: true,
55 Default: false,
56 },
57
58 "timeout": {
59 Type: schema.TypeInt,
60 Optional: true,
61 Default: 40,
62 },
63
64 "confirmations": {
65 Type: schema.TypeInt,
66 Optional: true,
67 },
68
69 "port": {
70 Type: schema.TypeInt,
71 Optional: true,
72 },
73
74 "trigger_rate": {
75 Type: schema.TypeInt,
76 Optional: true,
77 Default: 5,
78 },
79 },
80 }
81}
82
83func CreateTest(d *schema.ResourceData, meta interface{}) error {
84 client := meta.(*statuscake.Client)
85
86 newTest := &statuscake.Test{
87 WebsiteName: d.Get("website_name").(string),
88 WebsiteURL: d.Get("website_url").(string),
89 CheckRate: d.Get("check_rate").(int),
90 TestType: d.Get("test_type").(string),
91 Paused: d.Get("paused").(bool),
92 Timeout: d.Get("timeout").(int),
93 ContactID: d.Get("contact_id").(int),
94 Confirmation: d.Get("confirmations").(int),
95 Port: d.Get("port").(int),
96 TriggerRate: d.Get("trigger_rate").(int),
97 }
98
99 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
100
101 response, err := client.Tests().Update(newTest)
102 if err != nil {
103 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
104 }
105
106 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
107 d.SetId(fmt.Sprintf("%d", response.TestID))
108
109 return ReadTest(d, meta)
110}
111
112func UpdateTest(d *schema.ResourceData, meta interface{}) error {
113 client := meta.(*statuscake.Client)
114
115 params := getStatusCakeTestInput(d)
116
117 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
118 _, err := client.Tests().Update(params)
119 if err != nil {
120 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
121 }
122 return nil
123}
124
125func DeleteTest(d *schema.ResourceData, meta interface{}) error {
126 client := meta.(*statuscake.Client)
127
128 testId, parseErr := strconv.Atoi(d.Id())
129 if parseErr != nil {
130 return parseErr
131 }
132 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
133 err := client.Tests().Delete(testId)
134 if err != nil {
135 return err
136 }
137
138 return nil
139}
140
141func ReadTest(d *schema.ResourceData, meta interface{}) error {
142 client := meta.(*statuscake.Client)
143
144 testId, parseErr := strconv.Atoi(d.Id())
145 if parseErr != nil {
146 return parseErr
147 }
148 testResp, err := client.Tests().Detail(testId)
149 if err != nil {
150 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
151 }
152 d.Set("website_name", testResp.WebsiteName)
153 d.Set("website_url", testResp.WebsiteURL)
154 d.Set("check_rate", testResp.CheckRate)
155 d.Set("test_type", testResp.TestType)
156 d.Set("paused", testResp.Paused)
157 d.Set("timeout", testResp.Timeout)
158 d.Set("contact_id", testResp.ContactID)
159 d.Set("confirmations", testResp.Confirmation)
160 d.Set("port", testResp.Port)
161 d.Set("trigger_rate", testResp.TriggerRate)
162
163 return nil
164}
165
166func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
167 testId, parseErr := strconv.Atoi(d.Id())
168 if parseErr != nil {
169 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
170 }
171 test := &statuscake.Test{
172 TestID: testId,
173 }
174 if v, ok := d.GetOk("website_name"); ok {
175 test.WebsiteName = v.(string)
176 }
177 if v, ok := d.GetOk("website_url"); ok {
178 test.WebsiteURL = v.(string)
179 }
180 if v, ok := d.GetOk("check_rate"); ok {
181 test.CheckRate = v.(int)
182 }
183 if v, ok := d.GetOk("contact_id"); ok {
184 test.ContactID = v.(int)
185 }
186 if v, ok := d.GetOk("test_type"); ok {
187 test.TestType = v.(string)
188 }
189 if v, ok := d.GetOk("paused"); ok {
190 test.Paused = v.(bool)
191 }
192 if v, ok := d.GetOk("timeout"); ok {
193 test.Timeout = v.(int)
194 }
195 if v, ok := d.GetOk("contact_id"); ok {
196 test.ContactID = v.(int)
197 }
198 if v, ok := d.GetOk("confirmations"); ok {
199 test.Confirmation = v.(int)
200 }
201 if v, ok := d.GetOk("port"); ok {
202 test.Port = v.(int)
203 }
204 if v, ok := d.GetOk("trigger_rate"); ok {
205 test.TriggerRate = v.(int)
206 }
207
208 defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
209 "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
210 "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
211 "598, 599"
212
213 test.StatusCodes = defaultStatusCodes
214
215 return test
216}