plugins.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // filter out built in data sources
  42. if ds, exists := plugins.DataSources[pluginDef.Id]; exists {
  43. if ds.BuiltIn {
  44. continue
  45. }
  46. }
  47. result = append(result, listItem)
  48. }
  49. return Json(200, result)
  50. }
  51. func GetPluginSettingById(c *middleware.Context) Response {
  52. pluginId := c.Params(":pluginId")
  53. if def, exists := plugins.Plugins[pluginId]; !exists {
  54. return ApiError(404, "Plugin not found, no installed plugin with that id", nil)
  55. } else {
  56. dto := &dtos.PluginSetting{
  57. Type: def.Type,
  58. Id: def.Id,
  59. Name: def.Name,
  60. Info: &def.Info,
  61. Dependencies: &def.Dependencies,
  62. Includes: def.Includes,
  63. BaseUrl: def.BaseUrl,
  64. Module: def.Module,
  65. }
  66. if app, exists := plugins.Apps[pluginId]; exists {
  67. dto.Pages = app.Pages
  68. }
  69. query := m.GetPluginSettingByIdQuery{PluginId: pluginId, OrgId: c.OrgId}
  70. if err := bus.Dispatch(&query); err != nil {
  71. if err != m.ErrPluginSettingNotFound {
  72. return ApiError(500, "Failed to get login settings", nil)
  73. }
  74. } else {
  75. dto.Enabled = query.Result.Enabled
  76. dto.Pinned = query.Result.Pinned
  77. dto.JsonData = query.Result.JsonData
  78. }
  79. return Json(200, dto)
  80. }
  81. }
  82. func UpdatePluginSetting(c *middleware.Context, cmd m.UpdatePluginSettingCmd) Response {
  83. pluginId := c.Params(":pluginId")
  84. cmd.OrgId = c.OrgId
  85. cmd.PluginId = pluginId
  86. if _, ok := plugins.Apps[cmd.PluginId]; !ok {
  87. return ApiError(404, "Plugin not installed.", nil)
  88. }
  89. if err := bus.Dispatch(&cmd); err != nil {
  90. return ApiError(500, "Failed to update plugin setting", err)
  91. }
  92. return ApiSuccess("Plugin settings updated")
  93. }
  94. func GetPluginDashboards(c *middleware.Context) Response {
  95. pluginId := c.Params(":pluginId")
  96. if list, err := plugins.GetPluginDashboards(c.OrgId, pluginId); err != nil {
  97. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  98. return ApiError(404, notfound.Error(), nil)
  99. }
  100. return ApiError(500, "Failed to get plugin dashboards", err)
  101. } else {
  102. return Json(200, list)
  103. }
  104. }
  105. func GetPluginReadme(c *middleware.Context) Response {
  106. pluginId := c.Params(":pluginId")
  107. if content, err := plugins.GetPluginReadme(pluginId); err != nil {
  108. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  109. return ApiError(404, notfound.Error(), nil)
  110. }
  111. return ApiError(500, "Could not get readme", err)
  112. } else {
  113. return Respond(200, content)
  114. }
  115. }
  116. func ImportDashboard(c *middleware.Context, apiCmd dtos.ImportDashboardCommand) Response {
  117. cmd := plugins.ImportDashboardCommand{
  118. OrgId: c.OrgId,
  119. UserId: c.UserId,
  120. PluginId: apiCmd.PluginId,
  121. Path: apiCmd.Path,
  122. Inputs: apiCmd.Inputs,
  123. }
  124. if err := bus.Dispatch(&cmd); err != nil {
  125. return ApiError(500, "Failed to install dashboard", err)
  126. }
  127. return Json(200, cmd.Result)
  128. }