Skip to content

Commit

Permalink
Expose data source operations to Querier (#5310)
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias authored Jan 16, 2025
1 parent 10ab63b commit 7644c72
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 10 deletions.
94 changes: 94 additions & 0 deletions pkg/querier/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-FileCopyrightText: Copyright 2025 The Minder Authors
// SPDX-License-Identifier: Apache-2.0

// Package querier provides tools to interact with the Minder database
package querier

import (
"context"

"github.com/google/uuid"

dsservice "github.com/mindersec/minder/internal/datasources/service"
pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)

// DataSourceHandlers interface provides functions to interact with data sources
type DataSourceHandlers interface {
CreateDataSource(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
dataSource *pb.DataSource,
) (*pb.DataSource, error)
GetDataSourceByName(
ctx context.Context,
projectID uuid.UUID,
name string,
) (*pb.DataSource, error)
UpdateDataSource(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
dataSource *pb.DataSource,
) (*pb.DataSource, error)
DeleteDataSource(
ctx context.Context,
projectID uuid.UUID,
dataSourceID uuid.UUID,
) error
}

// CreateDataSource creates a data source
func (q *querierType) CreateDataSource(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
dataSource *pb.DataSource,
) (*pb.DataSource, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
if q.dataSourceSvc == nil {
return nil, ErrDataSourceSvcMissing
}
return q.dataSourceSvc.Create(ctx, projectID, subscriptionID, dataSource, dsservice.OptionsBuilder().WithTransaction(q.querier))
}

// UpdateDataSource updates a data source
func (q *querierType) UpdateDataSource(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
dataSource *pb.DataSource,
) (*pb.DataSource, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
if q.dataSourceSvc == nil {
return nil, ErrDataSourceSvcMissing
}
return q.dataSourceSvc.Update(ctx, projectID, subscriptionID, dataSource, dsservice.OptionsBuilder().WithTransaction(q.querier))
}

// GetDataSourceByName returns a data source by name and project IDs
func (q *querierType) GetDataSourceByName(ctx context.Context, projectID uuid.UUID, name string) (*pb.DataSource, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
if q.dataSourceSvc == nil {
return nil, ErrDataSourceSvcMissing
}
return q.dataSourceSvc.GetByName(ctx, name, projectID, dsservice.ReadBuilder().WithTransaction(q.querier))
}

// DeleteDataSource deletes a data source
func (q *querierType) DeleteDataSource(ctx context.Context, projectID uuid.UUID, dataSourceID uuid.UUID) error {
if q.querier == nil {
return ErrQuerierMissing
}
if q.dataSourceSvc == nil {
return ErrDataSourceSvcMissing
}
return q.dataSourceSvc.Delete(ctx, projectID, dataSourceID, dsservice.OptionsBuilder().WithTransaction(q.querier))
}
26 changes: 16 additions & 10 deletions pkg/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

datasourceservice "github.com/mindersec/minder/internal/datasources/service"
"github.com/mindersec/minder/internal/db"
"github.com/mindersec/minder/pkg/config/server"
"github.com/mindersec/minder/pkg/engine/selectors"
Expand All @@ -28,6 +29,8 @@ var (
ErrProfileSvcMissing = fmt.Errorf("profile service is missing")
// ErrRuleSvcMissing is returned when the rule service is not initialized
ErrRuleSvcMissing = fmt.Errorf("rule service is missing")
// ErrDataSourceSvcMissing is returned when the data source service is not initialized
ErrDataSourceSvcMissing = fmt.Errorf("data source service is missing")
)

// Store interface provides functions to execute db queries and transactions
Expand All @@ -48,17 +51,19 @@ type Querier interface {
RuleTypeHandlers
ProfileHandlers
BundleHandlers
DataSourceHandlers
Commit() error
Cancel() error
}

// querierType represents the database querier
type querierType struct {
store db.Store
tx *sql.Tx
querier db.ExtendQuerier
ruleSvc ruletypes.RuleTypeService
profileSvc profiles.ProfileService
store db.Store
tx *sql.Tx
querier db.ExtendQuerier
ruleSvc ruletypes.RuleTypeService
profileSvc profiles.ProfileService
dataSourceSvc datasourceservice.DataSourcesService
}

// Closer is a function that closes the database connection
Expand Down Expand Up @@ -95,11 +100,12 @@ func (q *querierType) BeginTx() (Querier, error) {
return nil, status.Errorf(codes.Internal, "failed to begin transaction")
}
return &querierType{
store: q.store,
querier: q.store.GetQuerierWithTransaction(tx),
ruleSvc: q.ruleSvc,
profileSvc: q.profileSvc,
tx: tx,
store: q.store,
querier: q.store.GetQuerierWithTransaction(tx),
ruleSvc: q.ruleSvc,
profileSvc: q.profileSvc,
dataSourceSvc: q.dataSourceSvc,
tx: tx,
}, nil
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/querier/ruletype.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// RuleTypeHandlers interface provides functions to interact with ruletypes
type RuleTypeHandlers interface {
ListRuleTypesByProject(ctx context.Context, projectID uuid.UUID) ([]*pb.RuleType, error)
ListRuleTypesReferencesByDataSource(ctx context.Context, dataSourceID uuid.UUID) ([]uuid.UUID, error)
CreateRuleType(ctx context.Context, projectID uuid.UUID, subscriptionID uuid.UUID, ruleType *pb.RuleType) (*pb.RuleType, error)
UpdateRuleType(ctx context.Context, projectID uuid.UUID, subscriptionID uuid.UUID, ruleType *pb.RuleType) (*pb.RuleType, error)
DeleteRuleType(ctx context.Context, ruleTypeID uuid.UUID) error
Expand Down Expand Up @@ -98,3 +99,22 @@ func (q *querierType) CreateRuleType(
}
return q.ruleSvc.CreateRuleType(ctx, projectID, subscriptionID, ruleType, q.querier)
}

// ListRuleTypesReferencesByDataSource returns a list of rule types using a data source
func (q *querierType) ListRuleTypesReferencesByDataSource(ctx context.Context, dataSourceID uuid.UUID) ([]uuid.UUID, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
ruleTypes, err := q.querier.ListRuleTypesReferencesByDataSource(ctx, dataSourceID)
if err != nil {
return nil, err
}

// Convert ruleTypes to a slice of strings
ruleTypeIds := make([]uuid.UUID, len(ruleTypes))
for i, r := range ruleTypes {
ruleTypeIds[i] = r.RuleTypeID
}

return ruleTypeIds, nil
}

0 comments on commit 7644c72

Please sign in to comment.