plugins.go 3.9 KB

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