models.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 PluginState string
  17. var (
  18. PluginStateAlpha PluginState = "alpha"
  19. PluginStateBeta PluginState = "beta"
  20. )
  21. type PluginNotFoundError struct {
  22. PluginId string
  23. }
  24. func (e PluginNotFoundError) Error() string {
  25. return fmt.Sprintf("Plugin with id %s not found", e.PluginId)
  26. }
  27. type PluginLoader interface {
  28. Load(decoder *json.Decoder, pluginDir string) error
  29. }
  30. type PluginBase struct {
  31. Type string `json:"type"`
  32. Name string `json:"name"`
  33. Id string `json:"id"`
  34. Info PluginInfo `json:"info"`
  35. Dependencies PluginDependencies `json:"dependencies"`
  36. Includes []*PluginInclude `json:"includes"`
  37. Module string `json:"module"`
  38. BaseUrl string `json:"baseUrl"`
  39. Category string `json:"category"`
  40. HideFromList bool `json:"hideFromList,omitempty"`
  41. Preload bool `json:"preload"`
  42. State PluginState `json:"state,omitempty"`
  43. IncludedInAppId string `json:"-"`
  44. PluginDir string `json:"-"`
  45. DefaultNavUrl string `json:"-"`
  46. IsCorePlugin bool `json:"-"`
  47. GrafanaNetVersion string `json:"-"`
  48. GrafanaNetHasUpdate bool `json:"-"`
  49. }
  50. func (pb *PluginBase) registerPlugin(pluginDir string) error {
  51. if _, exists := Plugins[pb.Id]; exists {
  52. return errors.New("Plugin with same id already exists")
  53. }
  54. if !strings.HasPrefix(pluginDir, setting.StaticRootPath) {
  55. plog.Info("Registering plugin", "name", pb.Name)
  56. }
  57. if len(pb.Dependencies.Plugins) == 0 {
  58. pb.Dependencies.Plugins = []PluginDependencyItem{}
  59. }
  60. if pb.Dependencies.GrafanaVersion == "" {
  61. pb.Dependencies.GrafanaVersion = "*"
  62. }
  63. for _, include := range pb.Includes {
  64. if include.Role == "" {
  65. include.Role = m.ROLE_VIEWER
  66. }
  67. }
  68. pb.PluginDir = pluginDir
  69. Plugins[pb.Id] = pb
  70. return nil
  71. }
  72. type PluginDependencies struct {
  73. GrafanaVersion string `json:"grafanaVersion"`
  74. Plugins []PluginDependencyItem `json:"plugins"`
  75. }
  76. type PluginInclude struct {
  77. Name string `json:"name"`
  78. Path string `json:"path"`
  79. Type string `json:"type"`
  80. Component string `json:"component"`
  81. Role m.RoleType `json:"role"`
  82. AddToNav bool `json:"addToNav"`
  83. DefaultNav bool `json:"defaultNav"`
  84. Slug string `json:"slug"`
  85. Id string `json:"-"`
  86. }
  87. type PluginDependencyItem struct {
  88. Type string `json:"type"`
  89. Id string `json:"id"`
  90. Name string `json:"name"`
  91. Version string `json:"version"`
  92. }
  93. type PluginBuildInfo struct {
  94. Time int64 `json:"time,omitempty"`
  95. Repo string `json:"repo,omitempty"`
  96. Branch string `json:"branch,omitempty"`
  97. Hash string `json:"hash,omitempty"`
  98. }
  99. type PluginInfo struct {
  100. Author PluginInfoLink `json:"author"`
  101. Description string `json:"description"`
  102. Links []PluginInfoLink `json:"links"`
  103. Logos PluginLogos `json:"logos"`
  104. Build PluginBuildInfo `json:"build"`
  105. Screenshots []PluginScreenshots `json:"screenshots"`
  106. Version string `json:"version"`
  107. Updated string `json:"updated"`
  108. }
  109. type PluginInfoLink struct {
  110. Name string `json:"name"`
  111. Url string `json:"url"`
  112. }
  113. type PluginLogos struct {
  114. Small string `json:"small"`
  115. Large string `json:"large"`
  116. }
  117. type PluginScreenshots struct {
  118. Path string `json:"path"`
  119. Name string `json:"name"`
  120. }
  121. type PluginStaticRoute struct {
  122. Directory string
  123. PluginId string
  124. }
  125. type EnabledPlugins struct {
  126. Panels []*PanelPlugin
  127. DataSources map[string]*DataSourcePlugin
  128. Apps []*AppPlugin
  129. }
  130. func NewEnabledPlugins() EnabledPlugins {
  131. return EnabledPlugins{
  132. Panels: make([]*PanelPlugin, 0),
  133. DataSources: make(map[string]*DataSourcePlugin),
  134. Apps: make([]*AppPlugin, 0),
  135. }
  136. }