Przeglądaj źródła

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

Torkel Ödegaard 11 lat temu
rodzic
commit
adf4e72cf8

+ 1 - 1
grafana

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

+ 2 - 2
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)

+ 22 - 2
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")
 }

+ 13 - 0
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 {

+ 8 - 0
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{})
 

+ 3 - 0
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)
 		})
 
 	})