mirror of https://github.com/grafana/grafana
public dashboards: insert default public dashboard config into database on save (#49131)
This PR adds endpoints for saving and retrieving a public dashboard configuration and and api endpoint to retrieve the public dashboard. All of this is highly experimental and APIs will change. Notably, we will be removing isPublic from the dashboard model and moving it over to the public dashboard table in the next release. Further context can be found here: https://github.com/grafana/grafana/pull/49131#issuecomment-1145456952pull/50135/head
parent
efca93a3f3
commit
52ed651958
@ -0,0 +1,61 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"errors" |
||||
"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" |
||||
) |
||||
|
||||
// gets public dashboard
|
||||
func (hs *HTTPServer) GetPublicDashboard(c *models.ReqContext) response.Response { |
||||
dash, err := hs.dashboardService.GetPublicDashboard(c.Req.Context(), web.Params(c.Req)[":uid"]) |
||||
if err != nil { |
||||
return handleDashboardErr(http.StatusInternalServerError, "Failed to get public dashboard", err) |
||||
} |
||||
return response.JSON(http.StatusOK, dash) |
||||
} |
||||
|
||||
// gets public dashboard configuration for dashboard
|
||||
func (hs *HTTPServer) GetPublicDashboardConfig(c *models.ReqContext) response.Response { |
||||
pdc, err := hs.dashboardService.GetPublicDashboardConfig(c.Req.Context(), c.OrgId, web.Params(c.Req)[":uid"]) |
||||
if err != nil { |
||||
return handleDashboardErr(http.StatusInternalServerError, "Failed to get public dashboard config", err) |
||||
} |
||||
return response.JSON(http.StatusOK, pdc) |
||||
} |
||||
|
||||
// sets public dashboard configuration for dashboard
|
||||
func (hs *HTTPServer) SavePublicDashboardConfig(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, |
||||
DashboardUid: web.Params(c.Req)[":uid"], |
||||
PublicDashboardConfig: pdc, |
||||
} |
||||
|
||||
pdc, err := hs.dashboardService.SavePublicDashboardConfig(c.Req.Context(), &dto) |
||||
if err != nil { |
||||
return handleDashboardErr(http.StatusInternalServerError, "Failed to save public dashboard configuration", err) |
||||
} |
||||
|
||||
return response.JSON(http.StatusOK, pdc) |
||||
} |
||||
|
||||
// util to help us unpack a dashboard err or use default http code and message
|
||||
func handleDashboardErr(defaultCode int, defaultMsg string, err error) response.Response { |
||||
var dashboardErr models.DashboardErr |
||||
|
||||
if ok := errors.As(err, &dashboardErr); ok { |
||||
return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), dashboardErr) |
||||
} |
||||
|
||||
return response.Error(defaultCode, defaultMsg, err) |
||||
} |
||||
@ -1,53 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"errors" |
||||
"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) |
||||
|
||||
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) |
||||
} |
||||
@ -1,134 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
"net/http" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/mock" |
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/dashboards" |
||||
"github.com/grafana/grafana/pkg/services/featuremgmt" |
||||
) |
||||
|
||||
func TestApiRetrieveConfig(t *testing.T) { |
||||
pdc := &models.PublicDashboardConfig{IsPublic: true} |
||||
|
||||
testCases := []struct { |
||||
name string |
||||
dashboardUid string |
||||
expectedHttpResponse int |
||||
publicDashboardConfigResult *models.PublicDashboardConfig |
||||
publicDashboardConfigError error |
||||
}{ |
||||
{ |
||||
name: "retrieves public dashboard config when dashboard is found", |
||||
dashboardUid: "1", |
||||
expectedHttpResponse: http.StatusOK, |
||||
publicDashboardConfigResult: pdc, |
||||
publicDashboardConfigError: nil, |
||||
}, |
||||
{ |
||||
name: "returns 404 when dashboard not found", |
||||
dashboardUid: "77777", |
||||
expectedHttpResponse: http.StatusNotFound, |
||||
publicDashboardConfigResult: nil, |
||||
publicDashboardConfigError: models.ErrDashboardNotFound, |
||||
}, |
||||
{ |
||||
name: "returns 500 when internal server error", |
||||
dashboardUid: "1", |
||||
expectedHttpResponse: http.StatusInternalServerError, |
||||
publicDashboardConfigResult: nil, |
||||
publicDashboardConfigError: errors.New("database broken"), |
||||
}, |
||||
} |
||||
|
||||
for _, test := range testCases { |
||||
t.Run(test.name, func(t *testing.T) { |
||||
sc := setupHTTPServerWithMockDb(t, false, false, featuremgmt.WithFeatures(featuremgmt.FlagPublicDashboards)) |
||||
dashSvc := dashboards.NewFakeDashboardService(t) |
||||
dashSvc.On("GetPublicDashboardConfig", mock.Anything, mock.AnythingOfType("int64"), mock.AnythingOfType("string")). |
||||
Return(test.publicDashboardConfigResult, test.publicDashboardConfigError) |
||||
sc.hs.dashboardService = dashSvc |
||||
|
||||
setInitCtxSignedInViewer(sc.initCtx) |
||||
response := callAPI( |
||||
sc.server, |
||||
http.MethodGet, |
||||
"/api/dashboards/uid/1/public-config", |
||||
nil, |
||||
t, |
||||
) |
||||
|
||||
assert.Equal(t, test.expectedHttpResponse, response.Code) |
||||
|
||||
if test.expectedHttpResponse == http.StatusOK { |
||||
var pdcResp models.PublicDashboardConfig |
||||
err := json.Unmarshal(response.Body.Bytes(), &pdcResp) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, test.publicDashboardConfigResult, &pdcResp) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestApiPersistsValue(t *testing.T) { |
||||
testCases := []struct { |
||||
name string |
||||
dashboardUid string |
||||
expectedHttpResponse int |
||||
saveDashboardError error |
||||
}{ |
||||
{ |
||||
name: "returns 200 when update persists", |
||||
dashboardUid: "1", |
||||
expectedHttpResponse: http.StatusOK, |
||||
saveDashboardError: nil, |
||||
}, |
||||
{ |
||||
name: "returns 500 when not persisted", |
||||
expectedHttpResponse: http.StatusInternalServerError, |
||||
saveDashboardError: errors.New("backend failed to save"), |
||||
}, |
||||
{ |
||||
name: "returns 404 when dashboard not found", |
||||
expectedHttpResponse: http.StatusNotFound, |
||||
saveDashboardError: models.ErrDashboardNotFound, |
||||
}, |
||||
} |
||||
|
||||
for _, test := range testCases { |
||||
t.Run(test.name, func(t *testing.T) { |
||||
sc := setupHTTPServerWithMockDb(t, false, false, featuremgmt.WithFeatures(featuremgmt.FlagPublicDashboards)) |
||||
dashSvc := dashboards.NewFakeDashboardService(t) |
||||
dashSvc.On("SavePublicDashboardConfig", mock.Anything, mock.AnythingOfType("*dashboards.SavePublicDashboardConfigDTO")). |
||||
Return(&models.PublicDashboardConfig{IsPublic: true}, test.saveDashboardError) |
||||
sc.hs.dashboardService = dashSvc |
||||
|
||||
setInitCtxSignedInViewer(sc.initCtx) |
||||
response := callAPI( |
||||
sc.server, |
||||
http.MethodPost, |
||||
"/api/dashboards/uid/1/public-config", |
||||
strings.NewReader(`{ "isPublic": true }`), |
||||
t, |
||||
) |
||||
|
||||
assert.Equal(t, test.expectedHttpResponse, response.Code) |
||||
|
||||
// check the result if it's a 200
|
||||
if response.Code == http.StatusOK { |
||||
respJSON, _ := simplejson.NewJson(response.Body.Bytes()) |
||||
val, _ := respJSON.Get("isPublic").Bool() |
||||
assert.Equal(t, true, val) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
@ -0,0 +1,227 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
"fmt" |
||||
"net/http" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/mock" |
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/dashboards" |
||||
"github.com/grafana/grafana/pkg/services/featuremgmt" |
||||
) |
||||
|
||||
func TestAPIGetPublicDashboard(t *testing.T) { |
||||
t.Run("It should 404 if featureflag is not enabled", func(t *testing.T) { |
||||
sc := setupHTTPServerWithMockDb(t, false, false, featuremgmt.WithFeatures()) |
||||
dashSvc := dashboards.NewFakeDashboardService(t) |
||||
dashSvc.On("GetPublicDashboard", mock.Anything, mock.AnythingOfType("string")). |
||||
Return(&models.Dashboard{}, nil).Maybe() |
||||
sc.hs.dashboardService = dashSvc |
||||
|
||||
setInitCtxSignedInViewer(sc.initCtx) |
||||
response := callAPI( |
||||
sc.server, |
||||
http.MethodGet, |
||||
"/api/public/dashboards", |
||||
nil, |
||||
t, |
||||
) |
||||
assert.Equal(t, http.StatusNotFound, response.Code) |
||||
response = callAPI( |
||||
sc.server, |
||||
http.MethodGet, |
||||
"/api/public/dashboards/asdf", |
||||
nil, |
||||
t, |
||||
) |
||||
assert.Equal(t, http.StatusNotFound, response.Code) |
||||
}) |
||||
|
||||
testCases := []struct { |
||||
name string |
||||
uid string |
||||
expectedHttpResponse int |
||||
publicDashboardResult *models.Dashboard |
||||
publicDashboardErr error |
||||
}{ |
||||
{ |
||||
name: "It gets a public dashboard", |
||||
uid: "pubdash-abcd1234", |
||||
expectedHttpResponse: http.StatusOK, |
||||
publicDashboardResult: &models.Dashboard{ |
||||
Uid: "dashboard-abcd1234", |
||||
}, |
||||
publicDashboardErr: nil, |
||||
}, |
||||
{ |
||||
name: "It should return 404 if isPublicDashboard is false", |
||||
uid: "pubdash-abcd1234", |
||||
expectedHttpResponse: http.StatusNotFound, |
||||
publicDashboardResult: nil, |
||||
publicDashboardErr: models.ErrPublicDashboardNotFound, |
||||
}, |
||||
} |
||||
|
||||
for _, test := range testCases { |
||||
t.Run(test.name, func(t *testing.T) { |
||||
sc := setupHTTPServerWithMockDb(t, false, false, featuremgmt.WithFeatures(featuremgmt.FlagPublicDashboards)) |
||||
dashSvc := dashboards.NewFakeDashboardService(t) |
||||
dashSvc.On("GetPublicDashboard", mock.Anything, mock.AnythingOfType("string")). |
||||
Return(test.publicDashboardResult, test.publicDashboardErr) |
||||
sc.hs.dashboardService = dashSvc |
||||
|
||||
setInitCtxSignedInViewer(sc.initCtx) |
||||
response := callAPI( |
||||
sc.server, |
||||
http.MethodGet, |
||||
fmt.Sprintf("/api/public/dashboards/%v", test.uid), |
||||
nil, |
||||
t, |
||||
) |
||||
|
||||
assert.Equal(t, test.expectedHttpResponse, response.Code) |
||||
|
||||
if test.publicDashboardErr == nil { |
||||
var dashResp models.Dashboard |
||||
err := json.Unmarshal(response.Body.Bytes(), &dashResp) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, test.publicDashboardResult.Uid, dashResp.Uid) |
||||
} else { |
||||
var errResp struct { |
||||
Error string `json:"error"` |
||||
} |
||||
err := json.Unmarshal(response.Body.Bytes(), &errResp) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, test.publicDashboardErr.Error(), errResp.Error) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestAPIGetPublicDashboardConfig(t *testing.T) { |
||||
pdc := &models.PublicDashboardConfig{IsPublic: true} |
||||
|
||||
testCases := []struct { |
||||
name string |
||||
dashboardUid string |
||||
expectedHttpResponse int |
||||
publicDashboardConfigResult *models.PublicDashboardConfig |
||||
publicDashboardConfigError error |
||||
}{ |
||||
{ |
||||
name: "retrieves public dashboard config when dashboard is found", |
||||
dashboardUid: "1", |
||||
expectedHttpResponse: http.StatusOK, |
||||
publicDashboardConfigResult: pdc, |
||||
publicDashboardConfigError: nil, |
||||
}, |
||||
{ |
||||
name: "returns 404 when dashboard not found", |
||||
dashboardUid: "77777", |
||||
expectedHttpResponse: http.StatusNotFound, |
||||
publicDashboardConfigResult: nil, |
||||
publicDashboardConfigError: models.ErrDashboardNotFound, |
||||
}, |
||||
{ |
||||
name: "returns 500 when internal server error", |
||||
dashboardUid: "1", |
||||
expectedHttpResponse: http.StatusInternalServerError, |
||||
publicDashboardConfigResult: nil, |
||||
publicDashboardConfigError: errors.New("database broken"), |
||||
}, |
||||
} |
||||
|
||||
for _, test := range testCases { |
||||
t.Run(test.name, func(t *testing.T) { |
||||
sc := setupHTTPServerWithMockDb(t, false, false, featuremgmt.WithFeatures(featuremgmt.FlagPublicDashboards)) |
||||
dashSvc := dashboards.NewFakeDashboardService(t) |
||||
dashSvc.On("GetPublicDashboardConfig", mock.Anything, mock.AnythingOfType("int64"), mock.AnythingOfType("string")). |
||||
Return(test.publicDashboardConfigResult, test.publicDashboardConfigError) |
||||
sc.hs.dashboardService = dashSvc |
||||
|
||||
setInitCtxSignedInViewer(sc.initCtx) |
||||
response := callAPI( |
||||
sc.server, |
||||
http.MethodGet, |
||||
"/api/dashboards/uid/1/public-config", |
||||
nil, |
||||
t, |
||||
) |
||||
|
||||
assert.Equal(t, test.expectedHttpResponse, response.Code) |
||||
|
||||
if response.Code == http.StatusOK { |
||||
var pdcResp models.PublicDashboardConfig |
||||
err := json.Unmarshal(response.Body.Bytes(), &pdcResp) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, test.publicDashboardConfigResult, &pdcResp) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestApiSavePublicDashboardConfig(t *testing.T) { |
||||
testCases := []struct { |
||||
name string |
||||
dashboardUid string |
||||
publicDashboardConfig *models.PublicDashboardConfig |
||||
expectedHttpResponse int |
||||
saveDashboardError error |
||||
}{ |
||||
{ |
||||
name: "returns 200 when update persists", |
||||
dashboardUid: "1", |
||||
publicDashboardConfig: &models.PublicDashboardConfig{IsPublic: true}, |
||||
expectedHttpResponse: http.StatusOK, |
||||
saveDashboardError: nil, |
||||
}, |
||||
{ |
||||
name: "returns 500 when not persisted", |
||||
expectedHttpResponse: http.StatusInternalServerError, |
||||
publicDashboardConfig: &models.PublicDashboardConfig{}, |
||||
saveDashboardError: errors.New("backend failed to save"), |
||||
}, |
||||
{ |
||||
name: "returns 404 when dashboard not found", |
||||
expectedHttpResponse: http.StatusNotFound, |
||||
publicDashboardConfig: &models.PublicDashboardConfig{}, |
||||
saveDashboardError: models.ErrDashboardNotFound, |
||||
}, |
||||
} |
||||
|
||||
for _, test := range testCases { |
||||
t.Run(test.name, func(t *testing.T) { |
||||
sc := setupHTTPServerWithMockDb(t, false, false, featuremgmt.WithFeatures(featuremgmt.FlagPublicDashboards)) |
||||
|
||||
dashSvc := dashboards.NewFakeDashboardService(t) |
||||
dashSvc.On("SavePublicDashboardConfig", mock.Anything, mock.AnythingOfType("*dashboards.SavePublicDashboardConfigDTO")). |
||||
Return(&models.PublicDashboardConfig{IsPublic: true}, test.saveDashboardError) |
||||
sc.hs.dashboardService = dashSvc |
||||
|
||||
setInitCtxSignedInViewer(sc.initCtx) |
||||
response := callAPI( |
||||
sc.server, |
||||
http.MethodPost, |
||||
"/api/dashboards/uid/1/public-config", |
||||
strings.NewReader(`{ "isPublic": true }`), |
||||
t, |
||||
) |
||||
|
||||
assert.Equal(t, test.expectedHttpResponse, response.Code) |
||||
|
||||
// check the result if it's a 200
|
||||
if response.Code == http.StatusOK { |
||||
val, err := json.Marshal(test.publicDashboardConfig) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, string(val), response.Body.String()) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
@ -0,0 +1,43 @@ |
||||
package models |
||||
|
||||
var ( |
||||
ErrPublicDashboardFailedGenerateUniqueUid = DashboardErr{ |
||||
Reason: "Failed to generate unique dashboard id", |
||||
StatusCode: 500, |
||||
} |
||||
ErrPublicDashboardNotFound = DashboardErr{ |
||||
Reason: "Public dashboard not found", |
||||
StatusCode: 404, |
||||
Status: "not-found", |
||||
} |
||||
ErrPublicDashboardIdentifierNotSet = DashboardErr{ |
||||
Reason: "No Uid for public dashboard specified", |
||||
StatusCode: 400, |
||||
} |
||||
) |
||||
|
||||
type PublicDashboardConfig struct { |
||||
IsPublic bool `json:"isPublic"` |
||||
PublicDashboard PublicDashboard `json:"publicDashboard"` |
||||
} |
||||
|
||||
type PublicDashboard struct { |
||||
Uid string `json:"uid" xorm:"uid"` |
||||
DashboardUid string `json:"dashboardUid" xorm:"dashboard_uid"` |
||||
OrgId int64 `json:"orgId" xorm:"org_id"` |
||||
TimeSettings string `json:"timeSettings" xorm:"time_settings"` |
||||
} |
||||
|
||||
func (pd PublicDashboard) TableName() string { |
||||
return "dashboard_public_config" |
||||
} |
||||
|
||||
//
|
||||
// COMMANDS
|
||||
//
|
||||
|
||||
type SavePublicDashboardConfigCommand struct { |
||||
DashboardUid string |
||||
OrgId int64 |
||||
PublicDashboardConfig PublicDashboardConfig |
||||
} |
||||
@ -0,0 +1,157 @@ |
||||
package database |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||
"github.com/grafana/grafana/pkg/util" |
||||
"github.com/grafana/grafana/pkg/util/errutil" |
||||
) |
||||
|
||||
// retrieves public dashboard configuration
|
||||
func (d *DashboardStore) GetPublicDashboard(uid string) (*models.PublicDashboard, *models.Dashboard, error) { |
||||
if uid == "" { |
||||
return nil, nil, models.ErrPublicDashboardIdentifierNotSet |
||||
} |
||||
|
||||
// get public dashboard
|
||||
pdRes := &models.PublicDashboard{Uid: uid} |
||||
err := d.sqlStore.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error { |
||||
has, err := sess.Get(pdRes) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if !has { |
||||
return models.ErrPublicDashboardNotFound |
||||
} |
||||
return nil |
||||
}) |
||||
|
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
// find dashboard
|
||||
dashRes := &models.Dashboard{OrgId: pdRes.OrgId, Uid: pdRes.DashboardUid} |
||||
err = d.sqlStore.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error { |
||||
has, err := sess.Get(dashRes) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if !has { |
||||
return models.ErrPublicDashboardNotFound |
||||
} |
||||
return nil |
||||
}) |
||||
|
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
return pdRes, dashRes, err |
||||
} |
||||
|
||||
// generates a new unique uid to retrieve a public dashboard
|
||||
func generateNewPublicDashboardUid(sess *sqlstore.DBSession) (string, error) { |
||||
for i := 0; i < 3; i++ { |
||||
uid := util.GenerateShortUID() |
||||
|
||||
exists, err := sess.Get(&models.PublicDashboard{Uid: uid}) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
|
||||
if !exists { |
||||
return uid, nil |
||||
} |
||||
} |
||||
|
||||
return "", models.ErrPublicDashboardFailedGenerateUniqueUid |
||||
} |
||||
|
||||
// retrieves public dashboard configuration
|
||||
func (d *DashboardStore) GetPublicDashboardConfig(orgId int64, dashboardUid string) (*models.PublicDashboardConfig, error) { |
||||
if dashboardUid == "" { |
||||
return nil, models.ErrDashboardIdentifierNotSet |
||||
} |
||||
|
||||
// get dashboard and publicDashboard
|
||||
dashRes := &models.Dashboard{OrgId: orgId, Uid: dashboardUid} |
||||
pdRes := &models.PublicDashboard{OrgId: orgId, DashboardUid: dashboardUid} |
||||
err := d.sqlStore.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error { |
||||
// dashboard
|
||||
has, err := sess.Get(dashRes) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if !has { |
||||
return models.ErrDashboardNotFound |
||||
} |
||||
|
||||
// publicDashboard
|
||||
_, err = sess.Get(pdRes) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
}) |
||||
|
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
pdc := &models.PublicDashboardConfig{ |
||||
IsPublic: dashRes.IsPublic, |
||||
PublicDashboard: *pdRes, |
||||
} |
||||
|
||||
return pdc, err |
||||
} |
||||
|
||||
// persists public dashboard configuration
|
||||
func (d *DashboardStore) SavePublicDashboardConfig(cmd models.SavePublicDashboardConfigCommand) (*models.PublicDashboardConfig, error) { |
||||
if len(cmd.PublicDashboardConfig.PublicDashboard.DashboardUid) == 0 { |
||||
return nil, models.ErrDashboardIdentifierNotSet |
||||
} |
||||
|
||||
err := d.sqlStore.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error { |
||||
// update isPublic on dashboard entry
|
||||
affectedRowCount, err := sess.Table("dashboard").Where("org_id = ? AND uid = ?", cmd.OrgId, cmd.DashboardUid).Update(map[string]interface{}{"is_public": cmd.PublicDashboardConfig.IsPublic}) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if affectedRowCount == 0 { |
||||
return models.ErrDashboardNotFound |
||||
} |
||||
|
||||
// update dashboard_public_config
|
||||
// if we have a uid, public dashboard config exists. delete it otherwise generate a uid
|
||||
if cmd.PublicDashboardConfig.PublicDashboard.Uid != "" { |
||||
if _, err = sess.Exec("DELETE FROM dashboard_public_config WHERE uid=?", cmd.PublicDashboardConfig.PublicDashboard.Uid); err != nil { |
||||
return err |
||||
} |
||||
} else { |
||||
uid, err := generateNewPublicDashboardUid(sess) |
||||
if err != nil { |
||||
return errutil.Wrapf(err, "Failed to generate UID for public dashboard") |
||||
} |
||||
cmd.PublicDashboardConfig.PublicDashboard.Uid = uid |
||||
} |
||||
|
||||
_, err = sess.Insert(&cmd.PublicDashboardConfig.PublicDashboard) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
}) |
||||
|
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &cmd.PublicDashboardConfig, nil |
||||
} |
||||
@ -0,0 +1,233 @@ |
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
package database |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/featuremgmt" |
||||
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||
"github.com/grafana/grafana/pkg/util" |
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
// GetPublicDashboard
|
||||
func TestGetPublicDashboard(t *testing.T) { |
||||
var sqlStore *sqlstore.SQLStore |
||||
var dashboardStore *DashboardStore |
||||
var savedDashboard *models.Dashboard |
||||
|
||||
setup := func() { |
||||
sqlStore = sqlstore.InitTestDB(t) |
||||
dashboardStore = ProvideDashboardStore(sqlStore) |
||||
savedDashboard = insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true) |
||||
} |
||||
|
||||
t.Run("returns PublicDashboard and Dashboard", func(t *testing.T) { |
||||
setup() |
||||
pdc, err := dashboardStore.SavePublicDashboardConfig(models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
PublicDashboardConfig: models.PublicDashboardConfig{ |
||||
IsPublic: true, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
Uid: "abc1234", |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
}, |
||||
}, |
||||
}) |
||||
require.NoError(t, err) |
||||
|
||||
pd, d, err := dashboardStore.GetPublicDashboard("abc1234") |
||||
require.NoError(t, err) |
||||
assert.Equal(t, pd, &pdc.PublicDashboard) |
||||
assert.Equal(t, d.Uid, pdc.PublicDashboard.DashboardUid) |
||||
}) |
||||
|
||||
t.Run("returns ErrPublicDashboardNotFound with empty uid", func(t *testing.T) { |
||||
setup() |
||||
_, _, err := dashboardStore.GetPublicDashboard("") |
||||
require.Error(t, models.ErrPublicDashboardIdentifierNotSet, err) |
||||
}) |
||||
|
||||
t.Run("returns ErrPublicDashboardNotFound when PublicDashboard not found", func(t *testing.T) { |
||||
setup() |
||||
_, _, err := dashboardStore.GetPublicDashboard("zzzzzz") |
||||
require.Error(t, models.ErrPublicDashboardNotFound, err) |
||||
}) |
||||
|
||||
t.Run("returns ErrDashboardNotFound when Dashboard not found", func(t *testing.T) { |
||||
setup() |
||||
_, err := dashboardStore.SavePublicDashboardConfig(models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
PublicDashboardConfig: models.PublicDashboardConfig{ |
||||
IsPublic: true, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
Uid: "abc1234", |
||||
DashboardUid: "nevergonnafindme", |
||||
OrgId: savedDashboard.OrgId, |
||||
}, |
||||
}, |
||||
}) |
||||
require.NoError(t, err) |
||||
_, _, err = dashboardStore.GetPublicDashboard("abc1234") |
||||
require.Error(t, models.ErrDashboardNotFound, err) |
||||
}) |
||||
|
||||
} |
||||
|
||||
// GetPublicDashboardConfig
|
||||
func TestGetPublicDashboardConfig(t *testing.T) { |
||||
var sqlStore *sqlstore.SQLStore |
||||
var dashboardStore *DashboardStore |
||||
var savedDashboard *models.Dashboard |
||||
|
||||
setup := func() { |
||||
sqlStore = sqlstore.InitTestDB(t) |
||||
dashboardStore = ProvideDashboardStore(sqlStore) |
||||
savedDashboard = insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true) |
||||
} |
||||
|
||||
t.Run("returns isPublic and set dashboardUid and orgId", func(t *testing.T) { |
||||
setup() |
||||
pdc, err := dashboardStore.GetPublicDashboardConfig(savedDashboard.OrgId, savedDashboard.Uid) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, &models.PublicDashboardConfig{IsPublic: false, PublicDashboard: models.PublicDashboard{DashboardUid: savedDashboard.Uid, OrgId: savedDashboard.OrgId}}, pdc) |
||||
}) |
||||
|
||||
t.Run("returns dashboard errDashboardIdentifierNotSet", func(t *testing.T) { |
||||
setup() |
||||
_, err := dashboardStore.GetPublicDashboardConfig(savedDashboard.OrgId, "") |
||||
require.Error(t, models.ErrDashboardIdentifierNotSet, err) |
||||
}) |
||||
|
||||
t.Run("returns isPublic along with public dashboard when exists", func(t *testing.T) { |
||||
setup() |
||||
// insert test public dashboard
|
||||
resp, err := dashboardStore.SavePublicDashboardConfig(models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
PublicDashboardConfig: models.PublicDashboardConfig{ |
||||
IsPublic: true, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
Uid: "pubdash-uid", |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
TimeSettings: "{from: now, to: then}", |
||||
}, |
||||
}, |
||||
}) |
||||
require.NoError(t, err) |
||||
|
||||
pdc, err := dashboardStore.GetPublicDashboardConfig(savedDashboard.OrgId, savedDashboard.Uid) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, resp, pdc) |
||||
}) |
||||
} |
||||
|
||||
// SavePublicDashboardConfig
|
||||
func TestSavePublicDashboardConfig(t *testing.T) { |
||||
var sqlStore *sqlstore.SQLStore |
||||
var dashboardStore *DashboardStore |
||||
var savedDashboard *models.Dashboard |
||||
var savedDashboard2 *models.Dashboard |
||||
|
||||
setup := func() { |
||||
sqlStore = sqlstore.InitTestDB(t, sqlstore.InitTestDBOpt{FeatureFlags: []string{featuremgmt.FlagPublicDashboards}}) |
||||
dashboardStore = ProvideDashboardStore(sqlStore) |
||||
savedDashboard = insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true) |
||||
savedDashboard2 = insertTestDashboard(t, dashboardStore, "testDashie2", 1, 0, true) |
||||
} |
||||
|
||||
t.Run("saves new public dashboard", func(t *testing.T) { |
||||
setup() |
||||
resp, err := dashboardStore.SavePublicDashboardConfig(models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
PublicDashboardConfig: models.PublicDashboardConfig{ |
||||
IsPublic: true, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
Uid: "pubdash-uid", |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
}, |
||||
}, |
||||
}) |
||||
require.NoError(t, err) |
||||
|
||||
pdc, err := dashboardStore.GetPublicDashboardConfig(savedDashboard.OrgId, savedDashboard.Uid) |
||||
require.NoError(t, err) |
||||
|
||||
//verify saved response and queried response are the same
|
||||
assert.Equal(t, resp, pdc) |
||||
|
||||
// verify we have a valid uid
|
||||
assert.True(t, util.IsValidShortUID(pdc.PublicDashboard.Uid)) |
||||
|
||||
// verify we didn't update all dashboards
|
||||
pdc2, err := dashboardStore.GetPublicDashboardConfig(savedDashboard2.OrgId, savedDashboard2.Uid) |
||||
assert.False(t, pdc2.IsPublic) |
||||
}) |
||||
|
||||
t.Run("returns ErrDashboardIdentifierNotSet", func(t *testing.T) { |
||||
setup() |
||||
_, err := dashboardStore.SavePublicDashboardConfig(models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
PublicDashboardConfig: models.PublicDashboardConfig{ |
||||
IsPublic: true, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
DashboardUid: "", |
||||
OrgId: savedDashboard.OrgId, |
||||
}, |
||||
}, |
||||
}) |
||||
require.Error(t, models.ErrDashboardIdentifierNotSet, err) |
||||
}) |
||||
|
||||
t.Run("overwrites existing public dashboard", func(t *testing.T) { |
||||
setup() |
||||
|
||||
pdUid := util.GenerateShortUID() |
||||
|
||||
// insert initial record
|
||||
_, err := dashboardStore.SavePublicDashboardConfig(models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
PublicDashboardConfig: models.PublicDashboardConfig{ |
||||
IsPublic: true, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
Uid: pdUid, |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
}, |
||||
}, |
||||
}) |
||||
require.NoError(t, err) |
||||
|
||||
// update initial record
|
||||
resp, err := dashboardStore.SavePublicDashboardConfig(models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
PublicDashboardConfig: models.PublicDashboardConfig{ |
||||
IsPublic: false, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
Uid: pdUid, |
||||
DashboardUid: savedDashboard.Uid, |
||||
OrgId: savedDashboard.OrgId, |
||||
TimeSettings: "{}", |
||||
}, |
||||
}, |
||||
}) |
||||
require.NoError(t, err) |
||||
|
||||
pdc, err := dashboardStore.GetPublicDashboardConfig(savedDashboard.OrgId, savedDashboard.Uid) |
||||
require.NoError(t, err) |
||||
assert.Equal(t, resp, pdc) |
||||
}) |
||||
} |
||||
@ -0,0 +1,60 @@ |
||||
package service |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/dashboards" |
||||
) |
||||
|
||||
// Gets public dashboard via generated Uid
|
||||
func (dr *DashboardServiceImpl) GetPublicDashboard(ctx context.Context, dashboardUid string) (*models.Dashboard, error) { |
||||
pdc, d, err := dr.dashboardStore.GetPublicDashboard(dashboardUid) |
||||
|
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if pdc == nil || d == nil { |
||||
return nil, models.ErrPublicDashboardNotFound |
||||
} |
||||
|
||||
if !d.IsPublic { |
||||
return nil, models.ErrPublicDashboardNotFound |
||||
} |
||||
|
||||
// FIXME insert logic to substitute pdc.TimeSettings into d
|
||||
|
||||
return d, nil |
||||
} |
||||
|
||||
// GetPublicDashboardConfig is a helper method to retrieve the public dashboard configuration for a given dashboard from the database
|
||||
func (dr *DashboardServiceImpl) GetPublicDashboardConfig(ctx context.Context, orgId int64, dashboardUid string) (*models.PublicDashboardConfig, error) { |
||||
pdc, err := dr.dashboardStore.GetPublicDashboardConfig(orgId, dashboardUid) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return pdc, nil |
||||
} |
||||
|
||||
// SavePublicDashboardConfig is a helper method to persist the sharing config
|
||||
// to the database. It handles validations for sharing config and persistence
|
||||
func (dr *DashboardServiceImpl) SavePublicDashboardConfig(ctx context.Context, dto *dashboards.SavePublicDashboardConfigDTO) (*models.PublicDashboardConfig, error) { |
||||
cmd := models.SavePublicDashboardConfigCommand{ |
||||
DashboardUid: dto.DashboardUid, |
||||
OrgId: dto.OrgId, |
||||
PublicDashboardConfig: *dto.PublicDashboardConfig, |
||||
} |
||||
|
||||
// Eventually we want this to propagate to array of public dashboards
|
||||
cmd.PublicDashboardConfig.PublicDashboard.OrgId = dto.OrgId |
||||
cmd.PublicDashboardConfig.PublicDashboard.DashboardUid = dto.DashboardUid |
||||
|
||||
pdc, err := dr.dashboardStore.SavePublicDashboardConfig(cmd) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return pdc, nil |
||||
} |
||||
@ -0,0 +1,162 @@ |
||||
package service |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson" |
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/dashboards" |
||||
"github.com/grafana/grafana/pkg/services/dashboards/database" |
||||
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/mock" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestGetPublicDashboard(t *testing.T) { |
||||
type storeResp struct { |
||||
pd *models.PublicDashboard |
||||
d *models.Dashboard |
||||
err error |
||||
} |
||||
|
||||
testCases := []struct { |
||||
name string |
||||
uid string |
||||
storeResp *storeResp |
||||
errResp error |
||||
dashResp *models.Dashboard |
||||
}{ |
||||
{ |
||||
name: "returns a dashboard", |
||||
uid: "abc123", |
||||
storeResp: &storeResp{pd: &models.PublicDashboard{}, d: &models.Dashboard{IsPublic: true}, err: nil}, |
||||
errResp: nil, |
||||
dashResp: &models.Dashboard{IsPublic: true}, |
||||
}, |
||||
{ |
||||
name: "returns ErrPublicDashboardNotFound when isPublic is false", |
||||
uid: "abc123", |
||||
storeResp: &storeResp{pd: &models.PublicDashboard{}, d: &models.Dashboard{IsPublic: false}, err: nil}, |
||||
errResp: models.ErrPublicDashboardNotFound, |
||||
dashResp: nil, |
||||
}, |
||||
{ |
||||
name: "returns ErrPublicDashboardNotFound if PublicDashboard missing", |
||||
uid: "abc123", |
||||
storeResp: &storeResp{pd: nil, d: nil, err: nil}, |
||||
errResp: models.ErrPublicDashboardNotFound, |
||||
dashResp: nil, |
||||
}, |
||||
{ |
||||
name: "returns ErrPublicDashboardNotFound if Dashboard missing", |
||||
uid: "abc123", |
||||
storeResp: &storeResp{pd: nil, d: nil, err: nil}, |
||||
errResp: models.ErrPublicDashboardNotFound, |
||||
dashResp: nil, |
||||
}, |
||||
} |
||||
|
||||
for _, test := range testCases { |
||||
t.Run(test.name, func(t *testing.T) { |
||||
fakeStore := dashboards.FakeDashboardStore{} |
||||
service := &DashboardServiceImpl{ |
||||
log: log.New("test.logger"), |
||||
dashboardStore: &fakeStore, |
||||
} |
||||
fakeStore.On("GetPublicDashboard", mock.Anything). |
||||
Return(test.storeResp.pd, test.storeResp.d, test.storeResp.err) |
||||
|
||||
dashboard, err := service.GetPublicDashboard(context.Background(), test.uid) |
||||
if test.errResp != nil { |
||||
assert.Error(t, test.errResp, err) |
||||
} else { |
||||
require.NoError(t, err) |
||||
} |
||||
assert.Equal(t, test.dashResp, dashboard) |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestSavePublicDashboard(t *testing.T) { |
||||
t.Run("gets PublicDashboard.orgId and PublicDashboard.DashboardUid set from SavePublicDashboardConfigDTO", func(t *testing.T) { |
||||
sqlStore := sqlstore.InitTestDB(t) |
||||
dashboardStore := database.ProvideDashboardStore(sqlStore) |
||||
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true) |
||||
|
||||
service := &DashboardServiceImpl{ |
||||
log: log.New("test.logger"), |
||||
dashboardStore: dashboardStore, |
||||
} |
||||
|
||||
dto := &dashboards.SavePublicDashboardConfigDTO{ |
||||
DashboardUid: dashboard.Uid, |
||||
OrgId: dashboard.OrgId, |
||||
PublicDashboardConfig: &models.PublicDashboardConfig{ |
||||
IsPublic: true, |
||||
PublicDashboard: models.PublicDashboard{ |
||||
DashboardUid: "NOTTHESAME", |
||||
OrgId: 9999999, |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
pdc, err := service.SavePublicDashboardConfig(context.Background(), dto) |
||||
require.NoError(t, err) |
||||
|
||||
assert.Equal(t, dashboard.Uid, pdc.PublicDashboard.DashboardUid) |
||||
assert.Equal(t, dashboard.OrgId, pdc.PublicDashboard.OrgId) |
||||
}) |
||||
|
||||
t.Run("PLACEHOLDER - dashboard with template variables cannot be saved", func(t *testing.T) { |
||||
//sqlStore := sqlstore.InitTestDB(t)
|
||||
//dashboardStore := database.ProvideDashboardStore(sqlStore)
|
||||
//dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true)
|
||||
|
||||
//service := &DashboardServiceImpl{
|
||||
//log: log.New("test.logger"),
|
||||
//dashboardStore: dashboardStore,
|
||||
//}
|
||||
|
||||
//dto := &dashboards.SavePublicDashboardConfigDTO{
|
||||
//DashboardUid: dashboard.Uid,
|
||||
//OrgId: dashboard.OrgId,
|
||||
//PublicDashboardConfig: &models.PublicDashboardConfig{
|
||||
//IsPublic: true,
|
||||
//PublicDashboard: models.PublicDashboard{
|
||||
//DashboardUid: "NOTTHESAME",
|
||||
//OrgId: 9999999,
|
||||
//},
|
||||
//},
|
||||
//}
|
||||
|
||||
//pdc, err := service.SavePublicDashboardConfig(context.Background(), dto)
|
||||
//require.NoError(t, err)
|
||||
|
||||
//assert.Equal(t, dashboard.Uid, pdc.PublicDashboard.DashboardUid)
|
||||
//assert.Equal(t, dashboard.OrgId, pdc.PublicDashboard.OrgId)
|
||||
}) |
||||
} |
||||
|
||||
func insertTestDashboard(t *testing.T, dashboardStore *database.DashboardStore, title string, orgId int64, |
||||
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard { |
||||
t.Helper() |
||||
cmd := models.SaveDashboardCommand{ |
||||
OrgId: orgId, |
||||
FolderId: folderId, |
||||
IsFolder: isFolder, |
||||
Dashboard: simplejson.NewFromAny(map[string]interface{}{ |
||||
"id": nil, |
||||
"title": title, |
||||
"tags": tags, |
||||
}), |
||||
} |
||||
dash, err := dashboardStore.SaveDashboard(cmd) |
||||
require.NoError(t, err) |
||||
require.NotNil(t, dash) |
||||
dash.Data.Set("id", dash.Id) |
||||
dash.Data.Set("uid", dash.Uid) |
||||
return dash |
||||
} |
||||
@ -1,17 +1,16 @@ |
||||
import { DashboardModel } from 'app/features/dashboard/state'; |
||||
import { VariableModel } from 'app/features/variables/types'; |
||||
|
||||
import { dashboardCanBePublic } from './SharePublicDashboardUtils'; |
||||
import { dashboardHasTemplateVariables } from './SharePublicDashboardUtils'; |
||||
|
||||
describe('dashboardCanBePublic', () => { |
||||
it('can be public with no template variables', () => { |
||||
//@ts-ignore
|
||||
const dashboard: DashboardModel = { templating: { list: [] } }; |
||||
expect(dashboardCanBePublic(dashboard)).toBe(true); |
||||
describe('dashboardHasTemplateVariables', () => { |
||||
it('false', () => { |
||||
let variables: VariableModel[] = []; |
||||
expect(dashboardHasTemplateVariables(variables)).toBe(false); |
||||
}); |
||||
|
||||
it('cannot be public with template variables', () => { |
||||
it('true', () => { |
||||
//@ts-ignore
|
||||
const dashboard: DashboardModel = { templating: { list: [{}] } }; |
||||
expect(dashboardCanBePublic(dashboard)).toBe(false); |
||||
let variables: VariableModel[] = ['a']; |
||||
expect(dashboardHasTemplateVariables(variables)).toBe(true); |
||||
}); |
||||
}); |
||||
|
||||
@ -1,21 +1,43 @@ |
||||
import { getBackendSrv } from '@grafana/runtime'; |
||||
import { DashboardModel } from 'app/features/dashboard/state'; |
||||
import { notifyApp } from 'app/core/actions'; |
||||
import { createSuccessNotification } from 'app/core/copy/appNotification'; |
||||
import { VariableModel } from 'app/features/variables/types'; |
||||
import { dispatch } from 'app/store/store'; |
||||
import { DashboardDataDTO, DashboardMeta } from 'app/types/dashboard'; |
||||
|
||||
export interface PublicDashboardConfig { |
||||
isPublic: boolean; |
||||
publicDashboard: { |
||||
uid: string; |
||||
dashboardUid: string; |
||||
timeSettings?: object; |
||||
}; |
||||
} |
||||
export interface DashboardResponse { |
||||
dashboard: DashboardDataDTO; |
||||
meta: DashboardMeta; |
||||
} |
||||
|
||||
export const dashboardCanBePublic = (dashboard: DashboardModel): boolean => { |
||||
return dashboard?.templating?.list.length === 0; |
||||
export const dashboardHasTemplateVariables = (variables: VariableModel[]): boolean => { |
||||
return variables.length > 0; |
||||
}; |
||||
|
||||
export const getPublicDashboardConfig = async (dashboardUid: string) => { |
||||
export const getPublicDashboardConfig = async ( |
||||
dashboardUid: string, |
||||
setPublicDashboardConfig: React.Dispatch<React.SetStateAction<PublicDashboardConfig>> |
||||
) => { |
||||
const url = `/api/dashboards/uid/${dashboardUid}/public-config`; |
||||
return getBackendSrv().get(url); |
||||
const pdResp: PublicDashboardConfig = await getBackendSrv().get(url); |
||||
setPublicDashboardConfig(pdResp); |
||||
}; |
||||
|
||||
export const savePublicDashboardConfig = async (dashboardUid: string, conf: PublicDashboardConfig) => { |
||||
const payload = { isPublic: conf.isPublic }; |
||||
export const savePublicDashboardConfig = async ( |
||||
dashboardUid: string, |
||||
publicDashboardConfig: PublicDashboardConfig, |
||||
setPublicDashboardConfig: Function |
||||
) => { |
||||
const url = `/api/dashboards/uid/${dashboardUid}/public-config`; |
||||
return getBackendSrv().post(url, payload); |
||||
const pdResp: PublicDashboardConfig = await getBackendSrv().post(url, publicDashboardConfig); |
||||
dispatch(notifyApp(createSuccessNotification('Dashboard sharing configuration saved'))); |
||||
setPublicDashboardConfig(pdResp); |
||||
}; |
||||
|
||||
Loading…
Reference in new issue