app_plugin.go 1.5 KB

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