plugins.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package api
  2. import (
  3. "sort"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/plugins"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. func (hs *HTTPServer) GetPluginList(c *m.ReqContext) 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 Error(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. if pluginDef.State == plugins.PluginStateAlpha && !hs.Cfg.EnableAlphaPanels {
  34. continue
  35. }
  36. listItem := dtos.PluginListItem{
  37. Id: pluginDef.Id,
  38. Name: pluginDef.Name,
  39. Type: pluginDef.Type,
  40. Info: &pluginDef.Info,
  41. LatestVersion: pluginDef.GrafanaNetVersion,
  42. HasUpdate: pluginDef.GrafanaNetHasUpdate,
  43. DefaultNavUrl: pluginDef.DefaultNavUrl,
  44. State: pluginDef.State,
  45. }
  46. if pluginSetting, exists := pluginSettingsMap[pluginDef.Id]; exists {
  47. listItem.Enabled = pluginSetting.Enabled
  48. listItem.Pinned = pluginSetting.Pinned
  49. }
  50. if listItem.DefaultNavUrl == "" || !listItem.Enabled {
  51. listItem.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + listItem.Id + "/edit"
  52. }
  53. // filter out disabled
  54. if enabledFilter == "1" && !listItem.Enabled {
  55. continue
  56. }
  57. // filter out built in data sources
  58. if ds, exists := plugins.DataSources[pluginDef.Id]; exists {
  59. if ds.BuiltIn {
  60. continue
  61. }
  62. }
  63. result = append(result, listItem)
  64. }
  65. sort.Sort(result)
  66. return JSON(200, result)
  67. }
  68. func GetPluginSettingByID(c *m.ReqContext) Response {
  69. pluginID := c.Params(":pluginId")
  70. def, exists := plugins.Plugins[pluginID]
  71. if !exists {
  72. return Error(404, "Plugin not found, no installed plugin with that id", nil)
  73. }
  74. dto := &dtos.PluginSetting{
  75. Type: def.Type,
  76. Id: def.Id,
  77. Name: def.Name,
  78. Info: &def.Info,
  79. Dependencies: &def.Dependencies,
  80. Includes: def.Includes,
  81. BaseUrl: def.BaseUrl,
  82. Module: def.Module,
  83. DefaultNavUrl: def.DefaultNavUrl,
  84. LatestVersion: def.GrafanaNetVersion,
  85. HasUpdate: def.GrafanaNetHasUpdate,
  86. State: def.State,
  87. }
  88. query := m.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId}
  89. if err := bus.Dispatch(&query); err != nil {
  90. if err != m.ErrPluginSettingNotFound {
  91. return Error(500, "Failed to get login settings", nil)
  92. }
  93. } else {
  94. dto.Enabled = query.Result.Enabled
  95. dto.Pinned = query.Result.Pinned
  96. dto.JsonData = query.Result.JsonData
  97. }
  98. return JSON(200, dto)
  99. }
  100. func UpdatePluginSetting(c *m.ReqContext, cmd m.UpdatePluginSettingCmd) Response {
  101. pluginID := c.Params(":pluginId")
  102. cmd.OrgId = c.OrgId
  103. cmd.PluginId = pluginID
  104. if _, ok := plugins.Apps[cmd.PluginId]; !ok {
  105. return Error(404, "Plugin not installed.", nil)
  106. }
  107. if err := bus.Dispatch(&cmd); err != nil {
  108. return Error(500, "Failed to update plugin setting", err)
  109. }
  110. return Success("Plugin settings updated")
  111. }
  112. func GetPluginDashboards(c *m.ReqContext) Response {
  113. pluginID := c.Params(":pluginId")
  114. list, err := plugins.GetPluginDashboards(c.OrgId, pluginID)
  115. if err != nil {
  116. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  117. return Error(404, notfound.Error(), nil)
  118. }
  119. return Error(500, "Failed to get plugin dashboards", err)
  120. }
  121. return JSON(200, list)
  122. }
  123. func GetPluginMarkdown(c *m.ReqContext) Response {
  124. pluginID := c.Params(":pluginId")
  125. name := c.Params(":name")
  126. content, err := plugins.GetPluginMarkdown(pluginID, name)
  127. if err != nil {
  128. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  129. return Error(404, notfound.Error(), nil)
  130. }
  131. return Error(500, "Could not get markdown file", err)
  132. }
  133. // fallback try readme
  134. if len(content) == 0 {
  135. content, err = plugins.GetPluginMarkdown(pluginID, "readme")
  136. if err != nil {
  137. return Error(501, "Could not get markdown file", err)
  138. }
  139. }
  140. resp := Respond(200, content)
  141. resp.Header("Content-Type", "text/plain; charset=utf-8")
  142. return resp
  143. }
  144. func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
  145. cmd := plugins.ImportDashboardCommand{
  146. OrgId: c.OrgId,
  147. User: c.SignedInUser,
  148. PluginId: apiCmd.PluginId,
  149. Path: apiCmd.Path,
  150. Inputs: apiCmd.Inputs,
  151. Overwrite: apiCmd.Overwrite,
  152. FolderId: apiCmd.FolderId,
  153. Dashboard: apiCmd.Dashboard,
  154. }
  155. if err := bus.Dispatch(&cmd); err != nil {
  156. return Error(500, "Failed to import dashboard", err)
  157. }
  158. return JSON(200, cmd.Result)
  159. }