models.go 3.7 KB

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