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 62
/
project_repo.go
106 lines (88 loc) · 2.82 KB
/
project_repo.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
package gormimpl
import (
"context"
"google.golang.org/grpc/codes"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/jinzhu/gorm"
flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
)
type ProjectRepo struct {
db *gorm.DB
errorTransformer errors.ErrorTransformer
metrics gormMetrics
}
func (r *ProjectRepo) Create(ctx context.Context, project models.Project) error {
timer := r.metrics.CreateDuration.Start()
tx := r.db.Create(&project)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *ProjectRepo) Get(ctx context.Context, projectID string) (models.Project, error) {
var project models.Project
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.Project{
Identifier: projectID,
}).First(&project)
timer.Stop()
if tx.Error != nil {
return models.Project{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
if tx.RecordNotFound() {
return models.Project{}, flyteAdminErrors.NewFlyteAdminErrorf(codes.NotFound, "project [%s] not found", projectID)
}
return project, nil
}
func (r *ProjectRepo) List(ctx context.Context, input interfaces.ListResourceInput) ([]models.Project, error) {
var projects []models.Project
tx := r.db.Offset(input.Offset)
if input.Limit != 0 {
tx = tx.Limit(input.Limit)
}
// Apply filters
// If no filter provided, default to filtering out archived projects
if len(input.InlineFilters) == 0 && len(input.MapFilters) == 0 {
tx = tx.Where("state != ?", int32(admin.Project_ARCHIVED))
} else {
var err error
tx, err = applyFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return nil, err
}
}
// Apply sort ordering
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx.Find(&projects)
timer.Stop()
if tx.Error != nil {
return nil, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return projects, nil
}
func NewProjectRepo(db *gorm.DB, errorTransformer errors.ErrorTransformer,
scope promutils.Scope) interfaces.ProjectRepoInterface {
metrics := newMetrics(scope)
return &ProjectRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}
func (r *ProjectRepo) UpdateProject(ctx context.Context, projectUpdate models.Project) error {
// Use gorm client to update the two fields that are changed.
writeTx := r.db.Model(&projectUpdate).Updates(projectUpdate)
// Return error if applies.
if writeTx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(writeTx.Error)
}
return nil
}