grafana/pkg/api/frontendsettings.go

154 lines
3.5 KiB
Go
Raw Normal View History

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"
"github.com/grafana/grafana/pkg/plugins"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, error) {
orgDataSources := make([]*m.DataSource, 0)
if c.OrgId != 0 {
query := m.GetDataSourcesQuery{OrgId: c.OrgId}
err := bus.Dispatch(&query)
if err != nil {
return nil, err
}
orgDataSources = query.Result
}
datasources := make(map[string]interface{})
2015-02-27 06:45:00 -06:00
var defaultDatasource string
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
err := bus.Dispatch(&orgApps)
if err != nil {
return nil, err
}
2015-12-22 04:37:44 -06:00
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
for _, ds := range orgDataSources {
url := ds.Url
if ds.Access == m.DS_ACCESS_PROXY {
url = setting.AppSubUrl + "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
}
var dsMap = map[string]interface{}{
2015-02-27 06:45:00 -06:00
"type": ds.Type,
"name": ds.Name,
2015-02-27 06:45:00 -06:00
"url": url,
}
2015-12-22 04:37:44 -06:00
meta, exists := enabledPlugins.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
}
dsMap["meta"] = meta
2015-02-27 06:45:00 -06:00
if ds.IsDefault {
defaultDatasource = ds.Name
}
if len(ds.JsonData) > 0 {
dsMap["jsonData"] = ds.JsonData
}
if ds.Access == m.DS_ACCESS_DIRECT {
if ds.BasicAuth {
dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword)
}
if ds.WithCredentials {
dsMap["withCredentials"] = ds.WithCredentials
}
if ds.Type == m.DS_INFLUXDB_08 {
dsMap["username"] = ds.User
dsMap["password"] = ds.Password
dsMap["url"] = url + "/db/" + ds.Database
}
if ds.Type == m.DS_INFLUXDB {
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
}
if ds.Type == m.DS_PROMETHEUS {
// add unproxied server URL for link to Prometheus web UI
dsMap["directUrl"] = ds.Url
}
datasources[ds.Name] = dsMap
}
// add grafana backend data source
grafanaDatasourceMeta, _ := plugins.DataSources["grafana"]
datasources["-- Grafana --"] = map[string]interface{}{
"type": "grafana",
"meta": grafanaDatasourceMeta,
}
// add mixed backend data source
datasources["-- Mixed --"] = map[string]interface{}{
"type": "mixed",
"meta": plugins.DataSources["mixed"],
}
if defaultDatasource == "" {
defaultDatasource = "-- Grafana --"
}
panels := map[string]interface{}{}
2015-12-22 04:37:44 -06:00
for _, panel := range enabledPlugins.Panels {
panels[panel.Type] = map[string]interface{}{
"module": panel.Module,
"name": panel.Name,
}
}
jsonObj := map[string]interface{}{
2015-02-27 06:45:00 -06:00
"defaultDatasource": defaultDatasource,
"datasources": datasources,
"panels": panels,
2015-02-27 06:45:00 -06:00
"appSubUrl": setting.AppSubUrl,
"allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
"authProxyEnabled": setting.AuthProxyEnabled,
"buildInfo": map[string]interface{}{
"version": setting.BuildVersion,
"commit": setting.BuildCommit,
"buildstamp": setting.BuildStamp,
},
}
return jsonObj, nil
}
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)
}