Alerting: improve alerting default datasource search when extracting alerts (#29993)
* improve alerting search datasource * correct the xorm get usage * adding default datasource unittest
This commit is contained in:
@@ -24,6 +24,7 @@ func init() {
|
||||
bus.AddHandler("sql", UpdateDataSource)
|
||||
bus.AddHandler("sql", GetDataSourceById)
|
||||
bus.AddHandler("sql", GetDataSourceByName)
|
||||
bus.AddHandler("sql", GetDefaultDataSource)
|
||||
}
|
||||
|
||||
func getDataSourceByID(id, orgID int64, engine *xorm.Engine) (*models.DataSource, error) {
|
||||
@@ -77,6 +78,20 @@ func GetDataSources(query *models.GetDataSourcesQuery) error {
|
||||
return sess.Find(&query.Result)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func DeleteDataSourceById(cmd *models.DeleteDataSourceByIdCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
var rawSQL = "DELETE FROM data_source WHERE id=? and org_id=?"
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -287,3 +289,51 @@ func TestDataAccess(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetDefaultDataSource(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
|
||||
t.Run("should return error if there is no default datasource", func(t *testing.T) {
|
||||
cmd := models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "nisse",
|
||||
Type: models.DS_GRAPHITE,
|
||||
Access: models.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
}
|
||||
|
||||
err := AddDataSource(&cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDefaultDataSourceQuery{OrgId: 10}
|
||||
err = GetDefaultDataSource(&query)
|
||||
require.Error(t, err)
|
||||
assert.True(t, errors.Is(err, models.ErrDataSourceNotFound))
|
||||
})
|
||||
|
||||
t.Run("should return default datasource if exists", func(t *testing.T) {
|
||||
cmd := models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "default datasource",
|
||||
Type: models.DS_GRAPHITE,
|
||||
Access: models.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
IsDefault: true,
|
||||
}
|
||||
|
||||
err := AddDataSource(&cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDefaultDataSourceQuery{OrgId: 10}
|
||||
err = GetDefaultDataSource(&query)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "default datasource", query.Result.Name)
|
||||
})
|
||||
|
||||
t.Run("should not return default datasource of other organisation", func(t *testing.T) {
|
||||
query := models.GetDefaultDataSourceQuery{OrgId: 1}
|
||||
err := GetDefaultDataSource(&query)
|
||||
require.Error(t, err)
|
||||
assert.True(t, errors.Is(err, models.ErrDataSourceNotFound))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -151,4 +151,7 @@ func addDataSourceMigration(mg *Migrator) {
|
||||
mg.AddMigration("Add unique index datasource_org_id_uid", NewAddIndexMigration(tableV2, &Index{
|
||||
Cols: []string{"org_id", "uid"}, Type: UniqueIndex,
|
||||
}))
|
||||
|
||||
mg.AddMigration("add unique index datasource_org_id_is_default", NewAddIndexMigration(tableV2, &Index{
|
||||
Cols: []string{"org_id", "is_default"}}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user