mirror of https://github.com/grafana/grafana
feat(plugins): removed external plugins and bundle code, not ready for master yet, will revert this commit in seperate branch
parent
2ec5bc77d7
commit
5eab5dc47b
@ -1,75 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"net/http" |
||||
"net/http/httputil" |
||||
"net/url" |
||||
|
||||
"github.com/Unknwon/macaron" |
||||
"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" |
||||
"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") |
||||
for _, route := range plugin.Routes { |
||||
url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path) |
||||
handlers := make([]macaron.Handler, 0) |
||||
if route.ReqSignedIn { |
||||
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})) |
||||
} |
||||
if route.ReqGrafanaAdmin { |
||||
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})) |
||||
} |
||||
if route.ReqSignedIn && route.ReqRole != "" { |
||||
if route.ReqRole == m.ROLE_ADMIN { |
||||
handlers = append(handlers, middleware.RoleAuth(m.ROLE_ADMIN)) |
||||
} else if route.ReqRole == m.ROLE_EDITOR { |
||||
handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)) |
||||
} |
||||
} |
||||
handlers = append(handlers, ExternalPlugin(route.Url)) |
||||
r.Route(url, route.Method, handlers...) |
||||
log.Info("Plugin: Adding route %s", url) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func ExternalPlugin(routeUrl string) macaron.Handler { |
||||
return func(c *middleware.Context) { |
||||
path := c.Params("*") |
||||
|
||||
//Create a HTTP header with the context in it.
|
||||
ctx, err := json.Marshal(c.SignedInUser) |
||||
if err != nil { |
||||
c.JsonApiErr(500, "failed to marshal context to json.", err) |
||||
return |
||||
} |
||||
targetUrl, _ := url.Parse(routeUrl) |
||||
proxy := NewExternalPluginProxy(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 { |
||||
director := func(req *http.Request) { |
||||
req.URL.Scheme = targetUrl.Scheme |
||||
req.URL.Host = targetUrl.Host |
||||
req.Host = targetUrl.Host |
||||
|
||||
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath) |
||||
|
||||
// clear cookie headers
|
||||
req.Header.Del("Cookie") |
||||
req.Header.Del("Set-Cookie") |
||||
req.Header.Add("Grafana-Context", ctx) |
||||
} |
||||
|
||||
return &httputil.ReverseProxy{Director: director} |
||||
} |
||||
@ -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") |
||||
} |
||||
@ -1,13 +0,0 @@ |
||||
Example app is available at https://github.com/raintank/grafana-plugin-example |
||||
|
||||
* Clone plugin repo git@github.com:raintank/grafana-plugin-example.git |
||||
|
||||
* Modify grafana.ini (or custom.ini if your developing Grafana locally) |
||||
|
||||
```ini |
||||
[plugin.external-test] |
||||
path = /<the_path_were_you_cloned_it>/grafana-plugin-example |
||||
``` |
||||
|
||||
|
||||
|
||||
@ -1,9 +0,0 @@ |
||||
{ |
||||
"pluginType": "bundle", |
||||
"type": "core", |
||||
"module": "", |
||||
"enabled": true, |
||||
"panelPlugins": ["graph", "singlestat", "text", "dashlist", "table"], |
||||
"datasourcePlugins": ["mixed", "grafana", "graphite", "cloudwatch", "elasticsearch", "influxdb", "influxdb_08", "kairosdb", "opentsdb", "prometheus"], |
||||
"externalPlugins": [] |
||||
} |
||||
Loading…
Reference in new issue