2016-01-10 14:37:11 -06:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-02-28 05:51:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-01-10 14:37:11 -06:00
|
|
|
)
|
|
|
|
|
2020-02-28 05:51:21 -06:00
|
|
|
func GetPluginSettings(orgId int64) (map[string]*models.PluginSettingInfoDTO, error) {
|
|
|
|
query := models.GetPluginSettingsQuery{OrgId: orgId}
|
2016-01-10 14:37:11 -06:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-28 05:51:21 -06:00
|
|
|
pluginMap := make(map[string]*models.PluginSettingInfoDTO)
|
2016-02-25 07:55:31 -06:00
|
|
|
for _, plug := range query.Result {
|
|
|
|
pluginMap[plug.PluginId] = plug
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
|
2016-03-08 11:17:47 -06:00
|
|
|
for _, pluginDef := range Plugins {
|
|
|
|
// ignore entries that exists
|
|
|
|
if _, ok := pluginMap[pluginDef.Id]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// default to enabled true
|
2020-02-28 05:51:21 -06:00
|
|
|
opt := &models.PluginSettingInfoDTO{
|
2016-05-17 14:51:19 -05:00
|
|
|
PluginId: pluginDef.Id,
|
|
|
|
OrgId: orgId,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// apps are disabled by default
|
|
|
|
if pluginDef.Type == PluginTypeApp {
|
|
|
|
opt.Enabled = false
|
|
|
|
}
|
2016-03-08 11:17:47 -06:00
|
|
|
|
|
|
|
// if it's included in app check app settings
|
|
|
|
if pluginDef.IncludedInAppId != "" {
|
2018-04-13 11:40:14 -05:00
|
|
|
// app components are by default disabled
|
2016-03-08 11:17:47 -06:00
|
|
|
opt.Enabled = false
|
|
|
|
|
|
|
|
if appSettings, ok := pluginMap[pluginDef.IncludedInAppId]; ok {
|
|
|
|
opt.Enabled = appSettings.Enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginMap[pluginDef.Id] = opt
|
|
|
|
}
|
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
return pluginMap, nil
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetEnabledPlugins(orgId int64) (*EnabledPlugins, error) {
|
|
|
|
enabledPlugins := NewEnabledPlugins()
|
2016-03-08 11:17:47 -06:00
|
|
|
pluginSettingMap, err := GetPluginSettings(orgId)
|
2016-01-10 14:37:11 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-08 11:17:47 -06:00
|
|
|
isPluginEnabled := func(pluginId string) bool {
|
|
|
|
_, ok := pluginSettingMap[pluginId]
|
|
|
|
return ok
|
|
|
|
}
|
2016-01-10 14:37:11 -06:00
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
for pluginId, app := range Apps {
|
2016-03-08 11:17:47 -06:00
|
|
|
if b, ok := pluginSettingMap[pluginId]; ok {
|
2016-01-10 14:37:11 -06:00
|
|
|
app.Pinned = b.Pinned
|
2016-02-25 07:55:31 -06:00
|
|
|
enabledPlugins.Apps = append(enabledPlugins.Apps, app)
|
2016-01-11 11:03:08 -06:00
|
|
|
}
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// add all plugins that are not part of an App.
|
2016-01-19 11:18:53 -06:00
|
|
|
for dsId, ds := range DataSources {
|
2016-03-08 11:17:47 -06:00
|
|
|
if isPluginEnabled(ds.Id) {
|
2016-01-19 11:18:53 -06:00
|
|
|
enabledPlugins.DataSources[dsId] = ds
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 11:18:53 -06:00
|
|
|
for _, panel := range Panels {
|
2016-03-08 11:17:47 -06:00
|
|
|
if isPluginEnabled(panel.Id) {
|
2016-01-19 11:18:53 -06:00
|
|
|
enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &enabledPlugins, nil
|
|
|
|
}
|
2020-06-22 10:49:13 -05:00
|
|
|
|
|
|
|
// IsAppInstalled checks if an app plugin with provided plugin ID is installed.
|
|
|
|
func IsAppInstalled(pluginID string) bool {
|
|
|
|
_, exists := Apps[pluginID]
|
|
|
|
return exists
|
|
|
|
}
|