datasource.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/grafana/grafana/pkg/components/securejsondata"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. )
  8. const (
  9. DS_GRAPHITE = "graphite"
  10. DS_INFLUXDB = "influxdb"
  11. DS_INFLUXDB_08 = "influxdb_08"
  12. DS_ES = "elasticsearch"
  13. DS_OPENTSDB = "opentsdb"
  14. DS_CLOUDWATCH = "cloudwatch"
  15. DS_KAIROSDB = "kairosdb"
  16. DS_PROMETHEUS = "prometheus"
  17. DS_POSTGRES = "postgres"
  18. DS_MYSQL = "mysql"
  19. DS_MSSQL = "mssql"
  20. DS_ACCESS_DIRECT = "direct"
  21. DS_ACCESS_PROXY = "proxy"
  22. DS_STACKDRIVER = "stackdriver"
  23. )
  24. var (
  25. ErrDataSourceNotFound = errors.New("Data source not found")
  26. ErrDataSourceNameExists = errors.New("Data source with same name already exists")
  27. ErrDataSourceUpdatingOldVersion = errors.New("Trying to update old version of datasource")
  28. ErrDatasourceIsReadOnly = errors.New("Data source is readonly. Can only be updated from configuration.")
  29. ErrDataSourceAccessDenied = errors.New("Data source access denied")
  30. )
  31. type DsAccess string
  32. type DataSource struct {
  33. Id int64
  34. OrgId int64
  35. Version int
  36. Name string
  37. Type string
  38. Access DsAccess
  39. Url string
  40. Password string
  41. User string
  42. Database string
  43. BasicAuth bool
  44. BasicAuthUser string
  45. BasicAuthPassword string
  46. WithCredentials bool
  47. IsDefault bool
  48. JsonData *simplejson.Json
  49. SecureJsonData securejsondata.SecureJsonData
  50. ReadOnly bool
  51. Created time.Time
  52. Updated time.Time
  53. }
  54. var knownDatasourcePlugins = map[string]bool{
  55. DS_ES: true,
  56. DS_GRAPHITE: true,
  57. DS_INFLUXDB: true,
  58. DS_INFLUXDB_08: true,
  59. DS_KAIROSDB: true,
  60. DS_CLOUDWATCH: true,
  61. DS_PROMETHEUS: true,
  62. DS_OPENTSDB: true,
  63. DS_POSTGRES: true,
  64. DS_MYSQL: true,
  65. DS_MSSQL: true,
  66. DS_STACKDRIVER: true,
  67. "opennms": true,
  68. "abhisant-druid-datasource": true,
  69. "dalmatinerdb-datasource": true,
  70. "gnocci": true,
  71. "zabbix": true,
  72. "newrelic-app": true,
  73. "grafana-datadog-datasource": true,
  74. "grafana-simple-json": true,
  75. "grafana-splunk-datasource": true,
  76. "udoprog-heroic-datasource": true,
  77. "grafana-openfalcon-datasource": true,
  78. "opennms-datasource": true,
  79. "rackerlabs-blueflood-datasource": true,
  80. "crate-datasource": true,
  81. "ayoungprogrammer-finance-datasource": true,
  82. "monasca-datasource": true,
  83. "vertamedia-clickhouse-datasource": true,
  84. "alexanderzobnin-zabbix-datasource": true,
  85. }
  86. func IsKnownDataSourcePlugin(dsType string) bool {
  87. _, exists := knownDatasourcePlugins[dsType]
  88. return exists
  89. }
  90. // ----------------------
  91. // COMMANDS
  92. // Also acts as api DTO
  93. type AddDataSourceCommand struct {
  94. Name string `json:"name" binding:"Required"`
  95. Type string `json:"type" binding:"Required"`
  96. Access DsAccess `json:"access" binding:"Required"`
  97. Url string `json:"url"`
  98. Password string `json:"password"`
  99. Database string `json:"database"`
  100. User string `json:"user"`
  101. BasicAuth bool `json:"basicAuth"`
  102. BasicAuthUser string `json:"basicAuthUser"`
  103. BasicAuthPassword string `json:"basicAuthPassword"`
  104. WithCredentials bool `json:"withCredentials"`
  105. IsDefault bool `json:"isDefault"`
  106. JsonData *simplejson.Json `json:"jsonData"`
  107. SecureJsonData map[string]string `json:"secureJsonData"`
  108. ReadOnly bool `json:"readOnly"`
  109. OrgId int64 `json:"-"`
  110. Result *DataSource
  111. }
  112. // Also acts as api DTO
  113. type UpdateDataSourceCommand struct {
  114. Name string `json:"name" binding:"Required"`
  115. Type string `json:"type" binding:"Required"`
  116. Access DsAccess `json:"access" binding:"Required"`
  117. Url string `json:"url"`
  118. Password string `json:"password"`
  119. User string `json:"user"`
  120. Database string `json:"database"`
  121. BasicAuth bool `json:"basicAuth"`
  122. BasicAuthUser string `json:"basicAuthUser"`
  123. BasicAuthPassword string `json:"basicAuthPassword"`
  124. WithCredentials bool `json:"withCredentials"`
  125. IsDefault bool `json:"isDefault"`
  126. JsonData *simplejson.Json `json:"jsonData"`
  127. SecureJsonData map[string]string `json:"secureJsonData"`
  128. Version int `json:"version"`
  129. ReadOnly bool `json:"readOnly"`
  130. OrgId int64 `json:"-"`
  131. Id int64 `json:"-"`
  132. Result *DataSource
  133. }
  134. type DeleteDataSourceByIdCommand struct {
  135. Id int64
  136. OrgId int64
  137. DeletedDatasourcesCount int64
  138. }
  139. type DeleteDataSourceByNameCommand struct {
  140. Name string
  141. OrgId int64
  142. DeletedDatasourcesCount int64
  143. }
  144. // ---------------------
  145. // QUERIES
  146. type GetDataSourcesQuery struct {
  147. OrgId int64
  148. User *SignedInUser
  149. Result []*DataSource
  150. }
  151. type GetAllDataSourcesQuery struct {
  152. Result []*DataSource
  153. }
  154. type GetDataSourceByIdQuery struct {
  155. Id int64
  156. OrgId int64
  157. Result *DataSource
  158. }
  159. type GetDataSourceByNameQuery struct {
  160. Name string
  161. OrgId int64
  162. Result *DataSource
  163. }
  164. // ---------------------
  165. // Permissions
  166. // ---------------------
  167. type DsPermissionType int
  168. const (
  169. DsPermissionNoAccess DsPermissionType = iota
  170. DsPermissionQuery
  171. )
  172. func (p DsPermissionType) String() string {
  173. names := map[int]string{
  174. int(DsPermissionQuery): "Query",
  175. int(DsPermissionNoAccess): "No Access",
  176. }
  177. return names[int(p)]
  178. }
  179. type GetDataSourcePermissionsForUserQuery struct {
  180. User *SignedInUser
  181. Result map[int64]DsPermissionType
  182. }
  183. type DatasourcesPermissionFilterQuery struct {
  184. User *SignedInUser
  185. Datasources []*DataSource
  186. Result []*DataSource
  187. }