models.go 3.7 KB

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