From a7c816c65e7decf7127d3ee97e071275321d56bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 19 Dec 2014 07:27:25 +0100 Subject: [PATCH] Datasource proxy, switch to lookup by id --- pkg/api/api.go | 2 +- pkg/api/api_config.go | 3 ++- pkg/api/api_dataproxy.go | 10 ++++------ pkg/models/datasource.go | 4 ++-- pkg/stores/sqlstore/sqlstore_datasource.go | 6 +++--- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index eaae0045682..f83efca3558 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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) diff --git a/pkg/api/api_config.go b/pkg/api/api_config.go index a88e3de27dd..c42d3efc0eb 100644 --- a/pkg/api/api_config.go +++ b/pkg/api/api_config.go @@ -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, diff --git a/pkg/api/api_dataproxy.go b/pkg/api/api_dataproxy.go index c97341b3f4c..62cb9a08d9e 100644 --- a/pkg/api/api_dataproxy.go +++ b/pkg/api/api_dataproxy.go @@ -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(), } diff --git a/pkg/models/datasource.go b/pkg/models/datasource.go index 3baf81af8ba..909250450b9 100644 --- a/pkg/models/datasource.go +++ b/pkg/models/datasource.go @@ -42,8 +42,8 @@ type GetDataSourcesQuery struct { Result []*DataSource } -type GetDataSourceByNameQuery struct { - Name string +type GetDataSourceByIdQuery struct { + Id int64 AccountId int64 Result DataSource } diff --git a/pkg/stores/sqlstore/sqlstore_datasource.go b/pkg/stores/sqlstore/sqlstore_datasource.go index adea310366a..71c6ea901eb 100644 --- a/pkg/stores/sqlstore/sqlstore_datasource.go +++ b/pkg/stores/sqlstore/sqlstore_datasource.go @@ -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 {