Skip to content

Commit

Permalink
Wire struct data source to DS machinery
Browse files Browse the repository at this point in the history
This commit connects the new structured data source to the data source
machinery.

Signed-off-by: Adolfo García Veytia (puerco) <puerco@stacklok.com>
  • Loading branch information
puerco committed Dec 11, 2024
1 parent 286ee7b commit 859c4c2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/datasources/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/mindersec/minder/internal/datasources/rest"
"github.com/mindersec/minder/internal/datasources/structured"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
v1datasources "github.com/mindersec/minder/pkg/datasources/v1"
)
Expand All @@ -24,6 +25,8 @@ func BuildFromProtobuf(ds *minderv1.DataSource) (v1datasources.DataSource, error
}

switch ds.GetDriver().(type) {
case *minderv1.DataSource_Structured:
return structured.NewStructDataSource(ds.GetStructured())
case *minderv1.DataSource_Rest:
return rest.NewRestDataSource(ds.GetRest())
default:
Expand Down
26 changes: 26 additions & 0 deletions internal/datasources/service/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func dataSourceDBToProtobuf(ds db.DataSource, dsfuncs []db.DataSourcesFunction)
dsfType := dsfuncs[0].Type

switch dsfType {
case v1datasources.DataSourceDriverStruct:
return dataSourceStructDBToProtobuf(outds, dsfuncs)
case v1datasources.DataSourceDriverRest:
return dataSourceRestDBToProtobuf(outds, dsfuncs)
default:
Expand Down Expand Up @@ -60,3 +62,27 @@ func dataSourceRestDBToProtobuf(ds *minderv1.DataSource, dsfuncs []db.DataSource

return ds, nil
}

func dataSourceStructDBToProtobuf(ds *minderv1.DataSource, dsfuncs []db.DataSourcesFunction) (*minderv1.DataSource, error) {
structured := &minderv1.StructDataSource{
Def: make(map[string]*minderv1.StructDataSource_Def, len(dsfuncs)),
}

for _, dsf := range dsfuncs {
key := dsf.Name
dsfToParse := &minderv1.StructDataSource_Def{
Path: &minderv1.StructDataSource_Def_Path{},
}
if err := protojson.Unmarshal(dsf.Definition, dsfToParse); err != nil {
return nil, fmt.Errorf("failed to unmarshal data source definition for %s: %w", key, err)
}

structured.Def[key] = dsfToParse
}

ds.Driver = &minderv1.DataSource_Structured{
Structured: structured,
}

return ds, nil
}
17 changes: 17 additions & 0 deletions internal/datasources/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,23 @@ func addDataSourceFunctions(
projectID uuid.UUID,
) error {
switch drv := ds.GetDriver().(type) {
case *minderv1.DataSource_Structured:
for name, def := range drv.Structured.GetDef() {
defBytes, err := protojson.Marshal(def)
if err != nil {
return fmt.Errorf("failed to marshal structured data definition: %w", err)
}

if _, err := tx.AddDataSourceFunction(ctx, db.AddDataSourceFunctionParams{
DataSourceID: dsID,
ProjectID: projectID,
Name: name,
Type: v1datasources.DataSourceDriverStruct,
Definition: defBytes,
}); err != nil {
return fmt.Errorf("failed to create data source function: %w", err)
}
}
case *minderv1.DataSource_Rest:
for name, def := range drv.Rest.GetDef() {
defBytes, err := protojson.Marshal(def)
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/protobuf/go/minder/v1/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (ds *DataSource) GetDriverType() string {
switch ds.GetDriver().(type) {
case *DataSource_Rest:
return v1datasources.DataSourceDriverRest
case *DataSource_Structured:
return v1datasources.DataSourceDriverStruct
default:
return "unknown"
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/protobuf/go/minder/v1/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,15 @@ func (ds *DataSource) Validate() error {
return val.Validate()
}

// Validate checks a structured data source to ensure it is valid
func (dsStructuredDriver *DataSource_Structured) Validate() error {
if dsStructuredDriver == nil || dsStructuredDriver.Structured == nil {
return fmt.Errorf("%w: structured driver is nil", ErrValidationFailed)
}

return nil
}

// Validate is the entrypoint for the actual driver's validation
func (dsRestDriver *DataSource_Rest) Validate() error {
if dsRestDriver == nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/datasources/v1/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
//go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE

const (
// DataSourceDriverStruct is the driver type for the structured data source
DataSourceDriverStruct = "structured"
// DataSourceDriverRest is the driver type for a REST data source.
DataSourceDriverRest = "rest"
)
Expand Down

0 comments on commit 859c4c2

Please sign in to comment.