mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Correlations: Allow creating correlations for provisioned data sources (#73737)
* Allow creating correlations for provisioned data sources * Update docs * Fix linting * Add missing props * Add missing props * Fix linting * Fix linting * Clarify error name * Removed error handling for a non-existing use case * Create a list of deleted data datasources based on all configs * 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 * Update comments * Override existing datasSource variable so the UID is retrieved correctly * Migrate correlations indices * Default org_id when migrating * Remove redundant default * Make PK non-nullable * Post merge fixes * Separate data sources / correlations provisioning * Adjust comments * Store new data sources in spy store so it can be used to test correlations as well * Fix linting * Update tests * Ensure response is closed * Avoid creating duplicates during provisioning * Fix updating provisioned column and update tests * Rename error message * Fix linting errors * Fix linting errors and rename variable * Update test * Update pkg/services/sqlstore/migrations/correlations_mig.go Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Remove unused error * Fix lining --------- Co-authored-by: Giordano Ricci <me@giordanoricci.com>
This commit is contained in:
@@ -3,6 +3,8 @@ package correlations
|
||||
import (
|
||||
"context"
|
||||
|
||||
"xorm.io/core"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
@@ -19,6 +21,7 @@ func (s CorrelationsService) createCorrelation(ctx context.Context, cmd CreateCo
|
||||
Label: cmd.Label,
|
||||
Description: cmd.Description,
|
||||
Config: cmd.Config,
|
||||
Provisioned: cmd.Provisioned,
|
||||
}
|
||||
|
||||
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *db.Session) error {
|
||||
@@ -28,15 +31,11 @@ func (s CorrelationsService) createCorrelation(ctx context.Context, cmd CreateCo
|
||||
OrgID: cmd.OrgId,
|
||||
UID: cmd.SourceUID,
|
||||
}
|
||||
dataSource, err := s.DataSourceService.GetDataSource(ctx, query)
|
||||
_, err = s.DataSourceService.GetDataSource(ctx, query)
|
||||
if err != nil {
|
||||
return ErrSourceDataSourceDoesNotExists
|
||||
}
|
||||
|
||||
if !cmd.SkipReadOnlyCheck && dataSource.ReadOnly {
|
||||
return ErrSourceDataSourceReadOnly
|
||||
}
|
||||
|
||||
if cmd.TargetUID != nil {
|
||||
if _, err = s.DataSourceService.GetDataSource(ctx, &datasources.GetDataSourceQuery{
|
||||
OrgID: cmd.OrgId,
|
||||
@@ -67,13 +66,19 @@ func (s CorrelationsService) deleteCorrelation(ctx context.Context, cmd DeleteCo
|
||||
OrgID: cmd.OrgId,
|
||||
UID: cmd.SourceUID,
|
||||
}
|
||||
dataSource, err := s.DataSourceService.GetDataSource(ctx, query)
|
||||
_, err := s.DataSourceService.GetDataSource(ctx, query)
|
||||
if err != nil {
|
||||
return ErrSourceDataSourceDoesNotExists
|
||||
}
|
||||
|
||||
if dataSource.ReadOnly {
|
||||
return ErrSourceDataSourceReadOnly
|
||||
correlation, err := s.GetCorrelation(ctx, GetCorrelationQuery(cmd))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if correlation.Provisioned {
|
||||
return ErrCorrelationReadOnly
|
||||
}
|
||||
|
||||
deletedCount, err := session.Delete(&Correlation{UID: cmd.UID, SourceUID: cmd.SourceUID})
|
||||
@@ -96,15 +101,11 @@ func (s CorrelationsService) updateCorrelation(ctx context.Context, cmd UpdateCo
|
||||
OrgID: cmd.OrgId,
|
||||
UID: cmd.SourceUID,
|
||||
}
|
||||
dataSource, err := s.DataSourceService.GetDataSource(ctx, query)
|
||||
_, err := s.DataSourceService.GetDataSource(ctx, query)
|
||||
if err != nil {
|
||||
return ErrSourceDataSourceDoesNotExists
|
||||
}
|
||||
|
||||
if dataSource.ReadOnly {
|
||||
return ErrSourceDataSourceReadOnly
|
||||
}
|
||||
|
||||
found, err := session.Get(&correlation)
|
||||
if !found {
|
||||
return ErrCorrelationNotFound
|
||||
@@ -112,6 +113,9 @@ func (s CorrelationsService) updateCorrelation(ctx context.Context, cmd UpdateCo
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if correlation.Provisioned {
|
||||
return ErrCorrelationReadOnly
|
||||
}
|
||||
|
||||
if cmd.Label != nil {
|
||||
correlation.Label = *cmd.Label
|
||||
@@ -270,7 +274,13 @@ func (s CorrelationsService) getCorrelations(ctx context.Context, cmd GetCorrela
|
||||
func (s CorrelationsService) deleteCorrelationsBySourceUID(ctx context.Context, cmd DeleteCorrelationsBySourceUIDCommand) error {
|
||||
return s.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
||||
// Correlations created before the fix #72498 may have org_id = 0, but it's deprecated and will be removed in #72325
|
||||
_, err := session.Where("source_uid = ? and (org_id = ? or org_id = 0)", cmd.SourceUID, cmd.OrgId).Delete(&Correlation{})
|
||||
db := session.Where("source_uid = ? and (org_id = ? or org_id = 0)", cmd.SourceUID, cmd.OrgId)
|
||||
if cmd.OnlyProvisioned {
|
||||
// bool in a struct needs to be in Where
|
||||
// https://github.com/go-xorm/xorm/blob/v0.7.9/engine_cond.go#L102
|
||||
db = db.And("provisioned = ?", true)
|
||||
}
|
||||
_, err := db.Delete(&Correlation{})
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -282,3 +292,38 @@ func (s CorrelationsService) deleteCorrelationsByTargetUID(ctx context.Context,
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
// internal use: It's require only for correct migration of existing records. Can be removed in Grafana 11.
|
||||
func (s CorrelationsService) createOrUpdateCorrelation(ctx context.Context, cmd CreateCorrelationCommand) error {
|
||||
correlation := Correlation{
|
||||
SourceUID: cmd.SourceUID,
|
||||
OrgID: cmd.OrgId,
|
||||
TargetUID: cmd.TargetUID,
|
||||
Label: cmd.Label,
|
||||
Description: cmd.Description,
|
||||
Config: cmd.Config,
|
||||
Provisioned: false,
|
||||
}
|
||||
|
||||
found := false
|
||||
err := s.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
||||
has, err := session.Get(&correlation)
|
||||
found = has
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if found && cmd.Provisioned {
|
||||
correlation.Provisioned = true
|
||||
return s.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
||||
_, err := session.ID(core.NewPK(correlation.UID, correlation.SourceUID, correlation.OrgID)).Cols("provisioned").Update(&correlation)
|
||||
return err
|
||||
})
|
||||
} else {
|
||||
_, err := s.createCorrelation(ctx, cmd)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user