forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest-team.go
299 lines (273 loc) · 8.1 KB
/
rest-team.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package sdk
/*
Copyright 2016 Alexander I.Grafov <[email protected]>
Copyright 2016-2022 The Grafana SDK authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// SearchTeams search teams with optional parameters.
// Reflects GET /api/teams/search API call.
func (r *Client) SearchTeams(ctx context.Context, params ...SearchTeamParams) (PageTeams, error) {
var (
raw []byte
pageTeams PageTeams
code int
err error
requestParams = make(url.Values)
)
for _, p := range params {
p(requestParams)
}
if raw, code, err = r.get(ctx, "api/teams/search", requestParams); err != nil {
return pageTeams, err
}
if code != 200 {
return pageTeams, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
if err := dec.Decode(&pageTeams); err != nil {
return pageTeams, fmt.Errorf("unmarshal teams: %s\n%s", err, raw)
}
return pageTeams, err
}
func (r *Client) GetTeamByName(ctx context.Context, name string) (Team, error) {
var (
team Team
err error
)
search, err := r.SearchTeams(ctx, WithTeam(name))
if err != nil {
return team, err
}
if len(search.Teams) == 0 {
return Team{}, TeamNotFound
}
return search.Teams[0], nil
}
// GetTeam gets an team by ID.
// Reflects GET /api/teams/:id API call.
func (r *Client) GetTeam(ctx context.Context, id uint) (Team, error) {
var (
raw []byte
team Team
code int
err error
)
if raw, code, err = r.get(ctx, fmt.Sprintf("api/teams/%d", id), nil); err != nil {
return team, err
}
if code != 200 {
return team, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
if err := dec.Decode(&team); err != nil {
return team, fmt.Errorf("unmarshal team: %s\n%s", err, raw)
}
return team, err
}
// CreateTeam creates a new team.
// Reflects POST /api/teams API call.
func (r *Client) CreateTeam(ctx context.Context, t Team) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(t); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.post(ctx, "api/teams", nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
// UpdateTeam updates a team.
// Reflects PUT /api/teams/:id API call.
func (r *Client) UpdateTeam(ctx context.Context, id uint, t Team) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(t); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.put(ctx, fmt.Sprintf("api/teams/%d", id), nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
// DeleteTeam deletes a team.
// Reflects DELETE /api/teams/:id API call.
func (r *Client) DeleteTeam(ctx context.Context, id uint) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, _, err = r.delete(ctx, fmt.Sprintf("api/teams/%d", id)); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
// GetTeamMembers gets the members of a team by id.
// Reflects GET /api/teams/:teamId/members API call.
func (r *Client) GetTeamMembers(ctx context.Context, teamId uint) ([]TeamMember, error) {
var (
raw []byte
teamMembers []TeamMember
code int
err error
)
if raw, code, err = r.get(ctx, fmt.Sprintf("api/teams/%d/members", teamId), nil); err != nil {
return teamMembers, err
}
if code != 200 {
return teamMembers, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
if err := dec.Decode(&teamMembers); err != nil {
return teamMembers, fmt.Errorf("unmarshal team: %s\n%s", err, raw)
}
return teamMembers, err
}
// AddTeamMember adds a member to a team.
// Reflects POST /api/teams/:teamId/members API call.
func (r *Client) AddTeamMember(ctx context.Context, teamId uint, userId uint) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(struct {
UserId uint `json:"userId"`
}{
UserId: userId,
}); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.post(ctx, fmt.Sprintf("api/teams/%d/members", teamId), nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
// DeleteTeamMember removes a ream member from a team by id.
// Reflects DELETE /api/teams/:teamId/:userId API call.
func (r *Client) DeleteTeamMember(ctx context.Context, teamId uint, userId uint) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, _, err = r.delete(ctx, fmt.Sprintf("api/teams/%d/members/%d", teamId, userId)); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
// GetTeamPreferences gets the preferences for a team by id.
// Reflects GET /api/teams/:teamId/preferences API call.
func (r *Client) GetTeamPreferences(ctx context.Context, teamId uint) (TeamPreferences, error) {
var (
raw []byte
teamPreferences TeamPreferences
code int
err error
)
if raw, code, err = r.get(ctx, fmt.Sprintf("api/teams/%d/preferences", teamId), nil); err != nil {
return teamPreferences, err
}
if code != 200 {
return teamPreferences, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
if err := dec.Decode(&teamPreferences); err != nil {
return teamPreferences, fmt.Errorf("unmarshal team: %s\n%s", err, raw)
}
return teamPreferences, err
}
// UpdateTeamPreferences updates the preferences for a team by id.
// Reflects PUT /api/teams/:teamId/preferences API call.
func (r *Client) UpdateTeamPreferences(ctx context.Context, teamId uint, tp TeamPreferences) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(tp); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.put(ctx, fmt.Sprintf("api/teams/%d/preferences", teamId), nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
// TeamNotFound is an error returned if the given team was not found.
var TeamNotFound = fmt.Errorf("team not found")
// SearchTeamParams is the type for all options implementing query parameters
// perpage optional. default 1000
// page optional. default 1
// http://docs.grafana.org/http_api/team/#search-teams
// http://docs.grafana.org/http_api/team/#search-teams-with-paging
type SearchTeamParams func(values url.Values)
// WithQuery adds a query parameter
func WithQuery(query string) SearchTeamParams {
return func(v url.Values) {
v.Set("query", query)
}
}
// WithPagesize adds a page size query parameter
func WithPagesize(size uint) SearchTeamParams {
return func(v url.Values) {
v.Set("perpage", strconv.FormatUint(uint64(size),10))
}
}
// WithPage adds a page number query parameter
func WithPage(page uint) SearchTeamParams {
return func(v url.Values) {
v.Set("page", strconv.FormatUint(uint64(page),10))
}
}
// WithTeam adds a query parameter
func WithTeam(team string) SearchTeamParams {
return func(v url.Values) {
v.Set("team", team)
}
}