2022-10-26 09:15:14 -05:00
|
|
|
package folder
|
|
|
|
|
|
|
|
import (
|
2023-05-10 08:20:16 -05:00
|
|
|
"fmt"
|
2022-10-26 09:15:14 -05:00
|
|
|
"time"
|
2022-10-28 08:35:49 -05:00
|
|
|
|
2024-01-25 04:10:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2023-05-10 08:20:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/slugify"
|
2023-08-30 09:51:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity"
|
2023-05-10 08:20:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2022-10-28 08:35:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2022-10-26 09:15:14 -05:00
|
|
|
)
|
|
|
|
|
2023-08-22 05:52:24 -05:00
|
|
|
var ErrMaximumDepthReached = errutil.BadRequest("folder.maximum-depth-reached", errutil.WithPublicMessage("Maximum nested folder depth reached"))
|
|
|
|
var ErrBadRequest = errutil.BadRequest("folder.bad-request")
|
|
|
|
var ErrDatabaseError = errutil.Internal("folder.database-error")
|
2024-01-25 03:29:56 -06:00
|
|
|
var ErrConflict = errutil.Conflict("folder.conflict")
|
2023-08-22 05:52:24 -05:00
|
|
|
var ErrInternal = errutil.Internal("folder.internal")
|
|
|
|
var ErrCircularReference = errutil.BadRequest("folder.circular-reference", errutil.WithPublicMessage("Circular reference detected"))
|
|
|
|
var ErrTargetRegistrySrvConflict = errutil.Internal("folder.target-registry-srv-conflict")
|
2023-12-04 03:34:38 -06:00
|
|
|
var ErrFolderNotEmpty = errutil.BadRequest("folder.not-empty", errutil.WithPublicMessage("Folder cannot be deleted: folder is not empty"))
|
2022-11-03 08:21:41 -05:00
|
|
|
|
2022-10-26 09:15:14 -05:00
|
|
|
const (
|
2023-12-05 09:13:31 -06:00
|
|
|
GeneralFolderUID = "general"
|
|
|
|
RootFolderUID = ""
|
|
|
|
MaxNestedFolderDepth = 4
|
|
|
|
SharedWithMeFolderUID = "sharedwithme"
|
2022-10-26 09:15:14 -05:00
|
|
|
)
|
|
|
|
|
2023-08-22 05:52:24 -05:00
|
|
|
var ErrFolderNotFound = errutil.NotFound("folder.notFound")
|
2022-10-28 08:35:49 -05:00
|
|
|
|
2022-10-26 09:15:14 -05:00
|
|
|
type Folder struct {
|
2023-11-20 14:44:51 -06:00
|
|
|
// Deprecated: use UID instead
|
2022-11-03 08:21:41 -05:00
|
|
|
ID int64 `xorm:"pk autoincr 'id'"`
|
|
|
|
OrgID int64 `xorm:"org_id"`
|
|
|
|
UID string `xorm:"uid"`
|
|
|
|
ParentUID string `xorm:"parent_uid"`
|
2022-10-26 09:15:14 -05:00
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
|
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
|
2022-10-28 07:23:39 -05:00
|
|
|
// TODO: validate if this field is required/relevant to folders.
|
2022-11-03 08:21:41 -05:00
|
|
|
// currently there is no such column
|
2024-01-25 01:27:13 -06:00
|
|
|
Version int
|
|
|
|
URL string
|
|
|
|
UpdatedBy int64
|
|
|
|
CreatedBy int64
|
|
|
|
HasACL bool
|
|
|
|
Fullpath string `xorm:"fullpath"`
|
|
|
|
FullpathUIDs string `xorm:"fullpath_uids"`
|
2022-11-10 03:41:03 -06:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:16:53 -06:00
|
|
|
var GeneralFolder = Folder{ID: 0, Title: "General"}
|
|
|
|
|
2023-12-05 09:13:31 -06:00
|
|
|
var SharedWithMeFolder = Folder{
|
|
|
|
Title: "Shared with me",
|
|
|
|
Description: "Dashboards and folders shared with me",
|
|
|
|
UID: SharedWithMeFolderUID,
|
|
|
|
ParentUID: "",
|
|
|
|
ID: -1,
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:16:53 -06:00
|
|
|
func (f *Folder) IsGeneral() bool {
|
2024-01-25 04:10:35 -06:00
|
|
|
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
|
2023-11-20 14:44:51 -06:00
|
|
|
// nolint:staticcheck
|
2023-02-08 09:16:53 -06:00
|
|
|
return f.ID == GeneralFolder.ID && f.Title == GeneralFolder.Title
|
|
|
|
}
|
|
|
|
|
2023-05-10 08:20:16 -05:00
|
|
|
func (f *Folder) WithURL() *Folder {
|
|
|
|
if f == nil || f.URL != "" {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy of dashboards.GetFolderURL()
|
|
|
|
f.URL = fmt.Sprintf("%s/dashboards/f/%s/%s", setting.AppSubUrl, f.UID, slugify.Slugify(f.Title))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:15:14 -05:00
|
|
|
// NewFolder tales a title and returns a Folder with the Created and Updated
|
|
|
|
// fields set to the current time.
|
|
|
|
func NewFolder(title string, description string) *Folder {
|
|
|
|
return &Folder{
|
|
|
|
Title: title,
|
|
|
|
Description: description,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFolderCommand captures the information required by the folder service
|
|
|
|
// to create a folder.
|
|
|
|
type CreateFolderCommand struct {
|
2022-11-03 08:21:41 -05:00
|
|
|
UID string `json:"uid"`
|
2022-11-10 03:41:03 -06:00
|
|
|
OrgID int64 `json:"-"`
|
2022-10-26 09:15:14 -05:00
|
|
|
Title string `json:"title"`
|
|
|
|
Description string `json:"description"`
|
2022-11-24 12:28:53 -06:00
|
|
|
ParentUID string `json:"parentUid"`
|
2022-11-23 03:13:47 -06:00
|
|
|
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester `json:"-"`
|
2022-10-26 09:15:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateFolderCommand captures the information required by the folder service
|
|
|
|
// to update a folder. Use Move to update a folder's parent folder.
|
|
|
|
type UpdateFolderCommand struct {
|
2022-12-20 07:00:33 -06:00
|
|
|
UID string `json:"-"`
|
|
|
|
OrgID int64 `json:"-"`
|
|
|
|
// NewTitle it's an optional parameter used for overriding the existing folder title
|
|
|
|
NewTitle *string `json:"title"` // keep same json tag with the legacy command for not breaking the existing APIs
|
|
|
|
// NewDescription it's an optional parameter used for overriding the existing folder description
|
|
|
|
NewDescription *string `json:"description"` // keep same json tag with the legacy command for not breaking the existing APIs
|
|
|
|
NewParentUID *string `json:"-"`
|
|
|
|
|
|
|
|
// Version only used by the legacy folder implementation
|
|
|
|
Version int `json:"version"`
|
|
|
|
// Overwrite only used by the legacy folder implementation
|
|
|
|
Overwrite bool `json:"overwrite"`
|
2022-11-23 03:13:47 -06:00
|
|
|
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester `json:"-"`
|
2022-10-26 09:15:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MoveFolderCommand captures the information required by the folder service
|
|
|
|
// to move a folder.
|
|
|
|
type MoveFolderCommand struct {
|
2023-01-25 09:04:08 -06:00
|
|
|
UID string `json:"-"`
|
|
|
|
NewParentUID string `json:"parentUid"`
|
2022-11-10 03:41:03 -06:00
|
|
|
OrgID int64 `json:"-"`
|
2022-11-23 03:13:47 -06:00
|
|
|
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester `json:"-"`
|
2022-10-26 09:15:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFolderCommand captures the information required by the folder service
|
|
|
|
// to delete a folder.
|
|
|
|
type DeleteFolderCommand struct {
|
2022-11-10 02:42:32 -06:00
|
|
|
UID string `json:"uid" xorm:"uid"`
|
|
|
|
OrgID int64 `json:"orgId" xorm:"org_id"`
|
|
|
|
ForceDeleteRules bool `json:"forceDeleteRules"`
|
2022-11-23 03:13:47 -06:00
|
|
|
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester `json:"-"`
|
2022-10-26 09:15:14 -05:00
|
|
|
}
|
|
|
|
|
2022-10-26 10:52:01 -05:00
|
|
|
// GetFolderQuery is used for all folder Get requests. Only one of UID, ID, or
|
2023-06-22 03:43:38 -05:00
|
|
|
// Title should be set; if multiple fields are set by the caller the dashboard
|
2023-10-12 10:31:49 -05:00
|
|
|
// service will select the field with the most specificity, in order: ID, UID,
|
|
|
|
// Title.
|
2022-10-26 10:52:01 -05:00
|
|
|
type GetFolderQuery struct {
|
2023-11-15 09:30:00 -06:00
|
|
|
UID *string
|
|
|
|
// Deprecated: use FolderUID instead
|
2024-01-25 03:29:56 -06:00
|
|
|
ID *int64
|
|
|
|
Title *string
|
|
|
|
ParentUID *string
|
|
|
|
OrgID int64
|
2022-11-23 03:13:47 -06:00
|
|
|
|
2023-09-06 04:16:10 -05:00
|
|
|
SignedInUser identity.Requester `json:"-"`
|
2022-10-26 09:15:14 -05:00
|
|
|
}
|
|
|
|
|
2024-01-25 01:27:13 -06:00
|
|
|
type GetFoldersQuery struct {
|
|
|
|
OrgID int64
|
|
|
|
UIDs []string
|
|
|
|
WithFullpath bool
|
|
|
|
WithFullpathUIDs bool
|
|
|
|
BatchSize uint64
|
|
|
|
|
|
|
|
SignedInUser identity.Requester `json:"-"`
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:52:01 -05:00
|
|
|
// GetParentsQuery captures the information required by the folder service to
|
2022-10-26 09:15:14 -05:00
|
|
|
// return a list of all parent folders of a given folder.
|
2022-10-26 10:52:01 -05:00
|
|
|
type GetParentsQuery struct {
|
2022-11-03 08:21:41 -05:00
|
|
|
UID string `xorm:"uid"`
|
|
|
|
OrgID int64 `xorm:"org_id"`
|
2022-10-26 09:15:14 -05:00
|
|
|
}
|
|
|
|
|
2022-12-19 02:52:04 -06:00
|
|
|
// GetChildrenQuery captures the information required by the folder service to
|
2022-10-26 09:15:14 -05:00
|
|
|
// return a list of child folders of the given folder.
|
|
|
|
|
2022-12-19 02:52:04 -06:00
|
|
|
type GetChildrenQuery struct {
|
2023-12-15 11:34:08 -06:00
|
|
|
UID string
|
|
|
|
OrgID int64
|
2022-10-26 09:15:14 -05:00
|
|
|
Depth int64
|
|
|
|
|
|
|
|
// Pagination options
|
|
|
|
Limit int64
|
|
|
|
Page int64
|
2022-12-19 02:52:04 -06:00
|
|
|
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester `json:"-"`
|
2023-12-15 11:34:08 -06:00
|
|
|
|
|
|
|
// array of folder uids to filter by
|
|
|
|
FolderUIDs []string `json:"-"`
|
2022-10-26 09:15:14 -05:00
|
|
|
}
|
2023-01-25 02:14:32 -06:00
|
|
|
|
|
|
|
type HasEditPermissionInFoldersQuery struct {
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester
|
2023-01-25 02:14:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type HasAdminPermissionInDashboardsOrFoldersQuery struct {
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester
|
2023-01-25 02:14:32 -06:00
|
|
|
}
|
2023-04-24 08:57:28 -05:00
|
|
|
|
2023-04-27 10:00:09 -05:00
|
|
|
// GetDescendantCountsQuery captures the information required by the folder service
|
|
|
|
// to return the count of descendants (direct and indirect) in a folder.
|
|
|
|
type GetDescendantCountsQuery struct {
|
2023-04-24 08:57:28 -05:00
|
|
|
UID *string
|
|
|
|
OrgID int64
|
|
|
|
|
2023-10-06 08:02:34 -05:00
|
|
|
SignedInUser identity.Requester `json:"-"`
|
2023-04-24 08:57:28 -05:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:00:09 -05:00
|
|
|
type DescendantCounts map[string]int64
|