-
Notifications
You must be signed in to change notification settings - Fork 583
/
description_entity_repo.go
115 lines (99 loc) · 3.72 KB
/
description_entity_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
107
108
109
110
111
112
113
114
115
package gormimpl
import (
"context"
"gorm.io/gorm"
"github.com/flyteorg/flyte/flyteadmin/pkg/common"
flyteAdminDbErrors "github.com/flyteorg/flyte/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyte/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyte/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyte/flytestdlib/promutils"
)
// DescriptionEntityRepo Implementation of DescriptionEntityRepoInterface.
type DescriptionEntityRepo struct {
db *gorm.DB
errorTransformer flyteAdminDbErrors.ErrorTransformer
metrics gormMetrics
}
func (r *DescriptionEntityRepo) Get(ctx context.Context, input interfaces.GetDescriptionEntityInput) (models.DescriptionEntity, error) {
var descriptionEntity models.DescriptionEntity
filters, err := getDescriptionEntityFilters(input.ResourceType, input.Project, input.Domain, input.Name, input.Version)
if err != nil {
return models.DescriptionEntity{}, err
}
tx := r.db.WithContext(ctx).Table(descriptionEntityTableName)
// Apply filters
tx, err = applyFilters(tx, filters, nil)
if err != nil {
return models.DescriptionEntity{}, err
}
timer := r.metrics.GetDuration.Start()
tx = tx.Take(&descriptionEntity)
timer.Stop()
if tx.Error != nil {
return models.DescriptionEntity{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return descriptionEntity, nil
}
func (r *DescriptionEntityRepo) List(
ctx context.Context, input interfaces.ListResourceInput) (interfaces.DescriptionEntityCollectionOutput, error) {
// First validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.DescriptionEntityCollectionOutput{}, err
}
var descriptionEntities []models.DescriptionEntity
tx := r.db.WithContext(ctx).Limit(input.Limit).Offset(input.Offset)
// Apply filters
tx, err := applyFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.DescriptionEntityCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx.Find(&descriptionEntities)
timer.Stop()
if tx.Error != nil {
return interfaces.DescriptionEntityCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.DescriptionEntityCollectionOutput{
Entities: descriptionEntities,
}, nil
}
func getDescriptionEntityFilters(resourceType core.ResourceType, project string, domain string, name string, version string) ([]common.InlineFilter, error) {
entity := common.ResourceTypeToEntity[resourceType]
filters := make([]common.InlineFilter, 0)
projectFilter, err := common.NewSingleValueFilter(entity, common.Equal, Project, project)
if err != nil {
return nil, err
}
filters = append(filters, projectFilter)
domainFilter, err := common.NewSingleValueFilter(entity, common.Equal, Domain, domain)
if err != nil {
return nil, err
}
filters = append(filters, domainFilter)
nameFilter, err := common.NewSingleValueFilter(entity, common.Equal, Name, name)
if err != nil {
return nil, err
}
filters = append(filters, nameFilter)
versionFilter, err := common.NewSingleValueFilter(entity, common.Equal, Version, version)
if err != nil {
return nil, err
}
filters = append(filters, versionFilter)
return filters, nil
}
// NewDescriptionEntityRepo Returns an instance of DescriptionRepoInterface
func NewDescriptionEntityRepo(
db *gorm.DB, errorTransformer flyteAdminDbErrors.ErrorTransformer, scope promutils.Scope) interfaces.DescriptionEntityRepoInterface {
metrics := newMetrics(scope)
return &DescriptionEntityRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}