This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
project_manager.go
135 lines (115 loc) · 3.86 KB
/
project_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package impl
import (
"context"
"strconv"
"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/util"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/validation"
"github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
repoInterfaces "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/transformers"
runtimeInterfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"google.golang.org/grpc/codes"
)
type ProjectManager struct {
db repoInterfaces.Repository
config runtimeInterfaces.Configuration
}
var alphabeticalSortParam, _ = common.NewSortParameter(admin.Sort{
Direction: admin.Sort_ASCENDING,
Key: "identifier",
})
func (m *ProjectManager) CreateProject(ctx context.Context, request admin.ProjectRegisterRequest) (
*admin.ProjectRegisterResponse, error) {
if err := validation.ValidateProjectRegisterRequest(request); err != nil {
return nil, err
}
projectModel := transformers.CreateProjectModel(request.Project)
err := m.db.ProjectRepo().Create(ctx, projectModel)
if err != nil {
return nil, err
}
return &admin.ProjectRegisterResponse{}, nil
}
func (m *ProjectManager) getDomains() []*admin.Domain {
configDomains := m.config.ApplicationConfiguration().GetDomainsConfig()
var domains = make([]*admin.Domain, len(*configDomains))
for index, configDomain := range *configDomains {
domains[index] = &admin.Domain{
Id: configDomain.ID,
Name: configDomain.Name,
}
}
return domains
}
func (m *ProjectManager) ListProjects(ctx context.Context, request admin.ProjectListRequest) (*admin.Projects, error) {
spec := util.FilterSpec{
RequestFilters: request.Filters,
}
filters, err := util.GetDbFilters(spec, common.Project)
if err != nil {
return nil, err
}
var sortParameter common.SortParameter
if request.SortBy != nil {
sortParameter, err = common.NewSortParameter(*request.SortBy)
if err != nil {
return nil, err
}
} else {
sortParameter = alphabeticalSortParam
}
offset, err := validation.ValidateToken(request.Token)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid pagination token %s for ListProjects", request.Token)
}
// And finally, query the database
listProjectsInput := repoInterfaces.ListResourceInput{
Limit: int(request.Limit),
Offset: offset,
InlineFilters: filters,
SortParameter: sortParameter,
}
projectModels, err := m.db.ProjectRepo().List(ctx, listProjectsInput)
if err != nil {
return nil, err
}
projects := transformers.FromProjectModels(projectModels, m.getDomains())
var token string
if len(projects) == int(request.Limit) {
token = strconv.Itoa(offset + len(projects))
}
return &admin.Projects{
Projects: projects,
Token: token,
}, nil
}
func (m *ProjectManager) UpdateProject(ctx context.Context, projectUpdate admin.Project) (*admin.ProjectUpdateResponse, error) {
var response admin.ProjectUpdateResponse
projectRepo := m.db.ProjectRepo()
// Fetch the existing project if exists. If not, return err and do not update.
_, err := projectRepo.Get(ctx, projectUpdate.Id)
if err != nil {
return nil, err
}
// Run validation on the request and return err if validation does not succeed.
if err := validation.ValidateProject(projectUpdate); err != nil {
return nil, err
}
// Transform the provided project into a model and apply to the DB.
projectUpdateModel := transformers.CreateProjectModel(&projectUpdate)
err = projectRepo.UpdateProject(ctx, projectUpdateModel)
if err != nil {
return nil, err
}
return &response, nil
}
func NewProjectManager(db repoInterfaces.Repository, config runtimeInterfaces.Configuration) interfaces.ProjectInterface {
return &ProjectManager{
db: db,
config: config,
}
}