plugins.go 4.2 KB

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