Commit a7c816c6 by Torkel Ödegaard

Datasource proxy, switch to lookup by id

parent a2a0e039
......@@ -33,7 +33,7 @@ func Register(m *macaron.Macaron) {
m.Delete("/api/admin/datasources/:id", auth, DeleteDataSource)
// data source proxy
m.Any("/api/datasources/proxy/:name/*", auth, ProxyDataSourceRequest)
m.Any("/api/datasources/proxy/:id/*", auth, ProxyDataSourceRequest)
// user register
m.Get("/register/*_", Index)
......
......@@ -2,6 +2,7 @@ package api
import (
"encoding/json"
"strconv"
"strings"
"github.com/torkelo/grafana-pro/pkg/bus"
......@@ -27,7 +28,7 @@ func renderConfig(data *configJsTmplModel) string {
for _, ds := range data.DataSources {
url := ds.Url
if ds.Access == m.DS_ACCESS_PROXY {
url = "/api/datasources/proxy/" + ds.Name
url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
}
datasources[ds.Name] = map[string]interface{}{
"type": ds.Type,
......
......@@ -7,7 +7,6 @@ import (
"strings"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models"
)
......@@ -36,18 +35,17 @@ func NewReverseProxy(target *url.URL, proxyPath string) *httputil.ReverseProxy {
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
log.Info("Proxy: %v", req.URL.Path)
}
return &httputil.ReverseProxy{Director: director}
}
// TODO: need to cache datasources
func ProxyDataSourceRequest(c *middleware.Context) {
name := c.Params(":name")
id := c.ParamsInt64(":id")
query := m.GetDataSourceByNameQuery{
Name: name,
query := m.GetDataSourceByIdQuery{
Id: id,
AccountId: c.GetAccountId(),
}
......
......@@ -42,8 +42,8 @@ type GetDataSourcesQuery struct {
Result []*DataSource
}
type GetDataSourceByNameQuery struct {
Name string
type GetDataSourceByIdQuery struct {
Id int64
AccountId int64
Result DataSource
}
......
......@@ -14,11 +14,11 @@ func init() {
bus.AddHandler("sql", AddDataSource)
bus.AddHandler("sql", DeleteDataSource)
bus.AddHandler("sql", UpdateDataSource)
bus.AddHandler("sql", GetDataSourceByName)
bus.AddHandler("sql", GetDataSourceById)
}
func GetDataSourceByName(query *m.GetDataSourceByNameQuery) error {
sess := x.Limit(100, 0).Where("account_id=? AND name=?", query.AccountId, query.Name)
func GetDataSourceById(query *m.GetDataSourceByIdQuery) error {
sess := x.Limit(100, 0).Where("account_id=? AND id=?", query.AccountId, query.Id)
has, err := sess.Get(&query.Result)
if !has {
......
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