refactor.
Rename externalPlugin to apiPlugin Rename bundle to app Move js, css, menuItem and staticRoot to be properties os App Add "app" field to panel, datasource and api plugin models. If populated then the plugin is only enabled if the specific app is enabled for the Org. If app is "", then the plugin is enabled for all orgs and can't be disabled.
This commit is contained in:
+3
-3
@@ -157,8 +157,8 @@ func Register(r *macaron.Macaron) {
|
||||
|
||||
// PluginBundles
|
||||
r.Group("/plugins", func() {
|
||||
r.Get("/", wrap(GetPluginBundles))
|
||||
r.Post("/", bind(m.UpdatePluginBundleCmd{}), wrap(UpdatePluginBundle))
|
||||
r.Get("/", wrap(GetAppPlugins))
|
||||
r.Post("/", bind(m.UpdateAppPluginCmd{}), wrap(UpdateAppPlugin))
|
||||
}, reqOrgAdmin)
|
||||
|
||||
r.Get("/frontend/settings/", GetFrontendSettings)
|
||||
@@ -196,7 +196,7 @@ func Register(r *macaron.Macaron) {
|
||||
// rendering
|
||||
r.Get("/render/*", reqSignedIn, RenderToPng)
|
||||
|
||||
InitExternalPluginRoutes(r)
|
||||
InitApiPluginRoutes(r)
|
||||
|
||||
r.NotFound(NotFoundHandler)
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func InitExternalPluginRoutes(r *macaron.Macaron) {
|
||||
for _, plugin := range plugins.ExternalPlugins {
|
||||
log.Info("Plugin: Adding proxy routes for backend plugin")
|
||||
func InitApiPluginRoutes(r *macaron.Macaron) {
|
||||
for _, plugin := range plugins.ApiPlugins {
|
||||
log.Info("Plugin: Adding proxy routes for api plugin")
|
||||
for _, route := range plugin.Routes {
|
||||
url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
|
||||
handlers := make([]macaron.Handler, 0)
|
||||
@@ -33,14 +33,14 @@ func InitExternalPluginRoutes(r *macaron.Macaron) {
|
||||
handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN))
|
||||
}
|
||||
}
|
||||
handlers = append(handlers, ExternalPlugin(route.Url))
|
||||
handlers = append(handlers, ApiPlugin(route.Url))
|
||||
r.Route(url, route.Method, handlers...)
|
||||
log.Info("Plugin: Adding route %s", url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ExternalPlugin(routeUrl string) macaron.Handler {
|
||||
func ApiPlugin(routeUrl string) macaron.Handler {
|
||||
return func(c *middleware.Context) {
|
||||
path := c.Params("*")
|
||||
|
||||
@@ -51,13 +51,13 @@ func ExternalPlugin(routeUrl string) macaron.Handler {
|
||||
return
|
||||
}
|
||||
targetUrl, _ := url.Parse(routeUrl)
|
||||
proxy := NewExternalPluginProxy(string(ctx), path, targetUrl)
|
||||
proxy := NewApiPluginProxy(string(ctx), path, targetUrl)
|
||||
proxy.Transport = dataProxyTransport
|
||||
proxy.ServeHTTP(c.RW(), c.Req.Request)
|
||||
}
|
||||
}
|
||||
|
||||
func NewExternalPluginProxy(ctx string, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
|
||||
func NewApiPluginProxy(ctx string, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
|
||||
director := func(req *http.Request) {
|
||||
req.URL.Scheme = targetUrl.Scheme
|
||||
req.URL.Host = targetUrl.Host
|
||||
@@ -0,0 +1,65 @@
|
||||
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)
|
||||
}
|
||||
|
||||
installedAppsMap := make(map[string]*dtos.AppPlugin)
|
||||
for t, a := range plugins.Apps {
|
||||
installedAppsMap[t] = &dtos.AppPlugin{
|
||||
Type: a.Type,
|
||||
Enabled: a.Enabled,
|
||||
Module: a.Module,
|
||||
JsonData: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
seenApps := make(map[string]bool)
|
||||
|
||||
result := make([]*dtos.AppPlugin, 0)
|
||||
for _, b := range query.Result {
|
||||
if def, ok := installedAppsMap[b.Type]; ok {
|
||||
result = append(result, &dtos.AppPlugin{
|
||||
Type: b.Type,
|
||||
Enabled: b.Enabled,
|
||||
Module: def.Module,
|
||||
JsonData: b.JsonData,
|
||||
})
|
||||
seenApps[b.Type] = true
|
||||
}
|
||||
}
|
||||
|
||||
for t, a := range installedAppsMap {
|
||||
if _, ok := seenApps[t]; !ok {
|
||||
result = append(result, a)
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
//"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
@@ -114,14 +115,14 @@ func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) {
|
||||
}
|
||||
|
||||
func GetDataSourcePlugins(c *middleware.Context) {
|
||||
dsList := make(map[string]interface{})
|
||||
dsList := make(map[string]*plugins.DataSourcePlugin)
|
||||
|
||||
orgBundles := m.GetPluginBundlesQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&orgBundles)
|
||||
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&orgApps)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Failed to get org plugin Bundles", err)
|
||||
}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgBundles.Result)
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
|
||||
|
||||
for key, value := range enabledPlugins.DataSourcePlugins {
|
||||
if !value.BuiltIn {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package dtos
|
||||
|
||||
type PluginBundle struct {
|
||||
type AppPlugin struct {
|
||||
Type string `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Module string `json:"module"`
|
||||
@@ -29,12 +29,12 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
|
||||
datasources := make(map[string]interface{})
|
||||
var defaultDatasource string
|
||||
|
||||
orgBundles := m.GetPluginBundlesQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&orgBundles)
|
||||
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&orgApps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgBundles.Result)
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
|
||||
|
||||
for _, ds := range orgDataSources {
|
||||
url := ds.Url
|
||||
|
||||
+4
-4
@@ -67,14 +67,14 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
|
||||
})
|
||||
}
|
||||
|
||||
orgBundles := m.GetPluginBundlesQuery{OrgId: c.OrgId}
|
||||
err = bus.Dispatch(&orgBundles)
|
||||
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
|
||||
err = bus.Dispatch(&orgApps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgBundles.Result)
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
|
||||
|
||||
for _, plugin := range enabledPlugins.ExternalPlugins {
|
||||
for _, plugin := range enabledPlugins.AppPlugins {
|
||||
for _, js := range plugin.Js {
|
||||
data.PluginJs = append(data.PluginJs, js.Module)
|
||||
}
|
||||
|
||||
@@ -1,65 +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 GetPluginBundles(c *middleware.Context) Response {
|
||||
query := m.GetPluginBundlesQuery{OrgId: c.OrgId}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed to list Plugin Bundles", err)
|
||||
}
|
||||
|
||||
installedBundlesMap := make(map[string]*dtos.PluginBundle)
|
||||
for t, b := range plugins.Bundles {
|
||||
installedBundlesMap[t] = &dtos.PluginBundle{
|
||||
Type: b.Type,
|
||||
Enabled: b.Enabled,
|
||||
Module: b.Module,
|
||||
JsonData: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
seenBundles := make(map[string]bool)
|
||||
|
||||
result := make([]*dtos.PluginBundle, 0)
|
||||
for _, b := range query.Result {
|
||||
if def, ok := installedBundlesMap[b.Type]; ok {
|
||||
result = append(result, &dtos.PluginBundle{
|
||||
Type: b.Type,
|
||||
Enabled: b.Enabled,
|
||||
Module: def.Module,
|
||||
JsonData: b.JsonData,
|
||||
})
|
||||
seenBundles[b.Type] = true
|
||||
}
|
||||
}
|
||||
|
||||
for t, b := range installedBundlesMap {
|
||||
if _, ok := seenBundles[t]; !ok {
|
||||
result = append(result, b)
|
||||
}
|
||||
}
|
||||
|
||||
return Json(200, result)
|
||||
}
|
||||
|
||||
func UpdatePluginBundle(c *middleware.Context, cmd m.UpdatePluginBundleCmd) Response {
|
||||
cmd.OrgId = c.OrgId
|
||||
|
||||
if _, ok := plugins.Bundles[cmd.Type]; !ok {
|
||||
return ApiError(404, "Bundle type not installed.", nil)
|
||||
}
|
||||
|
||||
err := bus.Dispatch(&cmd)
|
||||
if err != nil {
|
||||
return ApiError(500, "Failed to update plugin bundle", err)
|
||||
}
|
||||
|
||||
return ApiSuccess("Plugin updated")
|
||||
}
|
||||
Reference in New Issue
Block a user