-
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
base: main
Are you sure you want to change the base?
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, name, type, definition) | ||
VALUES ($1, $2, $3, $4) 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 name = $2 | ||
RETURNING *; | ||
Comment on lines
+24
to
+28
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. issue: these statements should filter by |
||
|
||
-- 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 | ||
RETURNING *; | ||
Comment on lines
+35
to
+38
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. issue: these statements should filter by |
||
|
||
-- 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; | ||
Comment on lines
+70
to
+72
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. issue: this statement should join data sources to filter by 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. same comment as above. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/mindersec/minder/internal/db" | ||
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
) | ||
|
||
func (d *dataSourceService) getDataSourceSomehow( | ||
ctx context.Context, | ||
project uuid.UUID, | ||
opts *ReadOptions, | ||
theSomehow func(ctx context.Context, qtx db.ExtendQuerier, projs []uuid.UUID) (db.DataSource, error), | ||
) (*minderv1.DataSource, error) { | ||
stx, err := d.txBuilder(d, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to start transaction: %w", err) | ||
} | ||
|
||
//nolint:gosec // we'll log this error later. | ||
defer stx.Rollback() | ||
|
||
tx := stx.Q() | ||
|
||
projs, err := listRelevantProjects(ctx, tx, project, opts.canSearchHierarchical()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list relevant projects: %w", err) | ||
} | ||
|
||
ds, err := theSomehow(ctx, tx, projs) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get data source by name: %w", err) | ||
} | ||
|
||
dsfuncs, err := tx.ListDataSourceFunctions(ctx, ds.ID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get data source functions: %w", err) | ||
} | ||
|
||
if err := stx.Commit(); err != nil { | ||
return nil, fmt.Errorf("failed to commit transaction: %w", err) | ||
} | ||
|
||
return dataSourceDBToProtobuf(ds, dsfuncs) | ||
} | ||
|
||
func listRelevantProjects( | ||
ctx context.Context, tx db.ExtendQuerier, project uuid.UUID, hierarchical bool, | ||
) ([]uuid.UUID, error) { | ||
if hierarchical { | ||
projs, err := tx.GetParentProjects(ctx, project) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return projs, nil | ||
} | ||
|
||
return []uuid.UUID{project}, 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.
issue:
INSERTS
should always specifyproject_id
.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.
@blkt there is no project_id in this table. It points to a data_sources row which already has a project ID check
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 add it?