Correlations: Add CreateCorrelation HTTP API (#51630)

* Correlations: add migration

* Correlations: Add CreateCorrelation API

* Correlations: Make correlations work with provisioning

* Handle version changes

* Fix lining error

* lint fixes

* rebuild betterer results

* add a UID to each correlation

* Fix lint errors

* add docs

* better wording in API docs

* remove leftover comment

* handle ds updates

* Fix error message typo

* add bad data test

* make correlations a separate table

* skip readonly check when provisioning correlations

* delete stale correlations when datasources are deleted

* restore provisioned readonly ds

* publish deletion event with full data

* generate swagger and HTTP API docs

* apply source datasource permission to create correlation API

* Fix tests & lint errors

* ignore empty deletion events

* fix last lint errors

* fix more lint error

* Only publish deletion event if datasource was actually deleted

* delete DS provisioning deletes correlations, added & fixed tests

* Fix unmarshalling tests

* Fix linting errors

* Fix deltion event tests

* fix small linting error

* fix lint errors

* update betterer

* fix test

* make path singular

* Revert "make path singular"

This reverts commit 420c3d315e.

* add integration tests

* remove unneeded id from correlations table

* update spec

* update leftover references to CorrelationDTO

* fix tests

* cleanup tests

* fix lint error
This commit is contained in:
Giordano Ricci
2022-07-25 15:19:07 +01:00
committed by GitHub
parent dbc2171401
commit 5ce4baf6f5
27 changed files with 1326 additions and 48 deletions
+9 -7
View File
@@ -134,13 +134,15 @@ func (ss *SQLStore) DeleteDataSource(ctx context.Context, cmd *datasources.Delet
}
// Publish data source deletion event
sess.publishAfterCommit(&events.DataSourceDeleted{
Timestamp: time.Now(),
Name: cmd.Name,
ID: cmd.ID,
UID: cmd.UID,
OrgID: cmd.OrgID,
})
if cmd.DeletedDatasourcesCount > 0 {
sess.publishAfterCommit(&events.DataSourceDeleted{
Timestamp: time.Now(),
Name: ds.Name,
ID: ds.Id,
UID: ds.Uid,
OrgID: ds.OrgId,
})
}
return nil
})
+22 -4
View File
@@ -251,7 +251,7 @@ func TestIntegrationDataAccess(t *testing.T) {
})
err := sqlStore.DeleteDataSource(context.Background(),
&datasources.DeleteDataSourceCommand{ID: ds.Id, UID: "nisse-uid", Name: "nisse", OrgID: int64(123123)})
&datasources.DeleteDataSourceCommand{ID: ds.Id, UID: ds.Uid, Name: ds.Name, OrgID: ds.OrgId})
require.NoError(t, err)
require.Eventually(t, func() bool {
@@ -259,9 +259,27 @@ func TestIntegrationDataAccess(t *testing.T) {
}, time.Second, time.Millisecond)
require.Equal(t, ds.Id, deleted.ID)
require.Equal(t, int64(123123), deleted.OrgID)
require.Equal(t, "nisse", deleted.Name)
require.Equal(t, "nisse-uid", deleted.UID)
require.Equal(t, ds.OrgId, deleted.OrgID)
require.Equal(t, ds.Name, deleted.Name)
require.Equal(t, ds.Uid, deleted.UID)
})
t.Run("does not fire an event when the datasource is not deleted", func(t *testing.T) {
sqlStore := InitTestDB(t)
var called bool
sqlStore.bus.AddEventListener(func(ctx context.Context, e *events.DataSourceDeleted) error {
called = true
return nil
})
err := sqlStore.DeleteDataSource(context.Background(),
&datasources.DeleteDataSourceCommand{ID: 1, UID: "non-existing", Name: "non-existing", OrgID: int64(10)})
require.NoError(t, err)
require.Never(t, func() bool {
return called
}, time.Second, time.Millisecond)
})
t.Run("DeleteDataSourceByName", func(t *testing.T) {
@@ -0,0 +1,21 @@
package migrations
import (
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
)
func addCorrelationsMigrations(mg *Migrator) {
correlationsV1 := Table{
Name: "correlation",
Columns: []*Column{
{Name: "uid", Type: DB_NVarchar, Length: 40, Nullable: false, IsPrimaryKey: true},
{Name: "source_uid", Type: DB_NVarchar, Length: 40, Nullable: false, IsPrimaryKey: true},
// Nullable because in the future we want to have correlations to external resources
{Name: "target_uid", Type: DB_NVarchar, Length: 40, Nullable: true},
{Name: "label", Type: DB_Text, Nullable: false},
{Name: "description", Type: DB_Text, Nullable: false},
},
}
mg.AddMigration("create correlation table v1", NewAddTableMigration(correlationsV1))
}
@@ -75,6 +75,8 @@ func (*OSSMigrations) AddMigration(mg *Migrator) {
addQueryHistoryStarMigrations(mg)
addCorrelationsMigrations(mg)
if mg.Cfg != nil && mg.Cfg.IsFeatureToggleEnabled != nil {
if mg.Cfg.IsFeatureToggleEnabled(featuremgmt.FlagDashboardComments) || mg.Cfg.IsFeatureToggleEnabled(featuremgmt.FlagAnnotationComments) {
addCommentGroupMigrations(mg)