Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Add a bunch of new APIs #16

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
170 changes: 170 additions & 0 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"strconv"
"strings"
)

type DashboardMeta struct {
IsStarred bool `json:"isStarred"`
Slug string `json:"slug"`
Uid string `json:"uid"`
Folder int64 `json:"folderId"`
}

Expand All @@ -24,13 +28,51 @@ type DashboardSaveResponse struct {
Version int64 `json:"version"`
}

const (
SearchTypeFolder = "dash-folder"
SearchTypeDashboard = "dash-db"
)

type SearchResultItem struct {
Id int64
Uid string
Title string
Url string
Type string
Uri string
}

func (item *SearchResultItem) IsFolder() bool {
return item.Type == SearchTypeFolder
}
func (item *SearchResultItem) Slug() string {
if item.IsFolder() {
return ""
}
return strings.Replace(item.Uri, "db/", "", 1)
}

type Dashboard struct {
Meta DashboardMeta `json:"meta"`
Model map[string]interface{} `json:"dashboard"`
Folder int64 `json:"folderId"`
Overwrite bool `json:overwrite`
}

type DashboardData struct {
Id int64 `json:"id"`
Uid string `json:"uid"`
Title string `json:"title"`
}
type DashboardVersionItem struct {
Id int64 `json:"id"`
Version int64 `json:"version"`
}
type DashboardVersion struct {
Id int64 `json:"id"`
Data *DashboardData `json:"data"`
}

// Deprecated: use NewDashboard instead
func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*DashboardSaveResponse, error) {
wrapper := map[string]interface{}{
Expand Down Expand Up @@ -119,6 +161,65 @@ func (c *Client) Dashboard(slug string) (*Dashboard, error) {
if os.Getenv("GF_LOG") != "" {
log.Printf("got back dashboard response %s", data)
}
result.Meta.Uid = result.Model["uid"].(string)
return result, err
}

func (c *Client) DashboardByUid(uid string) (*Dashboard, error) {
path := fmt.Sprintf("/api/dashboards/uid/%s", uid)
req, err := c.newRequest("GET", path, nil, nil)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

result := &Dashboard{}
err = json.Unmarshal(data, &result)
result.Folder = result.Meta.Folder
if os.Getenv("GF_LOG") != "" {
log.Printf("got back dashboard response %s", data)
}
result.Meta.Uid = result.Model["uid"].(string)
return result, err
}

func (c *Client) DashboardsByFolder(folderId int64) ([]*SearchResultItem, error) {
values := url.Values{}
values.Add("folderIds", strconv.Itoa(int(folderId)))
values.Add("type", "dash-db")

req, err := c.newRequest("GET", "/api/search", values, nil)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var result []*SearchResultItem
err = json.Unmarshal(data, &result)
return result, err
}

Expand All @@ -139,3 +240,72 @@ func (c *Client) DeleteDashboard(slug string) error {

return nil
}

func (c *Client) GetDashboardVersions(id int64) ([]*DashboardVersionItem, error) {
var list []*DashboardVersionItem
path := fmt.Sprintf("/api/dashboards/id/%d/versions/", id)
req, err := c.newRequest("GET", path, nil, nil)
if err != nil {
return list, err
}

resp, err := c.Do(req)
if err != nil {
return list, err
}
if resp.StatusCode != 200 {
return list, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return list, err
}

err = json.Unmarshal(data, &list)
if err != nil {
return list, err
}
return list, err
}

// Add this api to transform id to uid
func (c *Client) GetDashboardUidById(id int64) (string, error) {
versions, err := c.GetDashboardVersions(id)
if err != nil {
return "", err
}
path := fmt.Sprintf("/api/dashboards/id/%d/versions/%d", id, versions[0].Version)
req, err := c.newRequest("GET", path, nil, nil)
if err != nil {
return "", err
}

resp, err := c.Do(req)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

var result DashboardVersion
err = json.Unmarshal(data, &result)
if err != nil {
return "", err
}
return result.Data.Uid, err
}

func (c *Client) GetDashboardIdByUid(uid string) (int64, error) {
d, err := c.DashboardByUid(uid)
if err != nil {
return 0, err
}
return int64(d.Model["id"].(float64)), nil
}
24 changes: 24 additions & 0 deletions datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ func (c *Client) DataSource(id int64) (*DataSource, error) {
return result, err
}

func (c *Client) DataSources() ([]*DataSource, error) {
req, err := c.newRequest("GET", "/api/datasources", nil, nil)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var result []*DataSource
err = json.Unmarshal(data, &result)
return result, err
}

func (c *Client) DeleteDataSource(id int64) error {
path := fmt.Sprintf("/api/datasources/%d", id)
req, err := c.newRequest("DELETE", path, nil, nil)
Expand Down
33 changes: 33 additions & 0 deletions datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ import (

const (
createdDataSourceJSON = `{"id":1,"message":"Datasource added", "name": "test_datasource"}`
getDataSourcesJSON = `
[
{
"id":1,
"orgId":1,
"name":"datasource_elastic",
"type":"elasticsearch",
"access":"proxy",
"url":"http://mydatasource.com",
"password":"",
"user":"",
"database":"grafana-dash",
"basicAuth":false,
"basicAuthUser":"",
"basicAuthPassword":"",
"isDefault":false,
"jsonData":null
}
]
`
)

func gapiTestTools(code int, body string) (*httptest.Server, *Client) {
Expand Down Expand Up @@ -73,3 +93,16 @@ func TestNewDataSource(t *testing.T) {
t.Error("datasource creation response should return the created datasource ID")
}
}

func TestDataSources(t *testing.T) {
server, client := gapiTestTools(200, getDataSourcesJSON)
defer server.Close()

list, err := client.DataSources()
if err != nil {
t.Error(err)
}
if len(list) == 0 {
t.Error("Datasources parse error")
}
}
48 changes: 48 additions & 0 deletions folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ type Folder struct {
Title string `json:"title"`
}

type FolderPermission struct {
Role string `json:"role,omitempty"`
TeamId int64 `json:"teamId,omitempty"`
UserId int64 `json:"userId,omitempty"`
Permission PermissionType `json:"permission,omitempty"`
}

func (c *Client) Folders() ([]Folder, error) {
folders := make([]Folder, 0)

Expand Down Expand Up @@ -119,3 +126,44 @@ func (c *Client) DeleteFolder(id string) error {
}
return err
}

func (c *Client) GetFolderPermission(id string) ([]*FolderPermission, error) {
var permissionList []*FolderPermission
req, err := c.newRequest("GET", fmt.Sprintf("/api/folders/%s/permissions", id), nil, nil)
if err != nil {
return permissionList, err
}
resp, err := c.Do(req)
if err != nil {
return permissionList, err
}
if resp.StatusCode != 200 {
return permissionList, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return permissionList, err
}
err = json.Unmarshal(data, &permissionList)
return permissionList, err
}

func (c *Client) UpdateFolderPermission(id string, list []*FolderPermission) error {
dataMap := map[string]interface{}{
"items": list,
}
data, err := json.Marshal(dataMap)
req, err := c.newRequest("POST", fmt.Sprintf("/api/folders/%s/permissions", id), nil, bytes.NewBuffer(data))
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return nil
}
Loading