This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from smithclay/dashboard-api
Add Support for Dashboards API
- Loading branch information
Showing
4 changed files
with
446 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
func (c *Client) queryDashboards() ([]Dashboard, error) { | ||
dashboards := []Dashboard{} | ||
reqURL, err := url.Parse("/dashboards.json") | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
nextPath := reqURL.String() | ||
|
||
for nextPath != "" { | ||
resp := struct { | ||
Dashboards []Dashboard `json:"dashboards,omitempty"` | ||
}{} | ||
|
||
nextPath, err = c.Do("GET", nextPath, nil, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dashboards = append(dashboards, resp.Dashboards...) | ||
} | ||
return dashboards, nil | ||
} | ||
|
||
// GetDashboard returns a specific dashboard for the account. | ||
func (c *Client) GetDashboard(id int) (*Dashboard, error) { | ||
reqURL, err := url.Parse(fmt.Sprintf("/dashboards/%v.json", id)) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp := struct { | ||
Dashboard Dashboard `json:"dashboard,omitempty"` | ||
}{} | ||
|
||
_, err = c.Do("GET", reqURL.String(), nil, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp.Dashboard, nil | ||
} | ||
|
||
// ListDashboards returns all dashboards for the account. | ||
func (c *Client) ListDashboards() ([]Dashboard, error) { | ||
return c.queryDashboards() | ||
} | ||
|
||
// CreateDashboard creates dashboard given the passed configuration. | ||
func (c *Client) CreateDashboard(dashboard Dashboard) (*Dashboard, error) { | ||
req := struct { | ||
Dashboard Dashboard `json:"dashboard"` | ||
}{ | ||
Dashboard: dashboard, | ||
} | ||
|
||
resp := struct { | ||
Dashboard Dashboard `json:"dashboard,omitempty"` | ||
}{} | ||
|
||
u := &url.URL{Path: "/dashboards.json"} | ||
_, err := c.Do("POST", u.String(), req, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp.Dashboard, nil | ||
} | ||
|
||
// UpdateDashboard updates a dashboard given the passed configuration | ||
func (c *Client) UpdateDashboard(dashboard Dashboard) (*Dashboard, error) { | ||
id := dashboard.ID | ||
|
||
req := struct { | ||
Dashboard Dashboard `json:"dashboard"` | ||
}{ | ||
Dashboard: dashboard, | ||
} | ||
|
||
resp := struct { | ||
Dashboard Dashboard `json:"dashboard,omitempty"` | ||
}{} | ||
|
||
u := &url.URL{Path: fmt.Sprintf("/dashboards/%v.json", id)} | ||
_, err := c.Do("PUT", u.String(), req, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp.Dashboard, nil | ||
} | ||
|
||
// DeleteDashboard deletes an existing dashboard given the passed configuration | ||
func (c *Client) DeleteDashboard(id int) error { | ||
u := &url.URL{Path: fmt.Sprintf("/dashboards/%v.json", id)} | ||
_, err := c.Do("DELETE", u.String(), nil, nil) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestDashboards_Basic(t *testing.T) { | ||
c := newTestAPIClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(` | ||
{ | ||
"dashboards": [ | ||
{ | ||
"id": 129507, | ||
"title": "Election!", | ||
"icon": "bar-chart", | ||
"created_at": "2016-02-20T01:57:58Z", | ||
"updated_at": "2016-09-27T22:59:21Z", | ||
"visibility": "all", | ||
"editable": "editable_by_all", | ||
"ui_url": "https://insights.newrelic.com/accounts/1136088/dashboards/129507", | ||
"api_url": "https://api.newrelic.com/v2/dashboards/129507", | ||
"owner_email": "[email protected]", | ||
"filter": null | ||
} | ||
] | ||
} | ||
`)) | ||
})) | ||
|
||
apps, err := c.queryDashboards() | ||
if err != nil { | ||
t.Log(err) | ||
t.Fatal("queryDashboards error") | ||
} | ||
|
||
if len(apps) == 0 { | ||
t.Fatal("No dashboards found") | ||
} | ||
} | ||
|
||
func TestGetDashboard(t *testing.T) { | ||
c := newTestAPIClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(` | ||
{ | ||
"dashboard": { | ||
"id": 1234, | ||
"title": "Test", | ||
"icon": "bar-chart", | ||
"created_at": "2016-02-20T01:57:58Z", | ||
"updated_at": "2016-09-27T22:59:21Z", | ||
"visibility": "all", | ||
"editable": "editable_by_all", | ||
"ui_url": "https://insights.newrelic.com/accounts/1136088/dashboards/129507", | ||
"api_url": "https://api.newrelic.com/v2/dashboards/129507", | ||
"owner_email": "[email protected]", | ||
"metadata": { | ||
"version": 1 | ||
}, | ||
"filter": null, | ||
"widgets": [ | ||
{ | ||
"visualization": "facet_bar_chart", | ||
"account_id": 1, | ||
"data": [ | ||
{ | ||
"nrql": "SELECT percentile(duration, 95) FROM SyntheticCheck FACET monitorName since 7 days ago" | ||
} | ||
], | ||
"presentation": { | ||
"title": "95th Percentile Load Time (ms)", | ||
"notes": null, | ||
"drilldown_dashboard_id": null | ||
}, | ||
"layout": { | ||
"width": 2, | ||
"height": 1, | ||
"row": 1, | ||
"column": 1 | ||
} | ||
} | ||
] | ||
} | ||
} | ||
`)) | ||
})) | ||
|
||
dashboard, err := c.GetDashboard(1234) | ||
if err != nil { | ||
t.Log(err) | ||
t.Fatal("getDashboard error") | ||
} | ||
|
||
if len(dashboard.Widgets) == 0 { | ||
t.Fatal("Dashboard widgets found") | ||
} | ||
} | ||
|
||
func TestCreateDashboardCondition(t *testing.T) { | ||
c := newTestAPIClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(` | ||
{ | ||
"dashboard": { | ||
"id": 1234, | ||
"title": "Test", | ||
"icon": "bar-chart", | ||
"created_at": "2016-02-20T01:57:58Z", | ||
"updated_at": "2016-09-27T22:59:21Z", | ||
"visibility": "all", | ||
"editable": "editable_by_all", | ||
"ui_url": "https://insights.newrelic.com/accounts/1136088/dashboards/129507", | ||
"api_url": "https://api.newrelic.com/v2/dashboards/129507", | ||
"owner_email": "[email protected]", | ||
"metadata": { | ||
"version": 1 | ||
}, | ||
"filter": null, | ||
"widgets": [ | ||
{ | ||
"visualization": "facet_bar_chart", | ||
"account_id": 1, | ||
"data": [ | ||
{ | ||
"nrql": "SELECT percentile(duration, 95) FROM SyntheticCheck FACET monitorName since 7 days ago" | ||
} | ||
], | ||
"presentation": { | ||
"title": "95th Percentile Load Time (ms)", | ||
"notes": null, | ||
"drilldown_dashboard_id": null | ||
}, | ||
"layout": { | ||
"width": 2, | ||
"height": 1, | ||
"row": 1, | ||
"column": 1 | ||
} | ||
} | ||
] | ||
} | ||
} | ||
`)) | ||
})) | ||
|
||
dashboardWidgetLayout := DashboardWidgetLayout{ | ||
Width: 2, | ||
Height: 1, | ||
Row: 1, | ||
Column: 1, | ||
} | ||
|
||
dashboardWidgetPresentation := DashboardWidgetPresentation{ | ||
Title: "95th Percentile Load Time (ms)", | ||
Notes: "", | ||
} | ||
|
||
dashboardWidgetData := []DashboardWidgetData{ | ||
{ | ||
NRQL: "SELECT percentile(duration, 95) FROM SyntheticCheck FACET monitorName since 7 days ago", | ||
}, | ||
} | ||
|
||
dashboardWidgets := []DashboardWidget{ | ||
{ | ||
Visualization: "facet_bar_chart", | ||
AccountID: 1, | ||
Data: dashboardWidgetData, | ||
Presentation: dashboardWidgetPresentation, | ||
Layout: dashboardWidgetLayout, | ||
}, | ||
} | ||
|
||
dashboardMetadata := DashboardMetadata{ | ||
Version: 1, | ||
} | ||
|
||
dashboard := Dashboard{ | ||
Title: "Test", | ||
Icon: "bar_chart", | ||
Widgets: dashboardWidgets, | ||
Metadata: dashboardMetadata, | ||
} | ||
|
||
dashboardResp, err := c.CreateDashboard(dashboard) | ||
|
||
if err != nil { | ||
t.Log(err) | ||
t.Fatal("CreateDashboard error") | ||
} | ||
if dashboardResp == nil { | ||
t.Log(err) | ||
t.Fatal("CreateDashboard error") | ||
} | ||
if dashboard.Metadata.Version != 1 { | ||
t.Fatal("CreateDashboard metadata version incorrect") | ||
} | ||
if dashboardResp.ID != 1234 { | ||
t.Fatal("CreateDashboard ID was not parsed correctly") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.