2018-01-29 06:51:01 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-02-21 09:38:09 -06:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-29 06:51:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
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-03-07 10:54:50 -06:00
|
|
|
func GetFolders(c *m.ReqContext) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
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-03-22 16:13:46 -05:00
|
|
|
return JSON(200, result)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
func GetFolderByUID(c *m.ReqContext) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
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)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, folder))
|
2018-02-20 06:57:32 -06:00
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
func GetFolderByID(c *m.ReqContext) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
s := dashboards.NewFolderService(c.OrgId, c.SignedInUser)
|
2018-03-22 16:13:46 -05:00
|
|
|
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)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, folder))
|
2018-02-20 06:57:32 -06:00
|
|
|
}
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func CreateFolder(c *m.ReqContext, cmd m.CreateFolderCommand) Response {
|
2018-02-20 06:57:32 -06:00
|
|
|
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)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, cmd.Result))
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func UpdateFolder(c *m.ReqContext, 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)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, toFolderDto(g, cmd.Result))
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func DeleteFolder(c *m.ReqContext) 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-03-22 16:13:46 -05:00
|
|
|
return JSON(200, util.DynMap{
|
2018-02-21 09:38:09 -06:00
|
|
|
"title": f.Title,
|
|
|
|
"message": fmt.Sprintf("Folder %s deleted", f.Title),
|
|
|
|
})
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
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
|
2018-09-22 03:50:00 -05:00
|
|
|
updater, creator := anonString, anonString
|
2018-01-29 06:51:01 -06:00
|
|
|
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 {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, err.Error(), nil)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 06:57:32 -06:00
|
|
|
if err == m.ErrFolderAccessDenied {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(403, "Access denied", err)
|
2018-02-20 06:57:32 -06:00
|
|
|
}
|
|
|
|
|
2018-02-20 08:01:20 -06:00
|
|
|
if err == m.ErrFolderNotFound {
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(404, util.DynMap{"status": "not-found", "message": m.ErrFolderNotFound.Error()})
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|
|
|
|
|
2018-02-21 04:24:54 -06:00
|
|
|
if err == m.ErrFolderVersionMismatch {
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(412, util.DynMap{"status": "version-mismatch", "message": m.ErrFolderVersionMismatch.Error()})
|
2018-02-01 11:29:00 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Folder API error", err)
|
2018-01-29 06:51:01 -06:00
|
|
|
}
|