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

This commit is contained in:
Torkel Ödegaard 2014-12-17 03:09:54 +01:00
parent c7ed348ee8
commit adf4e72cf8
6 changed files with 49 additions and 5 deletions

@ -1 +1 @@
Subproject commit adb1502e728e5a312a6e4dc680edbd1f75219ea1
Subproject commit 0e970307160dfed9b09f6d1bf06dd49ff38035b7

View File

@ -27,8 +27,8 @@ func Register(m *macaron.Macaron) {
// datasources
m.Get("/admin/datasources/", auth, Index)
m.Get("/api/admin/datasource/list", auth, GetDataSources)
m.Post("/api/admin/datasource/add", auth, AddDataSource)
m.Get("/api/admin/datasources/list", auth, GetDataSources)
m.Post("/api/admin/datasources/add", auth, AddDataSource)
// user register
m.Get("/register/*_", Index)

View File

@ -1,6 +1,7 @@
package api
import (
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models"
@ -14,21 +15,40 @@ func GetDataSources(c *middleware.Context) {
c.JsonApiErr(500, "Failed to query datasources", err)
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) {
cmd := m.AddDataSourceCommand{}
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "bad request", nil)
c.JsonApiErr(400, "Validation failed", nil)
return
}
cmd.AccountId = c.Account.Id
err := bus.Dispatch(&cmd)
if err != nil {
c.JsonApiErr(500, "Failed to add datasource", err)
return
}
c.Status(204)
c.JsonOK("Datasource added")
}

View File

@ -38,6 +38,19 @@ type Collaborator struct {
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 {
model := &CurrentUser{}
if account != nil {

View File

@ -56,6 +56,14 @@ func (ctx *Context) Handle(status int, title string, err error) {
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) {
resp := make(map[string]interface{})

View File

@ -47,6 +47,9 @@ func TestDataAccess(t *testing.T) {
So(len(query.Resp), ShouldEqual, 1)
ds := query.Resp[0]
So(ds.AccountId, ShouldEqual, 10)
})
})