Correlations: Add organization id (#72258)

* Add org_id to correlations

* Add tests

* Allow org_id to be null in case org_id=0 is used

* Create organization to ensure stable id is generated

* Fix linting

* Ensure backwards compatibility

* Add deprecation information

* Migrate correlations indices

* Default org_id when migrating

* Remove redundant default

* Make PK non-nullable
This commit is contained in:
Piotr Jamróz
2023-08-24 09:39:30 +02:00
committed by GitHub
parent 9b891480d6
commit b30e0aa5aa
11 changed files with 153 additions and 7 deletions
@@ -28,7 +28,8 @@ var (
withoutDefaults = "testdata/appliedDefaults"
invalidAccess = "testdata/invalid-access"
oneDatasourceWithTwoCorrelations = "testdata/one-datasource-two-correlations"
oneDatasourceWithTwoCorrelations = "testdata/one-datasource-two-correlations"
correlationsDifferentOrganizations = "testdata/correlations-different-organizations"
)
func TestDatasourceAsConfig(t *testing.T) {
@@ -282,6 +283,26 @@ func TestDatasourceAsConfig(t *testing.T) {
require.Equal(t, 1, len(correlationsStore.deletedBySourceUID))
require.Equal(t, 1, len(correlationsStore.deletedByTargetUID))
})
t.Run("Using correct organization id", func(t *testing.T) {
store := &spyStore{items: []*datasources.DataSource{{Name: "Foo", OrgID: 2, ID: 1}}}
orgFake := &orgtest.FakeOrgService{}
correlationsStore := &mockCorrelationsStore{}
dc := newDatasourceProvisioner(logger, store, correlationsStore, orgFake)
err := dc.applyChanges(context.Background(), correlationsDifferentOrganizations)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}
require.Equal(t, 2, len(correlationsStore.created))
// triggered twice - clean up on delete + update (because of the store setup above)
require.Equal(t, 2, len(correlationsStore.deletedBySourceUID))
require.Equal(t, int64(2), correlationsStore.deletedBySourceUID[0].OrgId)
require.Equal(t, int64(2), correlationsStore.deletedBySourceUID[1].OrgId)
// triggered once - just the clean up
require.Equal(t, 1, len(correlationsStore.deletedByTargetUID))
require.Equal(t, int64(2), correlationsStore.deletedByTargetUID[0].OrgId)
})
})
}
@@ -97,6 +97,7 @@ func (dc *DatasourceProvisioner) apply(ctx context.Context, cfg *configs) error
if len(ds.Correlations) > 0 {
if err := dc.correlationsStore.DeleteCorrelationsBySourceUID(ctx, correlations.DeleteCorrelationsBySourceUIDCommand{
SourceUID: dataSource.UID,
OrgId: dataSource.OrgID,
}); err != nil {
return err
}
@@ -197,12 +198,14 @@ func (dc *DatasourceProvisioner) deleteDatasources(ctx context.Context, dsToDele
if dataSource != nil {
if err := dc.correlationsStore.DeleteCorrelationsBySourceUID(ctx, correlations.DeleteCorrelationsBySourceUIDCommand{
SourceUID: dataSource.UID,
OrgId: dataSource.OrgID,
}); err != nil {
return err
}
if err := dc.correlationsStore.DeleteCorrelationsByTargetUID(ctx, correlations.DeleteCorrelationsByTargetUIDCommand{
TargetUID: dataSource.UID,
OrgId: dataSource.OrgID,
}); err != nil {
return err
}
@@ -0,0 +1,6 @@
apiVersion: 1
datasources:
- name: Foo
orgId: 1
type: graphite
@@ -0,0 +1,15 @@
apiVersion: 1
deleteDatasources:
- name: Foo
orgId: 2
datasources:
- name: Foo
uid: foo
orgId: 2
type: graphite
correlations:
- targetUID: foo
label: self correlation
description: a description
@@ -0,0 +1,11 @@
apiVersion: 1
datasources:
- name: Foo
uid: foo
orgId: 3
type: graphite
correlations:
- targetUID: foo
label: self correlation
description: a description