@ -12,7 +12,7 @@ import (
jsoniter "github.com/json-iterator/go"
)
type Stor e interface {
type BaseDataSourceServic e interface {
GetDataSource ( ctx context . Context , query * datasources . GetDataSourceQuery ) ( * datasources . DataSource , error )
GetPrunableProvisionedDataSources ( ctx context . Context ) ( [ ] * datasources . DataSource , error )
AddDataSource ( ctx context . Context , cmd * datasources . AddDataSourceCommand ) ( * datasources . DataSource , error )
@ -35,8 +35,8 @@ var (
// Provision scans a directory for provisioning config files
// and provisions the datasource in those files.
func Provision ( ctx context . Context , configDirectory string , store Stor e, correlationsStore CorrelationsStore , orgService org . Service ) error {
dc := newDatasourceProvisioner ( log . New ( "provisioning.datasources" ) , stor e, correlationsStore , orgService )
func Provision ( ctx context . Context , configDirectory string , dsService BaseDataSourceServic e, correlationsStore CorrelationsStore , orgService org . Service ) error {
dc := newDatasourceProvisioner ( log . New ( "provisioning.datasources" ) , dsServic e, correlationsStore , orgService )
return dc . applyChanges ( ctx , configDirectory )
}
@ -45,15 +45,15 @@ func Provision(ctx context.Context, configDirectory string, store Store, correla
type DatasourceProvisioner struct {
log log . Logger
cfgProvider * configReader
store Stor e
dsService BaseDataSourceServic e
correlationsStore CorrelationsStore
}
func newDatasourceProvisioner ( log log . Logger , store Stor e, correlationsStore CorrelationsStore , orgService org . Service ) DatasourceProvisioner {
func newDatasourceProvisioner ( log log . Logger , dsService BaseDataSourceServic e, correlationsStore CorrelationsStore , orgService org . Service ) DatasourceProvisioner {
return DatasourceProvisioner {
log : log ,
cfgProvider : & configReader { log : log , orgService : orgService } ,
store : stor e,
dsService : dsServic e,
correlationsStore : correlationsStore ,
}
}
@ -65,7 +65,7 @@ func (dc *DatasourceProvisioner) provisionDataSources(ctx context.Context, cfg *
for _ , ds := range cfg . Datasources {
cmd := & datasources . GetDataSourceQuery { OrgID : ds . OrgID , Name : ds . Name }
dataSource , err := dc . stor e. GetDataSource ( ctx , cmd )
dataSource , err := dc . dsServic e. GetDataSource ( ctx , cmd )
if err != nil && ! errors . Is ( err , datasources . ErrDataSourceNotFound ) {
return err
}
@ -73,14 +73,14 @@ func (dc *DatasourceProvisioner) provisionDataSources(ctx context.Context, cfg *
if errors . Is ( err , datasources . ErrDataSourceNotFound ) {
insertCmd := createInsertCommand ( ds )
dc . log . Info ( "inserting datasource from configuration" , "name" , insertCmd . Name , "uid" , insertCmd . UID )
_ , err = dc . stor e. AddDataSource ( ctx , insertCmd )
_ , err = dc . dsServic e. AddDataSource ( ctx , insertCmd )
if err != nil {
return err
}
} else {
updateCmd := createUpdateCommand ( ds , dataSource . ID )
dc . log . Debug ( "updating datasource from configuration" , "name" , updateCmd . Name , "uid" , updateCmd . UID )
if _ , err := dc . stor e. UpdateDataSource ( ctx , updateCmd ) ; err != nil {
if _ , err := dc . dsServic e. UpdateDataSource ( ctx , updateCmd ) ; err != nil {
if errors . Is ( err , datasources . ErrDataSourceUpdatingOldVersion ) {
dc . log . Debug ( "ignoring old version of datasource" , "name" , updateCmd . Name , "uid" , updateCmd . UID )
} else {
@ -96,7 +96,7 @@ func (dc *DatasourceProvisioner) provisionDataSources(ctx context.Context, cfg *
func ( dc * DatasourceProvisioner ) provisionCorrelations ( ctx context . Context , cfg * configs ) error {
for _ , ds := range cfg . Datasources {
cmd := & datasources . GetDataSourceQuery { OrgID : ds . OrgID , Name : ds . Name }
dataSource , err := dc . stor e. GetDataSource ( ctx , cmd )
dataSource , err := dc . dsServic e. GetDataSource ( ctx , cmd )
if errors . Is ( err , datasources . ErrDataSourceNotFound ) {
return err
@ -154,7 +154,7 @@ func (dc *DatasourceProvisioner) applyChanges(ctx context.Context, configPath st
}
}
prunableProvisionedDataSources , err := dc . stor e. GetPrunableProvisionedDataSources ( ctx )
prunableProvisionedDataSources , err := dc . dsServic e. GetPrunableProvisionedDataSources ( ctx )
if err != nil {
return err
}
@ -238,17 +238,21 @@ func makeCreateCorrelationCommand(correlation map[string]any, SourceUID string,
func ( dc * DatasourceProvisioner ) deleteDatasources ( ctx context . Context , dsToDelete [ ] * deleteDatasourceConfig , willExistAfterProvisioning map [ DataSourceMapKey ] bool ) error {
for _ , ds := range dsToDelete {
getDsQuery := & datasources . GetDataSourceQuery { Name : ds . Name , OrgID : ds . OrgID }
_ , err := dc . stor e. GetDataSource ( ctx , getDsQuery )
existingDs , err := dc . dsServic e. GetDataSource ( ctx , getDsQuery )
if err != nil && ! errors . Is ( err , datasources . ErrDataSourceNotFound ) {
return err
if err != nil {
if errors . Is ( err , datasources . ErrDataSourceNotFound ) {
continue
} else {
return err
}
}
// Skip publishing the event as the data source is not really deleted, it will be re-created during provisioning
// This is to avoid cleaning up any resources related to the data source (e.g. correlations)
skipPublish := willExistAfterProvisioning [ DataSourceMapKey { Name : ds . Name , OrgId : ds . OrgID } ]
cmd := & datasources . DeleteDataSourceCommand { OrgID : ds . OrgID , Name : ds . Name , SkipPublish : skipPublish }
if err := dc . stor e. DeleteDataSource ( ctx , cmd ) ; err != nil {
cmd := & datasources . DeleteDataSourceCommand { OrgID : ds . OrgID , Name : ds . Name , UID : existingDs . UID , SkipPublish : skipPublish }
if err := dc . dsServic e. DeleteDataSource ( ctx , cmd ) ; err != nil {
return err
}