]>
Commit | Line | Data |
---|---|---|
478e1338 | 1 | package statuscake |
2 | ||
b8f05dfc | 3 | import ( |
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 | |
13 | func 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{ | |
90db3fb0 | 21 | "test_id": { |
b8f05dfc | 22 | Type: schema.TypeString, |
23 | Computed: true, | |
24 | }, | |
25 | ||
90db3fb0 | 26 | "website_name": { |
b8f05dfc | 27 | Type: schema.TypeString, |
28 | Required: true, | |
29 | }, | |
30 | ||
90db3fb0 | 31 | "website_url": { |
b8f05dfc | 32 | Type: schema.TypeString, |
33 | Required: true, | |
34 | }, | |
35 | ||
90db3fb0 | 36 | "contact_id": { |
e7b04dd5 LJ |
37 | Type: schema.TypeInt, |
38 | Optional: true, | |
39 | }, | |
40 | ||
90db3fb0 | 41 | "check_rate": { |
b8f05dfc | 42 | Type: schema.TypeInt, |
43 | Optional: true, | |
44 | Default: 300, | |
45 | }, | |
46 | ||
90db3fb0 | 47 | "test_type": { |
b8f05dfc | 48 | Type: schema.TypeString, |
49 | Required: true, | |
50 | }, | |
51 | ||
90db3fb0 | 52 | "paused": { |
b8f05dfc | 53 | Type: schema.TypeBool, |
54 | Optional: true, | |
55 | Default: false, | |
56 | }, | |
6ad7af97 | 57 | |
90db3fb0 | 58 | "timeout": { |
7dec75fc | 59 | Type: schema.TypeInt, |
6ad7af97 RG |
60 | Optional: true, |
61 | Default: 40, | |
7dec75fc | 62 | }, |
6ad7af97 | 63 | |
90db3fb0 PS |
64 | "confirmations": { |
65 | Type: schema.TypeInt, | |
66 | Optional: true, | |
67 | }, | |
68 | ||
69 | "port": { | |
b5815405 SC |
70 | Type: schema.TypeInt, |
71 | Optional: true, | |
72 | }, | |
b8f05dfc | 73 | }, |
478e1338 | 74 | } |
75 | } | |
76 | ||
77 | func CreateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 78 | client := meta.(*statuscake.Client) |
b8f05dfc | 79 | |
4eeeab64 | 80 | newTest := &statuscake.Test{ |
b5815405 SC |
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), | |
90db3fb0 | 89 | Port: d.Get("port").(int), |
b8f05dfc | 90 | } |
91 | ||
b8f05dfc | 92 | log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) |
93 | ||
32574941 | 94 | response, err := client.Tests().Update(newTest) |
b8f05dfc | 95 | if err != nil { |
96 | return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) | |
97 | } | |
98 | ||
99 | d.Set("test_id", fmt.Sprintf("%d", response.TestID)) | |
100 | d.SetId(fmt.Sprintf("%d", response.TestID)) | |
101 | ||
7dec75fc | 102 | return ReadTest(d, meta) |
478e1338 | 103 | } |
104 | ||
105 | func UpdateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 106 | client := meta.(*statuscake.Client) |
7dec75fc | 107 | |
108 | params := getStatusCakeTestInput(d) | |
109 | ||
110 | log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) | |
32574941 | 111 | _, err := client.Tests().Update(params) |
7dec75fc | 112 | if err != nil { |
113 | return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) | |
114 | } | |
478e1338 | 115 | return nil |
116 | } | |
117 | ||
118 | func DeleteTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 119 | client := meta.(*statuscake.Client) |
b8f05dfc | 120 | |
121 | testId, parseErr := strconv.Atoi(d.Id()) | |
122 | if parseErr != nil { | |
123 | return parseErr | |
124 | } | |
b8f05dfc | 125 | log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) |
7dec75fc | 126 | err := client.Tests().Delete(testId) |
b8f05dfc | 127 | if err != nil { |
128 | return err | |
129 | } | |
130 | ||
478e1338 | 131 | return nil |
132 | } | |
133 | ||
134 | func ReadTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 135 | client := meta.(*statuscake.Client) |
7dec75fc | 136 | |
137 | testId, parseErr := strconv.Atoi(d.Id()) | |
138 | if parseErr != nil { | |
139 | return parseErr | |
140 | } | |
32574941 | 141 | testResp, err := client.Tests().Detail(testId) |
7dec75fc | 142 | if err != nil { |
143 | return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) | |
144 | } | |
c0701076 RG |
145 | d.Set("website_name", testResp.WebsiteName) |
146 | d.Set("website_url", testResp.WebsiteURL) | |
7dec75fc | 147 | d.Set("check_rate", testResp.CheckRate) |
c0701076 RG |
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) | |
b5815405 | 152 | d.Set("confirmations", testResp.Confirmation) |
90db3fb0 | 153 | d.Set("port", testResp.Port) |
7dec75fc | 154 | |
478e1338 | 155 | return nil |
156 | } | |
7dec75fc | 157 | |
4eeeab64 | 158 | func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { |
7dec75fc | 159 | testId, parseErr := strconv.Atoi(d.Id()) |
160 | if parseErr != nil { | |
161 | log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) | |
162 | } | |
4eeeab64 | 163 | test := &statuscake.Test{ |
7dec75fc | 164 | TestID: testId, |
165 | } | |
166 | if v, ok := d.GetOk("website_name"); ok { | |
167 | test.WebsiteName = v.(string) | |
168 | } | |
169 | if v, ok := d.GetOk("website_url"); ok { | |
170 | test.WebsiteURL = v.(string) | |
171 | } | |
172 | if v, ok := d.GetOk("check_rate"); ok { | |
173 | test.CheckRate = v.(int) | |
174 | } | |
e7b04dd5 LJ |
175 | if v, ok := d.GetOk("contact_id"); ok { |
176 | test.ContactID = v.(int) | |
177 | } | |
7dec75fc | 178 | if v, ok := d.GetOk("test_type"); ok { |
179 | test.TestType = v.(string) | |
180 | } | |
181 | if v, ok := d.GetOk("paused"); ok { | |
182 | test.Paused = v.(bool) | |
183 | } | |
184 | if v, ok := d.GetOk("timeout"); ok { | |
185 | test.Timeout = v.(int) | |
186 | } | |
51688607 | 187 | if v, ok := d.GetOk("contact_id"); ok { |
188 | test.ContactID = v.(int) | |
189 | } | |
b5815405 SC |
190 | if v, ok := d.GetOk("confirmations"); ok { |
191 | test.Confirmation = v.(int) | |
192 | } | |
90db3fb0 PS |
193 | if v, ok := d.GetOk("port"); ok { |
194 | test.Port = v.(int) | |
195 | } | |
2a6605e9 AS |
196 | |
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, " + | |
200 | "598, 599" | |
201 | ||
202 | test.StatusCodes = defaultStatusCodes | |
203 | ||
7dec75fc | 204 | return test |
205 | } |