Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit d2fbe45

Browse files
authored
add support of team external group (#29)
1 parent 90e22a9 commit d2fbe45

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

team_external_group.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
// TeamGroup represents a Grafana TeamGroup.
10+
type TeamGroup struct {
11+
OrgID int64 `json:"orgId,omitempty"`
12+
TeamID int64 `json:"teamId,omitempty"`
13+
GroupID string `json:"groupID,omitempty"`
14+
}
15+
16+
// TeamGroups fetches and returns the list of Grafana team group whose Team ID it's passed.
17+
func (c *Client) TeamGroups(id int64) ([]TeamGroup, error) {
18+
teamGroups := make([]TeamGroup, 0)
19+
err := c.request("GET", fmt.Sprintf("/api/teams/%d/groups", id), nil, nil, &teamGroups)
20+
if err != nil {
21+
return teamGroups, err
22+
}
23+
24+
return teamGroups, nil
25+
}
26+
27+
// NewTeamGroup creates a new Grafana Team Group .
28+
func (c *Client) NewTeamGroup(id int64, groupID string) error {
29+
dataMap := map[string]string{
30+
"groupId": groupID,
31+
}
32+
data, err := json.Marshal(dataMap)
33+
if err != nil {
34+
return err
35+
}
36+
37+
return c.request("POST", fmt.Sprintf("/api/teams/%d/groups", id), nil, bytes.NewBuffer(data), nil)
38+
}
39+
40+
// DeleteTeam deletes the Grafana team whose ID it's passed.
41+
func (c *Client) DeleteTeamGroup(id int64, groupID string) error {
42+
return c.request("DELETE", fmt.Sprintf("/api/teams/%d/groups/%s", id, groupID), nil, nil, nil)
43+
}

team_external_group_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
const (
10+
getTeamGroupsJSON = `
11+
[
12+
{
13+
"orgId": 1,
14+
"teamId": 1,
15+
"groupId": "test"
16+
}
17+
]
18+
`
19+
createdTeamGroupJSON = `
20+
{
21+
"message":"Group added to Team"
22+
}
23+
`
24+
25+
deletedTeamGroupJSON = `
26+
{
27+
"message":"Team Group removed"
28+
}
29+
`
30+
)
31+
32+
func TestTeamGroups(t *testing.T) {
33+
server, client := gapiTestTools(t, 200, getTeamGroupsJSON)
34+
defer server.Close()
35+
36+
teamID := int64(1)
37+
teamGroups, err := client.TeamGroups(teamID)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
t.Log(pretty.PrettyFormat(teamGroups))
43+
44+
if len(teamGroups) != 1 {
45+
t.Error("Length of returned teamGroups should be 1")
46+
}
47+
if teamGroups[0].TeamID != 1 || teamGroups[0].OrgID != 1 || teamGroups[0].GroupID != "test" {
48+
t.Error("Not correctly parsing returned teamGroups.")
49+
}
50+
}
51+
52+
func TestNewTeamGroup(t *testing.T) {
53+
server, client := gapiTestTools(t, 200, createdTeamGroupJSON)
54+
defer server.Close()
55+
56+
err := client.NewTeamGroup(int64(1), "test")
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
}
61+
62+
func TestDeleteTeamGroup(t *testing.T) {
63+
server, client := gapiTestTools(t, 200, deletedTeamGroupJSON)
64+
defer server.Close()
65+
66+
err := client.DeleteTeamGroup(int64(1), "test")
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
}

0 commit comments

Comments
 (0)