models.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // 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. Component string `json:"component"`
  67. Role models.RoleType `json:"role"`
  68. AddToNav bool `json:"addToNav"`
  69. DefaultNav bool `json:"defaultNav"`
  70. Slug string `json:"slug"`
  71. Id string `json:"-"`
  72. }
  73. type PluginDependencyItem struct {
  74. Type string `json:"type"`
  75. Id string `json:"id"`
  76. Name string `json:"name"`
  77. Version string `json:"version"`
  78. }
  79. type PluginInfo struct {
  80. Author PluginInfoLink `json:"author"`
  81. Description string `json:"description"`
  82. Links []PluginInfoLink `json:"links"`
  83. Logos PluginLogos `json:"logos"`
  84. Screenshots []PluginScreenshots `json:"screenshots"`
  85. Version string `json:"version"`
  86. Updated string `json:"updated"`
  87. }
  88. type PluginInfoLink struct {
  89. Name string `json:"name"`
  90. Url string `json:"url"`
  91. }
  92. type PluginLogos struct {
  93. Small string `json:"small"`
  94. Large string `json:"large"`
  95. }
  96. type PluginScreenshots struct {
  97. Path string `json:"path"`
  98. Name string `json:"name"`
  99. }
  100. type PluginStaticRoute struct {
  101. Directory string
  102. PluginId string
  103. }
  104. type EnabledPlugins struct {
  105. Panels []*PanelPlugin
  106. DataSources map[string]*DataSourcePlugin
  107. Apps []*AppPlugin
  108. }
  109. func NewEnabledPlugins() EnabledPlugins {
  110. return EnabledPlugins{
  111. Panels: make([]*PanelPlugin, 0),
  112. DataSources: make(map[string]*DataSourcePlugin),
  113. Apps: make([]*AppPlugin, 0),
  114. }
  115. }