-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kick off data sources service #5022
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
-- CreateDataSource creates a new datasource in a given project. | ||
|
||
-- name: CreateDataSource :one | ||
INSERT INTO data_sources (project_id, name, display_name) | ||
VALUES ($1, $2, $3) RETURNING *; | ||
|
||
-- AddDataSourceFunction adds a function to a datasource. | ||
|
||
-- name: AddDataSourceFunction :one | ||
INSERT INTO data_sources_functions (data_source_id, project_id, name, type, definition) | ||
VALUES ($1, $2, $3, $4, $5) RETURNING *; | ||
|
||
-- UpdateDataSource updates a datasource in a given project. | ||
|
||
-- name: UpdateDataSource :one | ||
UPDATE data_sources | ||
SET display_name = $3 | ||
WHERE id = $1 AND project_id = $2 | ||
RETURNING *; | ||
|
||
-- UpdateDataSourceFunction updates a function in a datasource. We're | ||
-- only able to update the type and definition of the function. | ||
|
||
-- name: UpdateDataSourceFunction :one | ||
UPDATE data_sources_functions | ||
SET type = $3, definition = $4, updated_at = NOW() | ||
WHERE data_source_id = $1 AND project_id = $5 AND name = $2 | ||
RETURNING *; | ||
|
||
-- name: DeleteDataSource :one | ||
DELETE FROM data_sources | ||
WHERE id = $1 AND project_id = $2 | ||
RETURNING *; | ||
|
||
-- name: DeleteDataSourceFunction :one | ||
DELETE FROM data_sources_functions | ||
WHERE data_source_id = $1 AND name = $2 AND project_id = $3 | ||
RETURNING *; | ||
|
||
-- GetDataSource retrieves a datasource by its id and a project hierarchy. | ||
-- | ||
-- Note that to get a datasource for a given project, one can simply | ||
-- pass one project id in the project_id array. | ||
|
||
-- name: GetDataSource :one | ||
SELECT * FROM data_sources | ||
WHERE id = $1 AND project_id = ANY(sqlc.arg(projects)::uuid[]); | ||
|
||
-- GetDataSourceByName retrieves a datasource by its name and | ||
-- a project hierarchy. | ||
-- | ||
-- Note that to get a datasource for a given project, one can simply | ||
-- pass one project id in the project_id array. | ||
|
||
-- name: GetDataSourceByName :one | ||
SELECT * FROM data_sources | ||
WHERE name = $1 AND project_id = ANY(sqlc.arg(projects)::uuid[]); | ||
|
||
-- ListDataSources retrieves all datasources for project hierarchy. | ||
-- | ||
-- Note that to get a datasource for a given project, one can simply | ||
-- pass one project id in the project_id array. | ||
|
||
-- name: ListDataSources :many | ||
SELECT * FROM data_sources | ||
WHERE project_id = ANY(sqlc.arg(projects)::uuid[]); | ||
|
||
-- ListDataSourceFunctions retrieves all functions for a datasource. | ||
|
||
-- name: ListDataSourceFunctions :many | ||
SELECT * FROM data_sources_functions | ||
WHERE data_source_id = $1 AND project_id = $2; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package service | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"google.golang.org/protobuf/encoding/protojson" | ||
|
||
"github.com/mindersec/minder/internal/datasources" | ||
"github.com/mindersec/minder/internal/db" | ||
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
) | ||
|
||
func dataSourceDBToProtobuf(ds db.DataSource, dsfuncs []db.DataSourcesFunction) (*minderv1.DataSource, error) { | ||
outds := &minderv1.DataSource{ | ||
Version: minderv1.VersionV1, | ||
Type: string(minderv1.DataSourceResource), | ||
Id: ds.ID.String(), | ||
Name: ds.Name, | ||
Context: &minderv1.ContextV2{ | ||
ProjectId: ds.ProjectID.String(), | ||
}, | ||
} | ||
|
||
if len(dsfuncs) == 0 { | ||
return nil, errors.New("data source is invalid and has no defintions") | ||
} | ||
|
||
// All data source types should be equal... so we'll just take the first one. | ||
dsfType := dsfuncs[0].Type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we ensure/validate that all sources have the same type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should. I was thinking of doing this on the |
||
|
||
switch dsfType { | ||
case datasources.DataSourceDriverRest: | ||
return dataSourceRestDBToProtobuf(outds, dsfuncs) | ||
default: | ||
return nil, fmt.Errorf("unknown data source type: %s", dsfType) | ||
} | ||
} | ||
|
||
func dataSourceRestDBToProtobuf(ds *minderv1.DataSource, dsfuncs []db.DataSourcesFunction) (*minderv1.DataSource, error) { | ||
// At this point we have already validated that we have at least one function. | ||
ds.Driver = &minderv1.DataSource_Rest{ | ||
Rest: &minderv1.RestDataSource{ | ||
Def: make(map[string]*minderv1.RestDataSource_Def, len(dsfuncs)), | ||
}, | ||
} | ||
|
||
for _, dsf := range dsfuncs { | ||
key := dsf.Name | ||
dsfToParse := &minderv1.RestDataSource_Def{} | ||
if err := protojson.Unmarshal(dsf.Definition, dsfToParse); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal data source definition for %s: %w", key, err) | ||
} | ||
|
||
ds.GetRest().Def[key] = dsfToParse | ||
} | ||
|
||
return ds, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have delete on cascade based on the data source id for functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have that https://github.com/mindersec/minder/blob/main/database/migrations/000108_data_sources.up.sql#L47