2015-09-28 12:39:32 +02:00
|
|
|
package statuscake
|
|
|
|
|
2015-10-02 00:09:13 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
|
2015-10-08 16:36:11 +02:00
|
|
|
"github.com/DreamItGetIT/statuscake"
|
2015-10-02 00:09:13 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
2015-09-28 12:39:32 +02:00
|
|
|
|
|
|
|
func resourceStatusCakeTest() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: CreateTest,
|
|
|
|
Update: UpdateTest,
|
|
|
|
Delete: DeleteTest,
|
|
|
|
Read: ReadTest,
|
2015-10-02 00:09:13 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"test_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"website_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"website_url": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"check_rate": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 300,
|
|
|
|
},
|
|
|
|
|
|
|
|
"test_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"paused": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2015-10-04 00:11:26 +02:00
|
|
|
"timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-08-23 16:43:31 +02:00
|
|
|
"contact_id": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2015-10-02 00:09:13 +02:00
|
|
|
},
|
2015-09-28 12:39:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTest(d *schema.ResourceData, meta interface{}) error {
|
2015-10-08 16:36:11 +02:00
|
|
|
client := meta.(*statuscake.Client)
|
2015-10-02 00:09:13 +02:00
|
|
|
|
2015-10-08 16:36:11 +02:00
|
|
|
newTest := &statuscake.Test{
|
2015-10-04 00:11:26 +02:00
|
|
|
WebsiteName: d.Get("website_name").(string),
|
|
|
|
WebsiteURL: d.Get("website_url").(string),
|
|
|
|
CheckRate: d.Get("check_rate").(int),
|
2016-12-11 20:41:49 +01:00
|
|
|
TestType: d.Get("test_type").(string),
|
|
|
|
Paused: d.Get("paused").(bool),
|
|
|
|
Timeout: d.Get("timeout").(int),
|
|
|
|
ContactID: d.Get("contact_id").(int),
|
2015-10-02 00:09:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
|
|
|
|
|
2015-10-08 09:52:15 +02:00
|
|
|
response, err := client.Tests().Update(newTest)
|
2015-10-02 00:09:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("test_id", fmt.Sprintf("%d", response.TestID))
|
|
|
|
d.SetId(fmt.Sprintf("%d", response.TestID))
|
|
|
|
|
2015-10-04 00:11:26 +02:00
|
|
|
return ReadTest(d, meta)
|
2015-09-28 12:39:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateTest(d *schema.ResourceData, meta interface{}) error {
|
2015-10-08 16:36:11 +02:00
|
|
|
client := meta.(*statuscake.Client)
|
2015-10-04 00:11:26 +02:00
|
|
|
|
|
|
|
params := getStatusCakeTestInput(d)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
|
2015-10-08 09:52:15 +02:00
|
|
|
_, err := client.Tests().Update(params)
|
2015-10-04 00:11:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
|
|
|
|
}
|
2015-09-28 12:39:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteTest(d *schema.ResourceData, meta interface{}) error {
|
2015-10-08 16:36:11 +02:00
|
|
|
client := meta.(*statuscake.Client)
|
2015-10-02 00:09:13 +02:00
|
|
|
|
|
|
|
testId, parseErr := strconv.Atoi(d.Id())
|
|
|
|
if parseErr != nil {
|
|
|
|
return parseErr
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
|
2015-10-04 00:11:26 +02:00
|
|
|
err := client.Tests().Delete(testId)
|
2015-10-02 00:09:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-28 12:39:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadTest(d *schema.ResourceData, meta interface{}) error {
|
2015-10-08 16:36:11 +02:00
|
|
|
client := meta.(*statuscake.Client)
|
2015-10-04 00:11:26 +02:00
|
|
|
|
|
|
|
testId, parseErr := strconv.Atoi(d.Id())
|
|
|
|
if parseErr != nil {
|
|
|
|
return parseErr
|
|
|
|
}
|
2015-10-08 09:52:15 +02:00
|
|
|
testResp, err := client.Tests().Detail(testId)
|
2015-10-04 00:11:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
|
|
|
|
}
|
2016-12-11 20:41:49 +01:00
|
|
|
d.Set("website_name", testResp.WebsiteName)
|
|
|
|
d.Set("website_url", testResp.WebsiteURL)
|
2015-10-04 00:11:26 +02:00
|
|
|
d.Set("check_rate", testResp.CheckRate)
|
2016-12-11 20:41:49 +01:00
|
|
|
d.Set("test_type", testResp.TestType)
|
|
|
|
d.Set("paused", testResp.Paused)
|
|
|
|
d.Set("timeout", testResp.Timeout)
|
|
|
|
d.Set("contact_id", testResp.ContactID)
|
2015-10-04 00:11:26 +02:00
|
|
|
|
2015-09-28 12:39:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-04 00:11:26 +02:00
|
|
|
|
2015-10-08 16:36:11 +02:00
|
|
|
func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
|
2015-10-04 00:11:26 +02:00
|
|
|
testId, parseErr := strconv.Atoi(d.Id())
|
|
|
|
if parseErr != nil {
|
|
|
|
log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
|
|
|
|
}
|
2015-10-08 16:36:11 +02:00
|
|
|
test := &statuscake.Test{
|
2015-10-04 00:11:26 +02:00
|
|
|
TestID: testId,
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("website_name"); ok {
|
|
|
|
test.WebsiteName = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("website_url"); ok {
|
|
|
|
test.WebsiteURL = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("check_rate"); ok {
|
|
|
|
test.CheckRate = v.(int)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("test_type"); ok {
|
|
|
|
test.TestType = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("paused"); ok {
|
|
|
|
test.Paused = v.(bool)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("timeout"); ok {
|
|
|
|
test.Timeout = v.(int)
|
|
|
|
}
|
2016-08-23 16:43:31 +02:00
|
|
|
if v, ok := d.GetOk("contact_id"); ok {
|
|
|
|
test.ContactID = v.(int)
|
|
|
|
}
|
2015-10-04 00:11:26 +02:00
|
|
|
return test
|
|
|
|
}
|