-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommittee_service.go
More file actions
362 lines (292 loc) · 11.3 KB
/
committee_service.go
File metadata and controls
362 lines (292 loc) · 11.3 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright The Linux Foundation and each contributor to LFX.
// SPDX-License-Identifier: MIT
package service
import (
"context"
"fmt"
"log/slog"
committeeservice "github.com/linuxfoundation/lfx-v2-committee-service/gen/committee_service"
"github.com/linuxfoundation/lfx-v2-committee-service/internal/domain/port"
"github.com/linuxfoundation/lfx-v2-committee-service/internal/service"
"github.com/linuxfoundation/lfx-v2-committee-service/pkg/constants"
"github.com/linuxfoundation/lfx-v2-committee-service/pkg/redaction"
"goa.design/goa/v3/security"
)
// committeeServicesrvc service implementation with clean architecture
type committeeServicesrvc struct {
committeeWriterOrchestrator service.CommitteeWriter
committeeReaderOrchestrator service.CommitteeReader
auth port.Authenticator
storage port.CommitteeReaderWriter
}
// JWTAuth implements the authorization logic for service "committee-service"
// for the "jwt" security scheme.
func (s *committeeServicesrvc) JWTAuth(ctx context.Context, token string, scheme *security.JWTScheme) (context.Context, error) {
// Parse the Heimdall-authorized principal from the token
principal, err := s.auth.ParsePrincipal(ctx, token, slog.Default())
if err != nil {
slog.ErrorContext(ctx, "committeeService.jwt-auth",
"error", err,
"token_length", len(token),
)
return ctx, err
}
// Return a new context containing the principal as a value
return context.WithValue(ctx, constants.PrincipalContextID, principal), nil
}
// Create Committee
func (s *committeeServicesrvc) CreateCommittee(ctx context.Context, p *committeeservice.CreateCommitteePayload) (res *committeeservice.CommitteeFullWithReadonlyAttributes, err error) {
slog.DebugContext(ctx, "committeeService.create-committee",
"project_uid", p.ProjectUID,
"name", p.Name,
"x_sync", p.XSync,
)
// Convert payload to DTO
request := s.convertPayloadToDomain(p)
// Execute use case
response, err := s.committeeWriterOrchestrator.Create(ctx, request, p.XSync)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert response to GOA result
result := s.convertDomainToFullResponse(response)
return result, nil
}
// GetCommitteeBase retrieves the committee base information by UID.
func (s *committeeServicesrvc) GetCommitteeBase(ctx context.Context, p *committeeservice.GetCommitteeBasePayload) (res *committeeservice.GetCommitteeBaseResult, err error) {
slog.DebugContext(ctx, "committeeService.get-committee-base",
"committee_uid", p.UID,
)
// Execute use case
committeeBase, revision, err := s.committeeReaderOrchestrator.GetBase(ctx, *p.UID)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert domain model to GOA response
result := s.convertBaseToResponse(committeeBase)
// Create result with ETag (using revision from NATS)
revisionStr := fmt.Sprintf("%d", revision)
res = &committeeservice.GetCommitteeBaseResult{
CommitteeBase: result,
Etag: &revisionStr,
}
return res, nil
}
// Update Committee
func (s *committeeServicesrvc) UpdateCommitteeBase(ctx context.Context, p *committeeservice.UpdateCommitteeBasePayload) (res *committeeservice.CommitteeBaseWithReadonlyAttributes, err error) {
slog.DebugContext(ctx, "committeeService.update-committee-base",
"committee_uid", p.UID,
"x_sync", p.XSync,
)
// Parse ETag to get revision for optimistic locking
parsedRevision, err := etagValidator(p.IfMatch)
if err != nil {
slog.ErrorContext(ctx, "invalid ETag",
"error", err,
"etag", p.IfMatch,
"committee_uid", p.UID,
)
return nil, wrapError(ctx, err)
}
// Convert payload to domain model
committee := s.convertPayloadToUpdateBase(p)
// Execute use case
updatedCommittee, err := s.committeeWriterOrchestrator.Update(ctx, committee, parsedRevision, p.XSync)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert response to GOA result
result := s.convertBaseToResponse(&updatedCommittee.CommitteeBase)
return result, nil
}
// Delete Committee
func (s *committeeServicesrvc) DeleteCommittee(ctx context.Context, p *committeeservice.DeleteCommitteePayload) error {
slog.DebugContext(ctx, "committeeService.delete-committee",
"committee_uid", p.UID,
"x_sync", p.XSync,
)
// Parse ETag to get revision for optimistic locking
parsedRevision, err := etagValidator(p.IfMatch)
if err != nil {
slog.ErrorContext(ctx, "invalid ETag",
"error", err,
"etag", p.IfMatch,
"committee_uid", p.UID,
)
return wrapError(ctx, err)
}
// Execute delete use case
errDelete := s.committeeWriterOrchestrator.Delete(ctx, *p.UID, parsedRevision, p.XSync)
if errDelete != nil {
return wrapError(ctx, errDelete)
}
return nil
}
// Get Committee Settings
func (s *committeeServicesrvc) GetCommitteeSettings(ctx context.Context, p *committeeservice.GetCommitteeSettingsPayload) (res *committeeservice.GetCommitteeSettingsResult, err error) {
slog.DebugContext(ctx, "committeeService.get-committee-settings",
"committee_uid", p.UID,
)
// Execute use case
committeeSettings, revision, err := s.committeeReaderOrchestrator.GetSettings(ctx, *p.UID)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert domain model to GOA response
result := s.convertSettingsToResponse(committeeSettings)
// Create result with ETag (using revision from NATS)
revisionStr := fmt.Sprintf("%d", revision)
res = &committeeservice.GetCommitteeSettingsResult{
CommitteeSettings: result,
Etag: &revisionStr,
}
return res, nil
}
// Update Committee Settings
func (s *committeeServicesrvc) UpdateCommitteeSettings(ctx context.Context, p *committeeservice.UpdateCommitteeSettingsPayload) (res *committeeservice.CommitteeSettingsWithReadonlyAttributes, err error) {
slog.DebugContext(ctx, "committeeService.update-committee-settings",
"committee_uid", p.UID,
"x_sync", p.XSync,
)
// Parse ETag to get revision for optimistic locking
parsedRevision, err := etagValidator(p.IfMatch)
if err != nil {
slog.ErrorContext(ctx, "invalid ETag",
"error", err,
"etag", p.IfMatch,
"committee_uid", p.UID,
)
return nil, wrapError(ctx, err)
}
// Convert payload to domain model
settings := s.convertPayloadToUpdateSettings(p)
// Execute use case
updatedSettings, err := s.committeeWriterOrchestrator.UpdateSettings(ctx, settings, parsedRevision, p.XSync)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert response to GOA result
result := s.convertSettingsToResponse(updatedSettings)
return result, nil
}
// CreateCommitteeMember adds a new member to a committee
func (s *committeeServicesrvc) CreateCommitteeMember(ctx context.Context, p *committeeservice.CreateCommitteeMemberPayload) (res *committeeservice.CommitteeMemberFullWithReadonlyAttributes, err error) {
slog.DebugContext(ctx, "committeeMemberService.create-committee-member",
"committee_uid", p.UID,
"email", redaction.RedactEmail(p.Email),
"x_sync", p.XSync,
)
// Convert payload to domain model
request := s.convertMemberPayloadToDomain(p)
// Execute use case
response, err := s.committeeWriterOrchestrator.CreateMember(ctx, request, p.XSync)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert response to GOA result
result := s.convertMemberDomainToFullResponse(response)
return result, nil
}
// GetCommitteeMember retrieves a specific committee member by UID
func (s *committeeServicesrvc) GetCommitteeMember(ctx context.Context, p *committeeservice.GetCommitteeMemberPayload) (res *committeeservice.GetCommitteeMemberResult, err error) {
slog.DebugContext(ctx, "committeeMemberService.get-committee-member",
"committee_uid", p.UID,
"member_uid", p.MemberUID,
)
// Execute use case
committeeMember, revision, err := s.committeeReaderOrchestrator.GetMember(ctx, p.UID, p.MemberUID)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert domain model to GOA response
// Only basic info, no sensitive data
result := s.convertMemberDomainBasicResponse(committeeMember)
// Create result with ETag (using revision from NATS)
revisionStr := fmt.Sprintf("%d", revision)
res = &committeeservice.GetCommitteeMemberResult{
Member: result,
Etag: &revisionStr,
}
return res, nil
}
// UpdateCommitteeMember updates an existing committee member
func (s *committeeServicesrvc) UpdateCommitteeMember(ctx context.Context, p *committeeservice.UpdateCommitteeMemberPayload) (res *committeeservice.CommitteeMemberFullWithReadonlyAttributes, err error) {
slog.DebugContext(ctx, "committeeMemberService.update-committee-member",
"committee_uid", p.UID,
"member_uid", p.MemberUID,
"email", redaction.RedactEmail(p.Email),
"x_sync", p.XSync,
)
// Parse ETag to get revision for optimistic locking
parsedRevision, err := etagValidator(p.IfMatch)
if err != nil {
slog.ErrorContext(ctx, "invalid ETag",
"error", err,
"etag", p.IfMatch,
"committee_uid", p.UID,
"member_uid", p.MemberUID,
)
return nil, wrapError(ctx, err)
}
// Convert payload to domain model
committeeMember := s.convertPayloadToUpdateMember(p)
// Execute use case
updatedMember, err := s.committeeWriterOrchestrator.UpdateMember(ctx, committeeMember, parsedRevision, p.XSync)
if err != nil {
return nil, wrapError(ctx, err)
}
// Convert response to GOA result
result := s.convertMemberDomainToFullResponse(updatedMember)
return result, nil
}
// DeleteCommitteeMember removes a member from a committee
func (s *committeeServicesrvc) DeleteCommitteeMember(ctx context.Context, p *committeeservice.DeleteCommitteeMemberPayload) error {
slog.DebugContext(ctx, "committeeMemberService.delete-committee-member",
"committee_uid", p.UID,
"member_uid", p.MemberUID,
"x_sync", p.XSync,
)
// Parse ETag to get revision for optimistic locking
parsedRevision, err := etagValidator(p.IfMatch)
if err != nil {
slog.ErrorContext(ctx, "invalid ETag",
"error", err,
"etag", p.IfMatch,
"committee_uid", p.UID,
"member_uid", p.MemberUID,
)
return wrapError(ctx, err)
}
// Execute delete use case
errDelete := s.committeeWriterOrchestrator.DeleteMember(ctx, p.MemberUID, parsedRevision, p.XSync)
if errDelete != nil {
return wrapError(ctx, errDelete)
}
return nil
}
// Check if the service is able to take inbound requests.
func (s *committeeServicesrvc) Readyz(ctx context.Context) (res []byte, err error) {
// Check NATS readiness
if err := s.storage.IsReady(ctx); err != nil {
slog.ErrorContext(ctx, "service not ready", "error", err)
return nil, err // This will automatically return ServiceUnavailable
}
return []byte("OK\n"), nil
}
// Check if the service is alive.
func (s *committeeServicesrvc) Livez(ctx context.Context) (res []byte, err error) {
// This always returns as long as the service is still running. As this
// endpoint is expected to be used as a Kubernetes liveness check, this
// service must likewise self-detect non-recoverable errors and
// self-terminate.
return []byte("OK\n"), nil
}
// NewCommitteeService returns the committee-service service implementation with dependencies.
func NewCommitteeService(createCommitteeUseCase service.CommitteeWriter, readCommitteeUseCase service.CommitteeReader, authService port.Authenticator, storage port.CommitteeReaderWriter) committeeservice.Service {
return &committeeServicesrvc{
committeeWriterOrchestrator: createCommitteeUseCase,
committeeReaderOrchestrator: readCommitteeUseCase,
auth: authService,
storage: storage,
}
}