2018-01-29 06:51:01 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
func GetFolders(c *middleware.Context) Response {
|
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
folders, err := s.GetFolders(c.QueryInt("limit"))
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2018-02-20 06:57:32 -06:00
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
result := make([]dtos.FolderSearchHit, 0)
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
for _, f := range folders {
|
|
|
|
result = append(result, dtos.FolderSearchHit{
|
|
|
|
Id: f.Id,
|
|
|
|
Uid: f.Uid,
|
|
|
|
Title: f.Title,
|
|
|
|
})
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
return Json(200, result)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
func GetFolderByUid(c *middleware.Context) Response {
|
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
folder, err := s.GetFolderByUid(c.Params(":uid"))
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(folder.Id, c.OrgId, c.SignedInUser)
|
|
|
|
return Json(200, toFolderDto(g, folder))
|
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
func GetFolderById(c *middleware.Context) Response {
|
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
folder, err := s.GetFolderById(c.ParamsInt64(":id"))
|
2018-01-29 06:51:01 -06:00
|
|
|
if err != nil {
|
2018-02-20 06:57:32 -06:00
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(folder.Id, c.OrgId, c.SignedInUser)
|
|
|
|
return Json(200, toFolderDto(g, folder))
|
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
func CreateFolder(c *middleware.Context, cmd m.CreateFolderCommand) Response {
|
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
err := s.CreateFolder(&cmd)
|
2018-01-29 06:51:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(cmd.Result.Id, c.OrgId, c.SignedInUser)
|
|
|
|
return Json(200, toFolderDto(g, cmd.Result))
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateFolder(c *middleware.Context, cmd m.UpdateFolderCommand) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
err := s.UpdateFolder(c.Params(":uid"), &cmd)
|
2018-01-29 06:51:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
g := guardian.New(cmd.Result.Id, c.OrgId, c.SignedInUser)
|
|
|
|
return Json(200, toFolderDto(g, cmd.Result))
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteFolder(c *middleware.Context) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
|
|
|
f, err := s.DeleteFolder(c.Params(":uid"))
|
|
|
|
if err != nil {
|
|
|
|
return toFolderError(err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
var resp = map[string]interface{}{"title": f.Title}
|
2018-01-29 06:51:01 -06:00
|
|
|
return Json(200, resp)
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
func toFolderDto(g guardian.DashboardGuardian, folder *m.Folder) dtos.Folder {
|
2018-02-19 06:32:45 -06:00
|
|
|
canEdit, _ := g.CanEdit()
|
|
|
|
canSave, _ := g.CanSave()
|
|
|
|
canAdmin, _ := g.CanAdmin()
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
// Finding creator and last updater of the folder
|
|
|
|
updater, creator := "Anonymous", "Anonymous"
|
|
|
|
if folder.CreatedBy > 0 {
|
|
|
|
creator = getUserLogin(folder.CreatedBy)
|
|
|
|
}
|
2018-02-01 14:00:37 -06:00
|
|
|
if folder.UpdatedBy > 0 {
|
|
|
|
updater = getUserLogin(folder.UpdatedBy)
|
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
|
|
|
return dtos.Folder{
|
|
|
|
Id: folder.Id,
|
2018-02-01 14:00:37 -06:00
|
|
|
Uid: folder.Uid,
|
2018-01-29 06:51:01 -06:00
|
|
|
Title: folder.Title,
|
2018-02-20 06:57:32 -06:00
|
|
|
Url: folder.Url,
|
2018-01-29 06:51:01 -06:00
|
|
|
HasAcl: folder.HasAcl,
|
|
|
|
CanSave: canSave,
|
|
|
|
CanEdit: canEdit,
|
|
|
|
CanAdmin: canAdmin,
|
|
|
|
CreatedBy: creator,
|
|
|
|
Created: folder.Created,
|
|
|
|
UpdatedBy: updater,
|
|
|
|
Updated: folder.Updated,
|
|
|
|
Version: folder.Version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toFolderError(err error) Response {
|
2018-02-21 04:24:54 -06:00
|
|
|
if err == m.ErrFolderTitleEmpty ||
|
|
|
|
err == m.ErrFolderSameNameExists ||
|
|
|
|
err == m.ErrFolderWithSameUIDExists ||
|
|
|
|
err == m.ErrDashboardTypeMismatch ||
|
|
|
|
err == m.ErrDashboardInvalidUid ||
|
|
|
|
err == m.ErrDashboardUidToLong {
|
|
|
|
return ApiError(400, err.Error(), nil)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
if err == m.ErrFolderAccessDenied {
|
|
|
|
return ApiError(403, "Access denied", err)
|
|
|
|
}
|
|
|
|
|
2018-02-20 08:01:20 -06:00
|
|
|
if err == m.ErrFolderNotFound {
|
2018-01-29 06:51:01 -06:00
|
|
|
return Json(404, util.DynMap{"status": "not-found", "message": m.ErrFolderNotFound.Error()})
|
|
|
|
}
|
|
|
|
|
2018-02-21 04:24:54 -06:00
|
|
|
if err == m.ErrFolderVersionMismatch {
|
|
|
|
return Json(412, util.DynMap{"status": "version-mismatch", "message": m.ErrFolderVersionMismatch.Error()})
|
2018-02-01 11:29:00 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 08:01:20 -06:00
|
|
|
return ApiError(500, "Folder API error", err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|