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
ea7998ca
Commit
ea7998ca
authored
Feb 20, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
folders: use new folder service in folder api routes
parent
268fb4dc
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
106 deletions
+56
-106
pkg/api/api.go
+3
-2
pkg/api/dtos/folder.go
+6
-0
pkg/api/folder.go
+47
-104
pkg/api/folder_test.go
+0
-0
No files found.
pkg/api/api.go
View file @
ea7998ca
...
...
@@ -248,8 +248,9 @@ func (hs *HttpServer) registerRoutes() {
// Folders
apiRoute
.
Group
(
"/folders"
,
func
(
folderRoute
RouteRegister
)
{
folderRoute
.
Get
(
"/:uid"
,
wrap
(
GetFolder
))
folderRoute
.
Get
(
"/id/:id"
,
wrap
(
GetFolder
))
folderRoute
.
Get
(
"/"
,
wrap
(
GetFolders
))
folderRoute
.
Get
(
"/:uid"
,
wrap
(
GetFolderByUid
))
folderRoute
.
Get
(
"/id/:id"
,
wrap
(
GetFolderById
))
folderRoute
.
Post
(
"/"
,
bind
(
m
.
CreateFolderCommand
{}),
wrap
(
CreateFolder
))
folderRoute
.
Put
(
"/:uid"
,
bind
(
m
.
UpdateFolderCommand
{}),
wrap
(
UpdateFolder
))
folderRoute
.
Delete
(
"/:uid"
,
wrap
(
DeleteFolder
))
...
...
pkg/api/dtos/folder.go
View file @
ea7998ca
...
...
@@ -17,3 +17,9 @@ type Folder struct {
Updated
time
.
Time
`json:"updated"`
Version
int
`json:"version"`
}
type
FolderSearchHit
struct
{
Id
int64
`json:"id"`
Uid
string
`json:"uid"`
Title
string
`json:"title"`
}
pkg/api/folder.go
View file @
ea7998ca
package
api
import
(
"fmt"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
...
...
@@ -12,142 +9,84 @@ import (
"github.com/grafana/grafana/pkg/util"
)
func
getFolderHelper
(
orgId
int64
,
id
int64
,
uid
string
)
(
*
m
.
Dashboard
,
Response
)
{
query
:=
m
.
GetDashboardQuery
{
OrgId
:
orgId
,
Id
:
id
,
Uid
:
uid
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
if
err
==
m
.
ErrDashboardNotFound
{
err
=
m
.
ErrFolderNotFound
}
return
nil
,
ApiError
(
404
,
"Folder not found"
,
err
)
}
if
!
query
.
Result
.
IsFolder
{
return
nil
,
ApiError
(
404
,
"Folder not found"
,
m
.
ErrFolderNotFound
)
}
return
query
.
Result
,
nil
}
func
GetFolders
(
c
*
middleware
.
Context
)
Response
{
s
:=
dashboards
.
NewFolderService
(
c
.
OrgId
,
c
.
SignedInUser
)
folders
,
err
:=
s
.
GetFolders
(
c
.
QueryInt
(
"limit"
))
func
folderGuardianResponse
(
err
error
)
Response
{
if
err
!=
nil
{
return
ApiError
(
500
,
"Error while checking folder permissions"
,
err
)
return
toFolderError
(
err
)
}
return
ApiError
(
403
,
"Access denied to this folder"
,
nil
)
}
result
:=
make
([]
dtos
.
FolderSearchHit
,
0
)
func
GetFolder
(
c
*
middleware
.
Context
)
Response
{
folder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
c
.
ParamsInt64
(
":id"
),
c
.
Params
(
":uid"
))
if
rsp
!=
nil
{
return
rsp
for
_
,
f
:=
range
folders
{
result
=
append
(
result
,
dtos
.
FolderSearchHit
{
Id
:
f
.
Id
,
Uid
:
f
.
Uid
,
Title
:
f
.
Title
,
})
}
guardian
:=
guardian
.
New
(
folder
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
if
canView
,
err
:=
guardian
.
CanView
();
err
!=
nil
||
!
canView
{
fmt
.
Printf
(
"%v"
,
err
)
return
folderGuardianResponse
(
err
)
}
return
Json
(
200
,
toFolderDto
(
&
guardian
,
folder
))
return
Json
(
200
,
result
)
}
func
CreateFolder
(
c
*
middleware
.
Context
,
cmd
m
.
CreateFolderCommand
)
Response
{
cmd
.
OrgId
=
c
.
OrgId
cmd
.
UserId
=
c
.
UserId
dashFolder
:=
cmd
.
GetDashboardModel
()
func
GetFolderByUid
(
c
*
middleware
.
Context
)
Response
{
s
:=
dashboards
.
NewFolderService
(
c
.
OrgId
,
c
.
SignedInUser
)
folder
,
err
:=
s
.
GetFolderByUid
(
c
.
Params
(
":uid"
))
guardian
:=
guardian
.
New
(
0
,
c
.
OrgId
,
c
.
SignedInUser
)
if
canSave
,
err
:=
guardian
.
CanSave
();
err
!=
nil
||
!
canSave
{
return
folderGuardianResponse
(
err
)
if
err
!=
nil
{
return
toFolderError
(
err
)
}
if
dashFolder
.
Title
==
""
{
return
ApiError
(
400
,
m
.
ErrFolderTitleEmpty
.
Error
(),
nil
)
}
g
:=
guardian
.
New
(
folder
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
return
Json
(
200
,
toFolderDto
(
g
,
folder
)
)
}
limitReached
,
err
:=
middleware
.
QuotaReached
(
c
,
"folder"
)
func
GetFolderById
(
c
*
middleware
.
Context
)
Response
{
s
:=
dashboards
.
NewFolderService
(
c
.
OrgId
,
c
.
SignedInUser
)
folder
,
err
:=
s
.
GetFolderById
(
c
.
ParamsInt64
(
":id"
))
if
err
!=
nil
{
return
ApiError
(
500
,
"failed to get quota"
,
err
)
}
if
limitReached
{
return
ApiError
(
403
,
"Quota reached"
,
nil
)
}
saveDashboardDto
:=
&
dashboards
.
SaveDashboardDTO
{
Dashboard
:
dashFolder
,
OrgId
:
c
.
OrgId
,
UserId
:
c
.
UserId
,
return
toFolderError
(
err
)
}
folder
,
err
:=
dashboards
.
GetRepository
()
.
SaveDashboard
(
saveDashboardDto
)
g
:=
guardian
.
New
(
folder
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
return
Json
(
200
,
toFolderDto
(
g
,
folder
))
}
func
CreateFolder
(
c
*
middleware
.
Context
,
cmd
m
.
CreateFolderCommand
)
Response
{
s
:=
dashboards
.
NewFolderService
(
c
.
OrgId
,
c
.
SignedInUser
)
err
:=
s
.
CreateFolder
(
&
cmd
)
if
err
!=
nil
{
return
toFolderError
(
err
)
}
return
Json
(
200
,
toFolderDto
(
&
guardian
,
folder
))
g
:=
guardian
.
New
(
cmd
.
Result
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
return
Json
(
200
,
toFolderDto
(
g
,
cmd
.
Result
))
}
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
,
0
,
uid
)
if
rsp
!=
nil
{
return
rsp
}
guardian
:=
guardian
.
New
(
dashFolder
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
if
canSave
,
err
:=
guardian
.
CanSave
();
err
!=
nil
||
!
canSave
{
return
folderGuardianResponse
(
err
)
}
cmd
.
UpdateDashboardModel
(
dashFolder
)
if
dashFolder
.
Title
==
""
{
return
ApiError
(
400
,
m
.
ErrFolderTitleEmpty
.
Error
(),
nil
)
}
saveDashboardDto
:=
&
dashboards
.
SaveDashboardDTO
{
Dashboard
:
dashFolder
,
OrgId
:
c
.
OrgId
,
UserId
:
c
.
UserId
,
Overwrite
:
cmd
.
Overwrite
,
}
folder
,
err
:=
dashboards
.
GetRepository
()
.
SaveDashboard
(
saveDashboardDto
)
s
:=
dashboards
.
NewFolderService
(
c
.
OrgId
,
c
.
SignedInUser
)
err
:=
s
.
UpdateFolder
(
c
.
Params
(
":uid"
),
&
cmd
)
if
err
!=
nil
{
return
toFolderError
(
err
)
}
return
Json
(
200
,
toFolderDto
(
&
guardian
,
folder
))
g
:=
guardian
.
New
(
cmd
.
Result
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
return
Json
(
200
,
toFolderDto
(
g
,
cmd
.
Result
))
}
func
DeleteFolder
(
c
*
middleware
.
Context
)
Response
{
dashFolder
,
rsp
:=
getFolderHelper
(
c
.
OrgId
,
0
,
c
.
Params
(
":uid"
))
if
rsp
!=
nil
{
return
rsp
}
guardian
:=
guardian
.
New
(
dashFolder
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
if
canSave
,
err
:=
guardian
.
CanSave
();
err
!=
nil
||
!
canSave
{
return
folderGuardianResponse
(
err
)
}
deleteCmd
:=
m
.
DeleteDashboardCommand
{
OrgId
:
c
.
OrgId
,
Id
:
dashFolder
.
Id
}
if
err
:=
bus
.
Dispatch
(
&
deleteCmd
);
err
!=
nil
{
return
ApiError
(
500
,
"Failed to delete folder"
,
err
)
s
:=
dashboards
.
NewFolderService
(
c
.
OrgId
,
c
.
SignedInUser
)
f
,
err
:=
s
.
DeleteFolder
(
c
.
Params
(
":uid"
))
if
err
!=
nil
{
return
toFolderError
(
err
)
}
var
resp
=
map
[
string
]
interface
{}{
"title"
:
dashFolder
.
Title
}
var
resp
=
map
[
string
]
interface
{}{
"title"
:
f
.
Title
}
return
Json
(
200
,
resp
)
}
func
toFolderDto
(
g
*
guardian
.
DashboardGuardian
,
folder
*
m
.
Dashboard
)
dtos
.
Folder
{
func
toFolderDto
(
g
guardian
.
DashboardGuardian
,
folder
*
m
.
Folder
)
dtos
.
Folder
{
canEdit
,
_
:=
g
.
CanEdit
()
canSave
,
_
:=
g
.
CanSave
()
canAdmin
,
_
:=
g
.
CanAdmin
()
...
...
@@ -165,7 +104,7 @@ func toFolderDto(g *guardian.DashboardGuardian, folder *m.Dashboard) dtos.Folder
Id
:
folder
.
Id
,
Uid
:
folder
.
Uid
,
Title
:
folder
.
Title
,
Url
:
folder
.
GetUrl
()
,
Url
:
folder
.
Url
,
HasAcl
:
folder
.
HasAcl
,
CanSave
:
canSave
,
CanEdit
:
canEdit
,
...
...
@@ -183,6 +122,10 @@ func toFolderError(err error) Response {
return
ApiError
(
400
,
m
.
ErrFolderTitleEmpty
.
Error
(),
nil
)
}
if
err
==
m
.
ErrFolderAccessDenied
{
return
ApiError
(
403
,
"Access denied"
,
err
)
}
if
err
==
m
.
ErrDashboardWithSameNameInFolderExists
{
return
Json
(
412
,
util
.
DynMap
{
"status"
:
"name-exists"
,
"message"
:
m
.
ErrFolderSameNameExists
.
Error
()})
}
...
...
pkg/api/folder_test.go
View file @
ea7998ca
This diff is collapsed.
Click to expand it.
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