2014-12-16 05:04:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2014-12-16 14:05:49 -06:00
|
|
|
"time"
|
|
|
|
|
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)
|
2017-10-09 13:36:31 -05:00
|
|
|
bus.AddHandler("sql", GetAllDataSources)
|
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-02-29 06:35:15 -06:00
|
|
|
func GetDataSourceById(query *models.GetDataSourceByIdQuery) error {
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MDBDataSourceQueryByID.Inc()
|
2017-08-23 06:31:26 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
datasource := models.DataSource{OrgId: query.OrgId, Id: query.Id}
|
2016-06-06 10:11:46 -05:00
|
|
|
has, err := x.Get(&datasource)
|
2018-10-01 08:38:55 -05:00
|
|
|
|
2018-02-14 06:32:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-18 13:26:06 -06:00
|
|
|
|
2015-02-15 13:10:34 -06:00
|
|
|
if !has {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrDataSourceNotFound
|
2015-02-15 13:10:34 -06:00
|
|
|
}
|
2016-06-06 10:11:46 -05:00
|
|
|
|
|
|
|
query.Result = &datasource
|
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 GetAllDataSources(query *models.GetAllDataSourcesQuery) error {
|
2018-12-18 09:59:14 -06:00
|
|
|
sess := x.Limit(5000, 0).Asc("name")
|
2017-10-09 13:36:31 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query.Result = make([]*models.DataSource, 0)
|
2017-10-09 13:36:31 -05: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 {
|
2015-02-23 13:07:49 -06:00
|
|
|
var rawSql = "DELETE FROM data_source WHERE id=? and org_id=?"
|
2017-10-26 10:13:07 -05:00
|
|
|
result, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId)
|
|
|
|
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 {
|
2017-02-09 20:11:36 -06:00
|
|
|
var rawSql = "DELETE FROM data_source WHERE name=? and org_id=?"
|
2017-10-26 10:13:07 -05:00
|
|
|
result, err := sess.Exec(rawSql, cmd.Name, cmd.OrgId)
|
|
|
|
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-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,
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
if _, err := sess.Insert(ds); err != nil {
|
|
|
|
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 {
|
2015-03-30 20:11:39 -05:00
|
|
|
rawSql := "UPDATE data_source SET is_default=? WHERE org_id=? AND id <> ?"
|
2015-04-01 01:26:02 -05:00
|
|
|
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,
|
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
|
|
|
}
|