plugin_settings.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/grafana/grafana/pkg/setting"
  6. "github.com/grafana/grafana/pkg/util"
  7. )
  8. var (
  9. ErrPluginSettingNotFound = errors.New("Plugin setting not found")
  10. )
  11. type PluginSetting struct {
  12. Id int64
  13. PluginId string
  14. OrgId int64
  15. Enabled bool
  16. Pinned bool
  17. JsonData map[string]interface{}
  18. SecureJsonData SecureJsonData
  19. Created time.Time
  20. Updated time.Time
  21. }
  22. type SecureJsonData map[string][]byte
  23. func (s SecureJsonData) Decrypt() map[string]string {
  24. decrypted := make(map[string]string)
  25. for key, data := range s {
  26. decrypted[key] = string(util.Decrypt(data, setting.SecretKey))
  27. }
  28. return decrypted
  29. }
  30. // ----------------------
  31. // COMMANDS
  32. // Also acts as api DTO
  33. type UpdatePluginSettingCmd struct {
  34. Enabled bool `json:"enabled"`
  35. Pinned bool `json:"pinned"`
  36. JsonData map[string]interface{} `json:"jsonData"`
  37. SecureJsonData map[string]string `json:"secureJsonData"`
  38. PluginId string `json:"-"`
  39. OrgId int64 `json:"-"`
  40. }
  41. func (cmd *UpdatePluginSettingCmd) GetEncryptedJsonData() SecureJsonData {
  42. encrypted := make(SecureJsonData)
  43. for key, data := range cmd.SecureJsonData {
  44. encrypted[key] = util.Encrypt([]byte(data), setting.SecretKey)
  45. }
  46. return encrypted
  47. }
  48. // ---------------------
  49. // QUERIES
  50. type GetPluginSettingsQuery struct {
  51. OrgId int64
  52. Result []*PluginSettingInfoDTO
  53. }
  54. type PluginSettingInfoDTO struct {
  55. OrgId int64
  56. PluginId string
  57. Enabled bool
  58. Pinned bool
  59. }
  60. type GetPluginSettingByIdQuery struct {
  61. PluginId string
  62. OrgId int64
  63. Result *PluginSetting
  64. }