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 "contact_id": &schema.Schema{
41 "check_rate": &schema.Schema{
47 "test_type": &schema.Schema{
48 Type: schema.TypeString,
52 "paused": &schema.Schema{
53 Type: schema.TypeBool,
58 "timeout": &schema.Schema{
64 "confirmations": &schema.Schema{
72 func CreateTest(d *schema.ResourceData, meta interface{}) error {
73 client := meta.(*statuscake.Client)
75 newTest := &statuscake.Test{
76 WebsiteName: d.Get("website_name").(string),
77 WebsiteURL: d.Get("website_url").(string),
78 CheckRate: d.Get("check_rate").(int),
79 TestType: d.Get("test_type").(string),
80 Paused: d.Get("paused").(bool),
81 Timeout: d.Get("timeout").(int),
82 ContactID: d.Get("contact_id").(int),
83 Confirmation: d.Get("confirmations").(int),
86 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
88 response, err := client.Tests().Update(newTest)
90 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
93 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
94 d.SetId(fmt.Sprintf("%d", response.TestID))
96 return ReadTest(d, meta)
99 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
100 client := meta.(*statuscake.Client)
102 params := getStatusCakeTestInput(d)
104 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
105 _, err := client.Tests().Update(params)
107 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
112 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
113 client := meta.(*statuscake.Client)
115 testId, parseErr := strconv.Atoi(d.Id())
119 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
120 err := client.Tests().Delete(testId)
128 func ReadTest(d *schema.ResourceData, meta interface{}) error {
129 client := meta.(*statuscake.Client)
131 testId, parseErr := strconv.Atoi(d.Id())
135 testResp, err := client.Tests().Detail(testId)
137 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
139 d.Set("website_name", testResp.WebsiteName)
140 d.Set("website_url", testResp.WebsiteURL)
141 d.Set("check_rate", testResp.CheckRate)
142 d.Set("test_type", testResp.TestType)
143 d.Set("paused", testResp.Paused)
144 d.Set("timeout", testResp.Timeout)
145 d.Set("contact_id", testResp.ContactID)
146 d.Set("confirmations", testResp.Confirmation)
151 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
152 testId, parseErr := strconv.Atoi(d.Id())
154 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
156 test := &statuscake.Test{
159 if v, ok := d.GetOk("website_name"); ok {
160 test.WebsiteName = v.(string)
162 if v, ok := d.GetOk("website_url"); ok {
163 test.WebsiteURL = v.(string)
165 if v, ok := d.GetOk("check_rate"); ok {
166 test.CheckRate = v.(int)
168 if v, ok := d.GetOk("contact_id"); ok {
169 test.ContactID = v.(int)
171 if v, ok := d.GetOk("test_type"); ok {
172 test.TestType = v.(string)
174 if v, ok := d.GetOk("paused"); ok {
175 test.Paused = v.(bool)
177 if v, ok := d.GetOk("timeout"); ok {
178 test.Timeout = v.(int)
180 if v, ok := d.GetOk("contact_id"); ok {
181 test.ContactID = v.(int)
183 if v, ok := d.GetOk("confirmations"); ok {
184 test.Confirmation = v.(int)