From 681d2fba4e4e2064fcc2e35113041270c1f76d5d Mon Sep 17 00:00:00 2001 From: Rafael Raposo Date: Tue, 19 Mar 2024 08:04:23 +0100 Subject: [PATCH] Moved Validation to it's right place. Signed-off-by: Rafael Raposo --- flyteadmin/pkg/manager/impl/project_manager.go | 4 ++-- flyteadmin/pkg/manager/impl/project_manager_test.go | 2 +- .../pkg/manager/impl/validation/project_validator.go | 7 +++++++ .../impl/validation/project_validator_test.go | 12 ++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/flyteadmin/pkg/manager/impl/project_manager.go b/flyteadmin/pkg/manager/impl/project_manager.go index 124fb32410..abf46d2438 100644 --- a/flyteadmin/pkg/manager/impl/project_manager.go +++ b/flyteadmin/pkg/manager/impl/project_manager.go @@ -128,8 +128,8 @@ func (m *ProjectManager) UpdateProject(ctx context.Context, projectUpdate admin. } func (m *ProjectManager) GetProject(ctx context.Context, request admin.ProjectGetRequest) (*admin.Project, error) { - if request.Id == "" { - return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "project identifier is required") + if err := validation.ValidateProjectGetRequest(request); err != nil { + return nil, err } projectModel, err := m.db.ProjectRepo().Get(ctx, request.Id) if err != nil { diff --git a/flyteadmin/pkg/manager/impl/project_manager_test.go b/flyteadmin/pkg/manager/impl/project_manager_test.go index f12e41ae36..6fe18c90be 100644 --- a/flyteadmin/pkg/manager/impl/project_manager_test.go +++ b/flyteadmin/pkg/manager/impl/project_manager_test.go @@ -332,5 +332,5 @@ func TestProjectManager_TestGetProject_ErrorDueToEmptyProjectGetRequest(t *testi _, err := projectManager.GetProject(context.Background(), *mockedProject) - assert.EqualError(t, err, "project identifier is required") + assert.EqualError(t, err, "missing project_id") } diff --git a/flyteadmin/pkg/manager/impl/validation/project_validator.go b/flyteadmin/pkg/manager/impl/validation/project_validator.go index 6d3b455733..8577c13e2b 100644 --- a/flyteadmin/pkg/manager/impl/validation/project_validator.go +++ b/flyteadmin/pkg/manager/impl/validation/project_validator.go @@ -31,6 +31,13 @@ func ValidateProjectRegisterRequest(request admin.ProjectRegisterRequest) error return ValidateProject(project) } +func ValidateProjectGetRequest(request admin.ProjectGetRequest) error { + if err := ValidateEmptyStringField(request.Id, projectID); err != nil { + return err + } + return nil +} + func ValidateProject(project admin.Project) error { if err := ValidateEmptyStringField(project.Id, projectID); err != nil { return err diff --git a/flyteadmin/pkg/manager/impl/validation/project_validator_test.go b/flyteadmin/pkg/manager/impl/validation/project_validator_test.go index 718647b3aa..288389d59f 100644 --- a/flyteadmin/pkg/manager/impl/validation/project_validator_test.go +++ b/flyteadmin/pkg/manager/impl/validation/project_validator_test.go @@ -364,3 +364,15 @@ func TestValidateProjectExistsDb(t *testing.T) { assert.Error(t, err) }) } + +func TestValidateProjectGetRequest(t *testing.T) { + t.Run("base case", func(t *testing.T) { + assert.Nil(t, ValidateProjectGetRequest(admin.ProjectGetRequest{ + Id: "project-id", + })) + }) + + t.Run("missing project id", func(t *testing.T) { + assert.EqualError(t, ValidateProjectGetRequest(admin.ProjectGetRequest{}), "missing project_id") + }) +}