Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
773fff9f
Commit
773fff9f
authored
Feb 01, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
folders: changes and updated tests after merging permissions and new url structure
parent
5912cf4d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
69 additions
and
27 deletions
+69
-27
pkg/api/api.go
+4
-3
pkg/api/dtos/folder.go
+2
-1
pkg/api/folder.go
+18
-21
pkg/api/folder_test.go
+0
-0
pkg/models/folders.go
+45
-2
No files found.
pkg/api/api.go
View file @
773fff9f
...
...
@@ -248,10 +248,11 @@ func (hs *HttpServer) registerRoutes() {
// Folders
apiRoute
.
Group
(
"/folders"
,
func
(
folderRoute
RouteRegister
)
{
folderRoute
.
Get
(
"/:id"
,
wrap
(
GetFolderById
))
folderRoute
.
Get
(
"/:uid"
,
wrap
(
GetFolder
))
folderRoute
.
Get
(
"/id/:id"
,
wrap
(
GetFolder
))
folderRoute
.
Post
(
"/"
,
bind
(
m
.
CreateFolderCommand
{}),
wrap
(
CreateFolder
))
folderRoute
.
Put
(
"/:id"
,
bind
(
m
.
UpdateFolderCommand
{}),
wrap
(
UpdateFolder
))
folderRoute
.
Delete
(
"/:id"
,
wrap
(
DeleteFolder
))
folderRoute
.
Put
(
"/:
u
id"
,
bind
(
m
.
UpdateFolderCommand
{}),
wrap
(
UpdateFolder
))
folderRoute
.
Delete
(
"/:
u
id"
,
wrap
(
DeleteFolder
))
})
// Dashboard
...
...
pkg/api/dtos/folder.go
View file @
773fff9f
...
...
@@ -4,8 +4,9 @@ import "time"
type
Folder
struct
{
Id
int64
`json:"id"`
Uid
string
`json:"uid"`
Title
string
`json:"title"`
Slug
string
`json:"slug
"`
Url
string
`json:"url
"`
HasAcl
bool
`json:"hasAcl"`
CanSave
bool
`json:"canSave"`
CanEdit
bool
`json:"canEdit"`
...
...
pkg/api/folder.go
View file @
773fff9f
...
...
@@ -12,8 +12,8 @@ import (
"github.com/grafana/grafana/pkg/util"
)
func
getFolderHelper
(
orgId
int64
,
slug
string
,
id
int64
)
(
*
m
.
Dashboard
,
Response
)
{
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
Id
:
id
,
OrgId
:
orgI
d
}
func
getFolderHelper
(
orgId
int64
,
id
int64
,
uid
string
)
(
*
m
.
Dashboard
,
Response
)
{
query
:=
m
.
GetDashboardQuery
{
OrgId
:
orgId
,
Id
:
id
,
Uid
:
ui
d
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
if
err
==
m
.
ErrDashboardNotFound
{
err
=
m
.
ErrFolderNotFound
...
...
@@ -53,8 +53,8 @@ func GetFoldersForSignedInUser(c *middleware.Context) Response {
return
Json
(
200
,
query
.
Result
)
}
func
GetFolder
ById
(
c
*
middleware
.
Context
)
Response
{
folder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
""
,
c
.
ParamsInt64
(
":
id"
))
func
GetFolder
(
c
*
middleware
.
Context
)
Response
{
folder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
c
.
ParamsInt64
(
":id"
),
c
.
Params
(
":u
id"
))
if
rsp
!=
nil
{
return
rsp
}
...
...
@@ -72,14 +72,13 @@ func CreateFolder(c *middleware.Context, cmd m.CreateFolderCommand) Response {
cmd
.
OrgId
=
c
.
OrgId
cmd
.
UserId
=
c
.
UserId
dashFolder
:=
m
.
NewDashboardFolder
(
cmd
.
Title
)
dashFolder
:=
cmd
.
GetDashboardModel
(
)
guardian
:=
guardian
.
NewDashboardGuardian
(
0
,
c
.
OrgId
,
c
.
SignedInUser
)
if
canSave
,
err
:=
guardian
.
CanSave
();
err
!=
nil
||
!
canSave
{
return
folderGuardianResponse
(
err
)
}
// Check if Title is empty
if
dashFolder
.
Title
==
""
{
return
ApiError
(
400
,
m
.
ErrFolderTitleEmpty
.
Error
(),
nil
)
}
...
...
@@ -92,9 +91,6 @@ func CreateFolder(c *middleware.Context, cmd m.CreateFolderCommand) Response {
return
ApiError
(
403
,
"Quota reached"
,
nil
)
}
dashFolder
.
CreatedBy
=
c
.
UserId
dashFolder
.
UpdatedBy
=
c
.
UserId
dashItem
:=
&
dashboards
.
SaveDashboardItem
{
Dashboard
:
dashFolder
,
OrgId
:
c
.
OrgId
,
...
...
@@ -113,8 +109,9 @@ func CreateFolder(c *middleware.Context, cmd m.CreateFolderCommand) Response {
func
UpdateFolder
(
c
*
middleware
.
Context
,
cmd
m
.
UpdateFolderCommand
)
Response
{
cmd
.
OrgId
=
c
.
OrgId
cmd
.
UserId
=
c
.
UserId
uid
:=
c
.
Params
(
":uid"
)
dashFolder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
""
,
c
.
ParamsInt64
(
":id"
)
)
dashFolder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
0
,
uid
)
if
rsp
!=
nil
{
return
rsp
}
...
...
@@ -124,13 +121,8 @@ func UpdateFolder(c *middleware.Context, cmd m.UpdateFolderCommand) Response {
return
folderGuardianResponse
(
err
)
}
dashFolder
.
Data
.
Set
(
"title"
,
cmd
.
Title
)
dashFolder
.
Title
=
cmd
.
Title
dashFolder
.
Data
.
Set
(
"version"
,
cmd
.
Version
)
dashFolder
.
Version
=
cmd
.
Version
dashFolder
.
UpdatedBy
=
c
.
UserId
cmd
.
UpdateDashboardModel
(
dashFolder
)
// Check if Title is empty
if
dashFolder
.
Title
==
""
{
return
ApiError
(
400
,
m
.
ErrFolderTitleEmpty
.
Error
(),
nil
)
}
...
...
@@ -151,7 +143,7 @@ func UpdateFolder(c *middleware.Context, cmd m.UpdateFolderCommand) Response {
}
func
DeleteFolder
(
c
*
middleware
.
Context
)
Response
{
dashFolder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
""
,
c
.
ParamsInt64
(
":
id"
))
dashFolder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
0
,
c
.
Params
(
":u
id"
))
if
rsp
!=
nil
{
return
rsp
}
...
...
@@ -177,17 +169,18 @@ func toDto(guardian *guardian.DashboardGuardian, folder *m.Dashboard) dtos.Folde
// Finding creator and last updater of the folder
updater
,
creator
:=
"Anonymous"
,
"Anonymous"
if
folder
.
UpdatedBy
>
0
{
updater
=
getUserLogin
(
folder
.
UpdatedBy
)
}
if
folder
.
CreatedBy
>
0
{
creator
=
getUserLogin
(
folder
.
CreatedBy
)
}
if
folder
.
UpdatedBy
>
0
{
updater
=
getUserLogin
(
folder
.
UpdatedBy
)
}
return
dtos
.
Folder
{
Id
:
folder
.
Id
,
Uid
:
folder
.
Uid
,
Title
:
folder
.
Title
,
Slug
:
folder
.
Slug
,
Url
:
folder
.
GetUrl
()
,
HasAcl
:
folder
.
HasAcl
,
CanSave
:
canSave
,
CanEdit
:
canEdit
,
...
...
@@ -205,6 +198,10 @@ func toFolderError(err error) Response {
return
ApiError
(
400
,
m
.
ErrFolderTitleEmpty
.
Error
(),
nil
)
}
if
err
==
m
.
ErrDashboardWithSameNameInFolderExists
{
return
Json
(
412
,
util
.
DynMap
{
"status"
:
"name-exists"
,
"message"
:
m
.
ErrFolderSameNameExists
.
Error
()})
}
if
err
==
m
.
ErrDashboardWithSameUIDExists
{
return
Json
(
412
,
util
.
DynMap
{
"status"
:
"uid-exists"
,
"message"
:
m
.
ErrFolderWithSameUIDExists
.
Error
()})
}
...
...
pkg/api/folder_test.go
View file @
773fff9f
This diff is collapsed.
Click to expand it.
pkg/models/folders.go
View file @
773fff9f
...
...
@@ -2,6 +2,7 @@ package models
import
(
"errors"
"strings"
"time"
)
...
...
@@ -10,14 +11,16 @@ var (
ErrFolderNotFound
=
errors
.
New
(
"Folder not found"
)
ErrFolderVersionMismatch
=
errors
.
New
(
"The folder has been changed by someone else"
)
ErrFolderTitleEmpty
=
errors
.
New
(
"Folder title cannot be empty"
)
ErrFolderWithSameUIDExists
=
errors
.
New
(
"A folder with the same uid already exists"
)
ErrFolderWithSameUIDExists
=
errors
.
New
(
"A folder/dashboard with the same uid already exists"
)
ErrFolderSameNameExists
=
errors
.
New
(
"A folder or dashboard in the general folder with the same name already exists"
)
ErrFolderFailedGenerateUniqueUid
=
errors
.
New
(
"Failed to generate unique folder id"
)
)
type
Folder
struct
{
Id
int64
Uid
string
Title
string
Slug
string
Url
string
OrgId
int64
Version
int
...
...
@@ -29,6 +32,45 @@ type Folder struct {
HasAcl
bool
}
// GetDashboardModel turns the command into the savable model
func
(
cmd
*
CreateFolderCommand
)
GetDashboardModel
()
*
Dashboard
{
dashFolder
:=
NewDashboardFolder
(
strings
.
TrimSpace
(
cmd
.
Title
))
dashFolder
.
OrgId
=
cmd
.
OrgId
dashFolder
.
Uid
=
strings
.
TrimSpace
(
cmd
.
Uid
)
dashFolder
.
Data
.
Set
(
"uid"
,
cmd
.
Uid
)
userId
:=
cmd
.
UserId
if
userId
==
0
{
userId
=
-
1
}
dashFolder
.
CreatedBy
=
userId
dashFolder
.
UpdatedBy
=
userId
dashFolder
.
UpdateSlug
()
return
dashFolder
}
// UpdateDashboardModel updates an existing model from command into model for update
func
(
cmd
*
UpdateFolderCommand
)
UpdateDashboardModel
(
dashFolder
*
Dashboard
)
{
dashFolder
.
Title
=
strings
.
TrimSpace
(
cmd
.
Title
)
dashFolder
.
Data
.
Set
(
"title"
,
cmd
.
Title
)
dashFolder
.
Uid
=
dashFolder
.
Data
.
MustString
(
"uid"
)
dashFolder
.
Data
.
Set
(
"version"
,
cmd
.
Version
)
dashFolder
.
Version
=
cmd
.
Version
dashFolder
.
IsFolder
=
true
userId
:=
cmd
.
UserId
if
userId
==
0
{
userId
=
-
1
}
dashFolder
.
UpdatedBy
=
userId
dashFolder
.
UpdateSlug
()
}
//
// COMMANDS
//
...
...
@@ -36,6 +78,7 @@ type Folder struct {
type
CreateFolderCommand
struct
{
OrgId
int64
`json:"-"`
UserId
int64
`json:"userId"`
Uid
string
`json:"uid"`
Title
string
`json:"title"`
Result
*
Folder
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment