2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2014-12-29 06:36:08 -06:00
|
|
|
"strconv"
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-09-10 09:44:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/grafanads"
|
2020-01-30 10:35:32 -06:00
|
|
|
|
2019-05-10 04:37:43 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-10 00:46:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
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"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
2020-12-28 05:24:42 -06:00
|
|
|
func (hs *HTTPServer) getFSDataSources(c *models.ReqContext, enabledPlugins *plugins.EnabledPlugins) (map[string]interface{}, error) {
|
2020-03-04 05:57:20 -06:00
|
|
|
orgDataSources := make([]*models.DataSource, 0)
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2015-03-11 11:34:11 -05:00
|
|
|
if c.OrgId != 0 {
|
2020-12-28 05:24:42 -06:00
|
|
|
query := models.GetDataSourcesQuery{OrgId: c.OrgId, DataSourceLimit: hs.Cfg.DataSourceLimit}
|
2014-12-29 06:36:08 -06:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
dsFilterQuery := models.DatasourcesPermissionFilterQuery{
|
2018-10-10 08:54:37 -05:00
|
|
|
User: c.SignedInUser,
|
|
|
|
Datasources: query.Result,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&dsFilterQuery); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if !errors.Is(err, bus.ErrHandlerNotFound) {
|
2018-10-10 08:54:37 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
orgDataSources = query.Result
|
|
|
|
} else {
|
|
|
|
orgDataSources = dsFilterQuery.Result
|
|
|
|
}
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:10:06 -05:00
|
|
|
dataSources := make(map[string]interface{})
|
2019-04-11 01:02:22 -05:00
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
for _, ds := range orgDataSources {
|
2014-12-29 06:36:08 -06:00
|
|
|
url := ds.Url
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Access == models.DS_ACCESS_PROXY {
|
2016-09-23 05:29:53 -05:00
|
|
|
url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:10:06 -05:00
|
|
|
dsMap := map[string]interface{}{
|
|
|
|
"id": ds.Id,
|
|
|
|
"uid": ds.Uid,
|
|
|
|
"type": ds.Type,
|
|
|
|
"name": ds.Name,
|
|
|
|
"url": url,
|
|
|
|
"isDefault": ds.IsDefault,
|
2021-08-06 02:24:35 -05:00
|
|
|
"access": ds.Access,
|
2015-02-27 06:45:00 -06:00
|
|
|
}
|
|
|
|
|
2015-12-22 04:37:44 -06:00
|
|
|
meta, exists := enabledPlugins.DataSources[ds.Type]
|
2015-02-27 15:29:00 -06:00
|
|
|
if !exists {
|
2020-07-23 01:14:39 -05:00
|
|
|
log.Errorf(3, "Could not find plugin definition for data source: %v", ds.Type)
|
2015-03-27 08:23:23 -05:00
|
|
|
continue
|
2015-02-27 15:29:00 -06:00
|
|
|
}
|
|
|
|
dsMap["meta"] = meta
|
|
|
|
|
2019-05-10 04:37:43 -05:00
|
|
|
jsonData := ds.JsonData
|
|
|
|
if jsonData == nil {
|
|
|
|
jsonData = simplejson.New()
|
2015-05-20 11:29:20 -05:00
|
|
|
}
|
|
|
|
|
2019-05-10 04:37:43 -05:00
|
|
|
dsMap["jsonData"] = jsonData
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Access == models.DS_ACCESS_DIRECT {
|
2015-03-02 02:58:35 -06:00
|
|
|
if ds.BasicAuth {
|
2019-04-15 04:11:17 -05:00
|
|
|
dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.DecryptedBasicAuthPassword())
|
2015-03-02 02:58:35 -06:00
|
|
|
}
|
2015-12-08 21:37:08 -06:00
|
|
|
if ds.WithCredentials {
|
|
|
|
dsMap["withCredentials"] = ds.WithCredentials
|
|
|
|
}
|
2015-03-02 02:58:35 -06:00
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Type == models.DS_INFLUXDB_08 {
|
2014-12-29 06:36:08 -06:00
|
|
|
dsMap["username"] = ds.User
|
2019-04-15 04:11:17 -05:00
|
|
|
dsMap["password"] = ds.DecryptedPassword()
|
2014-12-29 06:36:08 -06:00
|
|
|
dsMap["url"] = url + "/db/" + ds.Database
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Type == models.DS_INFLUXDB {
|
2015-02-25 11:43:44 -06:00
|
|
|
dsMap["username"] = ds.User
|
2019-04-15 04:11:17 -05:00
|
|
|
dsMap["password"] = ds.DecryptedPassword()
|
2015-02-25 11:43:44 -06:00
|
|
|
dsMap["url"] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if (ds.Type == models.DS_INFLUXDB) || (ds.Type == models.DS_ES) {
|
2016-01-18 00:28:01 -06:00
|
|
|
dsMap["database"] = ds.Database
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Type == models.DS_PROMETHEUS {
|
2015-10-01 11:01:09 -05:00
|
|
|
// add unproxied server URL for link to Prometheus web UI
|
2019-05-10 04:37:43 -05:00
|
|
|
jsonData.Set("directUrl", ds.Url)
|
2015-10-01 11:01:09 -05:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:10:06 -05:00
|
|
|
dataSources[ds.Name] = dsMap
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:10:06 -05: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 10:06:10 -05:00
|
|
|
for _, ds := range hs.PluginManager.DataSources() {
|
2017-04-12 01:23:44 -05:00
|
|
|
if ds.BuiltIn {
|
2021-09-10 09:44:47 -05:00
|
|
|
info := map[string]interface{}{
|
2017-04-11 15:47:53 -05:00
|
|
|
"type": ds.Type,
|
|
|
|
"name": ds.Name,
|
2021-03-17 10:06:10 -05:00
|
|
|
"meta": hs.PluginManager.GetDataSource(ds.Id),
|
2017-04-11 15:47:53 -05:00
|
|
|
}
|
2021-09-10 09:44:47 -05:00
|
|
|
if ds.Name == grafanads.DatasourceName {
|
|
|
|
info["id"] = grafanads.DatasourceID
|
|
|
|
info["uid"] = grafanads.DatasourceUID
|
|
|
|
}
|
|
|
|
dataSources[ds.Name] = info
|
2017-04-11 15:47:53 -05:00
|
|
|
}
|
2015-08-16 13:52:30 -05:00
|
|
|
}
|
2015-02-28 02:46:37 -06:00
|
|
|
|
2020-09-07 15:10:06 -05: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 00:02:49 -06:00
|
|
|
enabledPlugins, err := hs.PluginManager.GetEnabledPlugins(c.OrgId)
|
2020-09-07 15:10:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-25 03:00:21 -06:00
|
|
|
|
2020-09-07 15:10:06 -05:00
|
|
|
pluginsToPreload := []string{}
|
|
|
|
for _, app := range enabledPlugins.Apps {
|
|
|
|
if app.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, app.Module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:24:42 -06:00
|
|
|
dataSources, err := hs.getFSDataSources(c, enabledPlugins)
|
2020-09-07 15:10:06 -05: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 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2015-11-21 06:46:18 -06:00
|
|
|
panels := map[string]interface{}{}
|
2015-12-22 04:37:44 -06:00
|
|
|
for _, panel := range enabledPlugins.Panels {
|
2019-04-12 06:46:42 -05:00
|
|
|
if panel.State == plugins.PluginStateAlpha && !hs.Cfg.PluginsEnableAlpha {
|
2018-10-09 10:47:43 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-11 01:02:22 -05:00
|
|
|
if panel.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, panel.Module)
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:15:44 -06:00
|
|
|
panels[panel.Id] = map[string]interface{}{
|
2019-05-10 00:46:14 -05: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 09:45:43 -05:00
|
|
|
"signature": panel.Signature,
|
2015-11-21 06:46:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 00:39:50 -05: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 06:36:08 -06:00
|
|
|
jsonObj := map[string]interface{}{
|
2021-09-15 06:00:38 -05:00
|
|
|
"defaultDatasource": defaultDS,
|
|
|
|
"datasources": dataSources,
|
|
|
|
"minRefreshInterval": setting.MinRefreshInterval,
|
|
|
|
"panels": panels,
|
|
|
|
"appUrl": hs.Cfg.AppURL,
|
|
|
|
"appSubUrl": hs.Cfg.AppSubURL,
|
|
|
|
"allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
|
|
|
|
"authProxyEnabled": setting.AuthProxyEnabled,
|
|
|
|
"ldapEnabled": hs.Cfg.LDAPEnabled,
|
|
|
|
"alertingEnabled": setting.AlertingEnabled,
|
|
|
|
"alertingErrorOrTimeout": setting.AlertingErrorOrTimeout,
|
|
|
|
"alertingNoDataOrNullValues": setting.AlertingNoDataOrNullValues,
|
|
|
|
"alertingMinInterval": setting.AlertingMinInterval,
|
|
|
|
"liveEnabled": hs.Cfg.LiveMaxConnections != 0,
|
|
|
|
"autoAssignOrg": setting.AutoAssignOrg,
|
|
|
|
"verifyEmailEnabled": setting.VerifyEmailEnabled,
|
|
|
|
"sigV4AuthEnabled": setting.SigV4AuthEnabled,
|
|
|
|
"exploreEnabled": setting.ExploreEnabled,
|
|
|
|
"googleAnalyticsId": setting.GoogleAnalyticsId,
|
|
|
|
"rudderstackWriteKey": setting.RudderstackWriteKey,
|
|
|
|
"rudderstackDataPlaneUrl": setting.RudderstackDataPlaneUrl,
|
|
|
|
"applicationInsightsConnectionString": hs.Cfg.ApplicationInsightsConnectionString,
|
|
|
|
"applicationInsightsEndpointUrl": hs.Cfg.ApplicationInsightsEndpointUrl,
|
|
|
|
"disableLoginForm": setting.DisableLoginForm,
|
|
|
|
"disableUserSignUp": !setting.AllowUserSignUp,
|
|
|
|
"loginHint": setting.LoginHint,
|
|
|
|
"passwordHint": setting.PasswordHint,
|
|
|
|
"externalUserMngInfo": setting.ExternalUserMngInfo,
|
|
|
|
"externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl,
|
|
|
|
"externalUserMngLinkName": setting.ExternalUserMngLinkName,
|
|
|
|
"viewersCanEdit": setting.ViewersCanEdit,
|
|
|
|
"editorsCanAdmin": hs.Cfg.EditorsCanAdmin,
|
|
|
|
"disableSanitizeHtml": hs.Cfg.DisableSanitizeHtml,
|
|
|
|
"pluginsToPreload": pluginsToPreload,
|
2015-01-05 03:46:58 -06:00
|
|
|
"buildInfo": map[string]interface{}{
|
2020-06-17 00:39:50 -05:00
|
|
|
"hideVersion": hideVersion,
|
|
|
|
"version": version,
|
|
|
|
"commit": commit,
|
|
|
|
"buildstamp": buildstamp,
|
2020-01-27 02:24:44 -06:00
|
|
|
"edition": hs.License.Edition(),
|
2021-03-17 10:06:10 -05:00
|
|
|
"latestVersion": hs.PluginManager.GrafanaLatestVersion(),
|
|
|
|
"hasUpdate": hs.PluginManager.GrafanaHasUpdate(),
|
2016-05-17 08:00:48 -05:00
|
|
|
"env": setting.Env,
|
2019-11-01 08:56:12 -05:00
|
|
|
"isEnterprise": hs.License.HasValidLicense(),
|
|
|
|
},
|
|
|
|
"licenseInfo": map[string]interface{}{
|
2020-11-05 04:55:40 -06: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 03:46:58 -06:00
|
|
|
},
|
2021-05-27 05:45:06 -05:00
|
|
|
"featureToggles": hs.Cfg.FeatureToggles,
|
|
|
|
"rendererAvailable": hs.RenderService.IsAvailable(),
|
2021-06-04 06:33:49 -05:00
|
|
|
"rendererVersion": hs.RenderService.Version(),
|
2021-05-27 05:45:06 -05:00
|
|
|
"http2Enabled": hs.Cfg.Protocol == setting.HTTP2Scheme,
|
|
|
|
"sentry": hs.Cfg.Sentry,
|
|
|
|
"pluginCatalogURL": hs.Cfg.PluginCatalogURL,
|
2021-08-04 04:49:05 -05:00
|
|
|
"pluginAdminEnabled": hs.Cfg.PluginAdminEnabled,
|
2021-05-31 05:30:32 -05:00
|
|
|
"pluginAdminExternalManageEnabled": hs.Cfg.PluginAdminEnabled && hs.Cfg.PluginAdminExternalManageEnabled,
|
2021-05-27 05:45:06 -05:00
|
|
|
"expressionsEnabled": hs.Cfg.ExpressionsEnabled,
|
|
|
|
"awsAllowedAuthProviders": hs.Cfg.AWSAllowedAuthProviders,
|
|
|
|
"awsAssumeRoleEnabled": hs.Cfg.AWSAssumeRoleEnabled,
|
2021-05-12 09:23:37 -05:00
|
|
|
"azure": map[string]interface{}{
|
|
|
|
"cloud": hs.Cfg.Azure.Cloud,
|
|
|
|
"managedIdentityEnabled": hs.Cfg.Azure.ManagedIdentityEnabled,
|
|
|
|
},
|
2021-05-26 12:27:28 -05:00
|
|
|
"caching": map[string]bool{
|
|
|
|
"enabled": hs.Cfg.SectionWithEnvOverrides("caching").Key("enabled").MustBool(true),
|
|
|
|
},
|
2021-09-29 09:16:40 -05:00
|
|
|
"unifiedAlertingEnabled": hs.Cfg.UnifiedAlerting.Enabled,
|
2021-07-21 15:48:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if hs.Cfg.GeomapDefaultBaseLayerConfig != nil {
|
|
|
|
jsonObj["geomapDefaultBaseLayerConfig"] = hs.Cfg.GeomapDefaultBaseLayerConfig
|
|
|
|
}
|
|
|
|
if !hs.Cfg.GeomapEnableCustomBaseLayers {
|
|
|
|
jsonObj["geomapDisableCustomBaseLayer"] = true
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObj, nil
|
|
|
|
}
|
2015-02-18 07:06:44 -06:00
|
|
|
|
2016-10-26 10:42:39 -05:00
|
|
|
func getPanelSort(id string) int {
|
|
|
|
sort := 100
|
|
|
|
switch id {
|
2021-01-10 10:41:20 -06:00
|
|
|
case "timeseries":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 1
|
2021-05-03 09:49:09 -05:00
|
|
|
case "barchart":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 2
|
2021-05-03 09:49:09 -05:00
|
|
|
case "stat":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 3
|
2021-05-03 09:49:09 -05:00
|
|
|
case "gauge":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 4
|
2021-05-03 09:49:09 -05:00
|
|
|
case "bargauge":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 5
|
2021-05-03 09:49:09 -05:00
|
|
|
case "table":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 6
|
2021-05-03 09:49:09 -05:00
|
|
|
case "singlestat":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 7
|
2021-05-03 09:49:09 -05:00
|
|
|
case "piechart":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 8
|
2021-05-17 15:00:04 -05:00
|
|
|
case "state-timeline":
|
2019-02-15 14:33:36 -06:00
|
|
|
sort = 9
|
2021-03-25 02:33:13 -05:00
|
|
|
case "heatmap":
|
2020-01-17 02:43:17 -06:00
|
|
|
sort = 10
|
2021-05-29 13:24:14 -05:00
|
|
|
case "status-history":
|
2021-01-10 10:41:20 -06:00
|
|
|
sort = 11
|
2021-05-20 10:36:20 -05:00
|
|
|
case "histogram":
|
2021-01-10 10:41:20 -06:00
|
|
|
sort = 12
|
2021-05-20 10:36:20 -05:00
|
|
|
case "graph":
|
2021-03-25 02:33:13 -05:00
|
|
|
sort = 13
|
2021-05-20 10:36:20 -05:00
|
|
|
case "text":
|
2021-05-03 09:49:09 -05:00
|
|
|
sort = 14
|
2021-05-20 10:36:20 -05:00
|
|
|
case "alertlist":
|
2021-05-05 06:19:14 -05:00
|
|
|
sort = 15
|
2021-05-20 10:36:20 -05:00
|
|
|
case "dashlist":
|
2021-05-17 15:00:04 -05:00
|
|
|
sort = 16
|
2021-05-20 10:36:20 -05:00
|
|
|
case "news":
|
|
|
|
sort = 17
|
2016-10-26 10:42:39 -05:00
|
|
|
}
|
|
|
|
return sort
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func (hs *HTTPServer) GetFrontendSettings(c *models.ReqContext) {
|
2018-10-09 10:47:43 -05:00
|
|
|
settings, err := hs.getFrontendSettingsMap(c)
|
2015-02-18 07:06:44 -06:00
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(400, "Failed to get frontend settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, settings)
|
|
|
|
}
|