]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - resource_statuscaketest.go
Fix `go vet -unreachable` warnings.
[github/fretlink/terraform-provider-statuscake.git] / resource_statuscaketest.go
CommitLineData
478e1338 1package statuscake
2
b8f05dfc 3import (
4 "fmt"
5 "strconv"
6
7 "log"
8
4eeeab64 9 "github.com/DreamItGetIT/statuscake"
b8f05dfc 10 "github.com/hashicorp/terraform/helper/schema"
11)
478e1338 12
13func resourceStatusCakeTest() *schema.Resource {
14 return &schema.Resource{
15 Create: CreateTest,
16 Update: UpdateTest,
17 Delete: DeleteTest,
18 Read: ReadTest,
b8f05dfc 19
20 Schema: map[string]*schema.Schema{
21 "test_id": &schema.Schema{
22 Type: schema.TypeString,
23 Computed: true,
24 },
25
26 "website_name": &schema.Schema{
27 Type: schema.TypeString,
28 Required: true,
29 },
30
31 "website_url": &schema.Schema{
32 Type: schema.TypeString,
33 Required: true,
34 },
35
36 "check_rate": &schema.Schema{
37 Type: schema.TypeInt,
38 Optional: true,
39 Default: 300,
40 },
41
42 "test_type": &schema.Schema{
43 Type: schema.TypeString,
44 Required: true,
45 },
46
47 "paused": &schema.Schema{
48 Type: schema.TypeBool,
49 Optional: true,
50 Default: false,
51 },
7dec75fc 52 "timeout": &schema.Schema{
53 Type: schema.TypeInt,
54 Computed: true,
55 },
b8f05dfc 56 },
478e1338 57 }
58}
59
60func CreateTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 61 client := meta.(*statuscake.Client)
b8f05dfc 62
4eeeab64 63 newTest := &statuscake.Test{
7dec75fc 64 WebsiteName: d.Get("website_name").(string),
65 WebsiteURL: d.Get("website_url").(string),
66 TestType: d.Get("test_type").(string),
67 CheckRate: d.Get("check_rate").(int),
b8f05dfc 68 }
69
b8f05dfc 70 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
71
32574941 72 response, err := client.Tests().Update(newTest)
b8f05dfc 73 if err != nil {
74 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
75 }
76
77 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
78 d.SetId(fmt.Sprintf("%d", response.TestID))
79
7dec75fc 80 return ReadTest(d, meta)
478e1338 81}
82
83func UpdateTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 84 client := meta.(*statuscake.Client)
7dec75fc 85
86 params := getStatusCakeTestInput(d)
87
88 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
32574941 89 _, err := client.Tests().Update(params)
7dec75fc 90 if err != nil {
91 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
92 }
478e1338 93 return nil
94}
95
96func DeleteTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 97 client := meta.(*statuscake.Client)
b8f05dfc 98
99 testId, parseErr := strconv.Atoi(d.Id())
100 if parseErr != nil {
101 return parseErr
102 }
b8f05dfc 103 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
7dec75fc 104 err := client.Tests().Delete(testId)
b8f05dfc 105 if err != nil {
106 return err
107 }
108
478e1338 109 return nil
110}
111
112func ReadTest(d *schema.ResourceData, meta interface{}) error {
4eeeab64 113 client := meta.(*statuscake.Client)
7dec75fc 114
115 testId, parseErr := strconv.Atoi(d.Id())
116 if parseErr != nil {
117 return parseErr
118 }
32574941 119 testResp, err := client.Tests().Detail(testId)
7dec75fc 120 if err != nil {
121 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
122 }
123 d.Set("check_rate", testResp.CheckRate)
124
478e1338 125 return nil
126}
7dec75fc 127
4eeeab64 128func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
7dec75fc 129 testId, parseErr := strconv.Atoi(d.Id())
130 if parseErr != nil {
131 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
132 }
4eeeab64 133 test := &statuscake.Test{
7dec75fc 134 TestID: testId,
135 }
136 if v, ok := d.GetOk("website_name"); ok {
137 test.WebsiteName = v.(string)
138 }
139 if v, ok := d.GetOk("website_url"); ok {
140 test.WebsiteURL = v.(string)
141 }
142 if v, ok := d.GetOk("check_rate"); ok {
143 test.CheckRate = v.(int)
144 }
145 if v, ok := d.GetOk("test_type"); ok {
146 test.TestType = v.(string)
147 }
148 if v, ok := d.GetOk("paused"); ok {
149 test.Paused = v.(bool)
150 }
151 if v, ok := d.GetOk("timeout"); ok {
152 test.Timeout = v.(int)
153 }
154 return test
155}