chronograf/server/dashboards.go

149 lines
3.7 KiB
Go
Raw Normal View History

package server
2016-12-14 07:56:26 +00:00
import (
2016-12-14 20:55:21 +00:00
"encoding/json"
2016-12-14 20:12:20 +00:00
"fmt"
"net/http"
2016-12-15 08:31:53 +00:00
"strconv"
2016-12-14 07:56:26 +00:00
2016-12-15 08:31:53 +00:00
"github.com/bouk/httprouter"
2016-12-14 20:12:20 +00:00
"github.com/influxdata/chronograf"
2016-12-14 07:56:26 +00:00
)
2016-12-08 00:31:22 +00:00
type dashboardLinks struct {
2016-12-14 20:12:20 +00:00
Self string `json:"self"` // Self link mapping to this resource
2016-12-08 00:31:22 +00:00
}
type dashboardResponse struct {
2016-12-14 20:12:20 +00:00
chronograf.Dashboard
Links dashboardLinks `json:"links"`
2016-12-08 00:31:22 +00:00
}
2016-12-14 06:57:52 +00:00
type getDashboardsResponse struct {
2016-12-14 07:56:26 +00:00
Dashboards []dashboardResponse `json:"dashboards"`
2016-12-14 06:57:52 +00:00
}
2016-12-14 07:56:26 +00:00
func newDashboardResponse(d chronograf.Dashboard) dashboardResponse {
2016-12-14 20:12:20 +00:00
base := "/chronograf/v1/dashboards"
return dashboardResponse{
Dashboard: d,
Links: dashboardLinks{
Self: fmt.Sprintf("%s/%d", base, d.ID),
},
}
2016-12-08 00:31:22 +00:00
}
// Dashboards returns all dashboards within the store
func (s *Service) Dashboards(w http.ResponseWriter, r *http.Request) {
2016-12-14 20:12:20 +00:00
ctx := r.Context()
dashboards, err := s.DashboardsStore.All(ctx)
if err != nil {
Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger)
return
}
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
func (s *Service) DashboardID(w http.ResponseWriter, r *http.Request) {
2016-12-14 20:12:20 +00:00
id, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
return
}
ctx := r.Context()
e, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id))
if err != nil {
notFound(w, id, s.Logger)
return
}
2016-12-14 20:54:58 +00:00
res := newDashboardResponse(*e)
2016-12-14 20:12:20 +00:00
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// NewDashboard creates and returns a new dashboard object
func (s *Service) NewDashboard(w http.ResponseWriter, r *http.Request) {
2016-12-14 20:54:58 +00:00
var dashboard *chronograf.Dashboard
if err := json.NewDecoder(r.Body).Decode(&dashboard); err != nil {
invalidJSON(w, s.Logger)
return
}
2016-12-14 20:54:58 +00:00
var err error
if dashboard, err = s.DashboardsStore.Add(r.Context(), dashboard); err != nil {
msg := fmt.Errorf("Error storing layout %v: %v", dashboard, err)
unknownErrorWithMessage(w, msg, s.Logger)
return
}
res := newDashboardResponse(*dashboard)
w.Header().Add("Location", res.Links.Self)
encodeJSON(w, http.StatusCreated, res, s.Logger)
}
// RemoveDashboard deletes a dashboard
func (s *Service) RemoveDashboard(w http.ResponseWriter, r *http.Request) {
2016-12-14 20:12:20 +00:00
id, err := paramID("id", r)
2016-12-14 17:37:47 +00:00
if err != nil {
2016-12-14 20:54:58 +00:00
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
2016-12-14 17:37:47 +00:00
return
}
ctx := r.Context()
e, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id))
if err != nil {
notFound(w, id, s.Logger)
return
}
2016-12-14 20:54:58 +00:00
if err := s.DashboardsStore.Delete(ctx, e); err != nil {
2016-12-14 17:37:47 +00:00
unknownErrorWithMessage(w, err, s.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
2016-12-15 08:31:53 +00:00
// UpdateDashboard replaces a dashboard
func (s *Service) UpdateDashboard(w http.ResponseWriter, r *http.Request) {
2016-12-15 08:31:53 +00:00
ctx := r.Context()
idParam, err := strconv.Atoi(httprouter.GetParamFromContext(ctx, "id"))
if err != nil {
msg := fmt.Sprintf("Could not parse dashboard ID: %s", err)
Error(w, http.StatusInternalServerError, msg, s.Logger)
}
id := chronograf.DashboardID(idParam)
_, err = s.DashboardsStore.Get(ctx, id)
if err != nil {
Error(w, http.StatusNotFound, fmt.Sprintf("ID %s not found", id), s.Logger)
return
}
2016-12-15 08:31:53 +00:00
var req *chronograf.Dashboard
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
invalidJSON(w, s.Logger)
return
}
req.ID = id
if err := s.DashboardsStore.Update(ctx, req); err != nil {
msg := fmt.Sprintf("Error updating dashboard ID %s: %v", id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger)
return
}
res := newDashboardResponse(*req)
encodeJSON(w, http.StatusOK, res, s.Logger)
}