models.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/log"
  8. m "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. for _, include := range pb.Includes {
  58. if include.Role == "" {
  59. include.Role = m.RoleType(m.ROLE_VIEWER)
  60. }
  61. }
  62. pb.PluginDir = pluginDir
  63. Plugins[pb.Id] = pb
  64. return nil
  65. }
  66. type PluginDependencies struct {
  67. GrafanaVersion string `json:"grafanaVersion"`
  68. Plugins []PluginDependencyItem `json:"plugins"`
  69. }
  70. type PluginInclude struct {
  71. Name string `json:"name"`
  72. Path string `json:"path"`
  73. Type string `json:"type"`
  74. Component string `json:"component"`
  75. Role m.RoleType `json:"role"`
  76. AddToNav bool `json:"addToNav"`
  77. DefaultNav bool `json:"defaultNav"`
  78. Slug string `json:"slug"`
  79. Id string `json:"-"`
  80. }
  81. type PluginDependencyItem struct {
  82. Type string `json:"type"`
  83. Id string `json:"id"`
  84. Name string `json:"name"`
  85. Version string `json:"version"`
  86. }
  87. type PluginInfo struct {
  88. Author PluginInfoLink `json:"author"`
  89. Description string `json:"description"`
  90. Links []PluginInfoLink `json:"links"`
  91. Logos PluginLogos `json:"logos"`
  92. Screenshots []PluginScreenshots `json:"screenshots"`
  93. Version string `json:"version"`
  94. Updated string `json:"updated"`
  95. }
  96. type PluginInfoLink struct {
  97. Name string `json:"name"`
  98. Url string `json:"url"`
  99. }
  100. type PluginLogos struct {
  101. Small string `json:"small"`
  102. Large string `json:"large"`
  103. }
  104. type PluginScreenshots struct {
  105. Path string `json:"path"`
  106. Name string `json:"name"`
  107. }
  108. type PluginStaticRoute struct {
  109. Directory string
  110. PluginId string
  111. }
  112. type EnabledPlugins struct {
  113. Panels []*PanelPlugin
  114. DataSources map[string]*DataSourcePlugin
  115. Apps []*AppPlugin
  116. }
  117. func NewEnabledPlugins() EnabledPlugins {
  118. return EnabledPlugins{
  119. Panels: make([]*PanelPlugin, 0),
  120. DataSources: make(map[string]*DataSourcePlugin),
  121. Apps: make([]*AppPlugin, 0),
  122. }
  123. }