plugin_bundle.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/plugins"
  8. )
  9. func GetPluginBundles(c *middleware.Context) Response {
  10. query := m.GetPluginBundlesQuery{OrgId: c.OrgId}
  11. if err := bus.Dispatch(&query); err != nil {
  12. return ApiError(500, "Failed to list Plugin Bundles", err)
  13. }
  14. installedBundlesMap := make(map[string]*dtos.PluginBundle)
  15. for t, b := range plugins.Bundles {
  16. installedBundlesMap[t] = &dtos.PluginBundle{
  17. Type: b.Type,
  18. Enabled: b.Enabled,
  19. Module: b.Module,
  20. JsonData: make(map[string]interface{}),
  21. }
  22. }
  23. seenBundles := make(map[string]bool)
  24. result := make([]*dtos.PluginBundle, 0)
  25. for _, b := range query.Result {
  26. if def, ok := installedBundlesMap[b.Type]; ok {
  27. result = append(result, &dtos.PluginBundle{
  28. Type: b.Type,
  29. Enabled: b.Enabled,
  30. Module: def.Module,
  31. JsonData: b.JsonData,
  32. })
  33. seenBundles[b.Type] = true
  34. }
  35. }
  36. for t, b := range installedBundlesMap {
  37. if _, ok := seenBundles[t]; !ok {
  38. result = append(result, b)
  39. }
  40. }
  41. return Json(200, result)
  42. }
  43. func UpdatePluginBundle(c *middleware.Context, cmd m.UpdatePluginBundleCmd) Response {
  44. cmd.OrgId = c.OrgId
  45. if _, ok := plugins.Bundles[cmd.Type]; !ok {
  46. return ApiError(404, "Bundle type not installed.", nil)
  47. }
  48. err := bus.Dispatch(&cmd)
  49. if err != nil {
  50. return ApiError(500, "Failed to update plugin bundle", err)
  51. }
  52. return ApiSuccess("Plugin updated")
  53. }