diff options
Diffstat (limited to 'vendor/github.com/DreamItGetIT/statuscake/tests.go')
-rw-r--r-- | vendor/github.com/DreamItGetIT/statuscake/tests.go | 298 |
1 files changed, 298 insertions, 0 deletions
diff --git a/vendor/github.com/DreamItGetIT/statuscake/tests.go b/vendor/github.com/DreamItGetIT/statuscake/tests.go new file mode 100644 index 0000000..4053e53 --- /dev/null +++ b/vendor/github.com/DreamItGetIT/statuscake/tests.go | |||
@@ -0,0 +1,298 @@ | |||
1 | package statuscake | ||
2 | |||
3 | import ( | ||
4 | "encoding/json" | ||
5 | "fmt" | ||
6 | "net/url" | ||
7 | "reflect" | ||
8 | "strings" | ||
9 | ) | ||
10 | |||
11 | const queryStringTag = "querystring" | ||
12 | |||
13 | // Test represents a statuscake Test | ||
14 | type Test struct { | ||
15 | // ThiTestID is an int, use this to get more details about this test. If not provided will insert a new check, else will update | ||
16 | TestID int `json:"TestID" querystring:"TestID" querystringoptions:"omitempty"` | ||
17 | |||
18 | // Sent tfalse To Unpause and true To Pause. | ||
19 | Paused bool `json:"Paused" querystring:"Paused"` | ||
20 | |||
21 | // Website name. Tags are stripped out | ||
22 | WebsiteName string `json:"WebsiteName" querystring:"WebsiteName"` | ||
23 | |||
24 | // Test location, either an IP (for TCP and Ping) or a fully qualified URL for other TestTypes | ||
25 | WebsiteURL string `json:"WebsiteURL" querystring:"WebsiteURL"` | ||
26 | |||
27 | // A Port to use on TCP Tests | ||
28 | Port int `json:"Port" querystring:"Port"` | ||
29 | |||
30 | // Contact group ID - will return int of contact group used else 0 | ||
31 | ContactID int `json:"ContactID" querystring:"ContactGroup"` | ||
32 | |||
33 | // Current status at last test | ||
34 | Status string `json:"Status"` | ||
35 | |||
36 | // 7 Day Uptime | ||
37 | Uptime float64 `json:"Uptime"` | ||
38 | |||
39 | // Any test locations seperated by a comma (using the Node Location IDs) | ||
40 | NodeLocations []string `json:"NodeLocations" querystring:"NodeLocations"` | ||
41 | |||
42 | // Timeout in an int form representing seconds. | ||
43 | Timeout int `json:"Timeout" querystring:"Timeout"` | ||
44 | |||
45 | // A URL to ping if a site goes down. | ||
46 | PingURL string `json:"PingURL" querystring:"PingURL"` | ||
47 | |||
48 | Confirmation int `json:"Confirmationi,string" querystring:"Confirmation"` | ||
49 | |||
50 | // The number of seconds between checks. | ||
51 | CheckRate int `json:"CheckRate" querystring:"CheckRate"` | ||
52 | |||
53 | // A Basic Auth User account to use to login | ||
54 | BasicUser string `json:"BasicUser" querystring:"BasicUser"` | ||
55 | |||
56 | // If BasicUser is set then this should be the password for the BasicUser | ||
57 | BasicPass string `json:"BasicPass" querystring:"BasicPass"` | ||
58 | |||
59 | // Set 1 to enable public reporting, 0 to disable | ||
60 | Public int `json:"Public" querystring:"Public"` | ||
61 | |||
62 | // A URL to a image to use for public reporting | ||
63 | LogoImage string `json:"LogoImage" querystring:"LogoImage"` | ||
64 | |||
65 | // Set to 0 to use branding (default) or 1 to disable public reporting branding | ||
66 | Branding int `json:"Branding" querystring:"Branding"` | ||
67 | |||
68 | // Used internally by the statuscake API | ||
69 | WebsiteHost string `json:"WebsiteHost"` | ||
70 | |||
71 | // Enable virus checking or not. 1 to enable | ||
72 | Virus int `json:"Virus" querystring:"Virus"` | ||
73 | |||
74 | // A string that should either be found or not found. | ||
75 | FindString string `json:"FindString" querystring:"FindString"` | ||
76 | |||
77 | // If the above string should be found to trigger a alert. true will trigger if FindString found | ||
78 | DoNotFind bool `json:"DoNotFind" querystring:"DoNotFind"` | ||
79 | |||
80 | // What type of test type to use. Accepted values are HTTP, TCP, PING | ||
81 | TestType string `json:"TestType" querystring:"TestType"` | ||
82 | |||
83 | // Use 1 to TURN OFF real browser testing | ||
84 | RealBrowser int `json:"RealBrowser" querystring:"RealBrowser"` | ||
85 | |||
86 | // How many minutes to wait before sending an alert | ||
87 | TriggerRate int `json:"TriggerRate" querystring:"TriggerRate"` | ||
88 | |||
89 | // Tags should be seperated by a comma - no spacing between tags (this,is,a set,of,tags) | ||
90 | TestTags string `json:"TestTags" querystring:"TestTags"` | ||
91 | |||
92 | // Comma Seperated List of StatusCodes to Trigger Error on (on Update will replace, so send full list each time) | ||
93 | StatusCodes string `json:"StatusCodes" querystring:"StatusCodes"` | ||
94 | } | ||
95 | |||
96 | // Validate checks if the Test is valid. If it's invalid, it returns a ValidationError with all invalid fields. It returns nil otherwise. | ||
97 | func (t *Test) Validate() error { | ||
98 | e := make(ValidationError) | ||
99 | |||
100 | if t.WebsiteName == "" { | ||
101 | e["WebsiteName"] = "is required" | ||
102 | } | ||
103 | |||
104 | if t.WebsiteURL == "" { | ||
105 | e["WebsiteURL"] = "is required" | ||
106 | } | ||
107 | |||
108 | if t.Timeout != 0 && (t.Timeout < 6 || t.Timeout > 99) { | ||
109 | e["Timeout"] = "must be 0 or between 6 and 99" | ||
110 | } | ||
111 | |||
112 | if t.Confirmation < 0 || t.Confirmation > 9 { | ||
113 | e["Confirmation"] = "must be between 0 and 9" | ||
114 | } | ||
115 | |||
116 | if t.CheckRate < 0 || t.CheckRate > 23999 { | ||
117 | e["CheckRate"] = "must be between 0 and 23999" | ||
118 | } | ||
119 | |||
120 | if t.Public < 0 || t.Public > 1 { | ||
121 | e["Public"] = "must be 0 or 1" | ||
122 | } | ||
123 | |||
124 | if t.Virus < 0 || t.Virus > 1 { | ||
125 | e["Virus"] = "must be 0 or 1" | ||
126 | } | ||
127 | |||
128 | if t.TestType != "HTTP" && t.TestType != "TCP" && t.TestType != "PING" { | ||
129 | e["TestType"] = "must be HTTP, TCP, or PING" | ||
130 | } | ||
131 | |||
132 | if t.RealBrowser < 0 || t.RealBrowser > 1 { | ||
133 | e["RealBrowser"] = "must be 0 or 1" | ||
134 | } | ||
135 | |||
136 | if t.TriggerRate < 0 || t.TriggerRate > 59 { | ||
137 | e["TriggerRate"] = "must be between 0 and 59" | ||
138 | } | ||
139 | |||
140 | if len(e) > 0 { | ||
141 | return e | ||
142 | } | ||
143 | |||
144 | return nil | ||
145 | } | ||
146 | |||
147 | // ToURLValues returns url.Values of all fields required to create/update a Test. | ||
148 | func (t Test) ToURLValues() url.Values { | ||
149 | values := make(url.Values) | ||
150 | st := reflect.TypeOf(t) | ||
151 | sv := reflect.ValueOf(t) | ||
152 | for i := 0; i < st.NumField(); i++ { | ||
153 | sf := st.Field(i) | ||
154 | tag := sf.Tag.Get(queryStringTag) | ||
155 | ft := sf.Type | ||
156 | if ft.Name() == "" && ft.Kind() == reflect.Ptr { | ||
157 | // Follow pointer. | ||
158 | ft = ft.Elem() | ||
159 | } | ||
160 | |||
161 | v := sv.Field(i) | ||
162 | options := sf.Tag.Get("querystringoptions") | ||
163 | omit := options == "omitempty" && isEmptyValue(v) | ||
164 | |||
165 | if tag != "" && !omit { | ||
166 | values.Set(tag, valueToQueryStringValue(v)) | ||
167 | } | ||
168 | } | ||
169 | |||
170 | return values | ||
171 | } | ||
172 | |||
173 | func isEmptyValue(v reflect.Value) bool { | ||
174 | switch v.Kind() { | ||
175 | case reflect.Array, reflect.Map, reflect.Slice, reflect.String: | ||
176 | return v.Len() == 0 | ||
177 | case reflect.Bool: | ||
178 | return !v.Bool() | ||
179 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
180 | return v.Int() == 0 | ||
181 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
182 | return v.Uint() == 0 | ||
183 | case reflect.Float32, reflect.Float64: | ||
184 | return v.Float() == 0 | ||
185 | case reflect.Interface, reflect.Ptr: | ||
186 | return v.IsNil() | ||
187 | } | ||
188 | |||
189 | return false | ||
190 | } | ||
191 | |||
192 | func valueToQueryStringValue(v reflect.Value) string { | ||
193 | if v.Type().Name() == "bool" { | ||
194 | if v.Bool() { | ||
195 | return "1" | ||
196 | } | ||
197 | |||
198 | return "0" | ||
199 | } | ||
200 | |||
201 | if v.Type().Kind() == reflect.Slice { | ||
202 | if ss, ok := v.Interface().([]string); ok { | ||
203 | return strings.Join(ss, ",") | ||
204 | } | ||
205 | } | ||
206 | |||
207 | return fmt.Sprint(v) | ||
208 | } | ||
209 | |||
210 | // Tests is a client that implements the `Tests` API. | ||
211 | type Tests interface { | ||
212 | All() ([]*Test, error) | ||
213 | Detail(int) (*Test, error) | ||
214 | Update(*Test) (*Test, error) | ||
215 | Delete(TestID int) error | ||
216 | } | ||
217 | |||
218 | type tests struct { | ||
219 | client apiClient | ||
220 | } | ||
221 | |||
222 | func newTests(c apiClient) Tests { | ||
223 | return &tests{ | ||
224 | client: c, | ||
225 | } | ||
226 | } | ||
227 | |||
228 | func (tt *tests) All() ([]*Test, error) { | ||
229 | resp, err := tt.client.get("/Tests", nil) | ||
230 | if err != nil { | ||
231 | return nil, err | ||
232 | } | ||
233 | defer resp.Body.Close() | ||
234 | |||
235 | var tests []*Test | ||
236 | err = json.NewDecoder(resp.Body).Decode(&tests) | ||
237 | |||
238 | return tests, err | ||
239 | } | ||
240 | |||
241 | func (tt *tests) Update(t *Test) (*Test, error) { | ||
242 | resp, err := tt.client.put("/Tests/Update", t.ToURLValues()) | ||
243 | if err != nil { | ||
244 | return nil, err | ||
245 | } | ||
246 | defer resp.Body.Close() | ||
247 | |||
248 | var ur updateResponse | ||
249 | err = json.NewDecoder(resp.Body).Decode(&ur) | ||
250 | if err != nil { | ||
251 | return nil, err | ||
252 | } | ||
253 | |||
254 | if !ur.Success { | ||
255 | return nil, &updateError{Issues: ur.Issues} | ||
256 | } | ||
257 | |||
258 | t2 := *t | ||
259 | t2.TestID = ur.InsertID | ||
260 | |||
261 | return &t2, err | ||
262 | } | ||
263 | |||
264 | func (tt *tests) Delete(testID int) error { | ||
265 | resp, err := tt.client.delete("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}}) | ||
266 | if err != nil { | ||
267 | return err | ||
268 | } | ||
269 | defer resp.Body.Close() | ||
270 | |||
271 | var dr deleteResponse | ||
272 | err = json.NewDecoder(resp.Body).Decode(&dr) | ||
273 | if err != nil { | ||
274 | return err | ||
275 | } | ||
276 | |||
277 | if !dr.Success { | ||
278 | return &deleteError{Message: dr.Error} | ||
279 | } | ||
280 | |||
281 | return nil | ||
282 | } | ||
283 | |||
284 | func (tt *tests) Detail(testID int) (*Test, error) { | ||
285 | resp, err := tt.client.get("/Tests/Details", url.Values{"TestID": {fmt.Sprint(testID)}}) | ||
286 | if err != nil { | ||
287 | return nil, err | ||
288 | } | ||
289 | defer resp.Body.Close() | ||
290 | |||
291 | var dr *detailResponse | ||
292 | err = json.NewDecoder(resp.Body).Decode(&dr) | ||
293 | if err != nil { | ||
294 | return nil, err | ||
295 | } | ||
296 | |||
297 | return dr.test(), nil | ||
298 | } | ||