models.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. StaticRoot string `json:"staticRoot"`
  35. StaticRootAbs string `json:"-"`
  36. IncludedInAppId string `json:"-"`
  37. PluginDir string `json:"-"`
  38. // cache for readme file contents
  39. Readme []byte `json:"-"`
  40. }
  41. func (pb *PluginBase) registerPlugin(pluginDir string) error {
  42. if _, exists := Plugins[pb.Id]; exists {
  43. return errors.New("Plugin with same id already exists")
  44. }
  45. if !strings.HasPrefix(pluginDir, setting.StaticRootPath) {
  46. log.Info("Plugins: Registering plugin %v", pb.Name)
  47. }
  48. if len(pb.Dependencies.Plugins) == 0 {
  49. pb.Dependencies.Plugins = []PluginDependencyItem{}
  50. }
  51. if pb.Dependencies.GrafanaVersion == "" {
  52. pb.Dependencies.GrafanaVersion = "*"
  53. }
  54. pb.PluginDir = pluginDir
  55. Plugins[pb.Id] = pb
  56. return nil
  57. }
  58. type PluginDependencies struct {
  59. GrafanaVersion string `json:"grafanaVersion"`
  60. Plugins []PluginDependencyItem `json:"plugins"`
  61. }
  62. type PluginInclude struct {
  63. Name string `json:"name"`
  64. Path string `json:"path"`
  65. Type string `json:"type"`
  66. Id string `json:"id"`
  67. }
  68. type PluginDependencyItem struct {
  69. Type string `json:"type"`
  70. Id string `json:"id"`
  71. Name string `json:"name"`
  72. Version string `json:"version"`
  73. }
  74. type PluginInfo struct {
  75. Author PluginInfoLink `json:"author"`
  76. Description string `json:"description"`
  77. Links []PluginInfoLink `json:"links"`
  78. Logos PluginLogos `json:"logos"`
  79. Screenshots []PluginScreenshots `json:"screenshots"`
  80. Version string `json:"version"`
  81. Updated string `json:"updated"`
  82. }
  83. type PluginInfoLink struct {
  84. Name string `json:"name"`
  85. Url string `json:"url"`
  86. }
  87. type PluginLogos struct {
  88. Small string `json:"small"`
  89. Large string `json:"large"`
  90. }
  91. type PluginScreenshots struct {
  92. Path string `json:"path"`
  93. Name string `json:"name"`
  94. }
  95. type PluginStaticRoute struct {
  96. Directory string
  97. PluginId string
  98. }
  99. type EnabledPlugins struct {
  100. Panels []*PanelPlugin
  101. DataSources map[string]*DataSourcePlugin
  102. Apps []*AppPlugin
  103. }
  104. func NewEnabledPlugins() EnabledPlugins {
  105. return EnabledPlugins{
  106. Panels: make([]*PanelPlugin, 0),
  107. DataSources: make(map[string]*DataSourcePlugin),
  108. Apps: make([]*AppPlugin, 0),
  109. }
  110. }