app_plugin.go 1.5 KB

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