models.go 3.6 KB

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