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{
21 "test_id": &schema.Schema{
22 Type: schema.TypeString,
26 "website_name": &schema.Schema{
27 Type: schema.TypeString,
31 "website_url": &schema.Schema{
32 Type: schema.TypeString,
36 "check_rate": &schema.Schema{
42 "test_type": &schema.Schema{
43 Type: schema.TypeString,
47 "paused": &schema.Schema{
48 Type: schema.TypeBool,
52 "timeout": &schema.Schema{
56 "contact_id": &schema.Schema{
64 func CreateTest(d *schema.ResourceData, meta interface{}) error {
65 client := meta.(*statuscake.Client)
67 newTest := &statuscake.Test{
68 WebsiteName: d.Get("website_name").(string),
69 WebsiteURL: d.Get("website_url").(string),
70 TestType: d.Get("test_type").(string),
71 CheckRate: d.Get("check_rate").(int),
74 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
76 response, err := client.Tests().Update(newTest)
78 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
81 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
82 d.SetId(fmt.Sprintf("%d", response.TestID))
84 return ReadTest(d, meta)
87 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
88 client := meta.(*statuscake.Client)
90 params := getStatusCakeTestInput(d)
92 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
93 _, err := client.Tests().Update(params)
95 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
100 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
101 client := meta.(*statuscake.Client)
103 testId, parseErr := strconv.Atoi(d.Id())
107 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
108 err := client.Tests().Delete(testId)
116 func ReadTest(d *schema.ResourceData, meta interface{}) error {
117 client := meta.(*statuscake.Client)
119 testId, parseErr := strconv.Atoi(d.Id())
123 testResp, err := client.Tests().Detail(testId)
125 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
127 d.Set("check_rate", testResp.CheckRate)
132 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
133 testId, parseErr := strconv.Atoi(d.Id())
135 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
137 test := &statuscake.Test{
140 if v, ok := d.GetOk("website_name"); ok {
141 test.WebsiteName = v.(string)
143 if v, ok := d.GetOk("website_url"); ok {
144 test.WebsiteURL = v.(string)
146 if v, ok := d.GetOk("check_rate"); ok {
147 test.CheckRate = v.(int)
149 if v, ok := d.GetOk("test_type"); ok {
150 test.TestType = v.(string)
152 if v, ok := d.GetOk("paused"); ok {
153 test.Paused = v.(bool)
155 if v, ok := d.GetOk("timeout"); ok {
156 test.Timeout = v.(int)
158 if v, ok := d.GetOk("contact_id"); ok {
159 test.ContactID = v.(int)