diff --git a/grafana b/grafana index adb1502e728..0e970307160 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit adb1502e728e5a312a6e4dc680edbd1f75219ea1 +Subproject commit 0e970307160dfed9b09f6d1bf06dd49ff38035b7 diff --git a/pkg/api/api.go b/pkg/api/api.go index c948ff044f2..b4bbb7dbc1c 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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) diff --git a/pkg/api/api_datasources.go b/pkg/api/api_datasources.go index 73b0658bc21..1819da86e83 100644 --- a/pkg/api/api_datasources.go +++ b/pkg/api/api_datasources.go @@ -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") } diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index 980e9766344..f39c00e6aef 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -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 { diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 3d82e098423..d2db4bc7722 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -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{}) diff --git a/pkg/stores/sqlstore/sqlstore_test.go b/pkg/stores/sqlstore/sqlstore_test.go index aa9f02b2f33..cc1f5b8bbf5 100644 --- a/pkg/stores/sqlstore/sqlstore_test.go +++ b/pkg/stores/sqlstore/sqlstore_test.go @@ -47,6 +47,9 @@ func TestDataAccess(t *testing.T) { So(len(query.Resp), ShouldEqual, 1) + ds := query.Resp[0] + + So(ds.AccountId, ShouldEqual, 10) }) })