-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathteams.go
189 lines (147 loc) · 4.43 KB
/
teams.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
package grafana
import (
"fmt"
)
type TeamsService struct {
client *Client
}
type Teams []Team
type Team struct {
ID int `json:"id,omitempty"`
OrgID int `json:"orgId,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
AvatarUrl string `json:"avatarUrl,omitempty"`
MemberCount int `json:"memberCount,omitempty"`
Permission int `json:"permission,omitempty"`
}
type TeamSearchResults struct {
Teams *Teams `json:"teams,omitempty"`
TotalCount int `json:"totalCount,omitempty"`
Page int `json:"page,omitempty"`
PerPage int `json:"perPage,omitempty"`
}
type TeamMembers []TeamMember
type TeamMember struct {
OrgID int `json:"orgId,omitempty"`
TeamID int `json:"teamId,omitempty"`
UserID int `json:"userId,omitempty"`
Email string `json:"email,omitempty"`
Login string `json:"login,omitempty"`
AvatarUrl string `json:"avatarUrl,omitempty"`
}
type TeamPreferences struct {
Theme string `json:"theme,omitempty"`
HomeTeamID int `json:"homeTeamId,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
func (s *TeamsService) GetTeamByID(id int) (*Team, *Response, error) {
u := fmt.Sprintf("teams/%d", id)
req, err := s.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
t := new(Team)
resp, err := s.client.Do(req, t)
if err != nil {
return nil, resp, err
}
return t, resp, err
}
type CreateTeamOptions struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
func (s *TeamsService) CreateTeam(opt *CreateTeamOptions) (*Response, error) {
req, err := s.client.NewRequest("POST", "teams", opt, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
type UpdateTeamByIDOptions CreateTeamOptions //set all fields or else existing fields will be removed
func (s *TeamsService) UpdateTeamByID(id int, opt *UpdateTeamByIDOptions) (*Response, error) {
u := fmt.Sprintf("teams/%d", id)
req, err := s.client.NewRequest("PUT", u, opt, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
func (s *TeamsService) DeleteTeamByID(id int) (*Response, error) {
u := fmt.Sprintf("teams/%d", id)
req, err := s.client.NewRequest("DELETE", u, nil, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
func (s *TeamsService) GetTeamMembersByTeamID(id int) (*TeamMembers, *Response, error) {
u := fmt.Sprintf("teams/%d/members", id)
req, err := s.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
tm := new(TeamMembers)
resp, err := s.client.Do(req, tm)
if err != nil {
return nil, resp, err
}
return tm, resp, err
}
type AddTeamMemberOptions struct {
UserID int `json:"userId,omitempty"`
}
func (s *TeamsService) AddTeamMember(id int, opt *AddTeamMemberOptions) (*Response, error) {
u := fmt.Sprintf("teams/%d/members", id)
req, err := s.client.NewRequest("POST", u, opt, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
func (s *TeamsService) RemoveMemberFromTeam(teamID, userID int) (*Response, error) {
u := fmt.Sprintf("teams/%d/members/%d", teamID, userID)
req, err := s.client.NewRequest("DELETE", u, nil, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
func (s *TeamsService) GetTeamPreferences(id int) (*TeamPreferences, *Response, error) {
u := fmt.Sprintf("teams/%d/preferences", id)
req, err := s.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
tp := new(TeamPreferences)
resp, err := s.client.Do(req, tp)
if err != nil {
return nil, resp, err
}
return tp, resp, err
}
type UpdateTeamPreferencesOptions TeamPreferences
func (s *TeamsService) UpdateTeamPreferences(id int, opt *UpdateTeamPreferencesOptions) (*Response, error) {
u := fmt.Sprintf("teams/%d/preferences", id)
req, err := s.client.NewRequest("PUT", u, opt, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
//https://grafana.com/docs/http_api/team/#team-search-with-paging
//can search by id, uid, name...
func (s *TeamsService) SearchTeamByQuery(query string) (*TeamSearchResults, *Response, error) {
u := fmt.Sprintf("teams/search?query=%s", query)
req, err := s.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
tsr := new(TeamSearchResults)
resp, err := s.client.Do(req, tsr)
if err != nil {
return nil, resp, err
}
return tsr, resp, err
}