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)
|
2014-12-16 09:45:07 -06:00
|
|
|
bus.AddHandler("sql", AddDataSource)
|
2017-02-09 20:11:36 -06:00
|
|
|
bus.AddHandler("sql", DeleteDataSourceById)
|
|
|
|
bus.AddHandler("sql", DeleteDataSourceByName)
|
2014-12-17 10:32:22 -06:00
|
|
|
bus.AddHandler("sql", UpdateDataSource)
|
2014-12-19 00:27:25 -06:00
|
|
|
bus.AddHandler("sql", GetDataSourceById)
|
2015-02-15 13:10:34 -06:00
|
|
|
bus.AddHandler("sql", GetDataSourceByName)
|
2014-12-18 13:26:06 -06:00
|
|
|
}
|
|
|
|
|
2020-08-21 02:30:06 -05:00
|
|
|
func getDataSourceByID(id, orgID int64, engine *xorm.Engine) (*models.DataSource, error) {
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MDBDataSourceQueryByID.Inc()
|
2017-08-23 06:31:26 -05:00
|
|
|
|
2020-08-21 02:30:06 -05:00
|
|
|
datasource := models.DataSource{OrgId: orgID, Id: id}
|
|
|
|
has, err := engine.Get(&datasource)
|
2018-02-14 06:32:52 -06:00
|
|
|
if err != nil {
|
2020-08-21 02:30:06 -05:00
|
|
|
sqlog.Error("Failed getting data source", "err", err, "id", id, "orgId", orgID)
|
|
|
|
return nil, err
|
2018-02-14 06:32:52 -06:00
|
|
|
}
|
2015-02-15 13:10:34 -06:00
|
|
|
if !has {
|
2020-08-21 02:30:06 -05:00
|
|
|
sqlog.Debug("Failed to find data source", "id", id, "orgId", orgID)
|
|
|
|
return nil, models.ErrDataSourceNotFound
|
2015-02-15 13:10:34 -06:00
|
|
|
}
|
2016-06-06 10:11:46 -05:00
|
|
|
|
2020-08-21 02:30:06 -05:00
|
|
|
return &datasource, nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
func (ss *SQLStore) GetDataSourceByID(id, orgID int64) (*models.DataSource, error) {
|
2020-08-21 02:30:06 -05:00
|
|
|
return getDataSourceByID(id, orgID, ss.engine)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDataSourceById(query *models.GetDataSourceByIdQuery) error {
|
|
|
|
ds, err := getDataSourceByID(query.Id, query.OrgId, x)
|
|
|
|
query.Result = ds
|
|
|
|
|
2015-02-15 13:10:34 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetDataSourceByName(query *models.GetDataSourceByNameQuery) error {
|
|
|
|
datasource := models.DataSource{OrgId: query.OrgId, Name: query.Name}
|
2016-06-06 10:11:46 -05:00
|
|
|
has, err := x.Get(&datasource)
|
2015-02-15 13:10:34 -06:00
|
|
|
|
2014-12-18 13:26:06 -06:00
|
|
|
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
|
2014-12-18 13:26:06 -06:00
|
|
|
return err
|
2014-12-16 05:04:08 -06:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func GetDataSources(query *models.GetDataSourcesQuery) error {
|
2018-12-18 09:59:14 -06:00
|
|
|
sess := x.Limit(5000, 0).Where("org_id=?", query.OrgId).Asc("name")
|
2014-12-16 14:05:49 -06:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func DeleteDataSourceById(cmd *models.DeleteDataSourceByIdCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawSQL = "DELETE FROM data_source WHERE id=? and org_id=?"
|
|
|
|
result, err := sess.Exec(rawSQL, cmd.Id, cmd.OrgId)
|
2017-10-26 10:13:07 -05:00
|
|
|
affected, _ := result.RowsAffected()
|
|
|
|
cmd.DeletedDatasourcesCount = affected
|
2014-12-17 10:32:22 -06:00
|
|
|
return err
|
|
|
|
})
|
2014-12-16 05:04:08 -06:00
|
|
|
}
|
2014-12-16 09:45:07 -06:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func DeleteDataSourceByName(cmd *models.DeleteDataSourceByNameCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawSQL = "DELETE FROM data_source WHERE name=? and org_id=?"
|
|
|
|
result, err := sess.Exec(rawSQL, cmd.Name, cmd.OrgId)
|
2017-10-26 10:13:07 -05:00
|
|
|
affected, _ := result.RowsAffected()
|
|
|
|
cmd.DeletedDatasourcesCount = affected
|
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
|
|
|
|
}
|