mirror of https://github.com/grafana/grafana
parent
bd4cb549d6
commit
c4a0fe0234
@ -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"` |
||||
} |
||||
@ -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") |
||||
} |
||||
@ -1,8 +1,34 @@ |
||||
package models |
||||
|
||||
import "time" |
||||
|
||||
type PluginBundle struct { |
||||
Id int64 |
||||
Type string |
||||
Org int64 |
||||
Enabled bool |
||||
Id int64 |
||||
Type string |
||||
OrgId int64 |
||||
Enabled bool |
||||
JsonData map[string]interface{} |
||||
|
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
// ----------------------
|
||||
// COMMANDS
|
||||
|
||||
// Also acts as api DTO
|
||||
type UpdatePluginBundleCmd struct { |
||||
Type string `json:"type" binding:"Required"` |
||||
Enabled bool `json:"enabled"` |
||||
JsonData map[string]interface{} `json:"jsonData"` |
||||
|
||||
Id int64 `json:"-"` |
||||
OrgId int64 `json:"-"` |
||||
} |
||||
|
||||
// ---------------------
|
||||
// QUERIES
|
||||
type GetPluginBundlesQuery struct { |
||||
OrgId int64 |
||||
Result []*PluginBundle |
||||
} |
||||
|
||||
@ -0,0 +1,26 @@ |
||||
package migrations |
||||
|
||||
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" |
||||
|
||||
func addPluginBundleMigration(mg *Migrator) { |
||||
|
||||
var pluginBundleV1 = Table{ |
||||
Name: "plugin_bundle", |
||||
Columns: []*Column{ |
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
||||
{Name: "org_id", Type: DB_BigInt, Nullable: true}, |
||||
{Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false}, |
||||
{Name: "enabled", Type: DB_Bool, Nullable: false}, |
||||
{Name: "json_data", Type: DB_Text, Nullable: true}, |
||||
{Name: "created", Type: DB_DateTime, Nullable: false}, |
||||
{Name: "updated", Type: DB_DateTime, Nullable: false}, |
||||
}, |
||||
Indices: []*Index{ |
||||
{Cols: []string{"org_id", "type"}, Type: UniqueIndex}, |
||||
}, |
||||
} |
||||
mg.AddMigration("create plugin_bundle table v1", NewAddTableMigration(pluginBundleV1)) |
||||
|
||||
//------- indexes ------------------
|
||||
addTableIndicesMigrations(mg, "v1", pluginBundleV1) |
||||
} |
||||
@ -0,0 +1,46 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
func init() { |
||||
bus.AddHandler("sql", GetPluginBundles) |
||||
bus.AddHandler("sql", UpdatePluginBundle) |
||||
} |
||||
|
||||
func GetPluginBundles(query *m.GetPluginBundlesQuery) error { |
||||
sess := x.Where("org_id=?", query.OrgId) |
||||
|
||||
query.Result = make([]*m.PluginBundle, 0) |
||||
return sess.Find(&query.Result) |
||||
} |
||||
|
||||
func UpdatePluginBundle(cmd *m.UpdatePluginBundleCmd) error { |
||||
return inTransaction2(func(sess *session) error { |
||||
var bundle m.PluginBundle |
||||
|
||||
exists, err := sess.Where("org_id=? and type=?", cmd.OrgId, cmd.Type).Get(&bundle) |
||||
sess.UseBool("enabled") |
||||
if !exists { |
||||
bundle = m.PluginBundle{ |
||||
Type: cmd.Type, |
||||
OrgId: cmd.OrgId, |
||||
Enabled: cmd.Enabled, |
||||
JsonData: cmd.JsonData, |
||||
Created: time.Now(), |
||||
Updated: time.Now(), |
||||
} |
||||
_, err = sess.Insert(&bundle) |
||||
return err |
||||
} else { |
||||
bundle.Enabled = cmd.Enabled |
||||
bundle.JsonData = cmd.JsonData |
||||
_, err = sess.Id(bundle.Id).Update(&bundle) |
||||
return err |
||||
} |
||||
}) |
||||
} |
||||
@ -1,8 +1,9 @@ |
||||
{ |
||||
"pluginType": "bundle", |
||||
"type": "core", |
||||
"module": "", |
||||
"enabled": true, |
||||
"panelPlugins": ["graph", "singlestat", "text", "dashlist"], |
||||
"datasourcePlugins": ["grafana", "graphite"], |
||||
"panelPlugins": ["graph", "singlestat", "text", "dashlist", "table"], |
||||
"datasourcePlugins": ["mixed", "grafana", "graphite", "cloudwatch", "elasticsearch", "influxdb", "influxdb_08", "kairosdb", "opentsdb", "prometheus"], |
||||
"externalPlugins": [] |
||||
} |
||||
|
||||
Loading…
Reference in new issue