mirror of https://github.com/grafana/grafana
feat(apps): lots of more work on apps, changed app_plugin to app_settings in order to to confuse the app plugin model (definition) and app org settings
parent
ab79348af5
commit
c1e94e61d0
@ -1,63 +0,0 @@ |
|||||||
package api |
|
||||||
|
|
||||||
import ( |
|
||||||
"github.com/grafana/grafana/pkg/api/dtos" |
|
||||||
"github.com/grafana/grafana/pkg/bus" |
|
||||||
"github.com/grafana/grafana/pkg/middleware" |
|
||||||
m "github.com/grafana/grafana/pkg/models" |
|
||||||
"github.com/grafana/grafana/pkg/plugins" |
|
||||||
) |
|
||||||
|
|
||||||
func GetAppPlugins(c *middleware.Context) Response { |
|
||||||
query := m.GetAppPluginsQuery{OrgId: c.OrgId} |
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil { |
|
||||||
return ApiError(500, "Failed to list Plugin Bundles", err) |
|
||||||
} |
|
||||||
|
|
||||||
translateToDto := func(app *plugins.AppPlugin) *dtos.AppPlugin { |
|
||||||
return &dtos.AppPlugin{ |
|
||||||
Name: app.Name, |
|
||||||
Type: app.Type, |
|
||||||
Enabled: app.Enabled, |
|
||||||
Pinned: app.Pinned, |
|
||||||
Module: app.Module, |
|
||||||
Info: &app.Info, |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
seenApps := make(map[string]bool) |
|
||||||
result := make([]*dtos.AppPlugin, 0) |
|
||||||
for _, orgApp := range query.Result { |
|
||||||
if def, ok := plugins.Apps[orgApp.Type]; ok { |
|
||||||
pluginDto := translateToDto(def) |
|
||||||
pluginDto.Enabled = orgApp.Enabled |
|
||||||
pluginDto.JsonData = orgApp.JsonData |
|
||||||
result = append(result, pluginDto) |
|
||||||
seenApps[orgApp.Type] = true |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
for _, app := range plugins.Apps { |
|
||||||
if _, ok := seenApps[app.Type]; !ok { |
|
||||||
result = append(result, translateToDto(app)) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return Json(200, result) |
|
||||||
} |
|
||||||
|
|
||||||
func UpdateAppPlugin(c *middleware.Context, cmd m.UpdateAppPluginCmd) Response { |
|
||||||
cmd.OrgId = c.OrgId |
|
||||||
|
|
||||||
if _, ok := plugins.Apps[cmd.Type]; !ok { |
|
||||||
return ApiError(404, "App type not installed.", nil) |
|
||||||
} |
|
||||||
|
|
||||||
err := bus.Dispatch(&cmd) |
|
||||||
if err != nil { |
|
||||||
return ApiError(500, "Failed to update App Plugin", err) |
|
||||||
} |
|
||||||
|
|
||||||
return ApiSuccess("App updated") |
|
||||||
} |
|
@ -0,0 +1,59 @@ |
|||||||
|
package api |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/grafana/grafana/pkg/api/dtos" |
||||||
|
"github.com/grafana/grafana/pkg/bus" |
||||||
|
"github.com/grafana/grafana/pkg/middleware" |
||||||
|
m "github.com/grafana/grafana/pkg/models" |
||||||
|
"github.com/grafana/grafana/pkg/plugins" |
||||||
|
) |
||||||
|
|
||||||
|
func GetOrgAppsList(c *middleware.Context) Response { |
||||||
|
orgApps, err := plugins.GetOrgAppSettings(c.OrgId) |
||||||
|
|
||||||
|
if err != nil { |
||||||
|
return ApiError(500, "Failed to list of apps", err) |
||||||
|
} |
||||||
|
|
||||||
|
result := make([]*dtos.AppSettings, 0) |
||||||
|
for _, app := range plugins.Apps { |
||||||
|
orgApp := orgApps[app.Id] |
||||||
|
result = append(result, dtos.NewAppSettingsDto(app, orgApp)) |
||||||
|
} |
||||||
|
|
||||||
|
return Json(200, result) |
||||||
|
} |
||||||
|
|
||||||
|
func GetAppSettingsById(c *middleware.Context) Response { |
||||||
|
appId := c.Params(":appId") |
||||||
|
|
||||||
|
if pluginDef, exists := plugins.Apps[appId]; !exists { |
||||||
|
return ApiError(404, "PluginId not found, no installed plugin with that id", nil) |
||||||
|
} else { |
||||||
|
orgApps, err := plugins.GetOrgAppSettings(c.OrgId) |
||||||
|
if err != nil { |
||||||
|
return ApiError(500, "Failed to get org app settings ", nil) |
||||||
|
} |
||||||
|
orgApp := orgApps[appId] |
||||||
|
|
||||||
|
return Json(200, dtos.NewAppSettingsDto(pluginDef, orgApp)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func UpdateAppSettings(c *middleware.Context, cmd m.UpdateAppSettingsCmd) Response { |
||||||
|
appId := c.Params(":appId") |
||||||
|
|
||||||
|
cmd.OrgId = c.OrgId |
||||||
|
cmd.AppId = appId |
||||||
|
|
||||||
|
if _, ok := plugins.Apps[cmd.AppId]; !ok { |
||||||
|
return ApiError(404, "App type not installed.", nil) |
||||||
|
} |
||||||
|
|
||||||
|
err := bus.Dispatch(&cmd) |
||||||
|
if err != nil { |
||||||
|
return ApiError(500, "Failed to update App Plugin", err) |
||||||
|
} |
||||||
|
|
||||||
|
return ApiSuccess("App updated") |
||||||
|
} |
@ -1,13 +0,0 @@ |
|||||||
package dtos |
|
||||||
|
|
||||||
import "github.com/grafana/grafana/pkg/plugins" |
|
||||||
|
|
||||||
type AppPlugin struct { |
|
||||||
Name string `json:"name"` |
|
||||||
Type string `json:"type"` |
|
||||||
Enabled bool `json:"enabled"` |
|
||||||
Pinned bool `json:"pinned"` |
|
||||||
Module string `json:"module"` |
|
||||||
Info *plugins.PluginInfo `json:"info"` |
|
||||||
JsonData map[string]interface{} `json:"jsonData"` |
|
||||||
} |
|
@ -0,0 +1,31 @@ |
|||||||
|
package dtos |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/grafana/grafana/pkg/models" |
||||||
|
"github.com/grafana/grafana/pkg/plugins" |
||||||
|
) |
||||||
|
|
||||||
|
type AppSettings struct { |
||||||
|
Name string `json:"name"` |
||||||
|
AppId string `json:"appId"` |
||||||
|
Enabled bool `json:"enabled"` |
||||||
|
Pinned bool `json:"pinned"` |
||||||
|
Info *plugins.PluginInfo `json:"info"` |
||||||
|
JsonData map[string]interface{} `json:"jsonData"` |
||||||
|
} |
||||||
|
|
||||||
|
func NewAppSettingsDto(def *plugins.AppPlugin, data *models.AppSettings) *AppSettings { |
||||||
|
dto := &AppSettings{ |
||||||
|
AppId: def.Id, |
||||||
|
Name: def.Name, |
||||||
|
Info: &def.Info, |
||||||
|
} |
||||||
|
|
||||||
|
if data != nil { |
||||||
|
dto.Enabled = data.Enabled |
||||||
|
dto.Pinned = data.Pinned |
||||||
|
dto.Info = &def.Info |
||||||
|
} |
||||||
|
|
||||||
|
return dto |
||||||
|
} |
@ -0,0 +1,96 @@ |
|||||||
|
package plugins |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/grafana/grafana/pkg/bus" |
||||||
|
m "github.com/grafana/grafana/pkg/models" |
||||||
|
) |
||||||
|
|
||||||
|
func GetOrgAppSettings(orgId int64) (map[string]*m.AppSettings, error) { |
||||||
|
query := m.GetAppSettingsQuery{OrgId: orgId} |
||||||
|
|
||||||
|
if err := bus.Dispatch(&query); err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
orgAppsMap := make(map[string]*m.AppSettings) |
||||||
|
for _, orgApp := range query.Result { |
||||||
|
orgAppsMap[orgApp.AppId] = orgApp |
||||||
|
} |
||||||
|
|
||||||
|
return orgAppsMap, nil |
||||||
|
} |
||||||
|
|
||||||
|
func GetEnabledPlugins(orgId int64) (*EnabledPlugins, error) { |
||||||
|
enabledPlugins := NewEnabledPlugins() |
||||||
|
orgApps, err := GetOrgAppSettings(orgId) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
seenPanels := make(map[string]bool) |
||||||
|
seenApi := make(map[string]bool) |
||||||
|
|
||||||
|
for appType, installedApp := range Apps { |
||||||
|
var app AppPlugin |
||||||
|
app = *installedApp |
||||||
|
|
||||||
|
// check if the app is stored in the DB for this org and if so, use the
|
||||||
|
// state stored there.
|
||||||
|
if b, ok := orgApps[appType]; ok { |
||||||
|
app.Enabled = b.Enabled |
||||||
|
app.Pinned = b.Pinned |
||||||
|
} |
||||||
|
|
||||||
|
// if app.Enabled {
|
||||||
|
// for _, d := range app.DatasourcePlugins {
|
||||||
|
// if ds, ok := DataSources[d]; ok {
|
||||||
|
// enabledPlugins.DataSourcePlugins[d] = ds
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// for _, p := range app.PanelPlugins {
|
||||||
|
// if panel, ok := Panels[p]; ok {
|
||||||
|
// if _, ok := seenPanels[p]; !ok {
|
||||||
|
// seenPanels[p] = true
|
||||||
|
// enabledPlugins.PanelPlugins = append(enabledPlugins.PanelPlugins, panel)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// for _, a := range app.ApiPlugins {
|
||||||
|
// if api, ok := ApiPlugins[a]; ok {
|
||||||
|
// if _, ok := seenApi[a]; !ok {
|
||||||
|
// seenApi[a] = true
|
||||||
|
// enabledPlugins.ApiPlugins = append(enabledPlugins.ApiPlugins, api)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// enabledPlugins.AppPlugins = append(enabledPlugins.AppPlugins, &app)
|
||||||
|
// }
|
||||||
|
} |
||||||
|
|
||||||
|
// add all plugins that are not part of an App.
|
||||||
|
for d, installedDs := range DataSources { |
||||||
|
if installedDs.App == "" { |
||||||
|
enabledPlugins.DataSources[d] = installedDs |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for p, panel := range Panels { |
||||||
|
if panel.App == "" { |
||||||
|
if _, ok := seenPanels[p]; !ok { |
||||||
|
seenPanels[p] = true |
||||||
|
enabledPlugins.Panels = append(enabledPlugins.Panels, panel) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for a, api := range ApiPlugins { |
||||||
|
if api.App == "" { |
||||||
|
if _, ok := seenApi[a]; !ok { |
||||||
|
seenApi[a] = true |
||||||
|
enabledPlugins.ApiList = append(enabledPlugins.ApiList, api) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return &enabledPlugins, nil |
||||||
|
} |
Loading…
Reference in new issue