-
Notifications
You must be signed in to change notification settings - Fork 13
/
wasm.go
268 lines (209 loc) · 7.66 KB
/
wasm.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
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
package store
import (
"context"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
"github.com/streamdal/streamdal/libs/protos/build/go/protos/shared"
"google.golang.org/protobuf/proto"
"github.com/streamdal/streamdal/apps/server/util"
"github.com/streamdal/streamdal/apps/server/validate"
)
var (
ErrWasmNotFound = errors.New("wasm not found")
)
// GetWasm will fetch Wasm from the store by name and ID
func (s *Store) GetWasm(ctx context.Context, name, id string) (*shared.WasmModule, error) {
s.log.Debugf("GetWasm(): Attempting to fetch Wasm by name '%s' and id '%s'", name, id)
data, err := s.read(ctx, RedisWasmKey(name, id))
if err != nil {
if err != redis.Nil {
return nil, errors.Wrap(err, "unable to read Wasm from store")
}
// Doing this so we can return a nil error
return nil, err
}
// Try to marshal the data into a Wasm entry
entry := &shared.WasmModule{}
if err := proto.Unmarshal(data, entry); err != nil {
return nil, errors.Wrap(err, "unable to unmarshal Wasm from store")
}
return entry, nil
}
// GetWasmByID will fetch Wasm from the store by ID (regardless of 'name')
func (s *Store) GetWasmByID(ctx context.Context, id string) (*shared.WasmModule, error) {
s.log.Debugf("GetWasmByID(): Attempting to fetch Wasm by id '%s'", id)
keys, err := s.options.RedisBackend.Keys(ctx, RedisWasmKey("*", id)).Result()
if err != nil {
return nil, errors.Wrap(err, "unable to list Wasm keys by id")
}
if len(keys) == 0 {
return nil, ErrWasmNotFound
}
if len(keys) > 1 {
return nil, errors.New("bug? found multiple wasm entries with the same ID")
}
name, err := util.GetWasmNameFromKey(keys[0])
if err != nil {
return nil, errors.Wrap(err, "unable to extract name from key")
}
return s.GetWasm(ctx, name, id)
}
// GetWasmByName will fetch Wasm from the store by name (regardless of 'id')
func (s *Store) GetWasmByName(ctx context.Context, name string) (*shared.WasmModule, error) {
redisKey := RedisWasmKey(name, "*")
s.log.Debugf("GetWasmByName(): Attempting to fetch Wasm by name '%s' using key '%s'", name, redisKey)
keys, err := s.options.RedisBackend.Keys(ctx, redisKey).Result()
if err != nil {
return nil, errors.Wrap(err, "unable to list Wasm keys by name")
}
if len(keys) == 0 {
return nil, ErrWasmNotFound
}
if len(keys) > 1 {
return nil, errors.New("bug? found multiple wasm entries with the same name")
}
id, err := util.GetWasmIDFromRedisKey(keys[0])
if err != nil {
return nil, errors.Wrap(err, "unable to extract id from key")
}
return s.GetWasm(ctx, name, id)
}
// SetWasm will store Wasm in the store by name and ID; it will overwrite an
// existing entry (if it exists).
func (s *Store) SetWasm(ctx context.Context, name, id string, wasm *shared.WasmModule) error {
if err := validate.SetWasm(name, id, wasm); err != nil {
return errors.Wrap(err, "unable to validate SetWasm params")
}
data, err := proto.Marshal(wasm)
if err != nil {
return errors.Wrapf(err, "unable to marshal '%s' Wasm", wasm.Name)
}
if err := s.write(ctx, RedisWasmKey(name, id), data); err != nil {
return errors.Wrapf(err, "unable to write '%s' Wasm to store", wasm.Name)
}
return nil
}
// SetWasmByID will overwrite an EXISTING Wasm entry by ID. This method
// requires for an existing Wasm object to exist in redi and will use the
// discovered 'id' to overwrite the entry.
func (s *Store) SetWasmByID(ctx context.Context, id string, wasm *shared.WasmModule) error {
if err := validate.SetWasmByID(id, wasm); err != nil {
return errors.Wrap(err, "unable to validate SetWasmByName params")
}
keys, err := s.options.RedisBackend.Keys(ctx, RedisWasmKey("*", id)).Result()
if err != nil {
return errors.Wrap(err, "unable to list Wasm keys by id")
}
if len(keys) == 0 {
return ErrWasmNotFound
}
if len(keys) > 1 {
return errors.New("bug? found multiple wasm entries with the same ID")
}
name, err := util.GetWasmNameFromKey(keys[0])
if err != nil {
return errors.Wrap(err, "unable to extract name from key")
}
return s.SetWasm(ctx, name, id, wasm)
}
// SetWasmByName will overwrite an EXISTING Wasm entry by name. This method
// requires for an existing Wasm object to exist in redis and will use the
// discovered 'name' to overwrite the entry.
func (s *Store) SetWasmByName(ctx context.Context, name string, wasm *shared.WasmModule) error {
if err := validate.SetWasmByName(name, wasm); err != nil {
return errors.Wrap(err, "unable to validate SetWasmByName params")
}
keys, err := s.options.RedisBackend.Keys(ctx, RedisWasmKey(name, "*")).Result()
if err != nil {
return errors.Wrap(err, "unable to list Wasm keys by id")
}
if len(keys) == 0 {
return ErrWasmNotFound
}
if len(keys) > 1 {
return errors.New("bug? found multiple wasm entries with the same ID")
}
id, err := util.GetWasmIDFromRedisKey(keys[0])
if err != nil {
return errors.Wrap(err, "unable to extract id from key")
}
return s.SetWasm(ctx, name, id, wasm)
}
// DeleteWasm will remove a Wasm entry from the store by name and ID.
func (s *Store) DeleteWasm(ctx context.Context, name, id string) error {
if err := validate.DeleteWasm(name, id); err != nil {
return errors.Wrap(err, "unable to validate DeleteWasm params")
}
if err := s.options.RedisBackend.Del(ctx, RedisWasmKey(name, id)).Err(); err != nil {
return errors.Wrap(err, "unable to delete Wasm from store")
}
return nil
}
// DeleteWasmByID will remove an EXISTING Wasm entry by ID. This method expects
// for a Wasm object with the given ID to exist. If it does not, the delete will
// error.
func (s *Store) DeleteWasmByID(ctx context.Context, id string) error {
if id == "" {
return errors.New("id is required")
}
keys, err := s.options.RedisBackend.Keys(ctx, RedisWasmKey("*", id)).Result()
if err != nil {
return errors.Wrap(err, "unable to list Wasm keys by id for delete")
}
if len(keys) == 0 {
return ErrWasmNotFound
}
if len(keys) > 1 {
return errors.New("bug? found multiple wasm entries with the same id for delete")
}
name, err := util.GetWasmNameFromKey(keys[0])
if err != nil {
return errors.Wrap(err, "unable to extract name from key for delete")
}
return s.DeleteWasm(ctx, name, id)
}
// DeleteWasmByName will remove an EXISTING Wasm entry by Name. This method expects
// for a Wasm object with the given Name to exist. If it does not, the delete will
// error.
func (s *Store) DeleteWasmByName(ctx context.Context, name string) error {
if name == "" {
return errors.New("name is required")
}
keys, err := s.options.RedisBackend.Keys(ctx, RedisWasmKey(name, "*")).Result()
if err != nil {
return errors.Wrap(err, "unable to list Wasm keys by name for delete")
}
if len(keys) == 0 {
return ErrWasmNotFound
}
if len(keys) > 1 {
return errors.New("bug? found multiple wasm entries with the same name for delete")
}
id, err := util.GetWasmIDFromRedisKey(keys[0])
if err != nil {
return errors.Wrap(err, "unable to extract id from key for delete")
}
return s.DeleteWasm(ctx, name, id)
}
func (s *Store) GetAllWasm(ctx context.Context) ([]*shared.WasmModule, error) {
// Fetch all keys that match the wasm key pattern
keys, err := s.options.RedisBackend.Keys(ctx, RedisWasmPrefix+":*").Result()
if err != nil {
return nil, errors.Wrap(err, "unable to list Wasm keys")
}
entries := make([]*shared.WasmModule, 0)
// Fetch all wasm entries
for _, key := range keys {
data, err := s.read(ctx, key)
if err != nil {
return nil, errors.Wrap(err, "unable to read Wasm from store")
}
// Try to marshal the data into a Wasm entry
entry := &shared.WasmModule{}
if err := proto.Unmarshal(data, entry); err != nil {
return nil, errors.Wrap(err, "unable to unmarshal Wasm from store")
}
entries = append(entries, entry)
}
return entries, nil
}