/
scope.go
242 lines (211 loc) · 6.16 KB
/
scope.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
240
241
242
package actions
import (
"context"
"fmt"
"net/http"
"github.com/teamkeel/keel/events"
"github.com/teamkeel/keel/functions"
"github.com/teamkeel/keel/proto"
"github.com/teamkeel/keel/runtime/common"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
var tracer = otel.Tracer("github.com/teamkeel/keel/runtime/actions")
const (
requestPasswordResetActionName = "requestPasswordReset"
passwordResetActionName = "resetPassword"
)
type Scope struct {
Context context.Context
Action *proto.Action
Model *proto.Model
Job *proto.Job
Schema *proto.Schema
}
func (s *Scope) WithContext(ctx context.Context) *Scope {
return &Scope{
Context: ctx,
Action: s.Action,
Model: s.Model,
Schema: s.Schema,
}
}
func NewScope(
ctx context.Context,
action *proto.Action,
schema *proto.Schema) *Scope {
model := proto.FindModel(schema.Models, action.ModelName)
return &Scope{
Context: ctx,
Action: action,
Model: model,
Job: nil,
Schema: schema,
}
}
func NewModelScope(
ctx context.Context,
model *proto.Model,
schema *proto.Schema) *Scope {
return &Scope{
Context: ctx,
Action: nil,
Model: model,
Job: nil,
Schema: schema,
}
}
func NewJobScope(
ctx context.Context,
job *proto.Job,
schema *proto.Schema) *Scope {
return &Scope{
Context: ctx,
Action: nil,
Model: nil,
Job: job,
Schema: schema,
}
}
func Execute(scope *Scope, input any) (result any, meta *common.ResponseMetadata, err error) {
ctx, span := tracer.Start(scope.Context, scope.Action.Name)
defer span.End()
span.SetAttributes(
attribute.String("action", scope.Action.Name),
attribute.String("model", scope.Model.Name),
)
scope = scope.WithContext(ctx)
// inputs can be anything - with arbitrary functions 'Any' type, they can be
// an array / number / string etc, which doesn't fit in with the traditional map[string]any definition of an inputs object
inputsAsMap, isMap := input.(map[string]any)
if isMap {
inputsAsMap, err = TransformInputTypes(scope.Schema, scope.Action, inputsAsMap)
if err != nil {
return nil, nil, err
}
}
switch scope.Action.Implementation {
case proto.ActionImplementation_ACTION_IMPLEMENTATION_CUSTOM:
result, meta, err = executeCustomFunction(scope, input)
case proto.ActionImplementation_ACTION_IMPLEMENTATION_RUNTIME:
if !isMap {
if input == nil {
inputsAsMap = make(map[string]any)
} else {
return nil, nil, fmt.Errorf("input %v is not in correct format", input)
}
}
result, err = executeRuntimeAction(scope, inputsAsMap)
case proto.ActionImplementation_ACTION_IMPLEMENTATION_AUTO:
if !isMap {
if input == nil {
inputsAsMap = make(map[string]any)
} else {
return nil, nil, fmt.Errorf("input %v is not in correct format", input)
}
}
result, err = executeAutoAction(scope, inputsAsMap)
default:
return nil, nil, fmt.Errorf("unhandled unknown action %s of type %s", scope.Action.Name, scope.Action.Implementation)
}
// Generate and send any events for this context.
// This must run regardless of the action succeeding or failing.
// Failure to generate events fail silently.
eventsErr := events.SendEvents(ctx, scope.Schema)
if eventsErr != nil {
span.RecordError(eventsErr)
span.SetStatus(codes.Error, eventsErr.Error())
}
return
}
func executeCustomFunction(scope *Scope, inputs any) (any, *common.ResponseMetadata, error) {
permissions := proto.PermissionsForAction(scope.Schema, scope.Action)
canAuthoriseEarly, authorised, err := TryResolveAuthorisationEarly(scope, permissions)
if err != nil {
return nil, nil, err
}
permissionState := common.NewPermissionState()
if canAuthoriseEarly {
if authorised {
permissionState.Grant()
} else {
return nil, nil, common.NewPermissionError()
}
}
resp, meta, err := functions.CallFunction(
scope.Context,
scope.Action.Name,
inputs,
permissionState,
)
if err != nil {
return nil, nil, err
}
m := &common.ResponseMetadata{
Headers: http.Header(meta.Headers),
Status: meta.Status,
}
// For now a custom list function just returns a list of records, but the API's
// all return an objects containing results and pagination info. So we need
// to "wrap" the results here.
// TODO: come up with a better implementation for list functions that can support
// pagination
if scope.Action.Type == proto.ActionType_ACTION_TYPE_LIST {
results, _ := resp.([]any)
return map[string]any{
"results": results,
"pageInfo": map[string]any{
// todo: need to get these values from custom function return value
// once we have changed the return type in the codegen and made changes
// to the model api to support paging in some guise.
"hasNextPage": false,
"totalCount": 0,
"count": 0,
"startCursor": "",
"endCursor": "",
},
}, m, nil
}
return resp, m, err
}
func executeRuntimeAction(scope *Scope, inputs map[string]any) (any, error) {
switch scope.Action.Name {
case requestPasswordResetActionName:
err := ResetRequestPassword(scope, inputs)
return map[string]any{}, err
case passwordResetActionName:
err := ResetPassword(scope, inputs)
return map[string]any{}, err
default:
return nil, fmt.Errorf("unhandled runtime action: %s", scope.Action.Name)
}
}
func executeAutoAction(scope *Scope, inputs map[string]any) (any, error) {
switch scope.Action.Type {
case proto.ActionType_ACTION_TYPE_GET:
v, err := Get(scope, inputs)
// Get() can return nil, but for some reason if we don't explicitly
// return nil here too the result becomes an empty map, which is rather
// odd.
// Simple repo of this: https://play.golang.com/p/MbBzvhrdOm_f
if v == nil {
return nil, err
}
return v, err
case proto.ActionType_ACTION_TYPE_UPDATE:
result, err := Update(scope, inputs)
return result, err
case proto.ActionType_ACTION_TYPE_CREATE:
result, err := Create(scope, inputs)
return result, err
case proto.ActionType_ACTION_TYPE_DELETE:
result, err := Delete(scope, inputs)
return result, err
case proto.ActionType_ACTION_TYPE_LIST:
result, err := List(scope, inputs)
return result, err
default:
return nil, fmt.Errorf("unhandled auto action type: %s", scope.Action.Type.String())
}
}