2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
2015-02-18 07:06:44 -06:00
|
|
|
func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, error) {
|
2015-02-23 13:07:49 -06:00
|
|
|
orgDataSources := make([]*m.DataSource, 0)
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2015-01-16 07:32:18 -06:00
|
|
|
if c.IsSignedIn {
|
2015-02-23 13:07:49 -06:00
|
|
|
query := m.GetDataSourcesQuery{OrgId: c.OrgId}
|
2014-12-29 06:36:08 -06:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
orgDataSources = query.Result
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
datasources := make(map[string]interface{})
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
for _, ds := range orgDataSources {
|
2014-12-29 06:36:08 -06:00
|
|
|
url := ds.Url
|
|
|
|
|
|
|
|
if ds.Access == m.DS_ACCESS_PROXY {
|
2015-01-05 00:59:18 -06:00
|
|
|
url = setting.AppSubUrl + "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var dsMap = map[string]interface{}{
|
2015-01-09 09:36:23 -06:00
|
|
|
"type": ds.Type,
|
|
|
|
"url": url,
|
|
|
|
"default": ds.IsDefault,
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ds.Type == m.DS_INFLUXDB {
|
|
|
|
if ds.Access == m.DS_ACCESS_DIRECT {
|
|
|
|
dsMap["username"] = ds.User
|
|
|
|
dsMap["password"] = ds.Password
|
|
|
|
dsMap["url"] = url + "/db/" + ds.Database
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 03:39:26 -06:00
|
|
|
if ds.Type == m.DS_ES {
|
|
|
|
dsMap["index"] = ds.Database
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:36:08 -06:00
|
|
|
datasources[ds.Name] = dsMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// add grafana backend data source
|
|
|
|
datasources["grafana"] = map[string]interface{}{
|
|
|
|
"type": "grafana",
|
|
|
|
"grafanaDB": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonObj := map[string]interface{}{
|
|
|
|
"datasources": datasources,
|
2015-01-05 00:59:18 -06:00
|
|
|
"appSubUrl": setting.AppSubUrl,
|
2015-01-05 03:46:58 -06:00
|
|
|
"buildInfo": map[string]interface{}{
|
|
|
|
"version": setting.BuildVersion,
|
|
|
|
"commit": setting.BuildCommit,
|
|
|
|
"buildstamp": setting.BuildStamp,
|
|
|
|
},
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObj, nil
|
|
|
|
}
|
2015-02-18 07:06:44 -06:00
|
|
|
|
|
|
|
func GetFrontendSettings(c *middleware.Context) {
|
|
|
|
settings, err := getFrontendSettingsMap(c)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(400, "Failed to get frontend settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, settings)
|
|
|
|
}
|