plugins.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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. 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. DefaultNavUrl: pluginDef.DefaultNavUrl,
  41. State: pluginDef.State,
  42. }
  43. if pluginSetting, exists := pluginSettingsMap[pluginDef.Id]; exists {
  44. listItem.Enabled = pluginSetting.Enabled
  45. listItem.Pinned = pluginSetting.Pinned
  46. }
  47. if listItem.DefaultNavUrl == "" || !listItem.Enabled {
  48. listItem.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + listItem.Id + "/edit"
  49. }
  50. // filter out disabled
  51. if enabledFilter == "1" && !listItem.Enabled {
  52. continue
  53. }
  54. // filter out built in data sources
  55. if ds, exists := plugins.DataSources[pluginDef.Id]; exists {
  56. if ds.BuiltIn {
  57. continue
  58. }
  59. }
  60. result = append(result, listItem)
  61. }
  62. sort.Sort(result)
  63. return JSON(200, result)
  64. }
  65. func GetPluginSettingByID(c *m.ReqContext) Response {
  66. pluginID := c.Params(":pluginId")
  67. def, exists := plugins.Plugins[pluginID]
  68. if !exists {
  69. return Error(404, "Plugin not found, no installed plugin with that id", nil)
  70. }
  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 Error(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. func UpdatePluginSetting(c *m.ReqContext, cmd m.UpdatePluginSettingCmd) Response {
  98. pluginID := c.Params(":pluginId")
  99. cmd.OrgId = c.OrgId
  100. cmd.PluginId = pluginID
  101. if _, ok := plugins.Apps[cmd.PluginId]; !ok {
  102. return Error(404, "Plugin not installed.", nil)
  103. }
  104. if err := bus.Dispatch(&cmd); err != nil {
  105. return Error(500, "Failed to update plugin setting", err)
  106. }
  107. return Success("Plugin settings updated")
  108. }
  109. func GetPluginDashboards(c *m.ReqContext) Response {
  110. pluginID := c.Params(":pluginId")
  111. list, err := plugins.GetPluginDashboards(c.OrgId, pluginID)
  112. if err != nil {
  113. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  114. return Error(404, notfound.Error(), nil)
  115. }
  116. return Error(500, "Failed to get plugin dashboards", err)
  117. }
  118. return JSON(200, list)
  119. }
  120. func GetPluginMarkdown(c *m.ReqContext) Response {
  121. pluginID := c.Params(":pluginId")
  122. name := c.Params(":name")
  123. content, err := plugins.GetPluginMarkdown(pluginID, name)
  124. if err != nil {
  125. if notfound, ok := err.(plugins.PluginNotFoundError); ok {
  126. return Error(404, notfound.Error(), nil)
  127. }
  128. return Error(500, "Could not get markdown file", err)
  129. }
  130. resp := Respond(200, content)
  131. resp.Header("Content-Type", "text/plain; charset=utf-8")
  132. return resp
  133. }
  134. func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
  135. cmd := plugins.ImportDashboardCommand{
  136. OrgId: c.OrgId,
  137. User: c.SignedInUser,
  138. PluginId: apiCmd.PluginId,
  139. Path: apiCmd.Path,
  140. Inputs: apiCmd.Inputs,
  141. Overwrite: apiCmd.Overwrite,
  142. FolderId: apiCmd.FolderId,
  143. Dashboard: apiCmd.Dashboard,
  144. }
  145. if err := bus.Dispatch(&cmd); err != nil {
  146. return Error(500, "Failed to import dashboard", err)
  147. }
  148. return JSON(200, cmd.Result)
  149. }