Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement GetProject endpoint in FlyteAdmin #4316

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
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 @@ -60,6 +60,7 @@ type projectEndpointMetrics struct {
register util.RequestMetrics
list util.RequestMetrics
update util.RequestMetrics
get util.RequestMetrics
}

type attributeEndpointMetrics struct {
Expand Down Expand Up @@ -174,6 +175,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)
}

m.Metrics.projectEndpointMetrics.list.Success()
return response, nil
}
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