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

Commit

Permalink
Merge pull request #12 from goraxe/master
Browse files Browse the repository at this point in the history
Add support for folders api
  • Loading branch information
tonglil authored Oct 22, 2018
2 parents bc20f1a + cebdee9 commit 9cc848d
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ _testmain.go
*.exe
*.test
*.prof

# vim swap files
.*.sw?
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (c *Client) newRequest(method, requestPath string, query url.Values, body i

if os.Getenv("GF_LOG") != "" {
if body == nil {
log.Println("request to ", url.String(), "with no body data")
log.Printf("request (%s) to %s with no body data", method, url.String())
} else {
log.Println("request to ", url.String(), "with body data", body.(*bytes.Buffer).String())
log.Printf("request (%s) to %s with body data: %s", method, url.String(), body.(*bytes.Buffer).String())
}
}

Expand Down
45 changes: 43 additions & 2 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
)

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

type DashboardSaveResponse struct {
Slug string `json:"slug"`
Id int64 `json:"id"`
Uid string `json:"uid"`
Status string `json:"status"`
Version int64 `json:"version"`
}

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

// Deprecated: use NewDashboard instead
func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*DashboardSaveResponse, error) {
wrapper := map[string]interface{}{
"dashboard": model,
Expand All @@ -38,6 +46,35 @@ func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*D
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
data, _ = ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("status: %d, body: %s", resp.StatusCode, data)
}

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

result := &DashboardSaveResponse{}
err = json.Unmarshal(data, &result)
return result, err
}

func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, error) {
data, err := json.Marshal(dashboard)
if err != nil {
return nil, err
}
req, err := c.newRequest("POST", "/api/dashboards/db", nil, bytes.NewBuffer(data))
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
Expand Down Expand Up @@ -78,6 +115,10 @@ func (c *Client) Dashboard(slug string) (*Dashboard, error) {

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)
}
return result, err
}

Expand Down
121 changes: 121 additions & 0 deletions folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package gapi

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)

type Folder struct {
Id int64 `json:"id"`
Uid string `json:"uid"`
Title string `json:"title"`
}

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

req, err := c.newRequest("GET", "/api/folders/", nil, nil)
if err != nil {
return folders, err
}
resp, err := c.Do(req)
if err != nil {
return folders, err
}
if resp.StatusCode != 200 {
return folders, errors.New(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return folders, err
}
err = json.Unmarshal(data, &folders)
return folders, err
}

func (c *Client) Folder(id int64) (*Folder, error) {
folder := &Folder{}
req, err := c.newRequest("GET", fmt.Sprintf("/api/folders/id/%d", id), nil, nil)
if err != nil {
return folder, err
}
resp, err := c.Do(req)
if err != nil {
return folder, err
}
if resp.StatusCode != 200 {
return folder, errors.New(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return folder, err
}
err = json.Unmarshal(data, &folder)
return folder, err
}

func (c *Client) NewFolder(title string) (Folder, error) {
folder := Folder{}
dataMap := map[string]string{
"title": title,
}
data, err := json.Marshal(dataMap)
req, err := c.newRequest("POST", "/api/folders", nil, bytes.NewBuffer(data))
if err != nil {
return folder, err
}
resp, err := c.Do(req)
if err != nil {
return folder, err
}
if resp.StatusCode != 200 {
data, _ = ioutil.ReadAll(resp.Body)
return folder, fmt.Errorf("status: %s body: %s", resp.Status, data)
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return folder, err
}
err = json.Unmarshal(data, &folder)
if err != nil {
return folder, err
}
return folder, err
}

func (c *Client) UpdateFolder(id string, name string) error {
dataMap := map[string]string{
"name": name,
}
data, err := json.Marshal(dataMap)
req, err := c.newRequest("PUT", fmt.Sprintf("/api/folders/%s", 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 err
}

func (c *Client) DeleteFolder(id string) error {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/folders/%s", id), 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)
}
return err
}
156 changes: 156 additions & 0 deletions folder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package gapi

import (
"github.com/gobs/pretty"
"testing"
)

const (
getFoldersJSON = `
[
{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
]
`
getFolderJSON = `
{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
`
createdFolderJSON = `
{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
`
updatedFolderJSON = `
{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet DEF",
"url": "/dashboards/f/nErXDvCkzz/department-def",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
`
deletedFolderJSON = `
{
"message":"Folder deleted"
}
`
)

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

folders, err := client.Folders()
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(folders))

if len(folders) != 1 {
t.Error("Length of returned folders should be 1")
}
if folders[0].Id != 1 || folders[0].Title != "Departmenet ABC" {
t.Error("Not correctly parsing returned folders.")
}
}

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

folder := int64(1)
resp, err := client.Folder(folder)
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(resp))

if resp.Id != folder || resp.Title != "Departmenet ABC" {
t.Error("Not correctly parsing returned folder.")
}
}

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

resp, err := client.NewFolder("test-folder")
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(resp))

if resp.Uid != "nErXDvCkzz" {
t.Error("Not correctly parsing returned creation message.")
}
}

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

err := client.UpdateFolder("nErXDvCkzz", "test-folder")
if err != nil {
t.Error(err)
}
}

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

err := client.DeleteFolder("nErXDvCkzz")
if err != nil {
t.Error(err)
}
}

0 comments on commit 9cc848d

Please sign in to comment.