plugins.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 GetPluginList(c *middleware.Context) Response {
  10. typeFilter := c.Query("type")
  11. enabledFilter := c.Query("enabled")
  12. embeddedFilter := c.Query("embedded")
  13. pluginSettingsMap, err := plugins.GetPluginSettings(c.OrgId)
  14. if err != nil {
  15. return ApiError(500, "Failed to get list of plugins", err)
  16. }
  17. result := make([]*dtos.PluginListItem, 0)
  18. for _, pluginDef := range plugins.Plugins {
  19. // filter out app sub plugins
  20. if embeddedFilter == "0" && pluginDef.IncludedInAppId != "" {
  21. continue
  22. }
  23. // filter on type
  24. if typeFilter != "" && typeFilter != pluginDef.Type {
  25. continue
  26. }
  27. listItem := &dtos.PluginListItem{
  28. Id: pluginDef.Id,
  29. Name: pluginDef.Name,
  30. Type: pluginDef.Type,
  31. Info: &pluginDef.Info,
  32. }
  33. if pluginSetting, exists := pluginSettingsMap[pluginDef.Id]; exists {
  34. listItem.Enabled = pluginSetting.Enabled
  35. listItem.Pinned = pluginSetting.Pinned
  36. }
  37. // filter out disabled
  38. if enabledFilter == "1" && !listItem.Enabled {
  39. continue
  40. }
  41. result = append(result, listItem)
  42. }
  43. return Json(200, result)
  44. }
  45. func GetPluginSettingById(c *middleware.Context) Response {
  46. pluginId := c.Params(":pluginId")
  47. if def, exists := plugins.Plugins[pluginId]; !exists {
  48. return ApiError(404, "Plugin not found, no installed plugin with that id", nil)
  49. } else {
  50. dto := &dtos.PluginSetting{
  51. Type: def.Type,
  52. Id: def.Id,
  53. Name: def.Name,
  54. Info: &def.Info,
  55. Dependencies: &def.Dependencies,
  56. Includes: def.Includes,
  57. BaseUrl: def.BaseUrl,
  58. Module: def.Module,
  59. }
  60. if app, exists := plugins.Apps[pluginId]; exists {
  61. dto.Pages = app.Pages
  62. }
  63. query := m.GetPluginSettingByIdQuery{PluginId: pluginId, OrgId: c.OrgId}
  64. if err := bus.Dispatch(&query); err != nil {
  65. if err != m.ErrPluginSettingNotFound {
  66. return ApiError(500, "Failed to get login settings", nil)
  67. }
  68. } else {
  69. dto.Enabled = query.Result.Enabled
  70. dto.Pinned = query.Result.Pinned
  71. dto.JsonData = query.Result.JsonData
  72. }
  73. return Json(200, dto)
  74. }
  75. }
  76. func UpdatePluginSetting(c *middleware.Context, cmd m.UpdatePluginSettingCmd) Response {
  77. pluginId := c.Params(":pluginId")
  78. cmd.OrgId = c.OrgId
  79. cmd.PluginId = pluginId
  80. if _, ok := plugins.Apps[cmd.PluginId]; !ok {
  81. return ApiError(404, "Plugin not installed.", nil)
  82. }
  83. if err := bus.Dispatch(&cmd); err != nil {
  84. return ApiError(500, "Failed to update plugin setting", err)
  85. }
  86. return ApiSuccess("Plugin settings updated")
  87. }
  88. func GetPluginDashboards(c *middleware.Context) Response {
  89. pluginId := c.Params(":pluginId")
  90. if list, err := plugins.GetPluginDashboards(c.OrgId, pluginId); err != nil {
  91. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  92. return ApiError(404, notfound.Error(), nil)
  93. }
  94. return ApiError(500, "Failed to get plugin dashboards", err)
  95. } else {
  96. return Json(200, list)
  97. }
  98. }
  99. func GetPluginReadme(c *middleware.Context) Response {
  100. pluginId := c.Params(":pluginId")
  101. if content, err := plugins.GetPluginReadme(pluginId); err != nil {
  102. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  103. return ApiError(404, notfound.Error(), nil)
  104. }
  105. return ApiError(500, "Could not get readme", err)
  106. } else {
  107. return Respond(200, content)
  108. }
  109. }
  110. func ImportDashboard(c *middleware.Context, apiCmd dtos.ImportDashboardCommand) Response {
  111. cmd := plugins.ImportDashboardCommand{
  112. OrgId: c.OrgId,
  113. UserId: c.UserId,
  114. PluginId: apiCmd.PluginId,
  115. Path: apiCmd.Path,
  116. Inputs: apiCmd.Inputs,
  117. }
  118. if err := bus.Dispatch(&cmd); err != nil {
  119. return ApiError(500, "Failed to install dashboard", err)
  120. }
  121. return Json(200, cmd.Result)
  122. }