-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add pkg/querier package Signed-off-by: Radoslav Dimitrov <[email protected]> * Add a Querier interface per transaction Signed-off-by: Radoslav Dimitrov <[email protected]> * Include the rule and profile services in the querier Signed-off-by: Radoslav Dimitrov <[email protected]> * Use the correct set bundle version query for the subscriptions table Signed-off-by: Radoslav Dimitrov <[email protected]> * Cleanup querier after transaction commit or cancel Signed-off-by: Radoslav Dimitrov <[email protected]> * Refactor if statements and remove duplicated log message Signed-off-by: Radoslav Dimitrov <[email protected]> --------- Signed-off-by: Radoslav Dimitrov <[email protected]>
- Loading branch information
Showing
12 changed files
with
643 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 querier provides tools to interact with the Minder database | ||
package querier | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/mindersec/minder/internal/db" | ||
) | ||
|
||
// BundleSubscription represents a bundle subscription | ||
type BundleSubscription struct { | ||
ID uuid.UUID | ||
ProjectID uuid.UUID | ||
BundleID uuid.UUID | ||
CurrentVersion string | ||
} | ||
|
||
// BundleHandlers interface provides functions to interact with bundles and subscriptions | ||
type BundleHandlers interface { | ||
GetSubscriptionByProjectBundle( | ||
ctx context.Context, | ||
projectID uuid.UUID, | ||
bundleNamespace, bundleName string, | ||
) (*BundleSubscription, error) | ||
SetCurrentVersion(ctx context.Context, projectID uuid.UUID, currentVersion string) error | ||
} | ||
|
||
// SetCurrentVersion sets the current version of the bundle for a project | ||
func (q *querierType) SetCurrentVersion(ctx context.Context, projectID uuid.UUID, currentVersion string) error { | ||
if q.querier == nil { | ||
return ErrQuerierMissing | ||
} | ||
return q.querier.SetSubscriptionBundleVersion(ctx, db.SetSubscriptionBundleVersionParams{ | ||
ProjectID: projectID, | ||
CurrentVersion: currentVersion, | ||
}) | ||
} | ||
|
||
// GetSubscriptionByProjectBundle gets a subscription by project bundle | ||
func (q *querierType) GetSubscriptionByProjectBundle( | ||
ctx context.Context, | ||
projectID uuid.UUID, | ||
bundleNamespace, bundleName string, | ||
) (*BundleSubscription, error) { | ||
if q.querier == nil { | ||
return nil, ErrQuerierMissing | ||
} | ||
ret, err := q.querier.GetSubscriptionByProjectBundle(ctx, db.GetSubscriptionByProjectBundleParams{ | ||
Namespace: bundleNamespace, | ||
Name: bundleName, | ||
ProjectID: projectID, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &BundleSubscription{ | ||
ID: ret.ID, | ||
ProjectID: ret.ProjectID, | ||
BundleID: ret.BundleID, | ||
CurrentVersion: ret.CurrentVersion, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 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" | ||
|
||
"github.com/mindersec/minder/internal/db" | ||
pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
"github.com/mindersec/minder/pkg/profiles" | ||
) | ||
|
||
// ProfileHandlers interface provides functions to interact with profiles | ||
type ProfileHandlers interface { | ||
CreateProfile( | ||
ctx context.Context, | ||
projectID uuid.UUID, | ||
subscriptionID uuid.UUID, | ||
profile *pb.Profile, | ||
) (*pb.Profile, error) | ||
UpdateProfile( | ||
ctx context.Context, | ||
projectID uuid.UUID, | ||
subscriptionID uuid.UUID, | ||
profile *pb.Profile, | ||
) (*pb.Profile, error) | ||
DeleteProfile(ctx context.Context, projectID uuid.UUID, profileID uuid.UUID) error | ||
ListProfilesInstantiatingRuleType(ctx context.Context, ruleTypeID uuid.UUID) ([]string, error) | ||
GetProfileByProjectAndName(ctx context.Context, projectID uuid.UUID, name string) (map[string]*pb.Profile, error) | ||
DeleteRuleInstanceOfProfileInProject(ctx context.Context, projectID, profileID, ruleTypeID uuid.UUID) error | ||
} | ||
|
||
// DeleteProfile deletes a profile | ||
func (q *querierType) DeleteProfile(ctx context.Context, projectID uuid.UUID, profileID uuid.UUID) error { | ||
if q.querier == nil { | ||
return ErrQuerierMissing | ||
} | ||
return q.querier.DeleteProfile(ctx, db.DeleteProfileParams{ | ||
ProjectID: projectID, | ||
ID: profileID, | ||
}) | ||
} | ||
|
||
// UpdateProfile updates a profile | ||
func (q *querierType) UpdateProfile( | ||
ctx context.Context, | ||
projectID uuid.UUID, | ||
subscriptionID uuid.UUID, | ||
profile *pb.Profile, | ||
) (*pb.Profile, error) { | ||
if q.querier == nil { | ||
return nil, ErrQuerierMissing | ||
} | ||
if q.profileSvc == nil { | ||
return nil, ErrProfileSvcMissing | ||
} | ||
return q.profileSvc.UpdateProfile(ctx, projectID, subscriptionID, profile, q.querier) | ||
} | ||
|
||
// CreateProfile creates a profile | ||
func (q *querierType) CreateProfile( | ||
ctx context.Context, | ||
projectID uuid.UUID, | ||
subscriptionID uuid.UUID, | ||
profile *pb.Profile, | ||
) (*pb.Profile, error) { | ||
if q.querier == nil { | ||
return nil, ErrQuerierMissing | ||
} | ||
if q.profileSvc == nil { | ||
return nil, ErrProfileSvcMissing | ||
} | ||
return q.profileSvc.CreateProfile(ctx, projectID, subscriptionID, profile, q.querier) | ||
} | ||
|
||
// ListProfilesInstantiatingRuleType returns a list of profiles instantiating a rule type | ||
func (q *querierType) ListProfilesInstantiatingRuleType(ctx context.Context, ruleTypeID uuid.UUID) ([]string, error) { | ||
if q.querier == nil { | ||
return nil, ErrQuerierMissing | ||
} | ||
return q.querier.ListProfilesInstantiatingRuleType(ctx, ruleTypeID) | ||
} | ||
|
||
// GetProfileByProjectAndName returns a profile by project ID and name | ||
func (q *querierType) GetProfileByProjectAndName( | ||
ctx context.Context, | ||
projectID uuid.UUID, | ||
name string, | ||
) (map[string]*pb.Profile, error) { | ||
if q.querier == nil { | ||
return nil, ErrQuerierMissing | ||
} | ||
ret, err := q.querier.GetProfileByProjectAndName(ctx, db.GetProfileByProjectAndNameParams{ | ||
Name: name, | ||
ProjectID: projectID, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return profiles.MergeDatabaseGetByNameIntoProfiles(ret), nil | ||
} | ||
|
||
// DeleteRuleInstanceOfProfileInProject deletes a rule instance for a profile in a project | ||
func (q *querierType) DeleteRuleInstanceOfProfileInProject( | ||
ctx context.Context, | ||
projectID, profileID, ruleTypeID uuid.UUID, | ||
) error { | ||
if q.querier == nil { | ||
return ErrQuerierMissing | ||
} | ||
return q.querier.DeleteRuleInstanceOfProfileInProject(ctx, db.DeleteRuleInstanceOfProfileInProjectParams{ | ||
ProjectID: projectID, | ||
ProfileID: profileID, | ||
RuleTypeID: ruleTypeID, | ||
}) | ||
} |
Oops, something went wrong.