-
Notifications
You must be signed in to change notification settings - Fork 7
/
handler.go
237 lines (183 loc) · 6.26 KB
/
handler.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
package client
import (
"context"
"github.com/apibrew/apibrew/pkg/abs"
"github.com/apibrew/apibrew/pkg/model"
"github.com/apibrew/apibrew/pkg/resource_model"
"github.com/apibrew/apibrew/pkg/service/annotations"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/structpb"
"reflect"
"strconv"
)
type RecordProcessFunc[Entity interface{}] func(ctx context.Context, event *model.Event, instance Entity) (Entity, error)
type LambdaProcessFunc[Entity interface{}] func(ctx context.Context, event *model.Event, instance Entity) error
type Handler[Entity interface{}] interface {
Name(string) Handler[Entity]
Before() Handler[Entity]
PreProcess(RecordProcessFunc[Entity]) Handler[Entity]
PostProcess(RecordProcessFunc[Entity]) Handler[Entity]
Lambda(action string, processor LambdaProcessFunc[Entity]) Handler[Entity]
Fire(ctx context.Context, action string, payload Entity) error
After() Handler[Entity]
Instead() Handler[Entity]
Create(RecordProcessFunc[Entity]) Handler[Entity]
Update(RecordProcessFunc[Entity]) Handler[Entity]
Delete(RecordProcessFunc[Entity]) Handler[Entity]
}
type handler[Entity interface{}] struct {
ext Extension
mapper abs.EntityMapper[Entity]
dhClient Client
extensionRepo Repository[*resource_model.Extension]
action resource_model.EventAction
order int32
finalizes bool
sync bool
name string
responds bool
}
func (h handler[Entity]) Name(name string) Handler[Entity] {
h.name = name
return h
}
func (h handler[Entity]) Before() Handler[Entity] {
h.order = 90
return h
}
func (h handler[Entity]) After() Handler[Entity] {
h.order = 110
return h
}
func (h handler[Entity]) Instead() Handler[Entity] {
h.order = 100
h.finalizes = true
return h
}
func (h handler[Entity]) Lambda(action string, processor LambdaProcessFunc[Entity]) Handler[Entity] {
h.sync = false
h.responds = false
h.finalizes = true
h.order = 1
h.action = resource_model.EventAction_CREATE
h.name = h.prepareName() + "_" + action
h.prepareExtension()
h.ext.RegisterFunction(h.prepareName(), h.prepareLambdaProcessFunc(action, processor))
return h
}
func (h handler[Entity]) handle(processFunc RecordProcessFunc[Entity]) {
h.name = h.prepareName()
h.prepareExtension()
h.ext.RegisterFunction(h.prepareName(), h.prepareProcessFunc(processFunc))
}
func (h handler[Entity]) prepareProcessFunc(processFunc RecordProcessFunc[Entity]) func(ctx context.Context, req *model.Event) (*model.Event, error) {
return func(ctx context.Context, req *model.Event) (*model.Event, error) {
processedRecords := make([]*model.Record, len(req.Records))
for i, record := range req.Records {
processedRecord, err := processFunc(ctx, req, h.mapper.FromRecord(record))
if err != nil {
return nil, err
}
processedRecords[i] = h.mapper.ToRecord(processedRecord)
}
req.Records = processedRecords
return req, nil
}
}
func (h handler[Entity]) Fire(ctx context.Context, action string, payload Entity) error {
if reflect.ValueOf(payload).IsZero() {
payload = h.mapper.New()
}
rec := h.mapper.ToRecord(payload)
ri := h.mapper.ResourceIdentity()
rec.Properties["action"] = structpb.NewStringValue(action)
_, err := h.dhClient.CreateRecord(ctx, ri.Namespace, ri.Name, rec)
if err != nil {
log.Error("Error while firing event: ", err)
}
return err
}
func (h handler[Entity]) prepareLambdaProcessFunc(action string, processFunc LambdaProcessFunc[Entity]) func(ctx context.Context, req *model.Event) (*model.Event, error) {
return func(ctx context.Context, req *model.Event) (*model.Event, error) {
for _, record := range req.Records {
if record.Properties["action"] == nil || record.Properties["action"].GetStringValue() != action { // @todo this logic should be in server side
continue
}
err := processFunc(ctx, req, h.mapper.FromRecord(record))
if err != nil {
return nil, err
}
}
return req, nil
}
}
func (h handler[Entity]) Create(processFunc RecordProcessFunc[Entity]) Handler[Entity] {
h.action = resource_model.EventAction_CREATE
h.handle(processFunc)
return h
}
func (h handler[Entity]) PreProcess(processFunc RecordProcessFunc[Entity]) Handler[Entity] {
return h.Before().Create(processFunc).Update(processFunc).Delete(processFunc)
}
func (h handler[Entity]) PostProcess(processFunc RecordProcessFunc[Entity]) Handler[Entity] {
return h.After().Create(processFunc).Update(processFunc).Delete(processFunc)
}
func (h handler[Entity]) Process(processFunc RecordProcessFunc[Entity]) Handler[Entity] {
h.action = resource_model.EventAction_CREATE
h.handle(processFunc)
return h
}
func (h handler[Entity]) Update(processFunc RecordProcessFunc[Entity]) Handler[Entity] {
h.action = resource_model.EventAction_UPDATE
h.handle(processFunc)
return h
}
func (h handler[Entity]) Delete(processFunc RecordProcessFunc[Entity]) Handler[Entity] {
h.action = resource_model.EventAction_DELETE
h.handle(processFunc)
return h
}
func (h handler[Entity]) prepareExtension() {
ri := h.mapper.ResourceIdentity()
extension := &resource_model.Extension{
Name: h.name,
Description: nil,
Selector: &resource_model.EventSelector{
Actions: []resource_model.EventAction{h.action},
Namespaces: []string{ri.Namespace},
Resources: []string{ri.Name},
},
Order: h.order,
Finalizes: h.finalizes,
Sync: h.sync,
Responds: h.responds,
Annotations: map[string]string{
annotations.ServiceKey: h.ext.getServiceKey(),
},
}
extension.Call = h.ext.PrepareCall(extension)
newExtension, err := h.extensionRepo.Apply(context.TODO(), extension)
if err != nil {
panic(err)
}
h.ext.RegisterExtension(newExtension)
}
func (h handler[Entity]) prepareName() string {
ri := h.mapper.ResourceIdentity()
if h.name == "" {
h.name = "def_" + ri.Namespace + "_" + ri.Name + "_" + string(h.action) + "_" + strconv.Itoa(int(h.order))
}
return h.name
}
func NewHandler[Entity interface{}](dhClient Client, ext Extension, mapper abs.EntityMapper[Entity]) Handler[Entity] {
extensionRepo := R[*resource_model.Extension](dhClient, resource_model.ExtensionMapperInstance)
return handler[Entity]{
dhClient: dhClient,
ext: ext,
sync: true,
mapper: mapper,
extensionRepo: extensionRepo,
responds: true,
order: 100,
}
}