Skip to content

Commit

Permalink
Feature new getProject endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Raposo <[email protected]>
  • Loading branch information
RRap0so committed Feb 2, 2024
1 parent aedde59 commit 2c328f6
Show file tree
Hide file tree
Showing 17 changed files with 1,479 additions and 42 deletions.
12 changes: 12 additions & 0 deletions flyteadmin/pkg/manager/impl/project_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ func (m *ProjectManager) UpdateProject(ctx context.Context, projectUpdate admin.
return &response, nil
}

func (m *ProjectManager) GetProject(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error) {
projectRequest := request.Project

projectModel, err := m.db.ProjectRepo().Get(ctx, projectRequest.Id)
if err != nil {
return nil, err
}

Check warning on line 136 in flyteadmin/pkg/manager/impl/project_manager.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/manager/impl/project_manager.go#L135-L136

Added lines #L135 - L136 were not covered by tests
projectResponse := transformers.FromProjectModel(projectModel, m.getDomains())

return &projectResponse, nil
}

func NewProjectManager(db repoInterfaces.Repository, config runtimeInterfaces.Configuration) interfaces.ProjectInterface {
return &ProjectManager{
db: db,
Expand Down
27 changes: 27 additions & 0 deletions flyteadmin/pkg/manager/impl/project_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,30 @@ func TestProjectManager_UpdateProject_ErrorDueToInvalidProjectName(t *testing.T)
})
assert.EqualError(t, err, "project_name cannot exceed 64 characters")
}

func TestGetProject(t *testing.T) {
repository := repositoryMocks.NewMockRepository()
mockedProject := &admin.Project{Id: project}
activeState := int32(admin.Project_ACTIVE)
repository.ProjectRepo().(*repositoryMocks.MockProjectRepo).GetFunction = func(ctx context.Context, projectID string) (models.Project, error) {

return models.Project{
BaseModel: models.BaseModel{},
Identifier: projectID,
Name: "a-mocked-project",
Description: "A mocked project",
State: &activeState,
}, nil
}

projectManager := NewProjectManager(repository, runtimeMocks.NewMockConfigurationProvider(
getMockApplicationConfigForProjectManagerTest(), nil, nil, nil, nil, nil))

resp, _ := projectManager.GetProject(context.Background(),
admin.ProjectRequest{Project: mockedProject})

assert.Equal(t, mockedProject.Id, resp.Id)
assert.Equal(t, "a-mocked-project", resp.Name)
assert.Equal(t, "A mocked project", resp.Description)
assert.Equal(t, admin.Project_ProjectState(0), resp.State)
}
1 change: 1 addition & 0 deletions flyteadmin/pkg/manager/interfaces/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type ProjectInterface interface {
CreateProject(ctx context.Context, request admin.ProjectRegisterRequest) (*admin.ProjectRegisterResponse, error)
ListProjects(ctx context.Context, request admin.ProjectListRequest) (*admin.Projects, error)
UpdateProject(ctx context.Context, request admin.Project) (*admin.ProjectUpdateResponse, error)
GetProject(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error)
}
13 changes: 13 additions & 0 deletions flyteadmin/pkg/manager/mocks/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
type CreateProjectFunc func(ctx context.Context, request admin.ProjectRegisterRequest) (*admin.ProjectRegisterResponse, error)
type ListProjectFunc func(ctx context.Context, request admin.ProjectListRequest) (*admin.Projects, error)
type UpdateProjectFunc func(ctx context.Context, request admin.Project) (*admin.ProjectUpdateResponse, error)
type GetProjectFunc func(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error)

type MockProjectManager struct {
listProjectFunc ListProjectFunc
createProjectFunc CreateProjectFunc
updateProjectFunc UpdateProjectFunc
getProjectFunc GetProjectFunc
}

func (m *MockProjectManager) SetCreateProject(createProjectFunc CreateProjectFunc) {
Expand Down Expand Up @@ -45,3 +47,14 @@ func (m *MockProjectManager) ListProjects(
}
return nil, nil
}

func (m *MockProjectManager) SetGetCallBack(getProjectFunc GetProjectFunc) {
m.getProjectFunc = getProjectFunc
}

func (m *MockProjectManager) GetProject(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error) {
if m.getProjectFunc != nil {
return m.getProjectFunc(ctx, request)
}
return nil, nil
}
2 changes: 2 additions & 0 deletions flyteadmin/pkg/rpc/adminservice/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type projectEndpointMetrics struct {
register util.RequestMetrics
list util.RequestMetrics
update util.RequestMetrics
get util.RequestMetrics
}

type attributeEndpointMetrics struct {
Expand Down Expand Up @@ -176,6 +177,7 @@ func InitMetrics(adminScope promutils.Scope) AdminMetrics {
register: util.NewRequestMetrics(adminScope, "register_project"),
list: util.NewRequestMetrics(adminScope, "list_projects"),
update: util.NewRequestMetrics(adminScope, "update_project"),
get: util.NewRequestMetrics(adminScope, "get_project"),
},
projectAttributesEndpointMetrics: attributeEndpointMetrics{
scope: adminScope,
Expand Down
18 changes: 18 additions & 0 deletions flyteadmin/pkg/rpc/adminservice/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ func (m *AdminService) UpdateProject(ctx context.Context, request *admin.Project

return response, nil
}

func (m *AdminService) GetProject(ctx context.Context, request *admin.ProjectRequest) (*admin.Project, error) {
defer m.interceptPanic(ctx, request)
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "Incorrect request, nil requests not allowed")
}
var response *admin.Project
var err error
m.Metrics.projectEndpointMetrics.get.Time(func() {
response, err = m.ProjectManager.GetProject(ctx, *request)
})
if err != nil {
return nil, util.TransformAndRecordError(err, &m.Metrics.projectEndpointMetrics.list)
}

Check warning on line 79 in flyteadmin/pkg/rpc/adminservice/project.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/rpc/adminservice/project.go#L67-L79

Added lines #L67 - L79 were not covered by tests

m.Metrics.projectEndpointMetrics.list.Success()
return response, nil

Check warning on line 82 in flyteadmin/pkg/rpc/adminservice/project.go

View check run for this annotation

Codecov / codecov/patch

flyteadmin/pkg/rpc/adminservice/project.go#L81-L82

Added lines #L81 - L82 were not covered by tests
}
12 changes: 12 additions & 0 deletions flyteadmin/pkg/rpc/adminservice/tests/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ func TestListProjects(t *testing.T) {
assert.Nil(t, err)
assert.True(t, proto.Equal(projects, resp))
}

func TestGetProject(t *testing.T) {
mockProjectManager := mocks.MockProjectManager{}
project := &admin.Project{Id: "project id", Name: "project"}
mockProjectManager.SetGetCallBack(func(ctx context.Context, request admin.ProjectRequest) (*admin.Project, error) {
assert.NotNil(t, request)
return project, nil
})
resp, err := mockProjectManager.GetProject(context.Background(), admin.ProjectRequest{})
assert.Nil(t, err)
assert.True(t, proto.Equal(project, resp))
}
Loading

0 comments on commit 2c328f6

Please sign in to comment.