Skip to content

Commit

Permalink
Default project ID for data source create/update (#5122)
Browse files Browse the repository at this point in the history
* Default project ID for data source create/update

We do some smart stuff in the middleware for the server to determine the
target project in case the user does not specify it. We're not so smart
ni persisting that to the create/update requests. This fixes that.

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 3, 2024
1 parent fe30177 commit 8c13a67
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/controlplane/handlers_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (s *Server) CreateDataSource(ctx context.Context,
return nil, status.Errorf(codes.InvalidArgument, "missing data source")
}

if err := s.forceDataSourceProject(ctx, dsReq); err != nil {
return nil, err
}

// Process the request
ret, err := s.dataSourcesService.Create(ctx, dsReq, nil)
if err != nil {
Expand Down Expand Up @@ -158,6 +162,10 @@ func (s *Server) UpdateDataSource(ctx context.Context,
return nil, status.Errorf(codes.InvalidArgument, "missing data source")
}

if err := s.forceDataSourceProject(ctx, dsReq); err != nil {
return nil, err
}

// Process the request
ret, err := s.dataSourcesService.Update(ctx, dsReq, nil)
if err != nil {
Expand Down Expand Up @@ -253,3 +261,20 @@ func (s *Server) DeleteDataSourceByName(ctx context.Context,
// Return the response
return &minderv1.DeleteDataSourceByNameResponse{Name: dsName}, nil
}

func (s *Server) forceDataSourceProject(ctx context.Context, in *minderv1.DataSource) error {
entityCtx := engcontext.EntityFromContext(ctx)

// Ensure the project is valid and exist in the db
if err := entityCtx.ValidateProject(ctx, s.store); err != nil {
return status.Errorf(codes.InvalidArgument, "error in entity context: %v", err)
}

// Force the context to have the observed project ID
if in.GetContext() == nil {
in.Context = &minderv1.ContextV2{}
}
in.GetContext().ProjectId = entityCtx.Project.ID.String()

return nil
}
9 changes: 9 additions & 0 deletions internal/controlplane/handlers_datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func TestCreateDataSource(t *testing.T) {
defer ctrl.Finish()

mockStore := mockdb.NewMockStore(ctrl)
// project validation
mockStore.EXPECT().GetProjectByID(gomock.Any(), projectID).Return(db.Project{
ID: projectID,
}, nil).AnyTimes()

mockDataSourceService := mock_service.NewMockDataSourcesService(ctrl)
featureClient := &flags.FakeClient{}
Expand Down Expand Up @@ -456,6 +460,11 @@ func TestUpdateDataSource(t *testing.T) {
defer ctrl.Finish()

mockStore := mockdb.NewMockStore(ctrl)
// project validation
mockStore.EXPECT().GetProjectByID(gomock.Any(), projectID).Return(db.Project{
ID: projectID,
}, nil).AnyTimes()

mockDataSourceService := mock_service.NewMockDataSourcesService(ctrl)
featureClient := &flags.FakeClient{}

Expand Down

0 comments on commit 8c13a67

Please sign in to comment.