Chore: Propagate context for data source provisioning (#40235)
* context all the things * apply feedback * rollback some alerting changes * rollback some alerting changes #2 * more rollbacks * more rollbacks #2 * more rollbacks #3 * more rollbacks #4 * fix integration test * add missing context * add missing and remove incorrect dispatch
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -15,24 +16,28 @@ import (
|
||||
|
||||
// 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 (ss *SQLStore) GetDataSource(query *models.GetDataSourceQuery) error {
|
||||
func (ss *SQLStore) GetDataSource(ctx context.Context, 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
|
||||
}
|
||||
|
||||
datasource := models.DataSource{Name: query.Name, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
|
||||
has, err := x.Get(&datasource)
|
||||
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
if query.OrgId == 0 || (query.Id == 0 && len(query.Name) == 0 && len(query.Uid) == 0) {
|
||||
return models.ErrDataSourceIdentifierNotSet
|
||||
}
|
||||
|
||||
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 {
|
||||
return models.ErrDataSourceNotFound
|
||||
}
|
||||
datasource := &models.DataSource{Name: query.Name, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
|
||||
has, err := sess.Get(datasource)
|
||||
|
||||
query.Result = &datasource
|
||||
return nil
|
||||
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 {
|
||||
return models.ErrDataSourceNotFound
|
||||
}
|
||||
|
||||
query.Result = datasource
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) GetDataSources(query *models.GetDataSourcesQuery) error {
|
||||
@@ -73,7 +78,7 @@ func (ss *SQLStore) GetDefaultDataSource(query *models.GetDefaultDataSourceQuery
|
||||
|
||||
// DeleteDataSource removes a datasource by org_id as well as either uid (preferred), id, or name
|
||||
// and is added to the bus.
|
||||
func (ss *SQLStore) DeleteDataSource(cmd *models.DeleteDataSourceCommand) error {
|
||||
func (ss *SQLStore) DeleteDataSource(ctx context.Context, cmd *models.DeleteDataSourceCommand) error {
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
makeQuery := func(sql string, p ...interface{}) {
|
||||
@@ -94,7 +99,7 @@ func (ss *SQLStore) DeleteDataSource(cmd *models.DeleteDataSourceCommand) error
|
||||
return models.ErrDataSourceIdentifierNotSet
|
||||
}
|
||||
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
result, err := sess.Exec(params...)
|
||||
cmd.DeletedDatasourcesCount, _ = result.RowsAffected()
|
||||
|
||||
@@ -110,8 +115,8 @@ func (ss *SQLStore) DeleteDataSource(cmd *models.DeleteDataSourceCommand) error
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) AddDataSource(cmd *models.AddDataSourceCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
func (ss *SQLStore) AddDataSource(ctx context.Context, cmd *models.AddDataSourceCommand) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
existing := models.DataSource{OrgId: cmd.OrgId, Name: cmd.Name}
|
||||
has, _ := sess.Get(&existing)
|
||||
|
||||
@@ -188,8 +193,8 @@ func updateIsDefaultFlag(ds *models.DataSource, sess *DBSession) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UpdateDataSource(cmd *models.UpdateDataSourceCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
func (ss *SQLStore) UpdateDataSource(ctx context.Context, cmd *models.UpdateDataSourceCommand) error {
|
||||
return inTransactionCtx(ctx, func(sess *DBSession) error {
|
||||
if cmd.JsonData == nil {
|
||||
cmd.JsonData = simplejson.New()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -35,7 +36,7 @@ func TestDataAccess(t *testing.T) {
|
||||
|
||||
initDatasource := func(sqlStore *SQLStore) *models.DataSource {
|
||||
cmd := defaultAddDatasourceCommand
|
||||
err := sqlStore.AddDataSource(&cmd)
|
||||
err := sqlStore.AddDataSource(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDataSourcesQuery{OrgId: 10}
|
||||
@@ -50,7 +51,7 @@ func TestDataAccess(t *testing.T) {
|
||||
t.Run("Can add datasource", func(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
|
||||
err := sqlStore.AddDataSource(&models.AddDataSourceCommand{
|
||||
err := sqlStore.AddDataSource(context.Background(), &models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "laban",
|
||||
Type: models.DS_GRAPHITE,
|
||||
@@ -85,9 +86,9 @@ func TestDataAccess(t *testing.T) {
|
||||
cmd2 := defaultAddDatasourceCommand
|
||||
cmd1.Uid = "test"
|
||||
cmd2.Uid = "test"
|
||||
err := sqlStore.AddDataSource(&cmd1)
|
||||
err := sqlStore.AddDataSource(context.Background(), &cmd1)
|
||||
require.NoError(t, err)
|
||||
err = sqlStore.AddDataSource(&cmd2)
|
||||
err = sqlStore.AddDataSource(context.Background(), &cmd2)
|
||||
require.Error(t, err)
|
||||
require.IsType(t, models.ErrDataSourceUidExists, err)
|
||||
})
|
||||
@@ -101,7 +102,7 @@ func TestDataAccess(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := sqlStore.AddDataSource(&defaultAddDatasourceCommand)
|
||||
err := sqlStore.AddDataSource(context.Background(), &defaultAddDatasourceCommand)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
@@ -127,7 +128,7 @@ func TestDataAccess(t *testing.T) {
|
||||
cmd := defaultUpdateDatasourceCommand
|
||||
cmd.Id = ds.Id
|
||||
cmd.Version = ds.Version
|
||||
err := sqlStore.UpdateDataSource(&cmd)
|
||||
err := sqlStore.UpdateDataSource(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
@@ -138,11 +139,11 @@ func TestDataAccess(t *testing.T) {
|
||||
|
||||
cmd := defaultUpdateDatasourceCommand
|
||||
cmd.Id = ds.Id
|
||||
err := sqlStore.UpdateDataSource(&cmd)
|
||||
err := sqlStore.UpdateDataSource(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDataSourceQuery{Id: ds.Id, OrgId: 10}
|
||||
err = sqlStore.GetDataSource(&query)
|
||||
err = sqlStore.GetDataSource(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ds.Uid, query.Result.Uid)
|
||||
})
|
||||
@@ -163,10 +164,10 @@ func TestDataAccess(t *testing.T) {
|
||||
// Make a copy as UpdateDataSource modifies it
|
||||
cmd2 := cmd
|
||||
|
||||
err := sqlStore.UpdateDataSource(&cmd)
|
||||
err := sqlStore.UpdateDataSource(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = sqlStore.UpdateDataSource(&cmd2)
|
||||
err = sqlStore.UpdateDataSource(context.Background(), &cmd2)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
@@ -183,7 +184,7 @@ func TestDataAccess(t *testing.T) {
|
||||
Url: "http://test",
|
||||
}
|
||||
|
||||
err := sqlStore.UpdateDataSource(cmd)
|
||||
err := sqlStore.UpdateDataSource(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
@@ -201,7 +202,7 @@ func TestDataAccess(t *testing.T) {
|
||||
Version: 90000,
|
||||
}
|
||||
|
||||
err := sqlStore.UpdateDataSource(cmd)
|
||||
err := sqlStore.UpdateDataSource(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
})
|
||||
@@ -211,7 +212,7 @@ func TestDataAccess(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
ds := initDatasource(sqlStore)
|
||||
|
||||
err := sqlStore.DeleteDataSource(&models.DeleteDataSourceCommand{ID: ds.Id, OrgID: ds.OrgId})
|
||||
err := sqlStore.DeleteDataSource(context.Background(), &models.DeleteDataSourceCommand{ID: ds.Id, OrgID: ds.OrgId})
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDataSourcesQuery{OrgId: 10}
|
||||
@@ -225,7 +226,7 @@ func TestDataAccess(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
ds := initDatasource(sqlStore)
|
||||
|
||||
err := sqlStore.DeleteDataSource(&models.DeleteDataSourceCommand{ID: ds.Id, OrgID: 123123})
|
||||
err := sqlStore.DeleteDataSource(context.Background(), &models.DeleteDataSourceCommand{ID: ds.Id, OrgID: 123123})
|
||||
require.NoError(t, err)
|
||||
query := models.GetDataSourcesQuery{OrgId: 10}
|
||||
err = sqlStore.GetDataSources(&query)
|
||||
@@ -245,7 +246,7 @@ func TestDataAccess(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
err := sqlStore.DeleteDataSource(&models.DeleteDataSourceCommand{ID: ds.Id, UID: "nisse-uid", Name: "nisse", OrgID: 123123})
|
||||
err := sqlStore.DeleteDataSource(context.Background(), &models.DeleteDataSourceCommand{ID: ds.Id, UID: "nisse-uid", Name: "nisse", OrgID: 123123})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
@@ -263,7 +264,7 @@ func TestDataAccess(t *testing.T) {
|
||||
ds := initDatasource(sqlStore)
|
||||
query := models.GetDataSourcesQuery{OrgId: 10}
|
||||
|
||||
err := sqlStore.DeleteDataSource(&models.DeleteDataSourceCommand{Name: ds.Name, OrgID: ds.OrgId})
|
||||
err := sqlStore.DeleteDataSource(context.Background(), &models.DeleteDataSourceCommand{Name: ds.Name, OrgID: ds.OrgId})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = sqlStore.GetDataSources(&query)
|
||||
@@ -277,7 +278,7 @@ func TestDataAccess(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
datasourceLimit := 6
|
||||
for i := 0; i < datasourceLimit+1; i++ {
|
||||
err := sqlStore.AddDataSource(&models.AddDataSourceCommand{
|
||||
err := sqlStore.AddDataSource(context.Background(), &models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "laban" + strconv.Itoa(i),
|
||||
Type: models.DS_GRAPHITE,
|
||||
@@ -300,7 +301,7 @@ func TestDataAccess(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
numberOfDatasource := 5100
|
||||
for i := 0; i < numberOfDatasource; i++ {
|
||||
err := sqlStore.AddDataSource(&models.AddDataSourceCommand{
|
||||
err := sqlStore.AddDataSource(context.Background(), &models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "laban" + strconv.Itoa(i),
|
||||
Type: models.DS_GRAPHITE,
|
||||
@@ -323,7 +324,7 @@ func TestDataAccess(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
numberOfDatasource := 5100
|
||||
for i := 0; i < numberOfDatasource; i++ {
|
||||
err := sqlStore.AddDataSource(&models.AddDataSourceCommand{
|
||||
err := sqlStore.AddDataSource(context.Background(), &models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "laban" + strconv.Itoa(i),
|
||||
Type: models.DS_GRAPHITE,
|
||||
@@ -347,7 +348,7 @@ func TestDataAccess(t *testing.T) {
|
||||
t.Run("Only returns datasources of specified type", func(t *testing.T) {
|
||||
sqlStore := InitTestDB(t)
|
||||
|
||||
err := sqlStore.AddDataSource(&models.AddDataSourceCommand{
|
||||
err := sqlStore.AddDataSource(context.Background(), &models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "Elasticsearch",
|
||||
Type: models.DS_ES,
|
||||
@@ -358,7 +359,7 @@ func TestDataAccess(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = sqlStore.AddDataSource(&models.AddDataSourceCommand{
|
||||
err = sqlStore.AddDataSource(context.Background(), &models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "Graphite",
|
||||
Type: models.DS_GRAPHITE,
|
||||
@@ -403,7 +404,7 @@ func TestGetDefaultDataSource(t *testing.T) {
|
||||
Url: "http://test",
|
||||
}
|
||||
|
||||
err := sqlStore.AddDataSource(&cmd)
|
||||
err := sqlStore.AddDataSource(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDefaultDataSourceQuery{OrgId: 10}
|
||||
@@ -424,7 +425,7 @@ func TestGetDefaultDataSource(t *testing.T) {
|
||||
IsDefault: true,
|
||||
}
|
||||
|
||||
err := sqlStore.AddDataSource(&cmd)
|
||||
err := sqlStore.AddDataSource(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDefaultDataSourceQuery{OrgId: 10}
|
||||
|
||||
Reference in New Issue
Block a user