2022-06-28 00:23:15 +08:00
|
|
|
package datasources
|
2014-12-16 19:04:08 +08:00
|
|
|
|
2014-12-19 03:26:06 +08:00
|
|
|
import (
|
|
|
|
|
"time"
|
2016-03-12 06:28:33 +08:00
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2022-11-15 03:08:10 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2022-08-10 17:56:48 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2014-12-19 03:26:06 +08:00
|
|
|
)
|
2014-12-16 19:04:08 +08:00
|
|
|
|
|
|
|
|
const (
|
2021-03-04 22:43:43 +08:00
|
|
|
DS_GRAPHITE = "graphite"
|
|
|
|
|
DS_INFLUXDB = "influxdb"
|
|
|
|
|
DS_INFLUXDB_08 = "influxdb_08"
|
|
|
|
|
DS_ES = "elasticsearch"
|
|
|
|
|
DS_PROMETHEUS = "prometheus"
|
2022-01-31 23:39:55 +08:00
|
|
|
DS_ALERTMANAGER = "alertmanager"
|
|
|
|
|
DS_JAEGER = "jaeger"
|
|
|
|
|
DS_LOKI = "loki"
|
|
|
|
|
DS_OPENTSDB = "opentsdb"
|
|
|
|
|
DS_TEMPO = "tempo"
|
|
|
|
|
DS_ZIPKIN = "zipkin"
|
2021-03-04 22:43:43 +08:00
|
|
|
DS_MYSQL = "mysql"
|
2022-01-31 23:39:55 +08:00
|
|
|
DS_POSTGRES = "postgres"
|
|
|
|
|
DS_MSSQL = "mssql"
|
2021-03-04 22:43:43 +08:00
|
|
|
DS_ACCESS_DIRECT = "direct"
|
|
|
|
|
DS_ACCESS_PROXY = "proxy"
|
|
|
|
|
DS_ES_OPEN_DISTRO = "grafana-es-open-distro-datasource"
|
2021-10-11 22:53:04 +08:00
|
|
|
DS_ES_OPENSEARCH = "grafana-opensearch-datasource"
|
2022-12-14 23:09:11 +08:00
|
|
|
DS_AZURE_MONITOR = "grafana-azure-monitor-datasource"
|
2023-07-21 02:44:12 +08:00
|
|
|
DS_TESTDATA = "testdata"
|
2023-04-19 23:04:30 +08:00
|
|
|
// CustomHeaderName is the prefix that is used to store the name of a custom header.
|
|
|
|
|
CustomHeaderName = "httpHeaderName"
|
|
|
|
|
// CustomHeaderValue is the prefix that is used to store the value of a custom header.
|
|
|
|
|
CustomHeaderValue = "httpHeaderValue"
|
2014-12-16 19:04:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DsAccess string
|
|
|
|
|
|
|
|
|
|
type DataSource struct {
|
2023-02-03 00:22:43 +08:00
|
|
|
ID int64 `json:"id,omitempty" xorm:"pk autoincr 'id'"`
|
|
|
|
|
OrgID int64 `json:"orgId,omitempty" xorm:"org_id"`
|
2022-07-06 01:54:07 +08:00
|
|
|
Version int `json:"version,omitempty"`
|
2014-12-16 19:04:08 +08:00
|
|
|
|
2022-06-03 23:38:22 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Access DsAccess `json:"access"`
|
2023-02-03 00:22:43 +08:00
|
|
|
URL string `json:"url" xorm:"url"`
|
2022-06-03 23:38:22 +08:00
|
|
|
// swagger:ignore
|
|
|
|
|
Password string `json:"-"`
|
|
|
|
|
User string `json:"user"`
|
|
|
|
|
Database string `json:"database"`
|
|
|
|
|
BasicAuth bool `json:"basicAuth"`
|
|
|
|
|
BasicAuthUser string `json:"basicAuthUser"`
|
|
|
|
|
// swagger:ignore
|
|
|
|
|
BasicAuthPassword string `json:"-"`
|
2021-10-07 22:33:50 +08:00
|
|
|
WithCredentials bool `json:"withCredentials"`
|
|
|
|
|
IsDefault bool `json:"isDefault"`
|
|
|
|
|
JsonData *simplejson.Json `json:"jsonData"`
|
|
|
|
|
SecureJsonData map[string][]byte `json:"secureJsonData"`
|
|
|
|
|
ReadOnly bool `json:"readOnly"`
|
2023-02-03 00:22:43 +08:00
|
|
|
UID string `json:"uid" xorm:"uid"`
|
2014-12-16 19:04:08 +08:00
|
|
|
|
2022-07-06 01:54:07 +08:00
|
|
|
Created time.Time `json:"created,omitempty"`
|
|
|
|
|
Updated time.Time `json:"updated,omitempty"`
|
2014-12-16 19:04:08 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-31 23:58:06 +08:00
|
|
|
// AllowedCookies parses the jsondata.keepCookies and returns a list of
|
|
|
|
|
// allowed cookies, otherwise an empty list.
|
|
|
|
|
func (ds DataSource) AllowedCookies() []string {
|
|
|
|
|
if ds.JsonData != nil {
|
|
|
|
|
if keepCookies := ds.JsonData.Get("keepCookies"); keepCookies != nil {
|
|
|
|
|
return keepCookies.MustStringArray()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return []string{}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 00:26:57 +08:00
|
|
|
// Specific error type for grpc secrets management so that we can show more detailed plugin errors to users
|
|
|
|
|
type ErrDatasourceSecretsPluginUserFriendly struct {
|
|
|
|
|
Err string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e ErrDatasourceSecretsPluginUserFriendly) Error() string {
|
|
|
|
|
return e.Err
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 18:01:37 +08:00
|
|
|
// ----------------------
|
|
|
|
|
// COMMANDS
|
2014-12-19 03:26:06 +08:00
|
|
|
|
2015-01-09 23:36:23 +08:00
|
|
|
// Also acts as api DTO
|
2014-12-16 23:45:07 +08:00
|
|
|
type AddDataSourceCommand struct {
|
2022-06-03 23:38:22 +08:00
|
|
|
Name string `json:"name" binding:"Required"`
|
|
|
|
|
Type string `json:"type" binding:"Required"`
|
|
|
|
|
Access DsAccess `json:"access" binding:"Required"`
|
2023-02-03 00:22:43 +08:00
|
|
|
URL string `json:"url"`
|
2022-06-03 23:38:22 +08:00
|
|
|
Database string `json:"database"`
|
|
|
|
|
User string `json:"user"`
|
|
|
|
|
BasicAuth bool `json:"basicAuth"`
|
|
|
|
|
BasicAuthUser string `json:"basicAuthUser"`
|
|
|
|
|
WithCredentials bool `json:"withCredentials"`
|
|
|
|
|
IsDefault bool `json:"isDefault"`
|
|
|
|
|
JsonData *simplejson.Json `json:"jsonData"`
|
|
|
|
|
SecureJsonData map[string]string `json:"secureJsonData"`
|
2023-02-03 00:22:43 +08:00
|
|
|
UID string `json:"uid"`
|
2015-03-02 16:58:35 +08:00
|
|
|
|
2023-02-03 00:22:43 +08:00
|
|
|
OrgID int64 `json:"-"`
|
|
|
|
|
UserID int64 `json:"-"`
|
2021-10-07 22:33:50 +08:00
|
|
|
ReadOnly bool `json:"-"`
|
|
|
|
|
EncryptedSecureJsonData map[string][]byte `json:"-"`
|
2022-06-17 00:26:57 +08:00
|
|
|
UpdateSecretFn UpdateSecretFn `json:"-"`
|
2014-12-16 23:45:07 +08:00
|
|
|
}
|
2014-12-18 00:32:22 +08:00
|
|
|
|
2015-01-09 23:36:23 +08:00
|
|
|
// Also acts as api DTO
|
2014-12-18 00:32:22 +08:00
|
|
|
type UpdateDataSourceCommand struct {
|
2022-06-03 23:38:22 +08:00
|
|
|
Name string `json:"name" binding:"Required"`
|
|
|
|
|
Type string `json:"type" binding:"Required"`
|
|
|
|
|
Access DsAccess `json:"access" binding:"Required"`
|
2023-02-03 00:22:43 +08:00
|
|
|
URL string `json:"url"`
|
2022-06-03 23:38:22 +08:00
|
|
|
User string `json:"user"`
|
|
|
|
|
Database string `json:"database"`
|
|
|
|
|
BasicAuth bool `json:"basicAuth"`
|
|
|
|
|
BasicAuthUser string `json:"basicAuthUser"`
|
|
|
|
|
WithCredentials bool `json:"withCredentials"`
|
|
|
|
|
IsDefault bool `json:"isDefault"`
|
|
|
|
|
JsonData *simplejson.Json `json:"jsonData"`
|
|
|
|
|
SecureJsonData map[string]string `json:"secureJsonData"`
|
|
|
|
|
Version int `json:"version"`
|
2023-02-03 00:22:43 +08:00
|
|
|
UID string `json:"uid"`
|
2015-03-01 00:27:30 +08:00
|
|
|
|
2023-02-03 00:22:43 +08:00
|
|
|
OrgID int64 `json:"-"`
|
|
|
|
|
ID int64 `json:"-"`
|
2021-10-07 22:33:50 +08:00
|
|
|
ReadOnly bool `json:"-"`
|
|
|
|
|
EncryptedSecureJsonData map[string][]byte `json:"-"`
|
2022-06-17 00:26:57 +08:00
|
|
|
UpdateSecretFn UpdateSecretFn `json:"-"`
|
2023-07-27 17:11:43 +08:00
|
|
|
IgnoreOldSecureJsonData bool `json:"-"`
|
2014-12-18 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 02:16:27 +08:00
|
|
|
// DeleteDataSourceCommand will delete a DataSource based on OrgID as well as the UID (preferred), ID, or Name.
|
|
|
|
|
// At least one of the UID, ID, or Name properties must be set in addition to OrgID.
|
|
|
|
|
type DeleteDataSourceCommand struct {
|
|
|
|
|
ID int64
|
|
|
|
|
UID string
|
|
|
|
|
Name string
|
2015-01-09 18:01:37 +08:00
|
|
|
|
2021-01-14 02:16:27 +08:00
|
|
|
OrgID int64
|
2017-10-26 23:13:07 +08:00
|
|
|
|
|
|
|
|
DeletedDatasourcesCount int64
|
2022-06-17 00:26:57 +08:00
|
|
|
|
|
|
|
|
UpdateSecretFn UpdateSecretFn
|
2017-02-10 10:11:36 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-17 00:26:57 +08:00
|
|
|
// Function for updating secrets along with datasources, to ensure atomicity
|
|
|
|
|
type UpdateSecretFn func() error
|
|
|
|
|
|
2015-01-09 18:01:37 +08:00
|
|
|
// ---------------------
|
|
|
|
|
// QUERIES
|
|
|
|
|
|
|
|
|
|
type GetDataSourcesQuery struct {
|
2023-02-03 00:22:43 +08:00
|
|
|
OrgID int64
|
2020-12-28 19:24:42 +08:00
|
|
|
DataSourceLimit int
|
2022-08-10 17:56:48 +08:00
|
|
|
User *user.SignedInUser
|
2015-01-09 18:01:37 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-21 02:18:21 +08:00
|
|
|
type GetAllDataSourcesQuery struct{}
|
2022-07-13 04:27:37 +08:00
|
|
|
|
2021-03-17 17:14:53 +08:00
|
|
|
type GetDataSourcesByTypeQuery struct {
|
2023-02-09 22:49:44 +08:00
|
|
|
OrgID int64 // optional: filter by org_id
|
|
|
|
|
Type string
|
2021-03-17 17:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-08 04:33:17 +08:00
|
|
|
type GetDefaultDataSourceQuery struct {
|
2023-02-09 22:49:44 +08:00
|
|
|
OrgID int64
|
|
|
|
|
User *user.SignedInUser
|
2021-01-08 04:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 02:16:27 +08:00
|
|
|
// GetDataSourceQuery will get a DataSource based on OrgID as well as the UID (preferred), ID, or Name.
|
|
|
|
|
// At least one of the UID, ID, or Name properties must be set in addition to OrgID.
|
|
|
|
|
type GetDataSourceQuery struct {
|
2023-02-03 00:22:43 +08:00
|
|
|
ID int64
|
|
|
|
|
UID string
|
2021-01-14 02:16:27 +08:00
|
|
|
Name string
|
|
|
|
|
|
2023-02-03 00:22:43 +08:00
|
|
|
OrgID int64
|
2015-02-16 03:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-10 20:21:57 +08:00
|
|
|
type DatasourcesPermissionFilterQuery struct {
|
2022-08-10 17:56:48 +08:00
|
|
|
User *user.SignedInUser
|
2018-10-10 20:21:57 +08:00
|
|
|
Datasources []*DataSource
|
|
|
|
|
}
|
2022-11-15 03:08:10 +08:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
QuotaTargetSrv quota.TargetSrv = "data_source"
|
|
|
|
|
QuotaTarget quota.Target = "data_source"
|
|
|
|
|
)
|