Stars: Add dashboard_uid and org_id to table (#96408)

This commit is contained in:
Stephanie Hingtgen
2024-11-15 09:51:31 -06:00
committed by GitHub
parent c6a90ed3cd
commit b12c731d59
14 changed files with 340 additions and 79 deletions
+18 -12
View File
@@ -4,9 +4,11 @@ import (
"context"
"net/http"
"strconv"
"time"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/infra/log"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/star"
@@ -16,15 +18,18 @@ import (
type API struct {
starService star.Service
dashboardService dashboards.DashboardService
logger log.Logger
}
func ProvideApi(
starService star.Service,
dashboardService dashboards.DashboardService,
) *API {
starLogger := log.New("stars.api")
api := &API{
starService: starService,
dashboardService: dashboardService,
logger: starLogger,
}
return api
}
@@ -63,11 +68,11 @@ func (api *API) GetStars(c *contextmodel.ReqContext) response.Response {
uids := []string{}
if len(iuserstars.UserStars) > 0 {
var ids []int64
for id := range iuserstars.UserStars {
ids = append(ids, id)
var uids []string
for uid := range iuserstars.UserStars {
uids = append(uids, uid)
}
starredDashboards, err := api.dashboardService.GetDashboards(c.Req.Context(), &dashboards.GetDashboardsQuery{DashboardIDs: ids, OrgID: c.SignedInUser.GetOrgID()})
starredDashboards, err := api.dashboardService.GetDashboards(c.Req.Context(), &dashboards.GetDashboardsQuery{DashboardUIDs: uids, OrgID: c.SignedInUser.GetOrgID()})
if err != nil {
return response.ErrOrFallback(http.StatusInternalServerError, "Failed to fetch dashboards", err)
}
@@ -106,7 +111,10 @@ func (api *API) StarDashboard(c *contextmodel.ReqContext) response.Response {
return response.Error(http.StatusBadRequest, "Invalid dashboard ID", nil)
}
cmd := star.StarDashboardCommand{UserID: userID, DashboardID: id}
api.logger.Warn("POST /user/stars/dashboard/{dashboard_id} is deprecated, please use POST /user/stars/dashboard/uid/{dashboard_uid} instead")
cmd := star.StarDashboardCommand{UserID: userID, DashboardID: id, Updated: time.Now()}
// nolint:staticcheck
if cmd.DashboardID <= 0 {
return response.Error(http.StatusBadRequest, "Missing dashboard id", nil)
}
@@ -146,7 +154,7 @@ func (api *API) StarDashboardByUID(c *contextmodel.ReqContext) response.Response
return rsp
}
cmd := star.StarDashboardCommand{UserID: userID, DashboardID: dash.ID}
cmd := star.StarDashboardCommand{UserID: userID, DashboardID: dash.ID, DashboardUID: uid, OrgID: c.SignedInUser.GetOrgID(), Updated: time.Now()}
if err := api.starService.Add(c.Req.Context(), &cmd); err != nil {
return response.Error(http.StatusInternalServerError, "Failed to star dashboard", err)
@@ -182,7 +190,10 @@ func (api *API) UnstarDashboard(c *contextmodel.ReqContext) response.Response {
return response.Error(http.StatusBadRequest, "Only users and service accounts can star dashboards", nil)
}
api.logger.Warn("DELETE /user/stars/dashboard/{dashboard_id} is deprecated, please use DELETE /user/stars/dashboard/uid/{dashboard_uid} instead")
cmd := star.UnstarDashboardCommand{UserID: userID, DashboardID: id}
// nolint:staticcheck
if cmd.DashboardID <= 0 {
return response.Error(http.StatusBadRequest, "Missing dashboard id", nil)
}
@@ -217,12 +228,7 @@ func (api *API) UnstarDashboardByUID(c *contextmodel.ReqContext) response.Respon
return response.Error(http.StatusBadRequest, "Only users and service accounts can star dashboards", nil)
}
dash, rsp := api.getDashboardHelper(c.Req.Context(), c.SignedInUser.GetOrgID(), 0, uid)
if rsp != nil {
return rsp
}
cmd := star.UnstarDashboardCommand{UserID: userID, DashboardID: dash.ID}
cmd := star.UnstarDashboardCommand{UserID: userID, DashboardUID: uid, OrgID: c.SignedInUser.GetOrgID()}
if err := api.starService.Delete(c.Req.Context(), &cmd); err != nil {
return response.Error(http.StatusInternalServerError, "Failed to unstar dashboard", err)
+74 -1
View File
@@ -5,6 +5,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/dashboards"
@@ -13,7 +14,7 @@ import (
"github.com/grafana/grafana/pkg/web"
)
func TestStarDashboard(t *testing.T) {
func TestStarDashboardID(t *testing.T) {
api := ProvideApi(startest.NewStarServiceFake(), dashboards.NewFakeDashboardService(t))
testCases := []struct {
@@ -82,3 +83,75 @@ func TestStarDashboard(t *testing.T) {
})
}
}
func TestStarDashboardUID(t *testing.T) {
svc := dashboards.NewFakeDashboardService(t)
svc.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{UID: "test", OrgID: 1}, nil)
api := ProvideApi(startest.NewStarServiceFake(), svc)
testCases := []struct {
name string
signedInUser *user.SignedInUser
expectedStatus int
params map[string]string
}{
{
name: "Star dashboard with user",
params: map[string]string{
":uid": "test",
},
signedInUser: &user.SignedInUser{
UserID: 1,
OrgID: 1,
IsAnonymous: false,
},
expectedStatus: 200,
},
{
name: "Star dashboard with anonymous user",
params: map[string]string{
":uid": "test",
},
signedInUser: &user.SignedInUser{
UserID: 0,
OrgID: 1,
IsAnonymous: true,
},
expectedStatus: 400,
},
{
name: "Star dashboard with API Key",
params: map[string]string{
":uid": "test",
},
signedInUser: &user.SignedInUser{
UserID: 0,
OrgID: 1,
ApiKeyID: 3,
IsAnonymous: false,
},
expectedStatus: 400,
},
{
name: "Star dashboard with Service Account",
params: map[string]string{
":uid": "test",
},
signedInUser: &user.SignedInUser{
UserID: 1,
OrgID: 3,
IsServiceAccount: true,
},
expectedStatus: 200,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := web.SetURLParams(&http.Request{}, tc.params)
c := &contextmodel.ReqContext{SignedInUser: tc.signedInUser, Context: &web.Context{Req: req}}
resp := api.StarDashboardByUID(c)
assert.Equal(t, tc.expectedStatus, resp.Status())
})
}
}