Stars: support running stars in mode 5 (#111754)
This commit is contained in:
@@ -1,56 +1,53 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
starService star.Service
|
||||
dashboardService dashboards.DashboardService
|
||||
logger log.Logger
|
||||
starService star.Service
|
||||
client K8sClients
|
||||
}
|
||||
|
||||
func ProvideApi(
|
||||
cfg *setting.Cfg, // for namespacer
|
||||
features featuremgmt.FeatureToggles,
|
||||
starService star.Service,
|
||||
dashboardService dashboards.DashboardService,
|
||||
configProvider apiserver.DirectRestConfigProvider,
|
||||
) *API {
|
||||
starLogger := log.New("stars.api")
|
||||
api := &API{
|
||||
starService: starService,
|
||||
dashboardService: dashboardService,
|
||||
logger: starLogger,
|
||||
if features.IsEnabledGlobally(featuremgmt.FlagKubernetesStars) {
|
||||
starService = nil // don't use it
|
||||
}
|
||||
return api
|
||||
}
|
||||
|
||||
func (api *API) getDashboardHelper(ctx context.Context, orgID int64, id int64, uid string) (*dashboards.Dashboard, response.Response) {
|
||||
var query dashboards.GetDashboardQuery
|
||||
|
||||
if len(uid) > 0 {
|
||||
query = dashboards.GetDashboardQuery{UID: uid, ID: id, OrgID: orgID}
|
||||
} else {
|
||||
query = dashboards.GetDashboardQuery{ID: id, OrgID: orgID}
|
||||
return &API{
|
||||
starService: starService,
|
||||
client: &k8sClients{
|
||||
namespacer: request.GetNamespaceMapper(cfg),
|
||||
configProvider: configProvider,
|
||||
},
|
||||
}
|
||||
|
||||
result, err := api.dashboardService.GetDashboard(ctx, &query)
|
||||
if err != nil {
|
||||
return nil, response.Error(http.StatusNotFound, "Dashboard not found", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (api *API) GetStars(c *contextmodel.ReqContext) response.Response {
|
||||
if api.starService == nil {
|
||||
stars, err := api.client.GetStars(c)
|
||||
if err != nil {
|
||||
logging.FromContext(c.Req.Context()).With("logger", "star.api").Warn("error", "err", err)
|
||||
}
|
||||
return response.JSON(http.StatusOK, stars)
|
||||
}
|
||||
|
||||
query := star.GetUserStarsQuery{
|
||||
UserID: c.UserID,
|
||||
}
|
||||
@@ -86,17 +83,25 @@ func (api *API) StarDashboardByUID(c *contextmodel.ReqContext) response.Response
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard UID", nil)
|
||||
}
|
||||
|
||||
if api.starService == nil {
|
||||
err := api.client.AddStar(c, uid)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to star dashboard", err)
|
||||
}
|
||||
return response.Success("Dashboard starred!")
|
||||
}
|
||||
|
||||
userID, err := identity.UserIdentifier(c.GetID())
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Only users and service accounts can star dashboards", nil)
|
||||
}
|
||||
|
||||
dash, rsp := api.getDashboardHelper(c.Req.Context(), c.GetOrgID(), 0, uid)
|
||||
dashboardID, rsp := api.client.GetDashboardID(c, uid)
|
||||
if rsp != nil {
|
||||
return rsp
|
||||
}
|
||||
|
||||
cmd := star.StarDashboardCommand{UserID: userID, DashboardID: dash.ID, DashboardUID: uid, OrgID: c.GetOrgID(), Updated: time.Now()}
|
||||
cmd := star.StarDashboardCommand{UserID: userID, DashboardID: dashboardID, DashboardUID: uid, OrgID: c.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)
|
||||
@@ -123,6 +128,14 @@ func (api *API) UnstarDashboardByUID(c *contextmodel.ReqContext) response.Respon
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard UID", nil)
|
||||
}
|
||||
|
||||
if api.starService == nil {
|
||||
err := api.client.RemoveStar(c, uid)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to unstar dashboard", err)
|
||||
}
|
||||
return response.Success("Dashboard unstarred")
|
||||
}
|
||||
|
||||
userID, err := identity.UserIdentifier(c.GetID())
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Only users and service accounts can star dashboards", nil)
|
||||
|
||||
Reference in New Issue
Block a user