add pluginBundle backend api methods and SQL storage
This commit is contained in:
+10
-4
@@ -13,7 +13,7 @@ func Register(r *macaron.Macaron) {
|
||||
reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
|
||||
reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
|
||||
reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
|
||||
regOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
|
||||
reqOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
|
||||
quota := middleware.Quota
|
||||
bind := binding.Bind
|
||||
|
||||
@@ -113,7 +113,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Get("/invites", wrap(GetPendingOrgInvites))
|
||||
r.Post("/invites", quota("user"), bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
|
||||
r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
|
||||
}, regOrgAdmin)
|
||||
}, reqOrgAdmin)
|
||||
|
||||
// create new org
|
||||
r.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
|
||||
@@ -140,7 +140,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Get("/", wrap(GetApiKeys))
|
||||
r.Post("/", quota("api_key"), bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
|
||||
r.Delete("/:id", wrap(DeleteApiKey))
|
||||
}, regOrgAdmin)
|
||||
}, reqOrgAdmin)
|
||||
|
||||
// Data sources
|
||||
r.Group("/datasources", func() {
|
||||
@@ -150,7 +150,13 @@ func Register(r *macaron.Macaron) {
|
||||
r.Delete("/:id", DeleteDataSource)
|
||||
r.Get("/:id", wrap(GetDataSourceById))
|
||||
r.Get("/plugins", GetDataSourcePlugins)
|
||||
}, regOrgAdmin)
|
||||
}, reqOrgAdmin)
|
||||
|
||||
// PluginBundles
|
||||
r.Group("/plugins", func() {
|
||||
r.Get("/", wrap(GetPluginBundles))
|
||||
r.Post("/", bind(m.UpdatePluginBundleCmd{}), wrap(UpdatePluginBundle))
|
||||
}, reqOrgAdmin)
|
||||
|
||||
r.Get("/frontend/settings/", GetFrontendSettings)
|
||||
r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
|
||||
|
||||
@@ -115,9 +115,13 @@ func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) {
|
||||
|
||||
func GetDataSourcePlugins(c *middleware.Context) {
|
||||
dsList := make(map[string]interface{})
|
||||
//TODO(awoods): query DB for orgPlugins
|
||||
orgPlugins := map[string]m.PluginBundle{}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgPlugins)
|
||||
|
||||
orgBundles := m.GetPluginBundlesQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&orgBundles)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Failed to get org plugin Bundles", err)
|
||||
}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgBundles.Result)
|
||||
|
||||
for key, value := range enabledPlugins.DataSourcePlugins {
|
||||
if !value.BuiltIn {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package dtos
|
||||
|
||||
type PluginBundle struct {
|
||||
Type string `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Module string `json:"module"`
|
||||
JsonData map[string]interface{} `json:"jsonData"`
|
||||
}
|
||||
@@ -29,9 +29,12 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
|
||||
datasources := make(map[string]interface{})
|
||||
var defaultDatasource string
|
||||
|
||||
//TODO(awoods): query DB to get list of the users plugin preferences.
|
||||
orgPlugins := map[string]m.PluginBundle{}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgPlugins)
|
||||
orgBundles := m.GetPluginBundlesQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&orgBundles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgBundles.Result)
|
||||
|
||||
for _, ds := range orgDataSources {
|
||||
url := ds.Url
|
||||
|
||||
+8
-3
@@ -2,6 +2,7 @@ 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"
|
||||
@@ -62,9 +63,13 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
|
||||
})
|
||||
}
|
||||
|
||||
//TODO(awoods): query DB to get list of the users plugin preferences.
|
||||
orgPlugins := map[string]m.PluginBundle{}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgPlugins)
|
||||
orgBundles := m.GetPluginBundlesQuery{OrgId: c.OrgId}
|
||||
err = bus.Dispatch(&orgBundles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enabledPlugins := plugins.GetEnabledPlugins(orgBundles.Result)
|
||||
|
||||
for _, plugin := range enabledPlugins.ExternalPlugins {
|
||||
for _, js := range plugin.Js {
|
||||
data.PluginJs = append(data.PluginJs, js.Module)
|
||||
|
||||
@@ -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 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