2014-12-29 20:36:08 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 20:34:28 +08:00
|
|
|
"errors"
|
2014-12-29 20:36:08 +08:00
|
|
|
"strconv"
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-01-31 00:35:32 +08:00
|
|
|
|
2019-05-10 17:37:43 +08:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-10 13:46:14 +08:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-13 14:45:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2015-02-28 05:29:00 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-12-29 20:36:08 +08:00
|
|
|
)
|
|
|
|
|
2020-12-28 19:24:42 +08:00
|
|
|
func (hs *HTTPServer) getFSDataSources(c *models.ReqContext, enabledPlugins *plugins.EnabledPlugins) (map[string]interface{}, error) {
|
2020-03-04 19:57:20 +08:00
|
|
|
orgDataSources := make([]*models.DataSource, 0)
|
2014-12-29 20:36:08 +08:00
|
|
|
|
2015-03-12 00:34:11 +08:00
|
|
|
if c.OrgId != 0 {
|
2020-12-28 19:24:42 +08:00
|
|
|
query := models.GetDataSourcesQuery{OrgId: c.OrgId, DataSourceLimit: hs.Cfg.DataSourceLimit}
|
2014-12-29 20:36:08 +08:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
dsFilterQuery := models.DatasourcesPermissionFilterQuery{
|
2018-10-10 21:54:37 +08:00
|
|
|
User: c.SignedInUser,
|
|
|
|
Datasources: query.Result,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&dsFilterQuery); err != nil {
|
2020-11-19 20:34:28 +08:00
|
|
|
if !errors.Is(err, bus.ErrHandlerNotFound) {
|
2018-10-10 21:54:37 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
orgDataSources = query.Result
|
|
|
|
} else {
|
|
|
|
orgDataSources = dsFilterQuery.Result
|
|
|
|
}
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
2020-09-08 04:10:06 +08:00
|
|
|
dataSources := make(map[string]interface{})
|
2019-04-11 14:02:22 +08:00
|
|
|
|
2015-02-24 03:07:49 +08:00
|
|
|
for _, ds := range orgDataSources {
|
2014-12-29 20:36:08 +08:00
|
|
|
url := ds.Url
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
if ds.Access == models.DS_ACCESS_PROXY {
|
2016-09-23 18:29:53 +08:00
|
|
|
url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
2020-09-08 04:10:06 +08:00
|
|
|
dsMap := map[string]interface{}{
|
|
|
|
"id": ds.Id,
|
|
|
|
"uid": ds.Uid,
|
|
|
|
"type": ds.Type,
|
|
|
|
"name": ds.Name,
|
|
|
|
"url": url,
|
|
|
|
"isDefault": ds.IsDefault,
|
2015-02-27 20:45:00 +08:00
|
|
|
}
|
|
|
|
|
2015-12-22 18:37:44 +08:00
|
|
|
meta, exists := enabledPlugins.DataSources[ds.Type]
|
2015-02-28 05:29:00 +08:00
|
|
|
if !exists {
|
2020-07-23 14:14:39 +08:00
|
|
|
log.Errorf(3, "Could not find plugin definition for data source: %v", ds.Type)
|
2015-03-27 21:23:23 +08:00
|
|
|
continue
|
2015-02-28 05:29:00 +08:00
|
|
|
}
|
|
|
|
dsMap["meta"] = meta
|
|
|
|
|
2019-05-10 17:37:43 +08:00
|
|
|
jsonData := ds.JsonData
|
|
|
|
if jsonData == nil {
|
|
|
|
jsonData = simplejson.New()
|
2015-05-21 00:29:20 +08:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:37:43 +08:00
|
|
|
dsMap["jsonData"] = jsonData
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
if ds.Access == models.DS_ACCESS_DIRECT {
|
2015-03-02 16:58:35 +08:00
|
|
|
if ds.BasicAuth {
|
2019-04-15 17:11:17 +08:00
|
|
|
dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.DecryptedBasicAuthPassword())
|
2015-03-02 16:58:35 +08:00
|
|
|
}
|
2015-12-09 11:37:08 +08:00
|
|
|
if ds.WithCredentials {
|
|
|
|
dsMap["withCredentials"] = ds.WithCredentials
|
|
|
|
}
|
2015-03-02 16:58:35 +08:00
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
if ds.Type == models.DS_INFLUXDB_08 {
|
2014-12-29 20:36:08 +08:00
|
|
|
dsMap["username"] = ds.User
|
2019-04-15 17:11:17 +08:00
|
|
|
dsMap["password"] = ds.DecryptedPassword()
|
2014-12-29 20:36:08 +08:00
|
|
|
dsMap["url"] = url + "/db/" + ds.Database
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
if ds.Type == models.DS_INFLUXDB {
|
2015-02-26 01:43:44 +08:00
|
|
|
dsMap["username"] = ds.User
|
2019-04-15 17:11:17 +08:00
|
|
|
dsMap["password"] = ds.DecryptedPassword()
|
2015-02-26 01:43:44 +08:00
|
|
|
dsMap["url"] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
if (ds.Type == models.DS_INFLUXDB) || (ds.Type == models.DS_ES) {
|
2016-01-18 14:28:01 +08:00
|
|
|
dsMap["database"] = ds.Database
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
if ds.Type == models.DS_PROMETHEUS {
|
2015-10-02 00:01:09 +08:00
|
|
|
// add unproxied server URL for link to Prometheus web UI
|
2019-05-10 17:37:43 +08:00
|
|
|
jsonData.Set("directUrl", ds.Url)
|
2015-10-02 00:01:09 +08:00
|
|
|
}
|
|
|
|
|
2020-09-08 04:10:06 +08:00
|
|
|
dataSources[ds.Name] = dsMap
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
2020-09-08 04:10:06 +08:00
|
|
|
// add data sources that are built in (meaning they are not added via data sources page, nor have any entry in
|
|
|
|
// the datasource table)
|
2021-03-17 23:06:10 +08:00
|
|
|
for _, ds := range hs.PluginManager.DataSources() {
|
2017-04-12 14:23:44 +08:00
|
|
|
if ds.BuiltIn {
|
2020-09-08 04:10:06 +08:00
|
|
|
dataSources[ds.Name] = map[string]interface{}{
|
2017-04-12 04:47:53 +08:00
|
|
|
"type": ds.Type,
|
|
|
|
"name": ds.Name,
|
2021-03-17 23:06:10 +08:00
|
|
|
"meta": hs.PluginManager.GetDataSource(ds.Id),
|
2017-04-12 04:47:53 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-17 02:52:30 +08:00
|
|
|
}
|
2015-02-28 16:46:37 +08:00
|
|
|
|
2020-09-08 04:10:06 +08:00
|
|
|
return dataSources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFrontendSettingsMap returns a json object with all the settings needed for front end initialisation.
|
|
|
|
func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]interface{}, error) {
|
2021-03-08 14:02:49 +08:00
|
|
|
enabledPlugins, err := hs.PluginManager.GetEnabledPlugins(c.OrgId)
|
2020-09-08 04:10:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-25 17:00:21 +08:00
|
|
|
|
2020-09-08 04:10:06 +08:00
|
|
|
pluginsToPreload := []string{}
|
|
|
|
for _, app := range enabledPlugins.Apps {
|
|
|
|
if app.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, app.Module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:24:42 +08:00
|
|
|
dataSources, err := hs.getFSDataSources(c, enabledPlugins)
|
2020-09-08 04:10:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultDS := "-- Grafana --"
|
|
|
|
for n, ds := range dataSources {
|
|
|
|
dsM := ds.(map[string]interface{})
|
|
|
|
if isDefault, _ := dsM["isDefault"].(bool); isDefault {
|
|
|
|
defaultDS = n
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := dsM["meta"].(*plugins.DataSourcePlugin)
|
|
|
|
if meta.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, meta.Module)
|
|
|
|
}
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
2015-11-21 20:46:18 +08:00
|
|
|
panels := map[string]interface{}{}
|
2015-12-22 18:37:44 +08:00
|
|
|
for _, panel := range enabledPlugins.Panels {
|
2019-04-12 19:46:42 +08:00
|
|
|
if panel.State == plugins.PluginStateAlpha && !hs.Cfg.PluginsEnableAlpha {
|
2018-10-09 23:47:43 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-11 14:02:22 +08:00
|
|
|
if panel.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, panel.Module)
|
|
|
|
}
|
|
|
|
|
2016-01-09 06:15:44 +08:00
|
|
|
panels[panel.Id] = map[string]interface{}{
|
2019-05-10 13:46:14 +08:00
|
|
|
"module": panel.Module,
|
|
|
|
"baseUrl": panel.BaseUrl,
|
|
|
|
"name": panel.Name,
|
|
|
|
"id": panel.Id,
|
|
|
|
"info": panel.Info,
|
|
|
|
"hideFromList": panel.HideFromList,
|
|
|
|
"sort": getPanelSort(panel.Id),
|
|
|
|
"skipDataQuery": panel.SkipDataQuery,
|
|
|
|
"state": panel.State,
|
2020-10-23 22:45:43 +08:00
|
|
|
"signature": panel.Signature,
|
2015-11-21 20:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 13:39:50 +08:00
|
|
|
hideVersion := hs.Cfg.AnonymousHideVersion && !c.IsSignedIn
|
|
|
|
version := setting.BuildVersion
|
|
|
|
commit := setting.BuildCommit
|
|
|
|
buildstamp := setting.BuildStamp
|
|
|
|
|
|
|
|
if hideVersion {
|
|
|
|
version = ""
|
|
|
|
commit = ""
|
|
|
|
buildstamp = 0
|
|
|
|
}
|
|
|
|
|
2014-12-29 20:36:08 +08:00
|
|
|
jsonObj := map[string]interface{}{
|
2020-09-08 04:10:06 +08:00
|
|
|
"defaultDatasource": defaultDS,
|
|
|
|
"datasources": dataSources,
|
2020-02-28 21:32:01 +08:00
|
|
|
"minRefreshInterval": setting.MinRefreshInterval,
|
2018-09-06 17:20:38 +08:00
|
|
|
"panels": panels,
|
2020-12-11 18:44:44 +08:00
|
|
|
"appUrl": hs.Cfg.AppURL,
|
|
|
|
"appSubUrl": hs.Cfg.AppSubURL,
|
2018-09-06 17:20:38 +08:00
|
|
|
"allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
|
|
|
|
"authProxyEnabled": setting.AuthProxyEnabled,
|
2020-12-11 18:44:44 +08:00
|
|
|
"ldapEnabled": hs.Cfg.LDAPEnabled,
|
2018-09-06 17:20:38 +08:00
|
|
|
"alertingEnabled": setting.AlertingEnabled,
|
|
|
|
"alertingErrorOrTimeout": setting.AlertingErrorOrTimeout,
|
|
|
|
"alertingNoDataOrNullValues": setting.AlertingNoDataOrNullValues,
|
2020-01-14 17:13:34 +08:00
|
|
|
"alertingMinInterval": setting.AlertingMinInterval,
|
2020-02-17 18:13:13 +08:00
|
|
|
"autoAssignOrg": setting.AutoAssignOrg,
|
2020-07-11 14:56:02 +08:00
|
|
|
"verifyEmailEnabled": setting.VerifyEmailEnabled,
|
2020-10-08 16:03:20 +08:00
|
|
|
"sigV4AuthEnabled": setting.SigV4AuthEnabled,
|
2018-09-06 17:20:38 +08:00
|
|
|
"exploreEnabled": setting.ExploreEnabled,
|
|
|
|
"googleAnalyticsId": setting.GoogleAnalyticsId,
|
|
|
|
"disableLoginForm": setting.DisableLoginForm,
|
2019-09-16 19:38:03 +08:00
|
|
|
"disableUserSignUp": !setting.AllowUserSignUp,
|
|
|
|
"loginHint": setting.LoginHint,
|
|
|
|
"passwordHint": setting.PasswordHint,
|
2018-09-06 17:20:38 +08:00
|
|
|
"externalUserMngInfo": setting.ExternalUserMngInfo,
|
|
|
|
"externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl,
|
|
|
|
"externalUserMngLinkName": setting.ExternalUserMngLinkName,
|
2019-01-21 15:47:41 +08:00
|
|
|
"viewersCanEdit": setting.ViewersCanEdit,
|
2019-03-12 14:32:47 +08:00
|
|
|
"editorsCanAdmin": hs.Cfg.EditorsCanAdmin,
|
2019-01-22 18:56:35 +08:00
|
|
|
"disableSanitizeHtml": hs.Cfg.DisableSanitizeHtml,
|
2019-04-11 14:02:22 +08:00
|
|
|
"pluginsToPreload": pluginsToPreload,
|
2015-01-05 17:46:58 +08:00
|
|
|
"buildInfo": map[string]interface{}{
|
2020-06-17 13:39:50 +08:00
|
|
|
"hideVersion": hideVersion,
|
|
|
|
"version": version,
|
|
|
|
"commit": commit,
|
|
|
|
"buildstamp": buildstamp,
|
2020-01-27 16:24:44 +08:00
|
|
|
"edition": hs.License.Edition(),
|
2021-03-17 23:06:10 +08:00
|
|
|
"latestVersion": hs.PluginManager.GrafanaLatestVersion(),
|
|
|
|
"hasUpdate": hs.PluginManager.GrafanaHasUpdate(),
|
2016-05-17 21:00:48 +08:00
|
|
|
"env": setting.Env,
|
2019-11-01 21:56:12 +08:00
|
|
|
"isEnterprise": hs.License.HasValidLicense(),
|
|
|
|
},
|
|
|
|
"licenseInfo": map[string]interface{}{
|
2020-11-05 18:55:40 +08:00
|
|
|
"hasLicense": hs.License.HasLicense(),
|
|
|
|
"hasValidLicense": hs.License.HasValidLicense(),
|
|
|
|
"expiry": hs.License.Expiry(),
|
|
|
|
"stateInfo": hs.License.StateInfo(),
|
|
|
|
"licenseUrl": hs.License.LicenseURL(c.SignedInUser),
|
|
|
|
"edition": hs.License.Edition(),
|
2015-01-05 17:46:58 +08:00
|
|
|
},
|
2021-02-25 01:08:13 +08:00
|
|
|
"featureToggles": hs.Cfg.FeatureToggles,
|
|
|
|
"rendererAvailable": hs.RenderService.IsAvailable(),
|
|
|
|
"http2Enabled": hs.Cfg.Protocol == setting.HTTP2Scheme,
|
|
|
|
"sentry": hs.Cfg.Sentry,
|
|
|
|
"marketplaceUrl": hs.Cfg.MarketplaceURL,
|
|
|
|
"expressionsEnabled": hs.Cfg.ExpressionsEnabled,
|
|
|
|
"awsAllowedAuthProviders": hs.Cfg.AWSAllowedAuthProviders,
|
|
|
|
"awsAssumeRoleEnabled": hs.Cfg.AWSAssumeRoleEnabled,
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObj, nil
|
|
|
|
}
|
2015-02-18 21:06:44 +08:00
|
|
|
|
2016-10-26 23:42:39 +08:00
|
|
|
func getPanelSort(id string) int {
|
|
|
|
sort := 100
|
|
|
|
switch id {
|
|
|
|
case "graph":
|
|
|
|
sort = 1
|
2021-01-11 00:41:20 +08:00
|
|
|
case "timeseries":
|
2016-10-26 23:42:39 +08:00
|
|
|
sort = 2
|
2021-01-11 00:41:20 +08:00
|
|
|
case "stat":
|
2016-10-26 23:42:39 +08:00
|
|
|
sort = 3
|
2021-01-11 00:41:20 +08:00
|
|
|
case "gauge":
|
2016-10-26 23:42:39 +08:00
|
|
|
sort = 4
|
2021-01-11 00:41:20 +08:00
|
|
|
case "bargauge":
|
2016-10-26 23:42:39 +08:00
|
|
|
sort = 5
|
2021-01-11 00:41:20 +08:00
|
|
|
case "table":
|
2016-10-26 23:42:39 +08:00
|
|
|
sort = 6
|
2021-01-11 00:41:20 +08:00
|
|
|
case "singlestat":
|
2017-05-03 14:56:51 +08:00
|
|
|
sort = 7
|
2021-03-25 15:33:13 +08:00
|
|
|
case "piechart":
|
2018-12-18 00:54:40 +08:00
|
|
|
sort = 8
|
2021-03-25 15:33:13 +08:00
|
|
|
case "text":
|
2019-02-16 04:33:36 +08:00
|
|
|
sort = 9
|
2021-03-25 15:33:13 +08:00
|
|
|
case "heatmap":
|
2020-01-17 16:43:17 +08:00
|
|
|
sort = 10
|
2021-03-25 15:33:13 +08:00
|
|
|
case "alertlist":
|
2021-01-11 00:41:20 +08:00
|
|
|
sort = 11
|
2021-03-25 15:33:13 +08:00
|
|
|
case "dashlist":
|
2021-01-11 00:41:20 +08:00
|
|
|
sort = 12
|
2021-03-25 15:33:13 +08:00
|
|
|
case "news":
|
|
|
|
sort = 13
|
2016-10-26 23:42:39 +08:00
|
|
|
}
|
|
|
|
return sort
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:57:20 +08:00
|
|
|
func (hs *HTTPServer) GetFrontendSettings(c *models.ReqContext) {
|
2018-10-09 23:47:43 +08:00
|
|
|
settings, err := hs.getFrontendSettingsMap(c)
|
2015-02-18 21:06:44 +08:00
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(400, "Failed to get frontend settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, settings)
|
|
|
|
}
|