models.go 3.3 KB

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