Skip to content

Commit

Permalink
Inform user that datasource is not found when creating rule type (#5163)
Browse files Browse the repository at this point in the history
* Inform user that datasource is not found when creating rule type

When trying to create a rule type with data sources, we verify that the
data sources must exist. In case they don't the error is now surfaced to
the user.

Closes #5148

Signed-off-by: Juan Antonio Osorio <[email protected]>

* Fix unit tests

Signed-off-by: Juan Antonio Osorio <[email protected]>

---------

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Dec 9, 2024
1 parent 5160fae commit dd7a702
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion internal/controlplane/handlers_ruletype.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ func (s *Server) CreateRuleType(
if errors.Is(err, ruletypes.ErrRuleTypeInvalid) {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid rule type definition: %s", err)
} else if errors.Is(err, ruletypes.ErrRuleAlreadyExists) {
return nil, status.Errorf(codes.AlreadyExists, "rule type %s already exists", crt.RuleType.GetName())
return nil, util.UserVisibleError(codes.AlreadyExists, "rule type %s already exists", crt.RuleType.GetName())
} else if errors.Is(err, ruletypes.ErrDataSourceNotFound) {
return nil, util.UserVisibleError(codes.InvalidArgument, "%s", err.Error())
}
return nil, status.Errorf(codes.Unknown, "failed to create rule type: %s", err)
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/ruletypes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (

//go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE

var (
// ErrDataSourceNotFound is returned when a data source is not found
ErrDataSourceNotFound = errors.New("data source not found")
)

// RuleTypeService encapsulates the creation and update of rule types
// TODO: in future, other operations such as delete should be moved here
type RuleTypeService interface {
Expand Down Expand Up @@ -155,7 +160,7 @@ func (_ *ruleTypeService) CreateRuleType(
// we need from the previous code is project id and rule id.
ds := ruleTypeDef.GetEval().GetDataSources()
if err := processDataSources(ctx, newDBRecord.ID, ds, projectID, projects, qtx); err != nil {
return nil, fmt.Errorf("failed updating references to data sources: %w", err)
return nil, fmt.Errorf("failed adding references to data sources: %w", err)
}

logger.BusinessRecord(ctx).RuleType = logger.RuleType{Name: newDBRecord.Name, ID: newDBRecord.ID}
Expand Down Expand Up @@ -334,7 +339,8 @@ func processDataSources(
// available within the project hierarchy.
datasources, err := getAvailableDataSources(ctx, ds, projectHierarchy, qtx)
if err != nil {
return fmt.Errorf("data source not available: %w", err)
// We already have enough context. Let's not over-wrap the error.
return err
}

// Then, we proceed to delete any data source reference we
Expand Down Expand Up @@ -377,7 +383,7 @@ func getAvailableDataSources(
}
dbDataSource, err := qtx.GetDataSourceByName(ctx, qarg)
if err != nil && errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("data source of name %s not found", datasource.Name)
return nil, fmt.Errorf("%w: %s", ErrDataSourceNotFound, datasource.Name)
}
if err != nil {
return nil, fmt.Errorf("failed getting data sources: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/ruletypes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ func TestRuleTypeService(t *testing.T) {
{
Name: "CreateRuleType with Data Sources not found",
RuleType: newRuleType(withBasicStructure, withDataSources),
ExpectedError: "data source not available",
ExpectedError: "data source not found",
DBSetup: dbf.NewDBMock(withHierarchyGet, withNotFoundGet, withSuccessfulCreate, withNotFoundGetDataSourcesByName),
TestMethod: create,
},
{
Name: "UpdateRuleType with Data Sources not found",
RuleType: newRuleType(withBasicStructure, withDataSources),
ExpectedError: "data source not available",
ExpectedError: "data source not found",
DBSetup: dbf.NewDBMock(withHierarchyGet, withSuccessfulGet, withSuccessfulUpdate, withNotFoundGetDataSourcesByName),
TestMethod: update,
},
Expand Down

0 comments on commit dd7a702

Please sign in to comment.