9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
13 func resourceStatusCakeTest() *schema.Resource {
14 return &schema.Resource{
20 Schema: map[string]*schema.Schema{
22 Type: schema.TypeString,
27 Type: schema.TypeString,
32 Type: schema.TypeString,
48 Type: schema.TypeString,
53 Type: schema.TypeBool,
83 func CreateTest(d *schema.ResourceData, meta interface{}) error {
84 client := meta.(*statuscake.Client)
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),
99 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
101 response, err := client.Tests().Update(newTest)
103 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
106 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
107 d.SetId(fmt.Sprintf("%d", response.TestID))
109 return ReadTest(d, meta)
112 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
113 client := meta.(*statuscake.Client)
115 params := getStatusCakeTestInput(d)
117 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
118 _, err := client.Tests().Update(params)
120 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
125 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
126 client := meta.(*statuscake.Client)
128 testId, parseErr := strconv.Atoi(d.Id())
132 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
133 err := client.Tests().Delete(testId)
141 func ReadTest(d *schema.ResourceData, meta interface{}) error {
142 client := meta.(*statuscake.Client)
144 testId, parseErr := strconv.Atoi(d.Id())
148 testResp, err := client.Tests().Detail(testId)
150 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
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)
166 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
167 testId, parseErr := strconv.Atoi(d.Id())
169 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
171 test := &statuscake.Test{
174 if v, ok := d.GetOk("website_name"); ok {
175 test.WebsiteName = v.(string)
177 if v, ok := d.GetOk("website_url"); ok {
178 test.WebsiteURL = v.(string)
180 if v, ok := d.GetOk("check_rate"); ok {
181 test.CheckRate = v.(int)
183 if v, ok := d.GetOk("contact_id"); ok {
184 test.ContactID = v.(int)
186 if v, ok := d.GetOk("test_type"); ok {
187 test.TestType = v.(string)
189 if v, ok := d.GetOk("paused"); ok {
190 test.Paused = v.(bool)
192 if v, ok := d.GetOk("timeout"); ok {
193 test.Timeout = v.(int)
195 if v, ok := d.GetOk("contact_id"); ok {
196 test.ContactID = v.(int)
198 if v, ok := d.GetOk("confirmations"); ok {
199 test.Confirmation = v.(int)
201 if v, ok := d.GetOk("port"); ok {
204 if v, ok := d.GetOk("trigger_rate"); ok {
205 test.TriggerRate = v.(int)
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, " +
213 test.StatusCodes = defaultStatusCodes