grafana/pkg/api/dashboard_public_config.go
Jeff Levin c7f8c2cc73
add isPublic to dashboard (#48012)
adds toggle to make a dashboard public

* config struct for public dashboard config
* api endpoints for public dashboard configuration
* ui for toggling public dashboard on and off
* load public dashboard config on share modal

Co-authored-by: Owen Smallwood <owen.smallwood@grafana.com>
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2022-05-17 14:11:55 -08:00

57 lines
1.6 KiB
Go

package api
import (
"errors"
"fmt"
"net/http"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/web"
)
// Sets sharing configuration for dashboard
func (hs *HTTPServer) GetPublicDashboard(c *models.ReqContext) response.Response {
pdc, err := hs.dashboardService.GetPublicDashboardConfig(c.Req.Context(), c.OrgId, web.Params(c.Req)[":uid"])
if errors.Is(err, models.ErrDashboardNotFound) {
return response.Error(http.StatusNotFound, "dashboard not found", err)
}
if err != nil {
return response.Error(http.StatusInternalServerError, "error retrieving public dashboard config", err)
}
return response.JSON(http.StatusOK, pdc)
}
// Sets sharing configuration for dashboard
func (hs *HTTPServer) SavePublicDashboard(c *models.ReqContext) response.Response {
pdc := &models.PublicDashboardConfig{}
if err := web.Bind(c.Req, pdc); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
dto := dashboards.SavePublicDashboardConfigDTO{
OrgId: c.OrgId,
Uid: web.Params(c.Req)[":uid"],
PublicDashboardConfig: *pdc,
}
pdc, err := hs.dashboardService.SavePublicDashboardConfig(c.Req.Context(), &dto)
fmt.Println("err:", err)
if errors.Is(err, models.ErrDashboardNotFound) {
return response.Error(http.StatusNotFound, "dashboard not found", err)
}
if err != nil {
return response.Error(http.StatusInternalServerError, "error updating public dashboard config", err)
}
return response.JSON(http.StatusOK, pdc)
}