2015-12-22 04:07:15 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2023-10-31 12:26:39 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/client-go/dynamic"
|
2021-11-19 07:32:14 -06:00
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2023-10-31 12:26:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2023-12-11 14:03:48 -06:00
|
|
|
"github.com/grafana/grafana/pkg/apis/playlist/v0alpha1"
|
2023-10-31 12:26:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2023-11-03 10:07:55 -05:00
|
|
|
internalplaylist "github.com/grafana/grafana/pkg/registry/apis/playlist"
|
2024-02-01 16:27:30 -06:00
|
|
|
grafanaapiserver "github.com/grafana/grafana/pkg/services/apiserver"
|
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2023-10-31 12:26:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-07-18 04:26:35 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/playlist"
|
2023-10-31 12:26:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil/errhttp"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-12-22 04:07:15 -06:00
|
|
|
)
|
|
|
|
|
2023-10-31 12:26:39 -05:00
|
|
|
func (hs *HTTPServer) registerPlaylistAPI(apiRoute routing.RouteRegister) {
|
|
|
|
// Register the actual handlers
|
|
|
|
apiRoute.Group("/playlists", func(playlistRoute routing.RouteRegister) {
|
2023-11-14 14:50:27 -06:00
|
|
|
if hs.Features.IsEnabledGlobally(featuremgmt.FlagKubernetesPlaylists) {
|
2023-11-03 11:25:29 -05:00
|
|
|
// Use k8s client to implement legacy API
|
|
|
|
handler := newPlaylistK8sHandler(hs)
|
|
|
|
playlistRoute.Get("/", handler.searchPlaylists)
|
|
|
|
playlistRoute.Get("/:uid", handler.getPlaylist)
|
|
|
|
playlistRoute.Get("/:uid/items", handler.getPlaylistItems)
|
|
|
|
playlistRoute.Delete("/:uid", handler.deletePlaylist)
|
|
|
|
playlistRoute.Put("/:uid", handler.updatePlaylist)
|
|
|
|
playlistRoute.Post("/", handler.createPlaylist)
|
|
|
|
} else {
|
|
|
|
// Legacy handlers
|
|
|
|
playlistRoute.Get("/", routing.Wrap(hs.SearchPlaylists))
|
|
|
|
playlistRoute.Get("/:uid", hs.validateOrgPlaylist, routing.Wrap(hs.GetPlaylist))
|
|
|
|
playlistRoute.Get("/:uid/items", hs.validateOrgPlaylist, routing.Wrap(hs.GetPlaylistItems))
|
|
|
|
playlistRoute.Delete("/:uid", middleware.ReqEditorRole, hs.validateOrgPlaylist, routing.Wrap(hs.DeletePlaylist))
|
|
|
|
playlistRoute.Put("/:uid", middleware.ReqEditorRole, hs.validateOrgPlaylist, routing.Wrap(hs.UpdatePlaylist))
|
|
|
|
playlistRoute.Post("/", middleware.ReqEditorRole, routing.Wrap(hs.CreatePlaylist))
|
|
|
|
}
|
2023-10-31 12:26:39 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) validateOrgPlaylist(c *contextmodel.ReqContext) {
|
2022-06-14 14:32:52 -05:00
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2023-10-06 04:34:36 -05:00
|
|
|
query := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.SignedInUser.GetOrgID()}
|
2022-10-04 10:11:18 -05:00
|
|
|
p, err := hs.playlistService.GetWithoutItems(c.Req.Context(), &query)
|
2015-12-22 04:07:15 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(404, "Playlist not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-18 04:26:35 -05:00
|
|
|
if p.OrgId == 0 {
|
2016-03-01 06:50:55 -06:00
|
|
|
c.JsonApiErr(404, "Playlist not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-06 04:34:36 -05:00
|
|
|
if p.OrgId != c.SignedInUser.GetOrgID() {
|
2015-12-22 04:07:15 -06:00
|
|
|
c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /playlists playlists searchPlaylists
|
|
|
|
//
|
|
|
|
// Get playlists.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: searchPlaylistsResponse
|
|
|
|
// 500: internalServerError
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) SearchPlaylists(c *contextmodel.ReqContext) response.Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
query := c.Query("query")
|
|
|
|
limit := c.QueryInt("limit")
|
|
|
|
|
|
|
|
if limit == 0 {
|
|
|
|
limit = 1000
|
|
|
|
}
|
|
|
|
|
2022-07-18 04:26:35 -05:00
|
|
|
searchQuery := playlist.GetPlaylistsQuery{
|
2016-01-18 09:00:11 -06:00
|
|
|
Name: query,
|
2015-12-22 04:07:15 -06:00
|
|
|
Limit: limit,
|
2023-10-06 04:34:36 -05:00
|
|
|
OrgId: c.SignedInUser.GetOrgID(),
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 04:26:35 -05:00
|
|
|
playlists, err := hs.playlistService.Search(c.Req.Context(), &searchQuery)
|
2015-12-22 04:07:15 -06:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Search failed", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 04:26:35 -05:00
|
|
|
return response.JSON(http.StatusOK, playlists)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /playlists/{uid} playlists getPlaylist
|
|
|
|
//
|
|
|
|
// Get playlist.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getPlaylistResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) GetPlaylist(c *contextmodel.ReqContext) response.Response {
|
2022-06-14 14:32:52 -05:00
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2023-10-06 04:34:36 -05:00
|
|
|
cmd := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.SignedInUser.GetOrgID()}
|
2015-12-22 04:07:15 -06:00
|
|
|
|
2022-10-04 10:11:18 -05:00
|
|
|
dto, err := hs.playlistService.Get(c.Req.Context(), &cmd)
|
2022-07-18 04:26:35 -05:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Playlist not found", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, dto)
|
2016-01-12 01:36:25 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /playlists/{uid}/items playlists getPlaylistItems
|
|
|
|
//
|
|
|
|
// Get playlist items.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getPlaylistItemsResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) GetPlaylistItems(c *contextmodel.ReqContext) response.Response {
|
2022-06-14 14:32:52 -05:00
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2023-10-06 04:34:36 -05:00
|
|
|
cmd := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.SignedInUser.GetOrgID()}
|
2016-01-08 10:32:55 -06:00
|
|
|
|
2022-10-04 10:11:18 -05:00
|
|
|
dto, err := hs.playlistService.Get(c.Req.Context(), &cmd)
|
2016-01-08 10:32:55 -06:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Playlist not found", err)
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:11:18 -05:00
|
|
|
return response.JSON(http.StatusOK, dto.Items)
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route DELETE /playlists/{uid} playlists deletePlaylist
|
|
|
|
//
|
|
|
|
// Delete playlist.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) DeletePlaylist(c *contextmodel.ReqContext) response.Response {
|
2022-06-14 14:32:52 -05:00
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2015-12-22 04:07:15 -06:00
|
|
|
|
2023-10-06 04:34:36 -05:00
|
|
|
cmd := playlist.DeletePlaylistCommand{UID: uid, OrgId: c.SignedInUser.GetOrgID()}
|
2022-07-18 04:26:35 -05:00
|
|
|
if err := hs.playlistService.Delete(c.Req.Context(), &cmd); err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to delete playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, "")
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route POST /playlists playlists createPlaylist
|
|
|
|
//
|
|
|
|
// Create playlist.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: createPlaylistResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) CreatePlaylist(c *contextmodel.ReqContext) response.Response {
|
2022-07-18 04:26:35 -05:00
|
|
|
cmd := playlist.CreatePlaylistCommand{}
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2023-10-06 04:34:36 -05:00
|
|
|
cmd.OrgId = c.SignedInUser.GetOrgID()
|
2016-01-18 09:00:11 -06:00
|
|
|
|
2022-07-18 04:26:35 -05:00
|
|
|
p, err := hs.playlistService.Create(c.Req.Context(), &cmd)
|
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to create playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 04:26:35 -05:00
|
|
|
return response.JSON(http.StatusOK, p)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route PUT /playlists/{uid} playlists updatePlaylist
|
|
|
|
//
|
|
|
|
// Update playlist.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: updatePlaylistResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) UpdatePlaylist(c *contextmodel.ReqContext) response.Response {
|
2022-07-18 04:26:35 -05:00
|
|
|
cmd := playlist.UpdatePlaylistCommand{}
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2023-10-06 04:34:36 -05:00
|
|
|
cmd.OrgId = c.SignedInUser.GetOrgID()
|
2022-06-14 14:32:52 -05:00
|
|
|
cmd.UID = web.Params(c.Req)[":uid"]
|
2016-01-18 09:05:38 -06:00
|
|
|
|
2022-10-04 10:11:18 -05:00
|
|
|
_, err := hs.playlistService.Update(c.Req.Context(), &cmd)
|
2022-07-18 04:26:35 -05:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to save playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:11:18 -05:00
|
|
|
dto, err := hs.playlistService.Get(c.Req.Context(), &playlist.GetPlaylistByUidQuery{
|
|
|
|
UID: cmd.UID,
|
2023-10-06 04:34:36 -05:00
|
|
|
OrgId: c.SignedInUser.GetOrgID(),
|
2022-10-04 10:11:18 -05:00
|
|
|
})
|
2016-01-08 10:32:55 -06:00
|
|
|
if err != nil {
|
2024-02-27 10:39:51 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to load playlist", err)
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
2022-10-04 10:11:18 -05:00
|
|
|
return response.JSON(http.StatusOK, dto)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
// swagger:parameters searchPlaylists
|
|
|
|
type SearchPlaylistsParams struct {
|
|
|
|
// in:query
|
|
|
|
// required:false
|
|
|
|
Query string `json:"query"`
|
|
|
|
// in:limit
|
|
|
|
// required:false
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters getPlaylist
|
|
|
|
type GetPlaylistParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
UID string `json:"uid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters getPlaylistItems
|
|
|
|
type GetPlaylistItemsParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
UID string `json:"uid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters getPlaylistDashboards
|
|
|
|
type GetPlaylistDashboardsParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
UID string `json:"uid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters deletePlaylist
|
|
|
|
type DeletePlaylistParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
UID string `json:"uid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters updatePlaylist
|
|
|
|
type UpdatePlaylistParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body playlist.UpdatePlaylistCommand
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
UID string `json:"uid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters createPlaylist
|
|
|
|
type CreatePlaylistParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body playlist.CreatePlaylistCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response searchPlaylistsResponse
|
|
|
|
type SearchPlaylistsResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body playlist.Playlists `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response getPlaylistResponse
|
|
|
|
type GetPlaylistResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body *playlist.PlaylistDTO `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response getPlaylistItemsResponse
|
|
|
|
type GetPlaylistItemsResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body []playlist.PlaylistItemDTO `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response getPlaylistDashboardsResponse
|
|
|
|
type GetPlaylistDashboardsResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body dtos.PlaylistDashboardsSlice `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response updatePlaylistResponse
|
|
|
|
type UpdatePlaylistResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body *playlist.PlaylistDTO `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response createPlaylistResponse
|
|
|
|
type CreatePlaylistResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body *playlist.Playlist `json:"body"`
|
|
|
|
}
|
2023-11-03 11:25:29 -05:00
|
|
|
|
|
|
|
type playlistK8sHandler struct {
|
|
|
|
namespacer request.NamespaceMapper
|
|
|
|
gvr schema.GroupVersionResource
|
|
|
|
clientConfigProvider grafanaapiserver.DirectRestConfigProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------------
|
|
|
|
// Playlist k8s wrapper functions
|
|
|
|
//-----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func newPlaylistK8sHandler(hs *HTTPServer) *playlistK8sHandler {
|
|
|
|
return &playlistK8sHandler{
|
2023-12-11 14:03:48 -06:00
|
|
|
gvr: v0alpha1.PlaylistResourceInfo.GroupVersionResource(),
|
2023-11-03 11:25:29 -05:00
|
|
|
namespacer: request.GetNamespaceMapper(hs.Cfg),
|
|
|
|
clientConfigProvider: hs.clientConfigProvider,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) searchPlaylists(c *contextmodel.ReqContext) {
|
|
|
|
client, ok := pk8s.getClient(c)
|
|
|
|
if !ok {
|
|
|
|
return // error is already sent
|
|
|
|
}
|
|
|
|
out, err := client.List(c.Req.Context(), v1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
pk8s.writeError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query := strings.ToUpper(c.Query("query"))
|
|
|
|
playlists := []playlist.Playlist{}
|
|
|
|
for _, item := range out.Items {
|
|
|
|
p := internalplaylist.UnstructuredToLegacyPlaylist(item)
|
|
|
|
if p == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if query != "" && !strings.Contains(strings.ToUpper(p.Name), query) {
|
|
|
|
continue // query filter
|
|
|
|
}
|
|
|
|
playlists = append(playlists, *p)
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, playlists)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) getPlaylist(c *contextmodel.ReqContext) {
|
|
|
|
client, ok := pk8s.getClient(c)
|
|
|
|
if !ok {
|
|
|
|
return // error is already sent
|
|
|
|
}
|
|
|
|
uid := web.Params(c.Req)[":uid"]
|
|
|
|
out, err := client.Get(c.Req.Context(), uid, v1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
pk8s.writeError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, internalplaylist.UnstructuredToLegacyPlaylistDTO(*out))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) getPlaylistItems(c *contextmodel.ReqContext) {
|
|
|
|
client, ok := pk8s.getClient(c)
|
|
|
|
if !ok {
|
|
|
|
return // error is already sent
|
|
|
|
}
|
|
|
|
uid := web.Params(c.Req)[":uid"]
|
|
|
|
out, err := client.Get(c.Req.Context(), uid, v1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
pk8s.writeError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, internalplaylist.UnstructuredToLegacyPlaylistDTO(*out).Items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) deletePlaylist(c *contextmodel.ReqContext) {
|
|
|
|
client, ok := pk8s.getClient(c)
|
|
|
|
if !ok {
|
|
|
|
return // error is already sent
|
|
|
|
}
|
|
|
|
uid := web.Params(c.Req)[":uid"]
|
|
|
|
err := client.Delete(c.Req.Context(), uid, v1.DeleteOptions{})
|
|
|
|
if err != nil {
|
|
|
|
pk8s.writeError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) updatePlaylist(c *contextmodel.ReqContext) {
|
|
|
|
client, ok := pk8s.getClient(c)
|
|
|
|
if !ok {
|
|
|
|
return // error is already sent
|
|
|
|
}
|
|
|
|
uid := web.Params(c.Req)[":uid"]
|
|
|
|
cmd := playlist.UpdatePlaylistCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
c.JsonApiErr(http.StatusBadRequest, "bad request data", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj := internalplaylist.LegacyUpdateCommandToUnstructured(cmd)
|
|
|
|
obj.SetName(uid)
|
|
|
|
out, err := client.Update(c.Req.Context(), &obj, v1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
pk8s.writeError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, internalplaylist.UnstructuredToLegacyPlaylistDTO(*out))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) createPlaylist(c *contextmodel.ReqContext) {
|
|
|
|
client, ok := pk8s.getClient(c)
|
|
|
|
if !ok {
|
|
|
|
return // error is already sent
|
|
|
|
}
|
|
|
|
cmd := playlist.UpdatePlaylistCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
c.JsonApiErr(http.StatusBadRequest, "bad request data", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj := internalplaylist.LegacyUpdateCommandToUnstructured(cmd)
|
|
|
|
out, err := client.Create(c.Req.Context(), &obj, v1.CreateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
pk8s.writeError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, internalplaylist.UnstructuredToLegacyPlaylistDTO(*out))
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------------
|
|
|
|
// Utility functions
|
|
|
|
//-----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) getClient(c *contextmodel.ReqContext) (dynamic.ResourceInterface, bool) {
|
|
|
|
dyn, err := dynamic.NewForConfig(pk8s.clientConfigProvider.GetDirectRestConfig(c))
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(500, "client", err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return dyn.Resource(pk8s.gvr).Namespace(pk8s.namespacer(c.OrgID)), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pk8s *playlistK8sHandler) writeError(c *contextmodel.ReqContext, err error) {
|
|
|
|
//nolint:errorlint
|
|
|
|
statusError, ok := err.(*errors.StatusError)
|
|
|
|
if ok {
|
|
|
|
c.JsonApiErr(int(statusError.Status().Code), statusError.Status().Message, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errhttp.Write(c.Req.Context(), err, c.Resp)
|
|
|
|
}
|