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
ce947d47
Commit
ce947d47
authored
Dec 17, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update data sources now work, #8
parent
adf4e72c
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
130 additions
and
18 deletions
+130
-18
grafana
+1
-1
pkg/api/api.go
+3
-1
pkg/api/api_datasources.go
+42
-4
pkg/api/dtos/models.go
+2
-3
pkg/models/datasource.go
+17
-1
pkg/stores/sqlstore/sqlstore_datasource.go
+32
-6
pkg/stores/sqlstore/sqlstore_test.go
+33
-2
No files found.
grafana
@
ad910939
Subproject commit
0e970307160dfed9b09f6d1bf06dd49ff38035b7
Subproject commit
ad91093902bdfc0d2a87bb362a76a9057aef4361
pkg/api/api.go
View file @
ce947d47
...
...
@@ -28,7 +28,9 @@ func Register(m *macaron.Macaron) {
// datasources
m
.
Get
(
"/admin/datasources/"
,
auth
,
Index
)
m
.
Get
(
"/api/admin/datasources/list"
,
auth
,
GetDataSources
)
m
.
Post
(
"/api/admin/datasources/add"
,
auth
,
AddDataSource
)
m
.
Put
(
"/api/admin/datasources"
,
auth
,
AddDataSource
)
m
.
Post
(
"/api/admin/datasources"
,
auth
,
UpdateDataSource
)
m
.
Delete
(
"/api/admin/datasources/:id"
,
auth
,
DeleteDataSource
)
// user register
m
.
Get
(
"/register/*_"
,
Index
)
...
...
pkg/api/api_datasources.go
View file @
ce947d47
...
...
@@ -16,9 +16,9 @@ func GetDataSources(c *middleware.Context) {
return
}
result
:=
make
([]
*
dtos
.
DataSource
,
len
(
query
.
Res
p
))
for
_
,
ds
:=
range
query
.
Resp
{
result
=
append
(
result
,
&
dtos
.
DataSource
{
result
:=
make
([]
*
dtos
.
DataSource
,
len
(
query
.
Res
ult
))
for
i
,
ds
:=
range
query
.
Result
{
result
[
i
]
=
&
dtos
.
DataSource
{
Id
:
ds
.
Id
,
AccountId
:
ds
.
AccountId
,
Name
:
ds
.
Name
,
...
...
@@ -28,12 +28,31 @@ func GetDataSources(c *middleware.Context) {
Password
:
ds
.
Password
,
User
:
ds
.
User
,
BasicAuth
:
ds
.
BasicAuth
,
}
)
}
}
c
.
JSON
(
200
,
result
)
}
func
DeleteDataSource
(
c
*
middleware
.
Context
)
{
id
:=
c
.
ParamsInt64
(
":id"
)
if
id
<=
0
{
c
.
JsonApiErr
(
400
,
"Missing valid datasource id"
,
nil
)
return
}
cmd
:=
&
m
.
DeleteDataSourceCommand
{
Id
:
id
,
AccountId
:
c
.
UserAccount
.
Id
}
err
:=
bus
.
Dispatch
(
cmd
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to delete datasource"
,
err
)
return
}
c
.
JsonOK
(
"Data source deleted"
)
}
func
AddDataSource
(
c
*
middleware
.
Context
)
{
cmd
:=
m
.
AddDataSourceCommand
{}
...
...
@@ -52,3 +71,22 @@ func AddDataSource(c *middleware.Context) {
c
.
JsonOK
(
"Datasource added"
)
}
func
UpdateDataSource
(
c
*
middleware
.
Context
)
{
cmd
:=
m
.
UpdateDataSourceCommand
{}
if
!
c
.
JsonBody
(
&
cmd
)
{
c
.
JsonApiErr
(
400
,
"Validation failed"
,
nil
)
return
}
cmd
.
AccountId
=
c
.
Account
.
Id
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to update datasource"
,
err
)
return
}
c
.
JsonOK
(
"Datasource updated"
)
}
pkg/api/dtos/models.go
View file @
ce947d47
...
...
@@ -39,9 +39,8 @@ type Collaborator struct {
}
type
DataSource
struct
{
Id
int64
`json:"id"`
AccountId
int64
`json:"accountId"`
Id
int64
`json:"id"`
AccountId
int64
`json:"accountId"`
Name
string
`json:"name"`
Type
models
.
DsType
`json:"type"`
Access
models
.
DsAccess
`json:"access"`
...
...
pkg/models/datasource.go
View file @
ce947d47
...
...
@@ -31,7 +31,7 @@ type DataSource struct {
type
GetDataSourcesQuery
struct
{
AccountId
int64
Res
p
[]
*
DataSource
Res
ult
[]
*
DataSource
}
type
AddDataSourceCommand
struct
{
...
...
@@ -43,3 +43,19 @@ type AddDataSourceCommand struct {
Password
string
User
string
}
type
UpdateDataSourceCommand
struct
{
Id
int64
AccountId
int64
Name
string
Type
DsType
Access
DsAccess
Url
string
Password
string
User
string
}
type
DeleteDataSourceCommand
struct
{
Id
int64
AccountId
int64
}
pkg/stores/sqlstore/sqlstore_datasource.go
View file @
ce947d47
...
...
@@ -12,13 +12,23 @@ import (
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetDataSources
)
bus
.
AddHandler
(
"sql"
,
AddDataSource
)
bus
.
AddHandler
(
"sql"
,
DeleteDataSource
)
bus
.
AddHandler
(
"sql"
,
UpdateDataSource
)
}
func
GetDataSources
(
query
*
m
.
GetDataSourcesQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"account_id=?"
,
query
.
AccountId
)
query
.
Resp
=
make
([]
*
m
.
DataSource
,
0
)
return
sess
.
Find
(
&
query
.
Resp
)
query
.
Result
=
make
([]
*
m
.
DataSource
,
0
)
return
sess
.
Find
(
&
query
.
Result
)
}
func
DeleteDataSource
(
cmd
*
m
.
DeleteDataSourceCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
rawSql
=
"DELETE FROM data_source WHERE id=? and account_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Id
,
cmd
.
AccountId
)
return
err
})
}
func
AddDataSource
(
cmd
*
m
.
AddDataSourceCommand
)
error
{
...
...
@@ -36,12 +46,28 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error {
Updated
:
time
.
Now
(),
}
if
ds
.
Id
==
0
{
_
,
err
=
sess
.
Insert
(
ds
)
}
else
{
_
,
err
=
sess
.
Id
(
ds
.
Id
)
.
Update
(
ds
)
_
,
err
=
sess
.
Insert
(
ds
)
return
err
})
}
func
UpdateDataSource
(
cmd
*
m
.
UpdateDataSourceCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
err
error
ds
:=
m
.
DataSource
{
Id
:
cmd
.
Id
,
AccountId
:
cmd
.
AccountId
,
Name
:
cmd
.
Name
,
Type
:
cmd
.
Type
,
Access
:
cmd
.
Access
,
Url
:
cmd
.
Url
,
Updated
:
time
.
Now
(),
}
_
,
err
=
sess
.
Where
(
"id=? and account_id=?"
,
ds
.
Id
,
ds
.
AccountId
)
.
Update
(
&
ds
)
return
err
})
}
pkg/stores/sqlstore/sqlstore_test.go
View file @
ce947d47
...
...
@@ -45,13 +45,44 @@ func TestDataAccess(t *testing.T) {
err
=
GetDataSources
(
&
query
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
query
.
Res
p
),
ShouldEqual
,
1
)
So
(
len
(
query
.
Res
ult
),
ShouldEqual
,
1
)
ds
:=
query
.
Res
p
[
0
]
ds
:=
query
.
Res
ult
[
0
]
So
(
ds
.
AccountId
,
ShouldEqual
,
10
)
})
Convey
(
"Given a datasource"
,
func
()
{
AddDataSource
(
&
m
.
AddDataSourceCommand
{
AccountId
:
10
,
Type
:
m
.
DS_GRAPHITE
,
Access
:
m
.
DS_ACCESS_DIRECT
,
Url
:
"http://test"
,
})
query
:=
m
.
GetDataSourcesQuery
{
AccountId
:
10
}
GetDataSources
(
&
query
)
ds
:=
query
.
Result
[
0
]
Convey
(
"Can delete datasource"
,
func
()
{
err
:=
DeleteDataSource
(
&
m
.
DeleteDataSourceCommand
{
Id
:
ds
.
Id
,
AccountId
:
ds
.
AccountId
})
So
(
err
,
ShouldBeNil
)
GetDataSources
(
&
query
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
0
)
})
Convey
(
"Can not delete datasource with wrong accountId"
,
func
()
{
err
:=
DeleteDataSource
(
&
m
.
DeleteDataSourceCommand
{
Id
:
ds
.
Id
,
AccountId
:
123123
})
So
(
err
,
ShouldBeNil
)
GetDataSources
(
&
query
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
1
)
})
})
})
}
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