grafana/pkg/api/folder.go

144 lines
3.7 KiB
Go
Raw Normal View History

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"
)
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 {
return toFolderError(err)
2018-01-29 06:51:01 -06:00
}
result := make([]dtos.FolderSearchHit, 0)
2018-01-29 06:51:01 -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
}
return Json(200, result)
2018-01-29 06:51:01 -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
if err != nil {
return toFolderError(err)
2018-01-29 06:51:01 -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
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 {
return toFolderError(err)
2018-01-29 06:51:01 -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
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)
}
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 {
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)
}
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 {
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
}
var resp = map[string]interface{}{"title": f.Title}
2018-01-29 06:51:01 -06:00
return Json(200, resp)
}
func toFolderDto(g guardian.DashboardGuardian, folder *m.Folder) dtos.Folder {
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)
}
if folder.UpdatedBy > 0 {
updater = getUserLogin(folder.UpdatedBy)
}
2018-01-29 06:51:01 -06:00
return dtos.Folder{
Id: folder.Id,
Uid: folder.Uid,
2018-01-29 06:51:01 -06:00
Title: folder.Title,
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
}
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-20 08:01:20 -06:00
return ApiError(500, "Folder API error", err)
2018-01-29 06:51:01 -06:00
}