Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read grafana response #33

Merged
merged 6 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ with Grafana then client SDK for Go will be uniquely useful.
Expr: "sample request 1"}
graph.AddTarget(&target)
row1.Add(graph)
c := sdk.NewClient("http://grafana.host", "grafana-api-key", sdk.DefaultHTTPClient)
if err = c.SetDashboard(board, false); err != nil {
grafanaURL := "http://grafana.host"
c := sdk.NewClient(grafanaURL, "grafana-api-key", sdk.DefaultHTTPClient)
response, err := c.SetDashboard(board, false)
err != nil {
fmt.Printf("error on uploading dashboard %s", board.Title)
}
} else {
fmt.Printf("dashboard URL: %v", grafanaURL+*resp.URL)
}
```

The library includes several demo apps for showing API usage:
Expand Down
3 changes: 2 additions & 1 deletion cmd/import-dashboards-raw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func main() {
log.Println(err)
continue
}
if err = c.SetRawDashboard(rawBoard); err != nil {
_, err := c.SetRawDashboard(rawBoard)
if err != nil {
log.Printf("error on importing dashboard from %s", file.Name())
continue
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/import-dashboards/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func main() {
continue
}
c.DeleteDashboard(board.UpdateSlug())
if err = c.SetDashboard(board, false); err != nil {
_, err := c.SetDashboard(board, false)
if err != nil {
log.Printf("error on importing dashboard %s", board.Title)
continue
}
Expand Down
36 changes: 15 additions & 21 deletions rest-dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *Client) SearchDashboards(query string, starred bool, tags ...string) ([
// newer version or with same dashboard title.
// Grafana only can create or update a dashboard in a database. File dashboards
// may be only loaded with HTTP API but not created or updated.
func (r *Client) SetDashboard(board Board, overwrite bool) error {
func (r *Client) SetDashboard(board Board, overwrite bool) (StatusMessage, error) {
var (
isBoardFromDB bool
newBoard struct {
Expand All @@ -169,36 +169,33 @@ func (r *Client) SetDashboard(board Board, overwrite bool) error {
err error
)
if board.Slug, isBoardFromDB = cleanPrefix(board.Slug); !isBoardFromDB {
return errors.New("only database dashboard (with 'db/' prefix in a slug) can be set")
return StatusMessage{}, errors.New("only database dashboard (with 'db/' prefix in a slug) can be set")
}
newBoard.Dashboard = board
newBoard.Overwrite = overwrite
if !overwrite {
newBoard.Dashboard.ID = 0
}
if raw, err = json.Marshal(newBoard); err != nil {
return err
return StatusMessage{}, err
}
if raw, code, err = r.post("api/dashboards/db", nil, raw); err != nil {
return err
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return err
return StatusMessage{}, err
}
switch code {
case 401:
return fmt.Errorf("%d %s", code, *resp.Message)
case 412:
return fmt.Errorf("%d %s", code, *resp.Message)
if code != 200 {
return resp, fmt.Errorf("HTTP error %d: returns %s", code, *resp.Message)
}
return nil
return resp, nil
}

// SetRawDashboard updates existing dashboard or creates a new one.
// Contrary to SetDashboard() it accepts raw JSON instead of Board structure.
// Grafana only can create or update a dashboard in a database. File dashboards
// may be only loaded with HTTP API but not created or updated.
func (r *Client) SetRawDashboard(raw []byte) error {
func (r *Client) SetRawDashboard(raw []byte) (StatusMessage, error) {
var (
rawResp []byte
resp StatusMessage
Expand All @@ -208,7 +205,7 @@ func (r *Client) SetRawDashboard(raw []byte) error {
plain = make(map[string]interface{})
)
if err = json.Unmarshal(raw, &plain); err != nil {
return err
return StatusMessage{}, err
}
// TODO(axel) fragile place, refactor it
plain["id"] = 0
Expand All @@ -217,18 +214,15 @@ func (r *Client) SetRawDashboard(raw []byte) error {
buf.Write(raw)
buf.WriteString(`, "overwrite": true}`)
if rawResp, code, err = r.post("api/dashboards/db", nil, buf.Bytes()); err != nil {
return err
return StatusMessage{}, err
}
if err = json.Unmarshal(rawResp, &resp); err != nil {
return err
return StatusMessage{}, err
}
switch code {
case 401:
return fmt.Errorf("%d %s", code, *resp.Message)
case 412:
return fmt.Errorf("%d %s", code, *resp.Message)
if code != 200 {
return StatusMessage{}, fmt.Errorf("HTTP error %d: returns %s", code, *resp.Message)
}
return nil
return resp, nil
}

// DeleteDashboard deletes dashboard that selected by slug string.
Expand Down
2 changes: 2 additions & 0 deletions rest-request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type StatusMessage struct {
Slug *string `json:"slug"`
Version *int `json:"version"`
Status *string `json:"resp"`
UID *string `json:"uid"`
URL *string `json:"url"`
}

// NewClient initializes client for interacting with an instance of Grafana server;
Expand Down