plugins.go 4.7 KB

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