app_plugin.go 1.5 KB

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