models.go 3.4 KB

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