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"
|
2015-03-27 08:23:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-02-27 15:29:00 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-03-02 02:58:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
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-03-11 11:34:11 -05:00
|
|
|
if c.OrgId != 0 {
|
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-27 06:45:00 -06:00
|
|
|
var defaultDatasource string
|
2014-12-29 06:36:08 -06:00
|
|
|
|
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-02-27 06:45:00 -06:00
|
|
|
"type": ds.Type,
|
2015-02-28 03:52:25 -06:00
|
|
|
"name": ds.Name,
|
2015-02-27 06:45:00 -06:00
|
|
|
"url": url,
|
|
|
|
}
|
|
|
|
|
2015-02-27 15:29:00 -06:00
|
|
|
meta, exists := plugins.DataSources[ds.Type]
|
|
|
|
if !exists {
|
2015-03-27 08:23:23 -05:00
|
|
|
log.Error(3, "Could not find plugin definition for data source: %v", ds.Type)
|
|
|
|
continue
|
2015-02-27 15:29:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dsMap["meta"] = meta
|
|
|
|
|
2015-02-27 06:45:00 -06:00
|
|
|
if ds.IsDefault {
|
|
|
|
defaultDatasource = ds.Name
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2015-05-20 11:29:20 -05:00
|
|
|
if len(ds.JsonData) > 0 {
|
|
|
|
dsMap["jsonData"] = ds.JsonData
|
|
|
|
}
|
|
|
|
|
2015-03-02 02:58:35 -06:00
|
|
|
if ds.Access == m.DS_ACCESS_DIRECT {
|
|
|
|
if ds.BasicAuth {
|
|
|
|
dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ds.Type == m.DS_INFLUXDB_08 {
|
2014-12-29 06:36:08 -06:00
|
|
|
dsMap["username"] = ds.User
|
|
|
|
dsMap["password"] = ds.Password
|
|
|
|
dsMap["url"] = url + "/db/" + ds.Database
|
|
|
|
}
|
|
|
|
|
2015-03-02 02:58:35 -06:00
|
|
|
if ds.Type == m.DS_INFLUXDB {
|
2015-02-25 11:43:44 -06:00
|
|
|
dsMap["username"] = ds.User
|
|
|
|
dsMap["password"] = ds.Password
|
|
|
|
dsMap["database"] = ds.Database
|
|
|
|
dsMap["url"] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2015-02-28 02:46:37 -06:00
|
|
|
grafanaDatasourceMeta, _ := plugins.DataSources["grafana"]
|
2015-08-17 10:07:33 -05:00
|
|
|
datasources["-- Grafana --"] = map[string]interface{}{
|
2015-02-28 02:46:37 -06:00
|
|
|
"type": "grafana",
|
|
|
|
"meta": grafanaDatasourceMeta,
|
|
|
|
}
|
2015-08-17 10:07:33 -05:00
|
|
|
|
2015-08-16 13:52:30 -05:00
|
|
|
// add mixed backend data source
|
2015-08-17 10:07:33 -05:00
|
|
|
datasources["-- Mixed --"] = map[string]interface{}{
|
2015-08-16 13:52:30 -05:00
|
|
|
"type": "mixed",
|
|
|
|
"meta": plugins.DataSources["mixed"],
|
|
|
|
}
|
2015-02-28 02:46:37 -06:00
|
|
|
|
|
|
|
if defaultDatasource == "" {
|
|
|
|
defaultDatasource = "grafana"
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonObj := map[string]interface{}{
|
2015-02-27 06:45:00 -06:00
|
|
|
"defaultDatasource": defaultDatasource,
|
|
|
|
"datasources": datasources,
|
|
|
|
"appSubUrl": setting.AppSubUrl,
|
2015-06-25 10:06:43 -05:00
|
|
|
"allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
|
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)
|
|
|
|
}
|