models.go 3.4 KB

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