models.go 3.5 KB

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