Provisioning: datasources auto deletion (#83034)
This commit is contained in:
@@ -21,6 +21,9 @@ type DataSourceService interface {
|
||||
// GetAllDataSources gets all datasources.
|
||||
GetAllDataSources(ctx context.Context, query *GetAllDataSourcesQuery) (res []*DataSource, err error)
|
||||
|
||||
// GetPrunableProvisionedDataSources gets all provisioned data sources that can be pruned.
|
||||
GetPrunableProvisionedDataSources(ctx context.Context) (res []*DataSource, err error)
|
||||
|
||||
// GetDataSourcesByType gets datasources by type.
|
||||
GetDataSourcesByType(ctx context.Context, query *GetDataSourcesByTypeQuery) ([]*DataSource, error)
|
||||
|
||||
|
||||
@@ -45,6 +45,16 @@ func (s *FakeDataSourceService) GetAllDataSources(ctx context.Context, query *da
|
||||
return s.DataSources, nil
|
||||
}
|
||||
|
||||
func (s *FakeDataSourceService) GetPrunableProvisionedDataSources(ctx context.Context) (res []*datasources.DataSource, err error) {
|
||||
var dataSources []*datasources.DataSource
|
||||
for _, dataSource := range s.DataSources {
|
||||
if dataSource.IsPrunable {
|
||||
dataSources = append(dataSources, dataSource)
|
||||
}
|
||||
}
|
||||
return dataSources, nil
|
||||
}
|
||||
|
||||
func (s *FakeDataSourceService) GetDataSourcesByType(ctx context.Context, query *datasources.GetDataSourcesByTypeQuery) ([]*datasources.DataSource, error) {
|
||||
var dataSources []*datasources.DataSource
|
||||
for _, datasource := range s.DataSources {
|
||||
|
||||
@@ -62,6 +62,8 @@ type DataSource struct {
|
||||
SecureJsonData map[string][]byte `json:"secureJsonData"`
|
||||
ReadOnly bool `json:"readOnly"`
|
||||
UID string `json:"uid" xorm:"uid"`
|
||||
// swagger:ignore
|
||||
IsPrunable bool `xorm:"is_prunable"`
|
||||
|
||||
Created time.Time `json:"created,omitempty"`
|
||||
Updated time.Time `json:"updated,omitempty"`
|
||||
@@ -161,6 +163,8 @@ type AddDataSourceCommand struct {
|
||||
JsonData *simplejson.Json `json:"jsonData"`
|
||||
SecureJsonData map[string]string `json:"secureJsonData"`
|
||||
UID string `json:"uid"`
|
||||
// swagger:ignore
|
||||
IsPrunable bool
|
||||
|
||||
OrgID int64 `json:"-"`
|
||||
UserID int64 `json:"-"`
|
||||
@@ -185,6 +189,8 @@ type UpdateDataSourceCommand struct {
|
||||
SecureJsonData map[string]string `json:"secureJsonData"`
|
||||
Version int `json:"version"`
|
||||
UID string `json:"uid"`
|
||||
// swagger:ignore
|
||||
IsPrunable bool
|
||||
|
||||
OrgID int64 `json:"-"`
|
||||
ID int64 `json:"-"`
|
||||
|
||||
@@ -174,6 +174,10 @@ func (s *Service) GetAllDataSources(ctx context.Context, query *datasources.GetA
|
||||
return s.SQLStore.GetAllDataSources(ctx, query)
|
||||
}
|
||||
|
||||
func (s *Service) GetPrunableProvisionedDataSources(ctx context.Context) (res []*datasources.DataSource, err error) {
|
||||
return s.SQLStore.GetPrunableProvisionedDataSources(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) GetDataSourcesByType(ctx context.Context, query *datasources.GetDataSourcesByTypeQuery) ([]*datasources.DataSource, error) {
|
||||
if query.AliasIDs == nil {
|
||||
// Populate alias IDs from plugin store
|
||||
|
||||
@@ -30,6 +30,7 @@ type Store interface {
|
||||
AddDataSource(context.Context, *datasources.AddDataSourceCommand) (*datasources.DataSource, error)
|
||||
UpdateDataSource(context.Context, *datasources.UpdateDataSourceCommand) (*datasources.DataSource, error)
|
||||
GetAllDataSources(ctx context.Context, query *datasources.GetAllDataSourcesQuery) (res []*datasources.DataSource, err error)
|
||||
GetPrunableProvisionedDataSources(ctx context.Context) (res []*datasources.DataSource, err error)
|
||||
|
||||
Count(context.Context, *quota.ScopeParameters) (*quota.Map, error)
|
||||
}
|
||||
@@ -124,6 +125,16 @@ func (ss *SqlStore) GetDataSourcesByType(ctx context.Context, query *datasources
|
||||
})
|
||||
}
|
||||
|
||||
// GetPrunableProvisionedDataSources returns all data sources that can be pruned
|
||||
func (ss *SqlStore) GetPrunableProvisionedDataSources(ctx context.Context) ([]*datasources.DataSource, error) {
|
||||
prunableQuery := "is_prunable = ?"
|
||||
|
||||
dataSources := make([]*datasources.DataSource, 0)
|
||||
return dataSources, ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
return sess.Where(prunableQuery, ss.db.GetDialect().BooleanStr(true)).Asc("id").Find(&dataSources)
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteDataSource removes a datasource by org_id as well as either uid (preferred), id, or name
|
||||
// and is added to the bus. It also removes permissions related to the datasource.
|
||||
func (ss *SqlStore) DeleteDataSource(ctx context.Context, cmd *datasources.DeleteDataSourceCommand) error {
|
||||
@@ -261,6 +272,7 @@ func (ss *SqlStore) AddDataSource(ctx context.Context, cmd *datasources.AddDataS
|
||||
Version: 1,
|
||||
ReadOnly: cmd.ReadOnly,
|
||||
UID: cmd.UID,
|
||||
IsPrunable: cmd.IsPrunable,
|
||||
}
|
||||
|
||||
if _, err := sess.Insert(ds); err != nil {
|
||||
@@ -328,12 +340,14 @@ func (ss *SqlStore) UpdateDataSource(ctx context.Context, cmd *datasources.Updat
|
||||
ReadOnly: cmd.ReadOnly,
|
||||
Version: cmd.Version + 1,
|
||||
UID: cmd.UID,
|
||||
IsPrunable: cmd.IsPrunable,
|
||||
}
|
||||
|
||||
sess.UseBool("is_default")
|
||||
sess.UseBool("basic_auth")
|
||||
sess.UseBool("with_credentials")
|
||||
sess.UseBool("read_only")
|
||||
sess.UseBool("is_prunable")
|
||||
// Make sure database field is zeroed out if empty. We want to migrate away from this field.
|
||||
sess.MustCols("database")
|
||||
// Make sure password are zeroed out if empty. We do this as we want to migrate passwords from
|
||||
|
||||
@@ -489,5 +489,41 @@ func TestIntegrationDataAccess(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(dataSources))
|
||||
})
|
||||
|
||||
t.Run("Get prunable data sources", func(t *testing.T) {
|
||||
db := db.InitTestDB(t)
|
||||
ss := SqlStore{db: db}
|
||||
|
||||
_, errPrunable := ss.AddDataSource(context.Background(), &datasources.AddDataSourceCommand{
|
||||
OrgID: 10,
|
||||
Name: "ElasticsearchPrunable",
|
||||
Type: "other",
|
||||
Access: datasources.DS_ACCESS_DIRECT,
|
||||
URL: "http://test",
|
||||
Database: "site",
|
||||
ReadOnly: true,
|
||||
IsPrunable: true,
|
||||
})
|
||||
require.NoError(t, errPrunable)
|
||||
|
||||
_, errNotPrunable := ss.AddDataSource(context.Background(), &datasources.AddDataSourceCommand{
|
||||
OrgID: 10,
|
||||
Name: "ElasticsearchNotPrunable",
|
||||
Type: "other",
|
||||
Access: datasources.DS_ACCESS_DIRECT,
|
||||
URL: "http://test",
|
||||
Database: "site",
|
||||
ReadOnly: true,
|
||||
})
|
||||
require.NoError(t, errNotPrunable)
|
||||
|
||||
dataSources, err := ss.GetPrunableProvisionedDataSources(context.Background())
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(dataSources))
|
||||
|
||||
dataSource := dataSources[0]
|
||||
require.Equal(t, "ElasticsearchPrunable", dataSource.Name)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user