forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.go
180 lines (152 loc) · 4.83 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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
)
// SearchTeam represents a search for a Grafana team.
type SearchTeam struct {
TotalCount int64 `json:"totalCount,omitempty"`
Teams []*Team `json:"teams,omitempty"`
Page int64 `json:"page,omitempty"`
PerPage int64 `json:"perPage,omitempty"`
}
// Team consists of a get response
// It's used in Add and Update API
type Team struct {
ID int64 `json:"id,omitempty"`
OrgID int64 `json:"orgId,omitempty"`
Name string `json:"name"`
Email string `json:"email,omitempty"`
AvatarURL string `json:"avatarUrl,omitempty"`
MemberCount int64 `json:"memberCount,omitempty"`
Permission int64 `json:"permission,omitempty"`
}
// TeamMember represents a Grafana team member.
type TeamMember struct {
OrgID int64 `json:"orgId,omitempty"`
TeamID int64 `json:"teamId,omitempty"`
UserID int64 `json:"userID,omitempty"`
Email string `json:"email,omitempty"`
Login string `json:"login,omitempty"`
AvatarURL string `json:"avatarUrl,omitempty"`
Permission int64 `json:"permission,omitempty"`
}
// Preferences represents Grafana preferences.
type Preferences struct {
Theme string `json:"theme"`
HomeDashboardID int64 `json:"homeDashboardID"`
Timezone string `json:"timezone"`
}
// SearchTeam searches Grafana teams and returns the results.
func (c *Client) SearchTeam(query string) (*SearchTeam, error) {
var result SearchTeam
page := "1"
perPage := "1000"
path := "/api/teams/search"
queryValues := url.Values{}
queryValues.Set("page", page)
queryValues.Set("perPage", perPage)
queryValues.Set("query", query)
err := c.request("GET", path, queryValues, nil, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// Team fetches and returns the Grafana team whose ID it's passed.
func (c *Client) Team(id int64) (*Team, error) {
team := &Team{}
err := c.request("GET", fmt.Sprintf("/api/teams/%d", id), nil, nil, team)
if err != nil {
return nil, err
}
return team, nil
}
// AddTeam makes a new team
// email arg is an optional value.
// If you don't want to set email, please set "" (empty string).
// When team creation is successful, returns the team ID.
func (c *Client) AddTeam(name string, email string) (int64, error) {
id := int64(0)
path := "/api/teams"
team := Team{
Name: name,
Email: email,
}
data, err := json.Marshal(team)
if err != nil {
return id, err
}
tmp := struct {
ID int64 `json:"teamId"`
}{}
err = c.request("POST", path, nil, bytes.NewBuffer(data), &tmp)
if err != nil {
return id, err
}
return tmp.ID, err
}
// UpdateTeam updates a Grafana team.
func (c *Client) UpdateTeam(id int64, name string, email string) error {
path := fmt.Sprintf("/api/teams/%d", id)
team := Team{
Name: name,
}
// add param if email exists
if email != "" {
team.Email = email
}
data, err := json.Marshal(team)
if err != nil {
return err
}
return c.request("PUT", path, nil, bytes.NewBuffer(data), nil)
}
// DeleteTeam deletes the Grafana team whose ID it's passed.
func (c *Client) DeleteTeam(id int64) error {
return c.request("DELETE", fmt.Sprintf("/api/teams/%d", id), nil, nil, nil)
}
// TeamMembers fetches and returns the team members for the Grafana team whose ID it's passed.
func (c *Client) TeamMembers(id int64) ([]*TeamMember, error) {
members := make([]*TeamMember, 0)
err := c.request("GET", fmt.Sprintf("/api/teams/%d/members", id), nil, nil, &members)
if err != nil {
return members, err
}
return members, nil
}
// AddTeamMember adds a user to the Grafana team whose ID it's passed.
func (c *Client) AddTeamMember(id int64, userID int64) error {
path := fmt.Sprintf("/api/teams/%d/members", id)
member := TeamMember{UserID: userID}
data, err := json.Marshal(member)
if err != nil {
return err
}
return c.request("POST", path, nil, bytes.NewBuffer(data), nil)
}
// RemoveMemberFromTeam removes a user from the Grafana team whose ID it's passed.
func (c *Client) RemoveMemberFromTeam(id int64, userID int64) error {
path := fmt.Sprintf("/api/teams/%d/members/%d", id, userID)
return c.request("DELETE", path, nil, nil, nil)
}
// TeamPreferences fetches and returns preferences for the Grafana team whose ID it's passed.
func (c *Client) TeamPreferences(id int64) (*Preferences, error) {
preferences := &Preferences{}
err := c.request("GET", fmt.Sprintf("/api/teams/%d/preferences", id), nil, nil, preferences)
if err != nil {
return nil, err
}
return preferences, nil
}
// UpdateTeamPreferences updates team preferences for the Grafana team whose ID it's passed.
func (c *Client) UpdateTeamPreferences(id int64, preferences Preferences) error {
path := fmt.Sprintf("/api/teams/%d/preferences", id)
data, err := json.Marshal(preferences)
if err != nil {
return err
}
return c.request("PUT", path, nil, bytes.NewBuffer(data), nil)
}