models.go 3.6 KB

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