-
Notifications
You must be signed in to change notification settings - Fork 84
/
resolver.go
239 lines (199 loc) · 10.1 KB
/
resolver.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package formationassignment
import (
"context"
dataloader "github.com/kyma-incubator/compass/components/director/internal/dataloaders"
"github.com/kyma-incubator/compass/components/director/internal/domain/tenant"
"github.com/kyma-incubator/compass/components/director/internal/model"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
"github.com/kyma-incubator/compass/components/director/pkg/graphql"
"github.com/kyma-incubator/compass/components/director/pkg/persistence"
"github.com/kyma-incubator/compass/components/director/pkg/str"
"github.com/pkg/errors"
)
// Resolver is a formation assignment resolver
type Resolver struct {
transact persistence.Transactioner
appRepo applicationRepo
appConverter applicationConverter
runtimeRepo runtimeRepo
runtimeConverter runtimeConverter
runtimeContextRepo runtimeContextRepo
runtimeContextConverter runtimeContextConverter
assignmentOperationService assignmentOperationService
assignmentOperationConverter assignmentOperationConverter
}
//go:generate mockery --name=applicationRepo --output=automock --outpkg=automock --case=underscore --exported=true --disable-version-string
type applicationRepo interface {
ListAllByIDs(ctx context.Context, tenantID string, ids []string) ([]*model.Application, error)
}
//go:generate mockery --name=runtimeRepo --output=automock --outpkg=automock --case=underscore --exported=true --disable-version-string
type runtimeRepo interface {
ListByIDs(ctx context.Context, tenant string, ids []string) ([]*model.Runtime, error)
}
//go:generate mockery --name=runtimeContextRepo --output=automock --outpkg=automock --case=underscore --exported=true --disable-version-string
type runtimeContextRepo interface {
ListByIDs(ctx context.Context, tenant string, ids []string) ([]*model.RuntimeContext, error)
}
//go:generate mockery --name=applicationConverter --output=automock --outpkg=automock --case=underscore --exported=true --disable-version-string
type applicationConverter interface {
ToGraphQL(in *model.Application) *graphql.Application
}
//go:generate mockery --name=runtimeConverter --output=automock --outpkg=automock --case=underscore --exported=true --disable-version-string
type runtimeConverter interface {
ToGraphQL(in *model.Runtime) *graphql.Runtime
}
//go:generate mockery --name=runtimeContextConverter --output=automock --outpkg=automock --case=underscore --exported=true --disable-version-string
type runtimeContextConverter interface {
ToGraphQL(in *model.RuntimeContext) *graphql.RuntimeContext
}
//go:generate mockery --exported --name=assignmentOperationService --output=automock --outpkg=automock --case=underscore --disable-version-string
type assignmentOperationService interface {
Create(ctx context.Context, in *model.AssignmentOperationInput) (string, error)
Finish(ctx context.Context, assignmentID, formationID string) error
Update(ctx context.Context, assignmentID, formationID string, newTrigger model.OperationTrigger) error
ListByFormationAssignmentIDs(ctx context.Context, formationAssignmentIDs []string, pageSize int, cursor string) ([]*model.AssignmentOperationPage, error)
DeleteByIDs(ctx context.Context, ids []string) error
}
//go:generate mockery --exported --name=assignmentOperationConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
type assignmentOperationConverter interface {
MultipleToGraphQL(in []*model.AssignmentOperation) []*graphql.AssignmentOperation
}
// NewResolver is a constructor for formation assignment resolver
func NewResolver(transact persistence.Transactioner, appRepo applicationRepo, appConverter applicationConverter, runtimeRepo runtimeRepo, runtimeConverter runtimeConverter, runtimeContextRepo runtimeContextRepo, runtimeContextConverter runtimeContextConverter, operationService assignmentOperationService, operationConverter assignmentOperationConverter) *Resolver {
return &Resolver{
transact: transact,
appRepo: appRepo,
appConverter: appConverter,
runtimeRepo: runtimeRepo,
runtimeConverter: runtimeConverter,
runtimeContextRepo: runtimeContextRepo,
runtimeContextConverter: runtimeContextConverter,
assignmentOperationService: operationService,
assignmentOperationConverter: operationConverter,
}
}
// FormationParticipantDataLoader is a dataloader for Source and Target entities of the formation assignment
func (r *Resolver) FormationParticipantDataLoader(params []dataloader.ParamFormationParticipant) ([]graphql.FormationParticipant, []error) {
if len(params) == 0 {
return nil, []error{apperrors.NewInternalError("No Formation Assignments found")}
}
ctx := params[0].Ctx
appIDs := make(map[string]struct{}, len(params))
runtimeIDs := make(map[string]struct{}, len(params))
runtimeCtxIDs := make(map[string]struct{}, len(params))
for _, param := range params {
if param.ParticipantID == "" {
return nil, []error{apperrors.NewInternalError("Cannot fetch Formation Participant. Participant ID is empty")}
}
switch param.ParticipantType {
case string(model.FormationAssignmentTypeApplication):
appIDs[param.ParticipantID] = struct{}{}
case string(model.FormationAssignmentTypeRuntime):
runtimeIDs[param.ParticipantID] = struct{}{}
case string(model.FormationAssignmentTypeRuntimeContext):
runtimeCtxIDs[param.ParticipantID] = struct{}{}
}
}
tx, err := r.transact.Begin()
if err != nil {
return nil, []error{err}
}
defer r.transact.RollbackUnlessCommitted(ctx, tx)
ctx = persistence.SaveToContext(ctx, tx)
tenantID, err := tenant.LoadFromContext(ctx)
if err != nil {
return nil, []error{errors.Wrapf(err, "while loading tenant from context")}
}
apps, err := r.appRepo.ListAllByIDs(ctx, tenantID, str.MapToSlice(appIDs))
if err != nil {
return nil, []error{errors.Wrapf(err, "while fetching applications")}
}
runtimes, err := r.runtimeRepo.ListByIDs(ctx, tenantID, str.MapToSlice(runtimeIDs))
if err != nil {
return nil, []error{errors.Wrapf(err, "while fetching runtimes")}
}
runtimeContexts, err := r.runtimeContextRepo.ListByIDs(ctx, tenantID, str.MapToSlice(runtimeCtxIDs))
if err != nil {
return nil, []error{errors.Wrapf(err, "while fetching runtimeContexts")}
}
if err = tx.Commit(); err != nil {
return nil, []error{err}
}
participants := make(map[string]graphql.FormationParticipant, len(apps)+len(runtimes)+len(runtimeContexts))
for _, app := range apps {
gqlApp := r.appConverter.ToGraphQL(app)
participants[gqlApp.ID] = gqlApp
}
for _, rt := range runtimes {
gqlRt := r.runtimeConverter.ToGraphQL(rt)
participants[gqlRt.ID] = gqlRt
}
for _, rtCtx := range runtimeContexts {
gqlRtCtx := r.runtimeContextConverter.ToGraphQL(rtCtx)
participants[gqlRtCtx.ID] = gqlRtCtx
}
result := make([]graphql.FormationParticipant, 0, len(params))
for _, param := range params {
result = append(result, participants[param.ParticipantID])
}
return result, nil
}
// TargetEntity is a resolver for the Target of the formation assignment
func (r *Resolver) TargetEntity(ctx context.Context, obj *graphql.FormationAssignment) (graphql.FormationParticipant, error) {
params := dataloader.ParamFormationParticipant{ID: obj.ID, ParticipantID: obj.Target, ParticipantType: string(obj.TargetType), Ctx: ctx}
return dataloader.ForTargetFormationParticipant(ctx).FormationParticipantDataloader.Load(params)
}
// SourceEntity is a resolver for the Source of the formation assignment
func (r *Resolver) SourceEntity(ctx context.Context, obj *graphql.FormationAssignment) (graphql.FormationParticipant, error) {
params := dataloader.ParamFormationParticipant{ID: obj.ID, ParticipantID: obj.Source, ParticipantType: string(obj.SourceType), Ctx: ctx}
return dataloader.ForSourceFormationParticipant(ctx).FormationParticipantDataloader.Load(params)
}
// AssignmentOperations retrieves a page of FormationAssignments for the specified Formation
func (r *Resolver) AssignmentOperations(ctx context.Context, obj *graphql.FormationAssignment, first *int, after *graphql.PageCursor) (*graphql.AssignmentOperationPage, error) {
param := dataloader.ParamAssignmentOperation{ID: obj.ID, Ctx: ctx, First: first, After: after}
return dataloader.FormationAssignmentFor(ctx).AssignmentOperationByID.Load(param)
}
// AssignmentOperationsDataLoader is a dataloader for the Operation entity of the formation assignment
func (r *Resolver) AssignmentOperationsDataLoader(keys []dataloader.ParamAssignmentOperation) ([]*graphql.AssignmentOperationPage, []error) {
if len(keys) == 0 {
return nil, []error{apperrors.NewInternalError("No Formation Assignments found")}
}
ctx := keys[0].Ctx
formationAssignmentIDs := make([]string, 0, len(keys))
for _, param := range keys {
if param.ID == "" {
return nil, []error{apperrors.NewInternalError("Cannot fetch Formation Assignment. ID is empty")}
}
formationAssignmentIDs = append(formationAssignmentIDs, param.ID)
}
var cursor string
if keys[0].After != nil {
cursor = string(*keys[0].After)
}
if keys[0].First == nil {
return nil, []error{apperrors.NewInvalidDataError("missing required parameter 'first'")}
}
tx, err := r.transact.Begin()
if err != nil {
return nil, []error{err}
}
defer r.transact.RollbackUnlessCommitted(ctx, tx)
ctx = persistence.SaveToContext(ctx, tx)
assignmentOperationsPages, err := r.assignmentOperationService.ListByFormationAssignmentIDs(ctx, formationAssignmentIDs, *keys[0].First, cursor)
if err != nil {
return nil, []error{errors.Wrapf(err, "while fetching assignment operations")}
}
if err = tx.Commit(); err != nil {
return nil, []error{err}
}
result := make([]*graphql.AssignmentOperationPage, 0, len(assignmentOperationsPages))
for _, page := range assignmentOperationsPages {
operations := r.assignmentOperationConverter.MultipleToGraphQL(page.Data)
result = append(result, &graphql.AssignmentOperationPage{Data: operations, TotalCount: page.TotalCount, PageInfo: &graphql.PageInfo{
StartCursor: graphql.PageCursor(page.PageInfo.StartCursor),
EndCursor: graphql.PageCursor(page.PageInfo.EndCursor),
HasNextPage: page.PageInfo.HasNextPage,
}})
}
return result, nil
}