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,
77 func CreateTest(d *schema.ResourceData, meta interface{}) error {
78 client := meta.(*statuscake.Client)
80 newTest := &statuscake.Test{
81 WebsiteName: d.Get("website_name").(string),
82 WebsiteURL: d.Get("website_url").(string),
83 CheckRate: d.Get("check_rate").(int),
84 TestType: d.Get("test_type").(string),
85 Paused: d.Get("paused").(bool),
86 Timeout: d.Get("timeout").(int),
87 ContactID: d.Get("contact_id").(int),
88 Confirmation: d.Get("confirmations").(int),
89 Port: d.Get("port").(int),
92 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
94 response, err := client.Tests().Update(newTest)
96 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
99 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
100 d.SetId(fmt.Sprintf("%d", response.TestID))
102 return ReadTest(d, meta)
105 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
106 client := meta.(*statuscake.Client)
108 params := getStatusCakeTestInput(d)
110 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
111 _, err := client.Tests().Update(params)
113 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
118 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
119 client := meta.(*statuscake.Client)
121 testId, parseErr := strconv.Atoi(d.Id())
125 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
126 err := client.Tests().Delete(testId)
134 func ReadTest(d *schema.ResourceData, meta interface{}) error {
135 client := meta.(*statuscake.Client)
137 testId, parseErr := strconv.Atoi(d.Id())
141 testResp, err := client.Tests().Detail(testId)
143 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
145 d.Set("website_name", testResp.WebsiteName)
146 d.Set("website_url", testResp.WebsiteURL)
147 d.Set("check_rate", testResp.CheckRate)
148 d.Set("test_type", testResp.TestType)
149 d.Set("paused", testResp.Paused)
150 d.Set("timeout", testResp.Timeout)
151 d.Set("contact_id", testResp.ContactID)
152 d.Set("confirmations", testResp.Confirmation)
153 d.Set("port", testResp.Port)
158 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
159 testId, parseErr := strconv.Atoi(d.Id())
161 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
163 test := &statuscake.Test{
166 if v, ok := d.GetOk("website_name"); ok {
167 test.WebsiteName = v.(string)
169 if v, ok := d.GetOk("website_url"); ok {
170 test.WebsiteURL = v.(string)
172 if v, ok := d.GetOk("check_rate"); ok {
173 test.CheckRate = v.(int)
175 if v, ok := d.GetOk("contact_id"); ok {
176 test.ContactID = v.(int)
178 if v, ok := d.GetOk("test_type"); ok {
179 test.TestType = v.(string)
181 if v, ok := d.GetOk("paused"); ok {
182 test.Paused = v.(bool)
184 if v, ok := d.GetOk("timeout"); ok {
185 test.Timeout = v.(int)
187 if v, ok := d.GetOk("contact_id"); ok {
188 test.ContactID = v.(int)
190 if v, ok := d.GetOk("confirmations"); ok {
191 test.Confirmation = v.(int)
193 if v, ok := d.GetOk("port"); ok {
197 defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " +
198 "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " +
199 "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " +
202 test.StatusCodes = defaultStatusCodes