-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtests.go
86 lines (75 loc) · 2.77 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package thousandeyes
import (
"encoding/json"
"fmt"
)
// GenericTest - GenericTest struct to represent all test types
type GenericTest struct {
// Common test fields
AlertsEnabled *bool `json:"alertsEnabled,omitempty" te:"int-bool"`
AlertRules *[]AlertRule `json:"alertRules,omitempty"`
APILinks *[]APILink `json:"apiLinks,omitempty"`
CreatedBy *string `json:"createdBy,omitempty"`
CreatedDate *string `json:"createdDate,omitempty"`
Description *string `json:"description,omitempty"`
Enabled *bool `json:"enabled,omitempty" te:"int-bool"`
Groups *[]GroupLabel `json:"groups,omitempty"`
ModifiedBy *string `json:"modifiedBy,omitempty"`
ModifiedDate *string `json:"modifiedDate,omitempty"`
SavedEvent *bool `json:"savedEvent,omitempty" te:"int-bool"`
SharedWithAccounts *[]SharedWithAccount `json:"sharedWithAccounts,omitempty"`
TestID *int64 `json:"testId,omitempty"`
TestName *string `json:"testName,omitempty"`
Type *string `json:"type,omitempty"`
// Fields unique to this test
Agents *[]Agent `json:"agents,omitempty"`
}
// MarshalJSON implements the json.Marshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t GenericTest) MarshalJSON() ([]byte, error) {
type aliasTest GenericTest
data, err := json.Marshal((aliasTest)(t))
if err != nil {
return nil, err
}
return jsonBoolToInt(&t, data)
}
// UnmarshalJSON implements the json.Unmarshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t *GenericTest) UnmarshalJSON(data []byte) error {
type aliasTest GenericTest
test := (*aliasTest)(t)
data, err := jsonIntToBool(t, data)
if err != nil {
return err
}
return json.Unmarshal(data, &test)
}
// GetTests - get all tests
func (c *Client) GetTests() (*[]GenericTest, error) {
resp, err := c.get("/tests")
if err != nil {
return nil, err
}
var target map[string][]GenericTest
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
tests := target["test"]
return &tests, nil
}
// GetTest - Get test
func (c *Client) GetTest(id int64) (*GenericTest, error) {
resp, err := c.get(fmt.Sprintf("/tests/%d", id))
if err != nil {
return nil, err
}
var target map[string][]GenericTest
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("could not decode JSON response: %v", dErr)
}
test := target["test"][0]
return &test, nil
}