forked from thousandeyes/thousandeyes-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_label.go
155 lines (134 loc) · 4.13 KB
/
group_label.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package thousandeyes
import (
"encoding/json"
"fmt"
)
// GroupLabels - list of labels
type GroupLabels []GroupLabel
// GroupLabel - label
type GroupLabel struct {
Name *string `json:"name,omitempty"`
GroupID *int64 `json:"groupId,omitempty"`
Builtin *bool `json:"builtin,omitempty" te:"int-bool"`
Type *string `json:"type,omitempty"`
Agents *[]Agent `json:"agents,omitempty"`
Tests *[]GenericTest `json:"tests,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 GroupLabel) MarshalJSON() ([]byte, error) {
type alias GroupLabel
if t.Tests != nil && len(*t.Tests) == 0 {
ts := make([]GenericTest, 0)
t.Tests = &ts
}
if t.Agents != nil && len(*t.Agents) == 0 {
as := make([]Agent, 0)
t.Agents = &as
}
data, err := json.Marshal((alias)(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 *GroupLabel) UnmarshalJSON(data []byte) error {
type alias GroupLabel
test := (*alias)(t)
data, err := jsonIntToBool(t, data)
if err != nil {
return err
}
return json.Unmarshal(data, &test)
}
// GetGroupLabels - Get labels
func (c *Client) GetGroupLabels() (*GroupLabels, error) {
resp, err := c.get("/groups")
if err != nil {
return nil, err
}
var target map[string]GroupLabels
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
labels := target["groups"]
return &labels, nil
}
// GetGroupLabelsByType - Get label by type
func (c *Client) GetGroupLabelsByType(t string) (*GroupLabels, error) {
resp, err := c.get("/groups/" + t)
if err != nil {
return &GroupLabels{}, err
}
var target map[string]GroupLabels
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
labels := target["groups"]
return &labels, nil
}
// GetGroupLabel - Get single group label by ID
func (c *Client) GetGroupLabel(id int64) (*GroupLabel, error) {
resp, err := c.get(fmt.Sprintf("/groups/%d", id))
if err != nil {
return &GroupLabel{}, err
}
var target map[string][]GroupLabel
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
return &target["groups"][0], nil
}
// CreateGroupLabel - Create label
func (c Client) CreateGroupLabel(a GroupLabel) (*GroupLabel, error) {
if a.Type == nil {
a.Type = String("")
}
path := fmt.Sprintf("/groups/%s/new", *a.Type)
// Now we must set Type to blank. Because even though it's required to know the submit path,
// TE will return an error if we also submit it a part of the object.
a.Type = String("")
resp, err := c.post(path, a, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 201 {
return nil, fmt.Errorf("failed to create label, response code %d", resp.StatusCode)
}
var target map[string]GroupLabels
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
return &target["groups"][0], nil
}
// DeleteGroupLabel - delete label
func (c Client) DeleteGroupLabel(id int64) error {
resp, err := c.post(fmt.Sprintf("/groups/%d/delete", id), nil, nil)
if err != nil {
return err
}
if resp.StatusCode != 204 {
return fmt.Errorf("failed to delete label, response code %d", resp.StatusCode)
}
return nil
}
// UpdateGroupLabel - update label
func (c Client) UpdateGroupLabel(id int64, a GroupLabel) (*GroupLabels, error) {
resp, err := c.post(fmt.Sprintf("/groups/%d/update", id), a, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to update label, response code %d", resp.StatusCode)
}
var target map[string]GroupLabels
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
labels := target["groups"]
return &labels, nil
}