2014-12-16 05:04:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2020-04-20 08:48:38 -05:00
|
|
|
"strings"
|
2014-12-16 14:05:49 -06:00
|
|
|
"time"
|
|
|
|
|
2020-06-29 07:08:32 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
|
|
|
|
2019-03-02 14:18:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-03-02 14:18:26 -06:00
|
|
|
|
2020-04-01 08:57:21 -05:00
|
|
|
"xorm.io/xorm"
|
2017-10-19 10:28:54 -05:00
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-11-16 02:54:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/securejsondata"
|
2019-02-23 16:35:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2014-12-16 05:04:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-12-16 14:05:49 -06:00
|
|
|
bus.AddHandler("sql", GetDataSources)
|
2021-01-13 12:16:27 -06:00
|
|
|
bus.AddHandler("sql", GetDataSource)
|
2014-12-16 09:45:07 -06:00
|
|
|
bus.AddHandler("sql", AddDataSource)
|
2021-01-13 12:16:27 -06:00
|
|
|
bus.AddHandler("sql", DeleteDataSource)
|
2014-12-17 10:32:22 -06:00
|
|
|
bus.AddHandler("sql", UpdateDataSource)
|
2021-01-07 14:33:17 -06:00
|
|
|
bus.AddHandler("sql", GetDefaultDataSource)
|
2014-12-18 13:26:06 -06:00
|
|
|
}
|
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
// GetDataSource returns a datasource by org_id and either uid (preferred), id, or name.
|
|
|
|
// Zero values (0, or "") should be used for the parameters that will not be queried.
|
|
|
|
func (ss *SQLStore) GetDataSource(uid string, id int64, name string, orgID int64) (*models.DataSource, error) {
|
|
|
|
query := &models.GetDataSourceQuery{
|
|
|
|
Id: id,
|
|
|
|
Uid: uid,
|
|
|
|
Name: name,
|
|
|
|
OrgId: orgID,
|
|
|
|
}
|
2017-08-23 06:31:26 -05:00
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
if err := GetDataSource(query); err != nil {
|
2020-08-21 02:30:06 -05:00
|
|
|
return nil, err
|
2018-02-14 06:32:52 -06:00
|
|
|
}
|
2016-06-06 10:11:46 -05:00
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
return query.Result, nil
|
2020-08-21 02:30:06 -05:00
|
|
|
}
|
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
// GetDataSource adds a datasource to the query model by querying by org_id as well as
|
|
|
|
// either uid (preferred), id, or name and is added to the bus.
|
|
|
|
func GetDataSource(query *models.GetDataSourceQuery) error {
|
|
|
|
metrics.MDBDataSourceQueryByID.Inc()
|
|
|
|
if query.OrgId == 0 || (query.Id == 0 && len(query.Name) == 0 && len(query.Uid) == 0) {
|
|
|
|
return models.ErrDataSourceIdentifierNotSet
|
|
|
|
}
|
2015-02-15 13:10:34 -06:00
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
datasource := models.DataSource{Name: query.Name, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
|
2016-06-06 10:11:46 -05:00
|
|
|
has, err := x.Get(&datasource)
|
2015-02-15 13:10:34 -06:00
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
if err != nil {
|
|
|
|
sqlog.Error("Failed getting data source", "err", err, "uid", query.Uid, "id", query.Id, "name", query.Name, "orgId", query.OrgId)
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrDataSourceNotFound
|
2014-12-18 13:26:06 -06:00
|
|
|
}
|
2016-06-06 10:11:46 -05:00
|
|
|
|
|
|
|
query.Result = &datasource
|
2021-01-13 12:16:27 -06:00
|
|
|
return nil
|
2014-12-16 05:04:08 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetDataSources(query *models.GetDataSourcesQuery) error {
|
2020-12-28 05:24:42 -06:00
|
|
|
var sess *xorm.Session
|
|
|
|
if query.DataSourceLimit <= 0 {
|
|
|
|
sess = x.Where("org_id=?", query.OrgId).Asc("name")
|
|
|
|
} else {
|
|
|
|
sess = x.Limit(query.DataSourceLimit, 0).Where("org_id=?", query.OrgId).Asc("name")
|
|
|
|
}
|
2020-02-29 06:35:15 -06:00
|
|
|
query.Result = make([]*models.DataSource, 0)
|
2014-12-17 10:32:22 -06:00
|
|
|
return sess.Find(&query.Result)
|
|
|
|
}
|
|
|
|
|
2021-01-07 14:33:17 -06:00
|
|
|
// GetDefaultDataSource is used to get the default datasource of organization
|
|
|
|
func GetDefaultDataSource(query *models.GetDefaultDataSourceQuery) error {
|
|
|
|
datasource := models.DataSource{}
|
|
|
|
|
|
|
|
exists, err := x.Where("org_id=? AND is_default=?", query.OrgId, true).Get(&datasource)
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return models.ErrDataSourceNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &datasource
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
// DeleteDataSource deletes a datasource by org_id and either uid (preferred), id, or name.
|
|
|
|
// Zero values (0, or "") should be used for the parameters that will not be queried.
|
|
|
|
func (ss *SQLStore) DeleteDataSource(uid string, id int64, name string, orgID int64) (int64, error) {
|
|
|
|
cmd := &models.DeleteDataSourceCommand{
|
|
|
|
ID: id,
|
|
|
|
UID: uid,
|
|
|
|
Name: name,
|
|
|
|
OrgID: orgID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DeleteDataSource(cmd); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd.DeletedDatasourcesCount, nil
|
2014-12-16 05:04:08 -06:00
|
|
|
}
|
2014-12-16 09:45:07 -06:00
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
// DeleteDataSource removes a datasource by org_id as well as either uid (preferred), id, or name
|
|
|
|
// and is added to the bus.
|
|
|
|
func DeleteDataSource(cmd *models.DeleteDataSourceCommand) error {
|
|
|
|
params := make([]interface{}, 0)
|
|
|
|
|
|
|
|
makeQuery := func(sql string, p ...interface{}) {
|
|
|
|
params = append(params, sql)
|
|
|
|
params = append(params, p...)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case cmd.OrgID == 0:
|
|
|
|
return models.ErrDataSourceIdentifierNotSet
|
|
|
|
case cmd.UID != "":
|
|
|
|
makeQuery("DELETE FROM data_source WHERE uid=? and org_id=?", cmd.UID, cmd.OrgID)
|
|
|
|
case cmd.ID != 0:
|
|
|
|
makeQuery("DELETE FROM data_source WHERE id=? and org_id=?", cmd.ID, cmd.OrgID)
|
|
|
|
case cmd.Name != "":
|
|
|
|
makeQuery("DELETE FROM data_source WHERE name=? and org_id=?", cmd.Name, cmd.OrgID)
|
|
|
|
default:
|
|
|
|
return models.ErrDataSourceIdentifierNotSet
|
|
|
|
}
|
|
|
|
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2021-01-13 12:16:27 -06:00
|
|
|
result, err := sess.Exec(params...)
|
|
|
|
cmd.DeletedDatasourcesCount, _ = result.RowsAffected()
|
2017-02-09 20:11:36 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func AddDataSource(cmd *models.AddDataSourceCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2020-02-29 06:35:15 -06:00
|
|
|
existing := models.DataSource{OrgId: cmd.OrgId, Name: cmd.Name}
|
2016-10-01 10:36:56 -05:00
|
|
|
has, _ := sess.Get(&existing)
|
2016-10-01 09:41:27 -05:00
|
|
|
|
|
|
|
if has {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrDataSourceNameExists
|
2016-10-01 09:41:27 -05:00
|
|
|
}
|
|
|
|
|
2019-03-02 14:18:26 -06:00
|
|
|
if cmd.JsonData == nil {
|
|
|
|
cmd.JsonData = simplejson.New()
|
|
|
|
}
|
|
|
|
|
2020-04-20 08:48:38 -05:00
|
|
|
if cmd.Uid == "" {
|
|
|
|
uid, err := generateNewDatasourceUid(sess, cmd.OrgId)
|
|
|
|
if err != nil {
|
|
|
|
return errutil.Wrapf(err, "Failed to generate UID for datasource %q", cmd.Name)
|
|
|
|
}
|
|
|
|
cmd.Uid = uid
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
ds := &models.DataSource{
|
2015-03-02 02:58:35 -06:00
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Access: cmd.Access,
|
|
|
|
Url: cmd.Url,
|
|
|
|
User: cmd.User,
|
|
|
|
Password: cmd.Password,
|
|
|
|
Database: cmd.Database,
|
|
|
|
IsDefault: cmd.IsDefault,
|
|
|
|
BasicAuth: cmd.BasicAuth,
|
|
|
|
BasicAuthUser: cmd.BasicAuthUser,
|
|
|
|
BasicAuthPassword: cmd.BasicAuthPassword,
|
2016-03-16 09:16:28 -05:00
|
|
|
WithCredentials: cmd.WithCredentials,
|
2015-03-02 02:58:35 -06:00
|
|
|
JsonData: cmd.JsonData,
|
2016-11-16 02:54:26 -06:00
|
|
|
SecureJsonData: securejsondata.GetEncryptedJsonData(cmd.SecureJsonData),
|
2015-03-02 02:58:35 -06:00
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2017-10-19 10:28:54 -05:00
|
|
|
Version: 1,
|
2017-10-24 08:28:39 -05:00
|
|
|
ReadOnly: cmd.ReadOnly,
|
2020-04-20 08:48:38 -05:00
|
|
|
Uid: cmd.Uid,
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
if _, err := sess.Insert(ds); err != nil {
|
2020-04-20 08:48:38 -05:00
|
|
|
if dialect.IsUniqueConstraintViolation(err) && strings.Contains(strings.ToLower(dialect.ErrorMessage(err)), "uid") {
|
|
|
|
return models.ErrDataSourceUidExists
|
|
|
|
}
|
2015-01-09 09:36:23 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := updateIsDefaultFlag(ds, sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 10:32:22 -06:00
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
cmd.Result = ds
|
|
|
|
return nil
|
2014-12-17 10:32:22 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func updateIsDefaultFlag(ds *models.DataSource, sess *DBSession) error {
|
2015-01-09 09:36:23 -06:00
|
|
|
// Handle is default flag
|
|
|
|
if ds.IsDefault {
|
2020-11-10 23:21:08 -06:00
|
|
|
rawSQL := "UPDATE data_source SET is_default=? WHERE org_id=? AND id <> ?"
|
|
|
|
if _, err := sess.Exec(rawSQL, false, ds.OrgId, ds.Id); err != nil {
|
2015-01-09 09:36:23 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func UpdateDataSource(cmd *models.UpdateDataSourceCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2019-03-02 14:18:26 -06:00
|
|
|
if cmd.JsonData == nil {
|
|
|
|
cmd.JsonData = simplejson.New()
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
ds := &models.DataSource{
|
2015-03-02 02:58:35 -06:00
|
|
|
Id: cmd.Id,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Access: cmd.Access,
|
|
|
|
Url: cmd.Url,
|
|
|
|
User: cmd.User,
|
|
|
|
Password: cmd.Password,
|
|
|
|
Database: cmd.Database,
|
|
|
|
IsDefault: cmd.IsDefault,
|
|
|
|
BasicAuth: cmd.BasicAuth,
|
|
|
|
BasicAuthUser: cmd.BasicAuthUser,
|
|
|
|
BasicAuthPassword: cmd.BasicAuthPassword,
|
2015-12-08 21:37:08 -06:00
|
|
|
WithCredentials: cmd.WithCredentials,
|
2015-03-02 02:58:35 -06:00
|
|
|
JsonData: cmd.JsonData,
|
2016-11-16 02:54:26 -06:00
|
|
|
SecureJsonData: securejsondata.GetEncryptedJsonData(cmd.SecureJsonData),
|
2015-03-02 02:58:35 -06:00
|
|
|
Updated: time.Now(),
|
2017-10-24 08:28:39 -05:00
|
|
|
ReadOnly: cmd.ReadOnly,
|
2017-05-11 03:50:24 -05:00
|
|
|
Version: cmd.Version + 1,
|
2020-04-20 08:48:38 -05:00
|
|
|
Uid: cmd.Uid,
|
2015-01-09 09:36:23 -06:00
|
|
|
}
|
|
|
|
|
2019-03-11 01:22:54 -05:00
|
|
|
sess.UseBool("is_default")
|
|
|
|
sess.UseBool("basic_auth")
|
|
|
|
sess.UseBool("with_credentials")
|
|
|
|
sess.UseBool("read_only")
|
2019-04-15 04:11:17 -05:00
|
|
|
// Make sure password are zeroed out if empty. We do this as we want to migrate passwords from
|
|
|
|
// plain text fields to SecureJsonData.
|
|
|
|
sess.MustCols("password")
|
|
|
|
sess.MustCols("basic_auth_password")
|
2019-03-11 01:22:54 -05:00
|
|
|
|
2017-10-19 10:28:54 -05:00
|
|
|
var updateSession *xorm.Session
|
|
|
|
if cmd.Version != 0 {
|
2017-10-25 02:39:27 -05:00
|
|
|
// the reason we allow cmd.version > db.version is make it possible for people to force
|
|
|
|
// updates to datasources using the datasource.yaml file without knowing exactly what version
|
|
|
|
// a datasource have in the db.
|
|
|
|
updateSession = sess.Where("id=? and org_id=? and version < ?", ds.Id, ds.OrgId, ds.Version)
|
2017-10-19 10:28:54 -05:00
|
|
|
} else {
|
|
|
|
updateSession = sess.Where("id=? and org_id=?", ds.Id, ds.OrgId)
|
|
|
|
}
|
|
|
|
|
2019-03-11 01:22:54 -05:00
|
|
|
affected, err := updateSession.Update(ds)
|
2015-01-09 09:36:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|
|
|
|
|
2017-10-19 10:28:54 -05:00
|
|
|
if affected == 0 {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrDataSourceUpdatingOldVersion
|
2017-10-19 10:28:54 -05:00
|
|
|
}
|
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
err = updateIsDefaultFlag(ds, sess)
|
2017-10-19 10:28:54 -05:00
|
|
|
|
|
|
|
cmd.Result = ds
|
2014-12-16 14:05:49 -06:00
|
|
|
return err
|
|
|
|
})
|
2014-12-16 09:45:07 -06:00
|
|
|
}
|
2020-04-20 08:48:38 -05:00
|
|
|
|
|
|
|
func generateNewDatasourceUid(sess *DBSession, orgId int64) (string, error) {
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
uid := generateNewUid()
|
|
|
|
|
|
|
|
exists, err := sess.Where("org_id=? AND uid=?", orgId, uid).Get(&models.DataSource{})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return uid, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", models.ErrDataSourceFailedGenerateUniqueUid
|
|
|
|
}
|