Commit adf4e72c by Torkel Ödegaard

More general backend work, in the middle of the night... Zzzz

parent c7ed348e
Subproject commit adb1502e728e5a312a6e4dc680edbd1f75219ea1 Subproject commit 0e970307160dfed9b09f6d1bf06dd49ff38035b7
...@@ -27,8 +27,8 @@ func Register(m *macaron.Macaron) { ...@@ -27,8 +27,8 @@ func Register(m *macaron.Macaron) {
// datasources // datasources
m.Get("/admin/datasources/", auth, Index) m.Get("/admin/datasources/", auth, Index)
m.Get("/api/admin/datasource/list", auth, GetDataSources) m.Get("/api/admin/datasources/list", auth, GetDataSources)
m.Post("/api/admin/datasource/add", auth, AddDataSource) m.Post("/api/admin/datasources/add", auth, AddDataSource)
// user register // user register
m.Get("/register/*_", Index) m.Get("/register/*_", Index)
......
package api package api
import ( import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
...@@ -14,21 +15,40 @@ func GetDataSources(c *middleware.Context) { ...@@ -14,21 +15,40 @@ func GetDataSources(c *middleware.Context) {
c.JsonApiErr(500, "Failed to query datasources", err) c.JsonApiErr(500, "Failed to query datasources", err)
return return
} }
result := make([]*dtos.DataSource, len(query.Resp))
for _, ds := range query.Resp {
result = append(result, &dtos.DataSource{
Id: ds.Id,
AccountId: ds.AccountId,
Name: ds.Name,
Url: ds.Url,
Type: ds.Type,
Access: ds.Access,
Password: ds.Password,
User: ds.User,
BasicAuth: ds.BasicAuth,
})
}
c.JSON(200, result)
} }
func AddDataSource(c *middleware.Context) { func AddDataSource(c *middleware.Context) {
cmd := m.AddDataSourceCommand{} cmd := m.AddDataSourceCommand{}
if !c.JsonBody(&cmd) { if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "bad request", nil) c.JsonApiErr(400, "Validation failed", nil)
return return
} }
cmd.AccountId = c.Account.Id
err := bus.Dispatch(&cmd) err := bus.Dispatch(&cmd)
if err != nil { if err != nil {
c.JsonApiErr(500, "Failed to add datasource", err) c.JsonApiErr(500, "Failed to add datasource", err)
return return
} }
c.Status(204) c.JsonOK("Datasource added")
} }
...@@ -38,6 +38,19 @@ type Collaborator struct { ...@@ -38,6 +38,19 @@ type Collaborator struct {
Role string `json:"role"` Role string `json:"role"`
} }
type DataSource struct {
Id int64 `json:"id"`
AccountId int64 `json:"accountId"`
Name string `json:"name"`
Type models.DsType `json:"type"`
Access models.DsAccess `json:"access"`
Url string `json:"url"`
Password string `json:"password"`
User string `json:"user"`
BasicAuth bool `json:"basicAuth"`
}
func NewCurrentUser(account *models.Account) *CurrentUser { func NewCurrentUser(account *models.Account) *CurrentUser {
model := &CurrentUser{} model := &CurrentUser{}
if account != nil { if account != nil {
......
...@@ -56,6 +56,14 @@ func (ctx *Context) Handle(status int, title string, err error) { ...@@ -56,6 +56,14 @@ func (ctx *Context) Handle(status int, title string, err error) {
ctx.HTML(status, strconv.Itoa(status)) ctx.HTML(status, strconv.Itoa(status))
} }
func (ctx *Context) JsonOK(message string) {
resp := make(map[string]interface{})
resp["message"] = message
ctx.JSON(200, resp)
}
func (ctx *Context) JsonApiErr(status int, message string, err error) { func (ctx *Context) JsonApiErr(status int, message string, err error) {
resp := make(map[string]interface{}) resp := make(map[string]interface{})
......
...@@ -47,6 +47,9 @@ func TestDataAccess(t *testing.T) { ...@@ -47,6 +47,9 @@ func TestDataAccess(t *testing.T) {
So(len(query.Resp), ShouldEqual, 1) So(len(query.Resp), ShouldEqual, 1)
ds := query.Resp[0]
So(ds.AccountId, ShouldEqual, 10)
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment