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

Commit f2dab71

Browse files
mdbinkel
andauthored
Add methods for interacting with org preferences API (and a small bug fix) (#102)
* add additional fields to type Preferences Signed-off-by: Mike Ball <[email protected]> * move preferences types to their own file Signed-off-by: Mike Ball <[email protected]> * add methods for interacting with org prefs API Signed-off-by: Mike Ball <[email protected]> * remove TODO comment from Preferences Signed-off-by: Mike Ball <[email protected]> * Update org_preferences.go Co-authored-by: Leandro López <[email protected]> Signed-off-by: Mike Ball <[email protected]> Co-authored-by: Leandro López <[email protected]>
1 parent abee6f8 commit f2dab71

File tree

4 files changed

+141
-7
lines changed

4 files changed

+141
-7
lines changed

org_preferences.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
)
7+
8+
// UpdateOrgPreferencesResponse represents the response to a request
9+
// updating Grafana org preferences.
10+
type UpdateOrgPreferencesResponse struct {
11+
Message string `json:"message"`
12+
}
13+
14+
// OrgPreferences fetches org preferences.
15+
func (c *Client) OrgPreferences() (Preferences, error) {
16+
var prefs Preferences
17+
err := c.request("GET", "/api/org/preferences", nil, nil, &prefs)
18+
return prefs, err
19+
}
20+
21+
// UpdateOrgPreferences updates only those org preferences specified in the passed Preferences, without impacting others.
22+
func (c *Client) UpdateOrgPreferences(p Preferences) (UpdateOrgPreferencesResponse, error) {
23+
var resp UpdateOrgPreferencesResponse
24+
data, err := json.Marshal(p)
25+
if err != nil {
26+
return resp, err
27+
}
28+
29+
err = c.request("PATCH", "/api/org/preferences", nil, bytes.NewBuffer(data), &resp)
30+
if err != nil {
31+
return resp, err
32+
}
33+
34+
return resp, err
35+
}
36+
37+
// UpdateAllOrgPreferences overrwrites all org preferences with the passed Preferences.
38+
func (c *Client) UpdateAllOrgPreferences(p Preferences) (UpdateOrgPreferencesResponse, error) {
39+
var resp UpdateOrgPreferencesResponse
40+
data, err := json.Marshal(p)
41+
if err != nil {
42+
return resp, err
43+
}
44+
45+
err = c.request("PUT", "/api/org/preferences", nil, bytes.NewBuffer(data), &resp)
46+
if err != nil {
47+
return resp, err
48+
}
49+
50+
return resp, err
51+
}

org_preferences_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
)
6+
7+
const (
8+
getOrgPreferencesJSON = `{"theme": "foo","homeDashboardId": 0,"timezone": "","weekStart": "","navbar": {"savedItems": null},"queryHistory": {"homeTab": ""}}`
9+
updateOrgPreferencesJSON = `{"message":"Preferences updated"}`
10+
)
11+
12+
func TestOrgPreferences(t *testing.T) {
13+
server, client := gapiTestTools(t, 200, getOrgPreferencesJSON)
14+
defer server.Close()
15+
16+
resp, err := client.OrgPreferences()
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
expected := "foo"
22+
if resp.Theme != expected {
23+
t.Errorf("Expected org preferences theme '%s'; got '%s'", expected, resp.Theme)
24+
}
25+
}
26+
27+
func TestUpdateOrgPreferences(t *testing.T) {
28+
server, client := gapiTestTools(t, 200, updateOrgPreferencesJSON)
29+
defer server.Close()
30+
31+
resp, err := client.UpdateOrgPreferences(Preferences{
32+
Theme: "foo",
33+
})
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
expected := "Preferences updated"
39+
if resp.Message != expected {
40+
t.Errorf("Expected org preferences message '%s'; got '%s'", expected, resp.Message)
41+
}
42+
}
43+
44+
func TestUpdateAllOrgPreference(t *testing.T) {
45+
server, client := gapiTestTools(t, 200, updateOrgPreferencesJSON)
46+
defer server.Close()
47+
48+
resp, err := client.UpdateAllOrgPreferences(Preferences{
49+
Theme: "foo",
50+
})
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
55+
expected := "Preferences updated"
56+
if resp.Message != expected {
57+
t.Errorf("Expected org preferences message '%s'; got '%s'", expected, resp.Message)
58+
}
59+
}

preferences.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gapi
2+
3+
// NavLink represents a Grafana nav link.
4+
type NavLink struct {
5+
ID string `json:"id,omitempty"`
6+
Text string `json:"text,omitempty"`
7+
URL string `json:"url,omitempty"`
8+
Target string `json:"target,omitempty"`
9+
}
10+
11+
// NavbarPreference represents a Grafana navbar preference.
12+
type NavbarPreference struct {
13+
SavedItems []NavLink `json:"savedItems"`
14+
}
15+
16+
// QueryHistoryPreference represents a Grafana query history preference.
17+
type QueryHistoryPreference struct {
18+
HomeTab string `json:"homeTab"`
19+
}
20+
21+
// Preferences represents Grafana preferences.
22+
type Preferences struct {
23+
Theme string `json:"theme"`
24+
HomeDashboardID int64 `json:"homeDashboardId"`
25+
HomeDashboardUID string `json:"homeDashboardUID,omitempty"`
26+
Timezone string `json:"timezone,omitempty"`
27+
WeekStart string `json:"weekStart,omitempty"`
28+
Locale string `json:"locale,omitempty"`
29+
Navbar NavbarPreference `json:"navbar,omitempty"`
30+
QueryHistory QueryHistoryPreference `json:"queryHistory,omitempty"`
31+
}

team.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ type TeamMember struct {
3838
Permission int64 `json:"permission,omitempty"`
3939
}
4040

41-
// Preferences represents Grafana preferences.
42-
type Preferences struct {
43-
Theme string `json:"theme"`
44-
HomeDashboardID int64 `json:"homeDashboardID"`
45-
Timezone string `json:"timezone"`
46-
}
47-
4841
// SearchTeam searches Grafana teams and returns the results.
4942
func (c *Client) SearchTeam(query string) (*SearchTeam, error) {
5043
var result SearchTeam

0 commit comments

Comments
 (0)