diff --git a/orgs.go b/orgs.go index 2ef4e2d..0bf9496 100644 --- a/orgs.go +++ b/orgs.go @@ -142,3 +142,25 @@ func (c *Client) DeleteOrg(id int64) error { } return err } + +// UpdateCurrentOrgPreferences changes the preferences of the currently-selected organization +// https://grafana.com/docs/grafana/latest/http_api/preferences/#update-current-org-prefs +func (c *Client) UpdateCurrentOrgPreferences(prefs map[string]interface{}) error { + payload, err := json.Marshal(prefs) + if err != nil { + return err + } + + req, err := c.newRequest("PUT", "/api/org/preferences", nil, bytes.NewBuffer(payload)) + if err != nil { + return err + } + + response, err := c.Do(req) + if err != nil { + return err + } else if response.StatusCode != 200 { + return errors.New(response.Status) + } + return nil +} diff --git a/user.go b/user.go index 66f5634..32c2e48 100644 --- a/user.go +++ b/user.go @@ -3,6 +3,7 @@ package gapi import ( "encoding/json" "errors" + "fmt" "io/ioutil" "net/url" ) @@ -74,3 +75,85 @@ func (c *Client) UserByEmail(email string) (User, error) { user = User(tmp) return user, err } + +// CurrentUser returns user info about the currently-logged-in user +// https://grafana.com/docs/grafana/latest/http_api/user/#actual-user +func (c *Client) CurrentUser() (User, error) { + user := User{} + req, err := c.newRequest("GET", "/api/user", nil, nil) + if err != nil { + return user, err + } + resp, err := c.Do(req) + if err != nil { + return user, err + } + if resp.StatusCode != 200 { + return user, errors.New(resp.Status) + } + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return user, err + } + tmp := struct { + Id int64 `json:"id,omitempty"` + Email string `json:"email,omitempty"` + Name string `json:"name,omitempty"` + Login string `json:"login,omitempty"` + Password string `json:"password,omitempty"` + IsAdmin bool `json:"isGrafanaAdmin,omitempty"` + }{} + err = json.Unmarshal(data, &tmp) + if err != nil { + return user, err + } + user = User(tmp) + return user, err +} + +type OrgMembership struct { + Id int64 `json:"orgId"` + Name string `json:"name"` + Role string `json:"role"` +} + +// CurrentUserOrgs returns org membership for the currently-logged-in user +// https://grafana.com/docs/grafana/latest/http_api/user/#organizations-of-the-actual-user +func (c *Client) CurrentUserOrgs() ([]OrgMembership, error) { + orgs := make([]OrgMembership, 0) + + req, err := c.newRequest("GET", "/api/user/orgs/", nil, nil) + if err != nil { + return orgs, err + } + resp, err := c.Do(req) + if err != nil { + return orgs, err + } + if resp.StatusCode != 200 { + return orgs, errors.New(resp.Status) + } + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return orgs, err + } + err = json.Unmarshal(data, &orgs) + return orgs, err +} + +// SwitchUserOrganization changes the user's currently-selected organization +// https://grafana.com/docs/grafana/latest/http_api/user/#switch-user-context-for-a-specified-user +func (c *Client) SwitchUserOrganization(userID, orgID int64) error { + req, err := c.newRequest("POST", fmt.Sprintf("/api/users/%d/using/%d", userID, orgID), nil, nil) + if err != nil { + return err + } + + response, err := c.Do(req) + if err != nil { + return err + } else if response.StatusCode != 200 { + return errors.New(response.Status) + } + return nil +}