plugins.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if app, exists := plugins.Apps[pluginId]; exists {
  69. dto.Pages = app.Pages
  70. }
  71. query := m.GetPluginSettingByIdQuery{PluginId: pluginId, OrgId: c.OrgId}
  72. if err := bus.Dispatch(&query); err != nil {
  73. if err != m.ErrPluginSettingNotFound {
  74. return ApiError(500, "Failed to get login settings", nil)
  75. }
  76. } else {
  77. dto.Enabled = query.Result.Enabled
  78. dto.Pinned = query.Result.Pinned
  79. dto.JsonData = query.Result.JsonData
  80. }
  81. return Json(200, dto)
  82. }
  83. }
  84. func UpdatePluginSetting(c *middleware.Context, cmd m.UpdatePluginSettingCmd) Response {
  85. pluginId := c.Params(":pluginId")
  86. cmd.OrgId = c.OrgId
  87. cmd.PluginId = pluginId
  88. if _, ok := plugins.Apps[cmd.PluginId]; !ok {
  89. return ApiError(404, "Plugin not installed.", nil)
  90. }
  91. if err := bus.Dispatch(&cmd); err != nil {
  92. return ApiError(500, "Failed to update plugin setting", err)
  93. }
  94. return ApiSuccess("Plugin settings updated")
  95. }
  96. func GetPluginDashboards(c *middleware.Context) Response {
  97. pluginId := c.Params(":pluginId")
  98. if list, err := plugins.GetPluginDashboards(c.OrgId, pluginId); err != nil {
  99. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  100. return ApiError(404, notfound.Error(), nil)
  101. }
  102. return ApiError(500, "Failed to get plugin dashboards", err)
  103. } else {
  104. return Json(200, list)
  105. }
  106. }
  107. func GetPluginReadme(c *middleware.Context) Response {
  108. pluginId := c.Params(":pluginId")
  109. if content, err := plugins.GetPluginReadme(pluginId); err != nil {
  110. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  111. return ApiError(404, notfound.Error(), nil)
  112. }
  113. return ApiError(500, "Could not get readme", err)
  114. } else {
  115. return Respond(200, content)
  116. }
  117. }
  118. func ImportDashboard(c *middleware.Context, apiCmd dtos.ImportDashboardCommand) Response {
  119. cmd := plugins.ImportDashboardCommand{
  120. OrgId: c.OrgId,
  121. UserId: c.UserId,
  122. PluginId: apiCmd.PluginId,
  123. Path: apiCmd.Path,
  124. Inputs: apiCmd.Inputs,
  125. }
  126. if err := bus.Dispatch(&cmd); err != nil {
  127. return ApiError(500, "Failed to install dashboard", err)
  128. }
  129. return Json(200, cmd.Result)
  130. }