app_plugin.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 GetAppPlugins(c *middleware.Context) Response {
  10. query := m.GetAppPluginsQuery{OrgId: c.OrgId}
  11. if err := bus.Dispatch(&query); err != nil {
  12. return ApiError(500, "Failed to list Plugin Bundles", err)
  13. }
  14. installedAppsMap := make(map[string]*dtos.AppPlugin)
  15. for t, a := range plugins.Apps {
  16. installedAppsMap[t] = &dtos.AppPlugin{
  17. Type: a.Type,
  18. Enabled: a.Enabled,
  19. Module: a.Module,
  20. JsonData: make(map[string]interface{}),
  21. }
  22. }
  23. seenApps := make(map[string]bool)
  24. result := make([]*dtos.AppPlugin, 0)
  25. for _, b := range query.Result {
  26. if def, ok := installedAppsMap[b.Type]; ok {
  27. result = append(result, &dtos.AppPlugin{
  28. Type: b.Type,
  29. Enabled: b.Enabled,
  30. Module: def.Module,
  31. JsonData: b.JsonData,
  32. })
  33. seenApps[b.Type] = true
  34. }
  35. }
  36. for t, a := range installedAppsMap {
  37. if _, ok := seenApps[t]; !ok {
  38. result = append(result, a)
  39. }
  40. }
  41. return Json(200, result)
  42. }
  43. func UpdateAppPlugin(c *middleware.Context, cmd m.UpdateAppPluginCmd) Response {
  44. cmd.OrgId = c.OrgId
  45. if _, ok := plugins.Apps[cmd.Type]; !ok {
  46. return ApiError(404, "App type not installed.", nil)
  47. }
  48. err := bus.Dispatch(&cmd)
  49. if err != nil {
  50. return ApiError(500, "Failed to update App Plugin", err)
  51. }
  52. return ApiSuccess("App updated")
  53. }