]>
Commit | Line | Data |
---|---|---|
478e1338 | 1 | package statuscake |
2 | ||
b8f05dfc | 3 | import ( |
4 | "fmt" | |
5 | "strconv" | |
6 | ||
7 | "log" | |
8 | ||
4eeeab64 | 9 | "github.com/DreamItGetIT/statuscake" |
b8f05dfc | 10 | "github.com/hashicorp/terraform/helper/schema" |
11 | ) | |
478e1338 | 12 | |
13 | func resourceStatusCakeTest() *schema.Resource { | |
14 | return &schema.Resource{ | |
15 | Create: CreateTest, | |
16 | Update: UpdateTest, | |
17 | Delete: DeleteTest, | |
18 | Read: ReadTest, | |
b8f05dfc | 19 | |
20 | Schema: map[string]*schema.Schema{ | |
90db3fb0 | 21 | "test_id": { |
b8f05dfc | 22 | Type: schema.TypeString, |
23 | Computed: true, | |
24 | }, | |
25 | ||
90db3fb0 | 26 | "website_name": { |
b8f05dfc | 27 | Type: schema.TypeString, |
28 | Required: true, | |
29 | }, | |
30 | ||
90db3fb0 | 31 | "website_url": { |
b8f05dfc | 32 | Type: schema.TypeString, |
33 | Required: true, | |
34 | }, | |
35 | ||
90db3fb0 | 36 | "contact_id": { |
e7b04dd5 LJ |
37 | Type: schema.TypeInt, |
38 | Optional: true, | |
39 | }, | |
40 | ||
90db3fb0 | 41 | "check_rate": { |
b8f05dfc | 42 | Type: schema.TypeInt, |
43 | Optional: true, | |
44 | Default: 300, | |
45 | }, | |
46 | ||
90db3fb0 | 47 | "test_type": { |
b8f05dfc | 48 | Type: schema.TypeString, |
49 | Required: true, | |
50 | }, | |
51 | ||
90db3fb0 | 52 | "paused": { |
b8f05dfc | 53 | Type: schema.TypeBool, |
54 | Optional: true, | |
55 | Default: false, | |
56 | }, | |
6ad7af97 | 57 | |
90db3fb0 | 58 | "timeout": { |
7dec75fc | 59 | Type: schema.TypeInt, |
6ad7af97 RG |
60 | Optional: true, |
61 | Default: 40, | |
7dec75fc | 62 | }, |
6ad7af97 | 63 | |
90db3fb0 PS |
64 | "confirmations": { |
65 | Type: schema.TypeInt, | |
66 | Optional: true, | |
67 | }, | |
68 | ||
69 | "port": { | |
b5815405 SC |
70 | Type: schema.TypeInt, |
71 | Optional: true, | |
72 | }, | |
0e962b8e RG |
73 | |
74 | "trigger_rate": { | |
75 | Type: schema.TypeInt, | |
76 | Optional: true, | |
77 | Default: 5, | |
78 | }, | |
b8f05dfc | 79 | }, |
478e1338 | 80 | } |
81 | } | |
82 | ||
83 | func CreateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 84 | client := meta.(*statuscake.Client) |
b8f05dfc | 85 | |
4eeeab64 | 86 | newTest := &statuscake.Test{ |
b5815405 SC |
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), | |
90db3fb0 | 95 | Port: d.Get("port").(int), |
0e962b8e | 96 | TriggerRate: d.Get("trigger_rate").(int), |
b8f05dfc | 97 | } |
98 | ||
b8f05dfc | 99 | log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) |
100 | ||
32574941 | 101 | response, err := client.Tests().Update(newTest) |
b8f05dfc | 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 | ||
7dec75fc | 109 | return ReadTest(d, meta) |
478e1338 | 110 | } |
111 | ||
112 | func UpdateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 113 | client := meta.(*statuscake.Client) |
7dec75fc | 114 | |
115 | params := getStatusCakeTestInput(d) | |
116 | ||
117 | log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) | |
32574941 | 118 | _, err := client.Tests().Update(params) |
7dec75fc | 119 | if err != nil { |
120 | return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) | |
121 | } | |
478e1338 | 122 | return nil |
123 | } | |
124 | ||
125 | func DeleteTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 126 | client := meta.(*statuscake.Client) |
b8f05dfc | 127 | |
128 | testId, parseErr := strconv.Atoi(d.Id()) | |
129 | if parseErr != nil { | |
130 | return parseErr | |
131 | } | |
b8f05dfc | 132 | log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) |
7dec75fc | 133 | err := client.Tests().Delete(testId) |
b8f05dfc | 134 | if err != nil { |
135 | return err | |
136 | } | |
137 | ||
478e1338 | 138 | return nil |
139 | } | |
140 | ||
141 | func ReadTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 142 | client := meta.(*statuscake.Client) |
7dec75fc | 143 | |
144 | testId, parseErr := strconv.Atoi(d.Id()) | |
145 | if parseErr != nil { | |
146 | return parseErr | |
147 | } | |
32574941 | 148 | testResp, err := client.Tests().Detail(testId) |
7dec75fc | 149 | if err != nil { |
150 | return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) | |
151 | } | |
c0701076 RG |
152 | d.Set("website_name", testResp.WebsiteName) |
153 | d.Set("website_url", testResp.WebsiteURL) | |
7dec75fc | 154 | d.Set("check_rate", testResp.CheckRate) |
c0701076 RG |
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) | |
b5815405 | 159 | d.Set("confirmations", testResp.Confirmation) |
90db3fb0 | 160 | d.Set("port", testResp.Port) |
0e962b8e | 161 | d.Set("trigger_rate", testResp.TriggerRate) |
7dec75fc | 162 | |
478e1338 | 163 | return nil |
164 | } | |
7dec75fc | 165 | |
4eeeab64 | 166 | func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { |
7dec75fc | 167 | testId, parseErr := strconv.Atoi(d.Id()) |
168 | if parseErr != nil { | |
169 | log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) | |
170 | } | |
4eeeab64 | 171 | test := &statuscake.Test{ |
7dec75fc | 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 | } | |
e7b04dd5 LJ |
183 | if v, ok := d.GetOk("contact_id"); ok { |
184 | test.ContactID = v.(int) | |
185 | } | |
7dec75fc | 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 | } | |
51688607 | 195 | if v, ok := d.GetOk("contact_id"); ok { |
196 | test.ContactID = v.(int) | |
197 | } | |
b5815405 SC |
198 | if v, ok := d.GetOk("confirmations"); ok { |
199 | test.Confirmation = v.(int) | |
200 | } | |
90db3fb0 PS |
201 | if v, ok := d.GetOk("port"); ok { |
202 | test.Port = v.(int) | |
203 | } | |
0e962b8e RG |
204 | if v, ok := d.GetOk("trigger_rate"); ok { |
205 | test.TriggerRate = v.(int) | |
206 | } | |
2a6605e9 AS |
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 | ||
7dec75fc | 215 | return test |
216 | } |