forked from nytm/go-grafana-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam.go
208 lines (190 loc) · 4.4 KB
/
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
package gapi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)
type Team struct {
Id int64 `json:"id,omitempty"`
OrgId int64 `json:"orgId,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
AvatarUrl string `json:"avatarUrl,omitempty"`
MemberCount int64 `json:"memberCount,omitempty"`
}
type TeamMember struct {
UserId int64 `json:"userId,omitempty"`
TeamId int64 `json:"teamId,omitempty"`
OrgId int64 `json:"orgId,omitempty"`
Login string `json:"login,omitempty"`
Email string `json:"email,omitempty"`
AvatarUrl string `json:"avatarUrl,omitempty"`
}
type TeamSearchResponse struct {
TotalCount int64 `json:"totalCount,omitempty"`
Teams []*Team `json:"teams,omitempty"`
Page int64 `json:"page,omitempty"`
PerPage int64 `json:"perPage,omitempty"`
}
type CreateTeamResponse struct {
Id int64 `json:"teamId"`
}
func (c *Client) Team(id int64) (Team, error) {
team := Team{}
req, err := c.newRequest("GET", fmt.Sprintf("/api/teams/%d", id), nil, nil)
if err != nil {
return team, err
}
resp, err := c.Do(req)
if err != nil {
return team, err
}
if resp.StatusCode != 200 {
return team, errors.New(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return team, err
}
err = json.Unmarshal(data, &team)
return team, err
}
func (c *Client) Teams() ([]*Team, error) {
var list []*Team
req, err := c.newRequest("GET", "/api/teams/search", nil, nil)
if err != nil {
return list, err
}
resp, err := c.Do(req)
if err != nil {
return list, err
}
if resp.StatusCode != 200 {
return list, errors.New(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return list, err
}
var r TeamSearchResponse
err = json.Unmarshal(data, &r)
return r.Teams, err
}
func (c *Client) NewTeam(name string) (Team, error) {
team := Team{
Name: name,
}
data, err := json.Marshal(team)
req, err := c.newRequest("POST", "/api/teams", nil, bytes.NewBuffer(data))
if err != nil {
return team, err
}
resp, err := c.Do(req)
if err != nil {
return team, err
}
if resp.StatusCode != 200 {
data, _ = ioutil.ReadAll(resp.Body)
return team, fmt.Errorf("status: %s body: %s", resp.Status, data)
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return team, err
}
var r CreateTeamResponse
err = json.Unmarshal(data, &r)
if err != nil {
return team, err
}
team.Id = r.Id
return team, err
}
func (c *Client) UpdateTeam(id string, name string) error {
dataMap := map[string]string{
"name": name,
}
data, err := json.Marshal(dataMap)
req, err := c.newRequest("PUT", fmt.Sprintf("/api/teams/%s", id), nil, bytes.NewBuffer(data))
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return err
}
func (c *Client) DeleteTeam(id string) error {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/teams/%s", id), nil, nil)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return err
}
func (c *Client) AddTeamMember(id string, userID int64) error {
dataMap := map[string]interface{}{
"userId": userID,
}
data, err := json.Marshal(dataMap)
if err != nil {
return err
}
req, err := c.newRequest("POST", fmt.Sprintf("/api/teams/%s/members", id), nil, bytes.NewBuffer(data))
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return err
}
func (c *Client) RemoveTeamMember(id string, userID int64) error {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/teams/%s/members/%d", id, userID), nil, nil)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return err
}
func (c *Client) TeamMembers(id string) ([]*TeamMember, error) {
req, err := c.newRequest("GET", fmt.Sprintf("/api/teams/%s/members", id), nil, nil)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var list []*TeamMember
err = json.Unmarshal(data, &list)
return list, err
}