merge upstream service changes
This commit is contained in:
@@ -246,6 +246,8 @@ require (
|
||||
github.com/grafana/grafana/pkg/semconv v0.0.0-20250627191313-2f1a6ae1712b // @grafana/grafana-app-platform-squad
|
||||
)
|
||||
|
||||
require github.com/grafana/grafana/pkg/build v0.0.0-20250701233214-68f68a522416
|
||||
|
||||
require (
|
||||
cel.dev/expr v0.23.1 // indirect
|
||||
cloud.google.com/go v0.120.0 // indirect
|
||||
|
||||
@@ -1645,6 +1645,8 @@ github.com/grafana/grafana/pkg/apis/secret v0.0.0-20250627191313-2f1a6ae1712b h1
|
||||
github.com/grafana/grafana/pkg/apis/secret v0.0.0-20250627191313-2f1a6ae1712b/go.mod h1:9YjiHZzii2DZfocRDJbqSeC8M3GWenU5yexeHHxsZ4Y=
|
||||
github.com/grafana/grafana/pkg/apiserver v0.0.0-20250627191313-2f1a6ae1712b h1:QyJLJn3xwFTIXu9KPZujsrIUN0X8DdiR9b2h75L0AfI=
|
||||
github.com/grafana/grafana/pkg/apiserver v0.0.0-20250627191313-2f1a6ae1712b/go.mod h1:6OKkPWDB8PetDXqMVMOWL35iTCEUdpATwwpuew0k8+o=
|
||||
github.com/grafana/grafana/pkg/build v0.0.0-20250701233214-68f68a522416 h1:u7ng2x1yyOLvBGKia3RWccDHv6Sqs2t+eSOFzGERlfI=
|
||||
github.com/grafana/grafana/pkg/build v0.0.0-20250701233214-68f68a522416/go.mod h1:uU3TK/cKf6IdO7ou7CKgNNb8GEnW761Ecdb8tk8DQLg=
|
||||
github.com/grafana/grafana/pkg/promlib v0.0.8 h1:VUWsqttdf0wMI4j9OX9oNrykguQpZcruudDAFpJJVw0=
|
||||
github.com/grafana/grafana/pkg/promlib v0.0.8/go.mod h1:U1ezG/MGaEPoThqsr3lymMPN5yIPdVTJnDZ+wcXT+ao=
|
||||
github.com/grafana/grafana/pkg/semconv v0.0.0-20250627191313-2f1a6ae1712b h1:m78RNSvTseSpvwQYe5HcdsiADSw4vj6+QUppuCL63gw=
|
||||
|
||||
@@ -20,9 +20,9 @@ var _ datasources.DataSourceService = &FakeDataSourceService{}
|
||||
|
||||
func (s *FakeDataSourceService) GetDataSource(ctx context.Context, query *datasources.GetDataSourceQuery) (*datasources.DataSource, error) {
|
||||
for _, dataSource := range s.DataSources {
|
||||
idMatch := query.ID != 0 && query.ID == dataSource.ID
|
||||
idMatch := query.ID != 0 && query.ID == dataSource.ID // nolint:staticcheck
|
||||
uidMatch := query.UID != "" && query.UID == dataSource.UID
|
||||
nameMatch := query.Name != "" && query.Name == dataSource.Name
|
||||
nameMatch := query.Name != "" && query.Name == dataSource.Name // nolint:staticcheck
|
||||
if idMatch || nameMatch || uidMatch {
|
||||
return dataSource, nil
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/ini.v1"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||
@@ -40,7 +40,6 @@ import (
|
||||
secretsmng "github.com/grafana/grafana/pkg/services/secrets/manager"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tests/testsuite"
|
||||
// testdatasource "github.com/grafana/grafana/pkg/tsdb/grafana-testdata-datasource"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
@@ -53,9 +52,9 @@ type dataSourceMockRetriever struct {
|
||||
|
||||
func (d *dataSourceMockRetriever) GetDataSource(ctx context.Context, query *datasources.GetDataSourceQuery) (*datasources.DataSource, error) {
|
||||
for _, dataSource := range d.res {
|
||||
idMatch := query.ID != 0 && query.ID == dataSource.ID
|
||||
idMatch := query.ID != 0 && query.ID == dataSource.ID // nolint:staticcheck
|
||||
uidMatch := query.UID != "" && query.UID == dataSource.UID
|
||||
nameMatch := query.Name != "" && query.Name == dataSource.Name
|
||||
nameMatch := query.Name != "" && query.Name == dataSource.Name // nolint:staticcheck
|
||||
if idMatch || nameMatch || uidMatch {
|
||||
return dataSource, nil
|
||||
}
|
||||
@@ -420,6 +419,27 @@ func TestIntegrationService_UpdateDataSource(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("should update with UID", func(t *testing.T) {
|
||||
dsService := initDSService(t)
|
||||
|
||||
ds, err := dsService.AddDataSource(context.Background(), &datasources.AddDataSourceCommand{
|
||||
OrgID: 1,
|
||||
Name: "test-datasource",
|
||||
URL: "http://before",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
cmd := &datasources.UpdateDataSourceCommand{
|
||||
UID: ds.UID,
|
||||
OrgID: ds.OrgID,
|
||||
URL: "http://after",
|
||||
}
|
||||
|
||||
after, err := dsService.UpdateDataSource(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "http://after", after.URL)
|
||||
})
|
||||
|
||||
t.Run("should return error if datasource with same name exist", func(t *testing.T) {
|
||||
dsService := initDSService(t)
|
||||
|
||||
@@ -759,6 +779,28 @@ func TestIntegrationService_UpdateDataSource(t *testing.T) {
|
||||
require.False(t, ok)
|
||||
require.Nil(t, updatedRules)
|
||||
})
|
||||
|
||||
t.Run("Should update with UID", func(t *testing.T) {
|
||||
dsService := initDSService(t)
|
||||
|
||||
ds, err := dsService.AddDataSource(context.Background(), &datasources.AddDataSourceCommand{
|
||||
OrgID: 1,
|
||||
Name: "test-datasource",
|
||||
Type: "test",
|
||||
URL: "http://before",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
updateCmd := &datasources.UpdateDataSourceCommand{
|
||||
UID: ds.UID,
|
||||
OrgID: ds.OrgID,
|
||||
URL: "http://after",
|
||||
}
|
||||
|
||||
updatedDS, err := dsService.UpdateDataSource(context.Background(), updateCmd)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "http://after", updatedDS.URL)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationService_DeleteDataSource(t *testing.T) {
|
||||
|
||||
@@ -61,23 +61,26 @@ func (ss *SqlStore) GetDataSource(ctx context.Context, query *datasources.GetDat
|
||||
}
|
||||
|
||||
func (ss *SqlStore) getDataSource(_ context.Context, query *datasources.GetDataSourceQuery, sess *db.Session) (*datasources.DataSource, error) {
|
||||
if query.OrgID == 0 {
|
||||
if query.OrgID == 0 || (query.ID == 0 && len(query.Name) == 0 && len(query.UID) == 0) { // nolint:staticcheck
|
||||
return nil, datasources.ErrDataSourceIdentifierNotSet
|
||||
}
|
||||
|
||||
if len(query.UID) > 0 {
|
||||
if err := util.ValidateUID(query.UID); err != nil {
|
||||
logDeprecatedInvalidDsUid(ss.logger, query.UID, query.Name, "read", fmt.Errorf("invalid UID"))
|
||||
logDeprecatedInvalidDsUid(ss.logger, query.UID, query.Name, "read", fmt.Errorf("invalid UID")) // nolint:staticcheck
|
||||
}
|
||||
} else if query.ID == 0 {
|
||||
return nil, datasources.ErrDataSourceIdentifierNotSet
|
||||
}
|
||||
|
||||
datasource := &datasources.DataSource{Name: query.Name, OrgID: query.OrgID, ID: query.ID, UID: query.UID}
|
||||
datasource := &datasources.DataSource{
|
||||
OrgID: query.OrgID,
|
||||
UID: query.UID,
|
||||
Name: query.Name, // nolint:staticcheck
|
||||
ID: query.ID, // nolint:staticcheck
|
||||
}
|
||||
has, err := sess.Get(datasource)
|
||||
|
||||
if err != nil {
|
||||
ss.logger.Error("Failed getting data source", "err", err, "uid", query.UID, "id", query.ID, "name", query.Name, "orgId", query.OrgID)
|
||||
ss.logger.Error("Failed getting data source", "err", err, "uid", query.UID, "id", query.ID, "name", query.Name, "orgId", query.OrgID) // nolint:staticcheck
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, datasources.ErrDataSourceNotFound
|
||||
@@ -326,13 +329,15 @@ func (ss *SqlStore) UpdateDataSource(ctx context.Context, cmd *datasources.Updat
|
||||
cmd.JsonData = simplejson.New()
|
||||
}
|
||||
|
||||
if cmd.OrgID == 0 || cmd.ID == 0 || cmd.UID == "" {
|
||||
if cmd.ID == 0 || cmd.OrgID == 0 {
|
||||
return datasources.ErrDataSourceIdentifierNotSet
|
||||
}
|
||||
|
||||
if err := util.ValidateUID(cmd.UID); err != nil {
|
||||
logDeprecatedInvalidDsUid(ss.logger, cmd.UID, cmd.Name, "update", err)
|
||||
return datasources.ErrDataSourceUIDInvalid.Errorf("invalid UID for datasource %s: %w", cmd.Name, err)
|
||||
if len(cmd.UID) > 0 {
|
||||
if err := util.ValidateUID(cmd.UID); err != nil {
|
||||
logDeprecatedInvalidDsUid(ss.logger, cmd.UID, cmd.Name, "update", err)
|
||||
return datasources.ErrDataSourceUIDInvalid.Errorf("invalid UID for datasource %s: %w", cmd.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
ds = &datasources.DataSource{
|
||||
|
||||
@@ -247,8 +247,8 @@ func TestIntegrationDataAccess(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("DeleteDataSourceById", func(t *testing.T) {
|
||||
t.Run("can delete datasource", func(t *testing.T) {
|
||||
t.Run("DeleteDataSource", func(t *testing.T) {
|
||||
t.Run("can delete datasource with ID", func(t *testing.T) {
|
||||
db := db.InitTestDB(t)
|
||||
ds := initDatasource(db)
|
||||
ss := SqlStore{db: db}
|
||||
@@ -263,6 +263,21 @@ func TestIntegrationDataAccess(t *testing.T) {
|
||||
require.Equal(t, 0, len(dataSources))
|
||||
})
|
||||
|
||||
t.Run("can delete datasource with UID", func(t *testing.T) {
|
||||
db := db.InitTestDB(t)
|
||||
ds := initDatasource(db)
|
||||
ss := SqlStore{db: db}
|
||||
|
||||
err := ss.DeleteDataSource(context.Background(), &datasources.DeleteDataSourceCommand{UID: ds.UID, OrgID: ds.OrgID})
|
||||
require.NoError(t, err)
|
||||
|
||||
query := datasources.GetDataSourcesQuery{OrgID: 10}
|
||||
dataSources, err := ss.GetDataSources(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, 0, len(dataSources))
|
||||
})
|
||||
|
||||
t.Run("Can not delete datasource with wrong orgID", func(t *testing.T) {
|
||||
db := db.InitTestDB(t)
|
||||
ds := initDatasource(db)
|
||||
|
||||
@@ -457,7 +457,7 @@ type spyStore struct {
|
||||
|
||||
func (s *spyStore) GetDataSource(ctx context.Context, query *datasources.GetDataSourceQuery) (*datasources.DataSource, error) {
|
||||
for _, v := range s.items {
|
||||
if query.Name == v.Name && query.OrgID == v.OrgID {
|
||||
if query.Name == v.Name && query.OrgID == v.OrgID { // nolint:staticcheck
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user