models.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. var (
  11. PluginTypeApp = "app"
  12. PluginTypeDatasource = "datasource"
  13. PluginTypePanel = "panel"
  14. PluginTypeDashboard = "dashboard"
  15. )
  16. type PluginNotFoundError struct {
  17. PluginId string
  18. }
  19. func (e PluginNotFoundError) Error() string {
  20. return fmt.Sprintf("Plugin with id %s not found", e.PluginId)
  21. }
  22. type PluginLoader interface {
  23. Load(decoder *json.Decoder, pluginDir string) error
  24. }
  25. type PluginBase struct {
  26. Type string `json:"type"`
  27. Name string `json:"name"`
  28. Id string `json:"id"`
  29. Info PluginInfo `json:"info"`
  30. Dependencies PluginDependencies `json:"dependencies"`
  31. Includes []*PluginInclude `json:"includes"`
  32. Module string `json:"module"`
  33. BaseUrl string `json:"baseUrl"`
  34. IncludedInAppId string `json:"-"`
  35. PluginDir string `json:"-"`
  36. // cache for readme file contents
  37. Readme []byte `json:"-"`
  38. }
  39. func (pb *PluginBase) registerPlugin(pluginDir string) error {
  40. if _, exists := Plugins[pb.Id]; exists {
  41. return errors.New("Plugin with same id already exists")
  42. }
  43. if !strings.HasPrefix(pluginDir, setting.StaticRootPath) {
  44. log.Info("Plugins: Registering plugin %v", pb.Name)
  45. }
  46. if len(pb.Dependencies.Plugins) == 0 {
  47. pb.Dependencies.Plugins = []PluginDependencyItem{}
  48. }
  49. if pb.Dependencies.GrafanaVersion == "" {
  50. pb.Dependencies.GrafanaVersion = "*"
  51. }
  52. pb.PluginDir = pluginDir
  53. Plugins[pb.Id] = pb
  54. return nil
  55. }
  56. type PluginDependencies struct {
  57. GrafanaVersion string `json:"grafanaVersion"`
  58. Plugins []PluginDependencyItem `json:"plugins"`
  59. }
  60. type PluginInclude struct {
  61. Name string `json:"name"`
  62. Path string `json:"path"`
  63. Type string `json:"type"`
  64. Id string `json:"id"`
  65. }
  66. type PluginDependencyItem struct {
  67. Type string `json:"type"`
  68. Id string `json:"id"`
  69. Name string `json:"name"`
  70. Version string `json:"version"`
  71. }
  72. type PluginInfo struct {
  73. Author PluginInfoLink `json:"author"`
  74. Description string `json:"description"`
  75. Links []PluginInfoLink `json:"links"`
  76. Logos PluginLogos `json:"logos"`
  77. Screenshots []PluginScreenshots `json:"screenshots"`
  78. Version string `json:"version"`
  79. Updated string `json:"updated"`
  80. }
  81. type PluginInfoLink struct {
  82. Name string `json:"name"`
  83. Url string `json:"url"`
  84. }
  85. type PluginLogos struct {
  86. Small string `json:"small"`
  87. Large string `json:"large"`
  88. }
  89. type PluginScreenshots struct {
  90. Path string `json:"path"`
  91. Name string `json:"name"`
  92. }
  93. type PluginStaticRoute struct {
  94. Directory string
  95. PluginId string
  96. }
  97. type EnabledPlugins struct {
  98. Panels []*PanelPlugin
  99. DataSources map[string]*DataSourcePlugin
  100. Apps []*AppPlugin
  101. }
  102. func NewEnabledPlugins() EnabledPlugins {
  103. return EnabledPlugins{
  104. Panels: make([]*PanelPlugin, 0),
  105. DataSources: make(map[string]*DataSourcePlugin),
  106. Apps: make([]*AppPlugin, 0),
  107. }
  108. }