diff --git a/bolt/dashboards.go b/bolt/dashboards.go index 67b17c9781..81d3b95be6 100644 --- a/bolt/dashboards.go +++ b/bolt/dashboards.go @@ -20,9 +20,9 @@ type DashboardsStore struct { } // All returns all known dashboards -func (s *DashboardsStore) All(ctx context.Context) ([]chronograf.Dashboard, error) { +func (d *DashboardsStore) All(ctx context.Context) ([]chronograf.Dashboard, error) { var srcs []chronograf.Dashboard - if err := s.client.db.View(func(tx *bolt.Tx) error { + if err := d.client.db.View(func(tx *bolt.Tx) error { if err := tx.Bucket(DashboardBucket).ForEach(func(k, v []byte) error { var src chronograf.Dashboard if err := internal.UnmarshalDashboard(v, &src); err != nil { diff --git a/chronograf.go b/chronograf.go index 4e541eee04..96a9de27e1 100644 --- a/chronograf.go +++ b/chronograf.go @@ -247,6 +247,7 @@ type DashboardCell struct { // DashboardsStore is the storage and retrieval of dashboards type DashboardsStore interface { + All(context.Context) ([]Dashboard, error) // Create a new Dashboard in the DashboardStore Add(context.Context, *Dashboard) (*Dashboard, error) // Delete the Dashboard from the DashboardStore if `ID` exists. diff --git a/server/dashboards.go b/server/dashboards.go index 2dd457d3a0..f0e307d76b 100644 --- a/server/dashboards.go +++ b/server/dashboards.go @@ -1,39 +1,53 @@ package server -import "net/http" +import ( + "fmt" + "net/http" + + "github.com/influxdata/chronograf" +) type dashboardLinks struct { Self string `json:"self"` // Self link mapping to this resource } type dashboardResponse struct { - *chronograf.Dashboard + chronograf.Dashboard Links dashboardLinks `json:"links"` } type getDashboardsResponse struct { - Dashboards []ldashboardResponse `json:"dashboards"` + Dashboards []dashboardResponse `json:"dashboards"` } -func newDashboardResponse(d *chronograf.Dashboard) dashboardResponse { +func newDashboardResponse(d chronograf.Dashboard) dashboardResponse { base := "/chronograf/v1/dashboards" return dashboardResponse{ Dashboard: d, Links: dashboardLinks{ Self: fmt.Sprintf("%s/%d", base, d.ID), - } + }, } } // Dashboards returns all dashboards within the store func (s *Service) Dashboards(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() dashboards, err := s.DashboardsStore.All(ctx) if err != nil { Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger) return } - encodeJSON(w, http.StatusOK, getDashboardsResponse{Dashboards: []dashboardResponse{}}, s.Logger) + res := getDashboardsResponse{ + Dashboards: []dashboardResponse{}, + } + + for _, dashboard := range dashboards { + res.Dashboards = append(res.Dashboards, newDashboardResponse(dashboard)) + } + + encodeJSON(w, http.StatusOK, res, s.Logger) } // DashboardID returns a single specified dashboard