plugins.go 4.0 KB

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